# SYNTAX TEST "Packages/Python/Python.sublime-syntax" ############################### # Strings and embedded syntaxes ############################### var = "\x00 \xaa \xAF \070 \0 \r \n \t \\ \a \b \' \v \f \u0aF1 \UFe0a182f \N{SPACE}" # ^ meta.string.python # ^^^^ constant.character.escape.hex # ^^^^ constant.character.escape.hex # ^^^^ constant.character.escape.hex # ^^^^ constant.character.escape.octal # ^^ constant.character.escape # ^^ constant.character.escape # ^^ constant.character.escape # ^^ constant.character.escape # ^^ constant.character.escape # ^^ constant.character.escape # ^^ constant.character.escape # ^^ constant.character.escape # ^^ constant.character.escape # ^^ constant.character.escape # ^^^^^^ constant.character.escape.unicode # ^^^^^^^^^^ constant.character.escape.unicode # ^^^^^^^^^ constant.character.escape.unicode invalid_escapes = "\. \-" # ^^ invalid.deprecated.character.escape.python # ^^ invalid.deprecated.character.escape.python conn.execute("SELECT * FROM foobar") # ^ meta.string.python keyword.other.DML.sql conn.execute('SELECT * FROM foobar') # ^ keyword.other.DML.sql conn.execute(U"SELECT * FROM foobar") # ^ keyword.other.DML.sql conn.execute(U'SELECT * FROM foobar') # ^ keyword.other.DML.sql # In this example, the Python string is not raw, so \t is a python escape conn.execute(u"SELECT * FROM foobar WHERE foo = '\t'") # ^ keyword.other.DML.sql # ^ constant.character.escape.python conn.execute(u'SELECT * FROM foobar') # ^ keyword.other.DML.sql # In this example, the Python string is raw, so the \b should be a SQL escape conn.execute(r"SELECT * FROM foobar WHERE baz = '\b") # ^ meta.string.python keyword.other.DML.sql # ^ constant.character.escape.sql # This tests to ensure the Python placeholder will be highlighted even in a raw SQL string conn.execute(r'SELECT * FROM foobar WHERE %s') # ^ keyword.other.DML.sql # ^ constant.other.placeholder.python conn.execute(r"SELECT * FROM foobar") # ^ keyword.other.DML.sql conn.execute(r'SELECT * FROM foobar') # ^ keyword.other.DML.sql conn.execute(r"""SELECT * FROM foobar WHERE %s and foo = '\t'""") # ^ keyword.other.DML.sql # ^ constant.other.placeholder.python # ^ constant.character.escape.sql # Capital R prevents all syntax embedding conn.execute(R'SELECT * FROM foobar') # ^ meta.string.python - keyword.other.DML.sql conn.execute(R"SELECT * FROM foobar") # ^ - keyword.other.DML.sql conn.execute(R"""SELECT * FROM foobar""") # ^ - keyword.other.DML.sql conn.execute(r'''SELECT * FROM foobar''') # ^ keyword.other.DML.sql conn.execute(u"""SELECT * FROM foobar WHERE %s and foo = '\t'""") # ^ keyword.other.DML.sql # ^ constant.other.placeholder.python # ^ constant.character.escape.python regex = r'\b ([fobar]*){1}(?:a|b)?' # ^ meta.string.python keyword.control.anchor.regexp # ^ keyword.operator.quantifier.regexp regex = r'.* # Not a comment (yet)' # ^^^^^^^^^^^^^^^^^^^^^ - comment # ^ punctuation.definition.string.end.python - comment # ^ - invalid regex = r".* # Not a comment (yet)" # ^^^^^^^^^^^^^^^^^^^^^ - comment # ^ punctuation.definition.string.end.python - comment # ^ - invalid regex = r'''\b ([fobar]*){1}(?:a|b)?''' # ^ keyword.control.anchor.regexp # ^ keyword.operator.quantifier.regexp regex = r"""\b ([fobar]*){1}(?:a|b)?""" # ^ keyword.control.anchor.regexp # ^ keyword.operator.quantifier.regexp # Capital R prevents all syntax embedding regex = R'\b ([fobar]*){1}(?:a|b)?' # ^ - keyword.control.anchor.regexp # ^ - keyword.operator.quantifier.regexp regex = R"\b ([fobar]*){1}(?:a|b)?" # ^ - keyword.control.anchor.regexp # ^ - keyword.operator.quantifier.regexp bad_string = 'SELECT * FROM users # ^ invalid.illegal.unclosed-string more_bad_string = r" # ^ invalid.illegal.unclosed-string.python string = ''' # <- string.quoted.single.block ''' string = """ # <- string.quoted.double.block """ string = """ # ^^^ string.quoted.double.block - string string \ # <- punctuation.separator.continuation.line.python """ string = r""" # ^^^ meta.string.python string.quoted.double.block \ # <- - punctuation """ string = r""" # An indented comment. # ^ - comment # ^ comment.line.number-sign.regexp ### <> @includes some &punctutation. # <- comment.line.number-sign.regexp """ string = ''' # ^^^ string.quoted.single.block ''' string = r''' # ^^^ string.quoted.single.block ''' string = r''' # An indented comment. # ^ - comment # ^ comment.line.number-sign.regexp ### <> @includes some &punctutation. # <- comment.line.number-sign.regexp ''' string = r''' [set] # ^^^^^ constant.other.character-class.set.regexp # ^ punctuation.definition.character-class.begin.regexp # ^ punctuation.definition.character-class.end.regexp (group) # ^^^^^^^ meta.group.regexp # ^ punctuation.definition.group.begin.regexp # ^ punctuation.definition.group.end.regexp (?9,}".format(1000) # ^^^^^^^ constant.other.placeholder "Testing {:j^9,}".format(1000) # ^^^^^^^ constant.other.placeholder # ^^^^^ constant.other.format-spec "result: {value:{width}.{precision}}" # ^^^^^^^^^^^^^^^^^^^^^^^^^^^ constant.other.placeholder # ^^^^^^^^^^^^^^^^^^^^ meta.format-spec.python # ^ punctuation.definition.placeholder.begin # ^^^^^^^ constant.other.placeholder constant.other.placeholder # ^ punctuation.definition.placeholder.begin # ^^^^^^^^^^^ constant.other.placeholder constant.other.placeholder # ^^ punctuation.definition.placeholder.end a=["aaaa{", "bbbb{"] # ^ - constant.other.placeholder # ^ punctuation.definition.string.end.python foo = "{text{" # Comment # ^^^^^^ - constant.other.placeholder # ^ punctuation.definition.string.end bar = "}}" # Comment # ^^ constant.character.escape f"string" # <- storage.type.string #^^^^^^^^ string.quoted.double RF"""string""" #^^ storage.type.string - string # ^^^^^^^^^^^^ meta.string.interpolated string.quoted.double.block F'''string''' # <- storage.type.string #^^^^^^^^^^^^ meta.string.interpolated string.quoted.single.block rf'string' #^^ storage.type.string - string # ^^^^^^^^ meta.string.interpolated string.quoted.single rf'\r\n' f'\r\n' Rf'\r\n' # ^^^^ source.regexp constant.character.escape.backslash.regexp # ^^^^ constant.character.escape.python # ^^^^ - constant rf"\r\n" f"\r\n" Rf'\r\n' # ^^^^ source.regexp constant.character.escape.backslash.regexp # ^^^^ constant.character.escape.python # ^^^^ - constant expr = fr"^\s*({label}|{notlabel})" # ^^^^^^^^^^^^^^^^^^^^^^^^ meta.string.interpolated.python # ^ meta.string.interpolated.python string.quoted.double.python source.regexp.python keyword.control.anchor.regexp # ^ source.regexp.python meta.group.regexp punctuation.definition.group.begin.regexp # ^^^^^^^ source.python meta.string.interpolated.python meta.interpolation.python # ^^^^^ source.python.embedded meta.qualified-name.python meta.generic-name.python # ^ source.regexp.python meta.group.regexp punctuation.definition.group.end.regexp line = re.sub(rf" ?\{{\\i.?\}}({x})\{{\\i.?\}}", r"\1", line) # ^^^^^ constant.character.escape.backslash.regexp # ^^ constant.character.escape.python # ^^^ constant.character.escape.backslash.regexp # ^^ constant.character.escape.python # ^ punctuation.section.interpolation.begin.python f"\{{{x}\}} test" # ^ invalid.deprecated.character.escape.python # ^^ constant.character.escape.python # ^ punctuation.section.interpolation.begin.python f"{something}" #^^^^^^^^^^^^ meta.string.interpolated # <- storage.type.string #^ punctuation.definition.string.begin # ^ punctuation.section.interpolation.begin # ^ punctuation.section.interpolation.end # ^ punctuation.definition.string.end # ^^^^^^^^^ source source.python.embedded # ^ source - meta, string, source source f"{True!a:02f}" #^^^^^^^^^^^^^^ meta.string.interpolated # ^ - source source.python.embedded # ^^^^ source source.python.embedded constant.language # ^^^^^^^ - source source.python.embedded # ^^ storage.modifier.conversion - constant.other.format-spec # ^^^^ constant.other.format-spec # ^ punctuation.section.interpolation.end # ^ punctuation.definition.string.end # ^ source - meta, string, source source f"result: {value:{width}.{precision}}\n" # ^ punctuation.section.interpolation.begin.python - source source # ^^^^^ source source.python.embedded # ^^ - source source # ^ punctuation.section.interpolation.begin.python # ^^^^^ source source.python.embedded # ^ punctuation.section.interpolation.end.python # ^ - source source # ^ punctuation.section.interpolation.begin.python # ^^^^^^^^^ source source.python.embedded # ^^ punctuation.section.interpolation.end.python - source source # ^^ constant.character.escape # ^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.interpolation.python # ^^^^^^^^^^^^^^^^^^^^ meta.format-spec.python # ^^^^^^ - meta.interpolation.python meta.interpolation.python # ^^^^^^^ meta.interpolation.python meta.interpolation.python # ^ - meta.interpolation.python meta.interpolation.python # ^^^^^^^^^^^ meta.interpolation.python meta.interpolation.python # ^^^ - meta.interpolation.python meta.interpolation.python rf"{value:{width!s:d}}" # <- storage.type.string.python - string # ^^^^^^^^^^^^^^^^^^^^^ meta.string.interpolated # ^^^^^ source source.python.embedded # ^^ storage.modifier.conversion # ^^ constant.other.format-spec F""" {} {\} } #^^^^^^^^^^^ meta.string.interpolated #^^^ punctuation.definition.string.begin # ^^ invalid.illegal.empty-expression # ^ invalid.illegal.backslash-in-fstring # ^ invalid.illegal.stray-brace """ fr''' # ^ - invalid ''' # Most of these were inspired by # https://github.com/python/cpython/commit/9a4135e939bc223f592045a38e0f927ba170da32 f'{x=:}' # ^ storage.modifier.debug.python f'{x=:.2f}' # ^ storage.modifier.debug.python f'{x=!r}' # ^ storage.modifier.debug.python f'{x=!a}' # ^ storage.modifier.debug.python f'{x=!s:*^20}' # ^ storage.modifier.debug.python # ^^ storage.modifier.conversion.python # ^^^^^ meta.format-spec.python f'{"Σ"=}' # ^ storage.modifier.debug.python f'{0==1}' # ^^ -storage.modifier.debug.python f'{0!=1}' # ^ -storage.modifier.debug.python f'{0<=1}' # ^ -storage.modifier.debug.python f'{0>=1}' # ^ -storage.modifier.debug.python f'{f(a="3=")}' # ^^^^ -storage.modifier.debug.python f" { % ^ invalid.illegal.unclosed-string # TODO make this test pass }" f' \ {1 + 2!a:02f}' #^^^^^^^^^^^^^^ meta.string.interpolated # ^^^^^ source source.python.embedded f"{d for d in range(10)}" # yes, this doesn't make sense # ^^^ keyword.control.loop.for.generator.python f' # ^ invalid.illegal.unclosed-string # <- - meta # this test is to ensure we're not matching anything here anymore