[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/MagicStack/MagicPython/latest/grammars/MagicPython.cson [Back]  [Original]

# AUTOGENERATED FROM grammars/src/MagicPython.syntax.yaml
name: "MagicPython"
scopeName: "source.python"
fileTypes: [
  "py"
  "py3"
  "rpy"
  "pyw"
  "cpy"
  "pyi"
  "SConstruct"
  "Sconstruct"
  "sconstruct"
  "SConscript"
  "gyp"
  "gypi"
  "wsgi"
  "kv"
  "Snakefile"
  "smk"
  "tac"
]
firstLineMatch: "^#![ \\t]*/.*\\bpython[\\d\\.]*\\b"
uuid: "742deb57-6e38-4192-bed6-410746efd85d"
patterns: [
  {
    include: "#statement"
  }
  {
    include: "#expression"
  }
]
repository:
  impossible:
    comment: "This is a special rule that should be used where no match is desired. It is not a good idea to match something like '1{0}' because in some cases that can result in infinite loops in token generation. So the rule instead matches and impossible expression to allow a match to fail and move to the next token."
    match: "$.^"
  statement:
    patterns: [
      {
        include: "#import"
      }
      {
        include: "#class-declaration"
      }
      {
        include: "#function-declaration"
      }
      {
        include: "#generator"
      }
      {
        include: "#statement-keyword"
      }
      {
        include: "#assignment-operator"
      }
      {
        include: "#decorator"
      }
      {
        include: "#docstring-statement"
      }
      {
        include: "#semicolon"
      }
    ]
  semicolon:
    patterns: [
      {
        name: "invalid.deprecated.semicolon.python"
        match: "\\;$"
      }
    ]
  comments:
    patterns: [
      {
        name: "comment.line.number-sign.python"
        contentName: "meta.typehint.comment.python"
        begin: '''
          (?x)
            (?:
              \\# \\s* (type:)
              \\s*+ (?# we want `\\s*+` which is possessive quantifier since
                       we do not actually want to backtrack when matching
                       whitespace here)
              (?! $ | \\#)
            )
          
        '''
        end: "(?:$|(?=\\#))"
        beginCaptures:
          "0":
            name: "meta.typehint.comment.python"
          "1":
            name: "comment.typehint.directive.notation.python"
        patterns: [
          {
            name: "comment.typehint.ignore.notation.python"
            match: '''
              (?x)
                \\G ignore
                (?= \\s* (?: $ | \\#))
              
            '''
          }
          {
            name: "comment.typehint.type.notation.python"
            match: '''
              (?x)
                (?>>|\\.\\.\\.) \\s) (?=\\s*\\S)
        )
      
    '''
    captures:
      "1":
        name: "keyword.control.flow.python"
  "statement-keyword":
    patterns: [
      {
        name: "storage.type.function.python"
        match: "\\b((async\\s+)?\\s*def)\\b"
      }
      {
        name: "keyword.control.flow.python"
        comment: '''
          if `as` is eventually followed by `:` or line continuation
          it's probably control flow like:
              with foo as bar, \\
                   Foo as Bar:
                try:
                  do_stuff()
                except Exception as e:
                  pass
          
        '''
        match: "\\b(?>= | //= | \\*\\*=
          | \\+= | -= | /= | @=
          | \\*= | %= | ~= | \\^= | &= | \\|=
          | =(?!=)
      
    '''
  operator:
    match: '''
      (?x)
          \\b(?> | & | \\| | \\^ | ~)                          (?# 3)
      
          | (\\*\\* | \\* | \\+ | - | % | // | / | @)                (?# 4)
      
          | (!= | == | >= | )                          (?# 5)
      
          | (:=)                                                 (?# 6)
      
    '''
    captures:
      "1":
        name: "keyword.operator.logical.python"
      "2":
        name: "keyword.control.flow.python"
      "3":
        name: "keyword.operator.bitwise.python"
      "4":
        name: "keyword.operator.arithmetic.python"
      "5":
        name: "keyword.operator.comparison.python"
      "6":
        name: "keyword.operator.assignment.python"
  punctuation:
    patterns: [
      {
        name: "punctuation.separator.colon.python"
        match: ":"
      }
      {
        name: "punctuation.separator.element.python"
        match: ","
      }
    ]
  literal:
    patterns: [
      {
        name: "constant.language.python"
        match: "\\b(True|False|None|NotImplemented|Ellipsis)\\b"
      }
      {
        include: "#number"
      }
    ]
  number:
    name: "constant.numeric.python"
    patterns: [
      {
        include: "#number-float"
      }
      {
        include: "#number-dec"
      }
      {
        include: "#number-hex"
      }
      {
        include: "#number-oct"
      }
      {
        include: "#number-bin"
      }
      {
        include: "#number-long"
      }
      {
        name: "invalid.illegal.name.python"
        match: "\\b[0-9]+\\w+"
      }
    ]
  "number-float":
    name: "constant.numeric.float.python"
    match: '''
      (?x)
        (?=^]? [-+ ]? \\#?
                    \\d* ,? (\\.\\d+)? [bcdeEfFgGnosxX%]? )?
                })
            )
          
        '''
        captures:
          "1":
            name: "constant.character.format.placeholder.other.python"
          "3":
            name: "storage.type.format.python"
          "4":
            name: "storage.type.format.python"
      }
      {
        name: "meta.format.brace.python"
        match: '''
          (?x)
            (
              {
                \\w* (\\.[[:alpha:]_]\\w* | \\[[^\\]'"]+\\])*
                (![rsa])?
                (:)
                  [^'"{}\\n]* (?:
                    \\{ [^'"}\\n]*? \\} [^'"{}\\n]*
                  )*
              }
            )
          
        '''
        captures:
          "1":
            name: "constant.character.format.placeholder.other.python"
          "3":
            name: "storage.type.format.python"
          "4":
            name: "storage.type.format.python"
      }
    ]
  "fstring-formatting":
    patterns: [
      {
        include: "#fstring-formatting-braces"
      }
      {
        include: "#fstring-formatting-singe-brace"
      }
    ]
  "fstring-formatting-singe-brace":
    name: "invalid.illegal.brace.python"
    match: "(}(?!}))"
  import:
    comment: '''
      Import statements used to correctly mark `from`, `import`, and `as`
      
    '''
    patterns: [
      {
        begin: "\\b(?=^]? [-+ ]? \\#?
                \\d* ,? (\\.\\d+)? [bcdeEfFgGnosxX%]? )(?=})
          
        '''
        captures:
          "1":
            name: "storage.type.format.python"
          "2":
            name: "storage.type.format.python"
      }
      {
        include: "#fstring-terminator-single-tail"
      }
    ]
  "fstring-terminator-single-tail":
    begin: "((?:=?)(?:![rsa])?)(:)(?=.*?{)"
    end: "(?=})|(?=\\n)"
    beginCaptures:
      "1":
        name: "storage.type.format.python"
      "2":
        name: "storage.type.format.python"
    patterns: [
      {
        include: "#fstring-illegal-single-brace"
      }
      {
        include: "#fstring-single-brace"
      }
      {
        name: "storage.type.format.python"
        match: "([bcdeEfFgGnosxX%])(?=})"
      }
      {
        name: "storage.type.format.python"
        match: "(\\.\\d+)"
      }
      {
        name: "storage.type.format.python"
        match: "(,)"
      }
      {
        name: "storage.type.format.python"
        match: "(\\d+)"
      }
      {
        name: "storage.type.format.python"
        match: "(\\#)"
      }
      {
        name: "storage.type.format.python"
        match: "([-+ ])"
      }
      {
        name: "storage.type.format.python"
        match: "([=^])"
      }
      {
        name: "storage.type.format.python"
        match: "(\\w)"
      }
    ]
  "fstring-fnorm-quoted-multi-line":
    name: "meta.fstring.python"
    begin: "(\\b[fF])([bBuU])?('''|\"\"\")"
    end: "(\\3)"
    beginCaptures:
      "1":
        name: "storage.type.string.python string.quoted.multi.python string.interpolated.python"
      "2":
        name: "invalid.illegal.prefix.python"
      "3":
        name: "string.quoted.multi.python string.interpolated.python punctuation.definition.string.begin.python"
    endCaptures:
      "1":
        name: "string.quoted.multi.python string.interpolated.python punctuation.definition.string.end.python"
      "2":
        name: "invalid.illegal.newline.python"
    patterns: [
      {
        include: "#fstring-guts"
      }
      {
        include: "#fstring-illegal-multi-brace"
      }
      {
        include: "#fstring-multi-brace"
      }
      {
        include: "#fstring-multi-core"
      }
    ]
  "fstring-normf-quoted-multi-line":
    name: "meta.fstring.python"
    begin: "(\\b[bBuU])([fF])('''|\"\"\")"
    end: "(\\3)"
    beginCaptures:
      "1":
        name: "invalid.illegal.prefix.python"
      "2":
        name: "storage.type.string.python string.quoted.multi.python string.interpolated.python"
      "3":
        name: "string.quoted.multi.python punctuation.definition.string.begin.python"
    endCaptures:
      "1":
        name: "string.quoted.multi.python string.interpolated.python punctuation.definition.string.end.python"
      "2":
        name: "invalid.illegal.newline.python"
    patterns: [
      {
        include: "#fstring-guts"
      }
      {
        include: "#fstring-illegal-multi-brace"
      }
      {
        include: "#fstring-multi-brace"
      }
      {
        include: "#fstring-multi-core"
      }
    ]
  "fstring-raw-quoted-multi-line":
    name: "meta.fstring.python"
    begin: "(\\b(?:[rR][fF]|[fF][rR]))('''|\"\"\")"
    end: "(\\2)"
    beginCaptures:
      "1":
        name: "storage.type.string.python string.quoted.raw.multi.python string.interpolated.python"
      "2":
        name: "string.quoted.raw.multi.python punctuation.definition.string.begin.python"
    endCaptures:
      "1":
        name: "string.quoted.raw.multi.python string.interpolated.python punctuation.definition.string.end.python"
      "2":
        name: "invalid.illegal.newline.python"
    patterns: [
      {
        include: "#fstring-raw-guts"
      }
      {
        include: "#fstring-illegal-multi-brace"
      }
      {
        include: "#fstring-multi-brace"
      }
      {
        include: "#fstring-raw-multi-core"
      }
    ]
  "fstring-multi-core":
    name: "string.quoted.multi.python string.interpolated.python"
    match: '''
      (?x)
        (.+?)
          (
            (?# .* and .*? in multi-line match need special handling of
              newlines otherwise SublimeText and Atom will match slightly
              differently.
      
              The guard for newlines has to be separate from the
              lookahead because of special $ matching rule.)
            ($\\n?)
            |
            (?=[\\\\\\}\\{]|\'''|""")
          )
        (?# due to how multiline regexps are matched we need a special case
          for matching a newline character)
        | \\n
      
    '''
  "fstring-raw-multi-core":
    name: "string.quoted.raw.multi.python string.interpolated.python"
    match: '''
      (?x)
        (.+?)
          (
            (?# .* and .*? in multi-line match need special handling of
              newlines otherwise SublimeText and Atom will match slightly
              differently.
      
              The guard for newlines has to be separate from the
              lookahead because of special $ matching rule.)
            ($\\n?)
            |
            (?=[\\\\\\}\\{]|\'''|""")
          )
        (?# due to how multiline regexps are matched we need a special case
          for matching a newline character)
        | \\n
      
    '''
  "fstring-multi-brace":
    comment: "value interpolation using { ... }"
    begin: "(\\{)"
    end: '''
      (?x)
        (\\})
      
    '''
    beginCaptures:
      "1":
        name: "constant.character.format.placeholder.other.python"
    endCaptures:
      "1":
        name: "constant.character.format.placeholder.other.python"
    patterns: [
      {
        include: "#fstring-terminator-multi"
      }
      {
        include: "#f-expression"
      }
    ]
  "fstring-terminator-multi":
    patterns: [
      {
        name: "storage.type.format.python"
        match: "(=(![rsa])?)(?=})"
      }
      {
        name: "storage.type.format.python"
        match: "(=?![rsa])(?=})"
      }
      {
        match: '''
          (?x)
            ( (?: =?) (?: ![rsa])? )
              ( : \\w? [=^]? [-+ ]? \\#?
                \\d* ,? (\\.\\d+)? [bcdeEfFgGnosxX%]? )(?=})
          
        '''
        captures:
          "1":
            name: "storage.type.format.python"
          "2":
            name: "storage.type.format.python"
      }
      {
        include: "#fstring-terminator-multi-tail"
      }
    ]
  "fstring-terminator-multi-tail":
    begin: "((?:=?)(?:![rsa])?)(:)(?=.*?{)"
    end: "(?=})"
    beginCaptures:
      "1":
        name: "storage.type.format.python"
      "2":
        name: "storage.type.format.python"
    patterns: [
      {
        include: "#fstring-illegal-multi-brace"
      }
      {
        include: "#fstring-multi-brace"
      }
      {
        name: "storage.type.format.python"
        match: "([bcdeEfFgGnosxX%])(?=})"
      }
      {
        name: "storage.type.format.python"
        match: "(\\.\\d+)"
      }
      {
        name: "storage.type.format.python"
        match: "(,)"
      }
      {
        name: "storage.type.format.python"
        match: "(\\d+)"
      }
      {
        name: "storage.type.format.python"
        match: "(\\#)"
      }
      {
        name: "storage.type.format.python"
        match: "([-+ ])"
      }
      {
        name: "storage.type.format.python"
        match: "([=^])"
      }
      {
        name: "storage.type.format.python"
        match: "(\\w)"
      }
    ]

Web Proxy Viewer  |  New URL  |  Original Page