package p import ( . "github.com/alecthomas/chroma" // nolint "github.com/alecthomas/chroma/lexers/internal" ) // Python lexer. var Python = internal.Register(MustNewLazyLexer( &Config{ Name: "Python", Aliases: []string{"python", "py", "sage", "python3", "py3"}, Filenames: []string{"*.py", "*.pyi", "*.pyw", "*.jy", "*.sage", "*.sc", "SConstruct", "SConscript", "*.bzl", "BUCK", "BUILD", "BUILD.bazel", "WORKSPACE", "*.tac"}, MimeTypes: []string{"text/x-python", "application/x-python", "text/x-python3", "application/x-python3"}, }, pythonRules, )) func pythonRules() Rules { const pythonIdentifier = `[_\p{L}][_\p{L}\p{N}]*` return Rules{ "root": { {`\n`, Text, nil}, {`^(\s*)([rRuUbB]{,2})("""(?:.|\n)*?""")`, ByGroups(Text, LiteralStringAffix, LiteralStringDoc), nil}, {`^(\s*)([rRuUbB]{,2})('''(?:.|\n)*?''')`, ByGroups(Text, LiteralStringAffix, LiteralStringDoc), nil}, {`\A#!.+$`, CommentHashbang, nil}, {`#.*$`, CommentSingle, nil}, {`\\\n`, Text, nil}, {`\\`, Text, nil}, Include("keywords"), {`(def)((?:\s|\\\s)+)`, ByGroups(Keyword, Text), Push("funcname")}, {`(class)((?:\s|\\\s)+)`, ByGroups(Keyword, Text), Push("classname")}, {`(from)((?:\s|\\\s)+)`, ByGroups(KeywordNamespace, Text), Push("fromimport")}, {`(import)((?:\s|\\\s)+)`, ByGroups(KeywordNamespace, Text), Push("import")}, Include("expr"), }, "expr": { {`(?i)(rf|fr)(""")`, ByGroups(LiteralStringAffix, LiteralStringDouble), Combined("rfstringescape", "tdqf")}, {`(?i)(rf|fr)(''')`, ByGroups(LiteralStringAffix, LiteralStringSingle), Combined("rfstringescape", "tsqf")}, {`(?i)(rf|fr)(")`, ByGroups(LiteralStringAffix, LiteralStringDouble), Combined("rfstringescape", "dqf")}, {`(?i)(rf|fr)(')`, ByGroups(LiteralStringAffix, LiteralStringSingle), Combined("rfstringescape", "sqf")}, {`([fF])(""")`, ByGroups(LiteralStringAffix, LiteralStringDouble), Combined("fstringescape", "tdqf")}, {`([fF])(''')`, ByGroups(LiteralStringAffix, LiteralStringSingle), Combined("fstringescape", "tsqf")}, {`([fF])(")`, ByGroups(LiteralStringAffix, LiteralStringDouble), Combined("fstringescape", "dqf")}, {`([fF])(')`, ByGroups(LiteralStringAffix, LiteralStringSingle), Combined("fstringescape", "sqf")}, {`(?i)(rb|br|r)(""")`, ByGroups(LiteralStringAffix, LiteralStringDouble), Push("tdqs")}, {`(?i)(rb|br|r)(''')`, ByGroups(LiteralStringAffix, LiteralStringSingle), Push("tsqs")}, {`(?i)(rb|br|r)(")`, ByGroups(LiteralStringAffix, LiteralStringDouble), Push("dqs")}, {`(?i)(rb|br|r)(')`, ByGroups(LiteralStringAffix, LiteralStringSingle), Push("sqs")}, {`([uUbB]?)(""")`, ByGroups(LiteralStringAffix, LiteralStringDouble), Combined("stringescape", "tdqs")}, {`([uUbB]?)(''')`, ByGroups(LiteralStringAffix, LiteralStringSingle), Combined("stringescape", "tsqs")}, {`([uUbB]?)(")`, ByGroups(LiteralStringAffix, LiteralStringDouble), Combined("stringescape", "dqs")}, {`([uUbB]?)(')`, ByGroups(LiteralStringAffix, LiteralStringSingle), Combined("stringescape", "sqs")}, {`[^\S\n]+`, Text, nil}, Include("numbers"), {`!=|==|<<|>>|:=|[-~+/*%=<>&^|.]`, Operator, nil}, {`[]{}:(),;[]`, Punctuation, nil}, {`(in|is|and|or|not)\b`, OperatorWord, nil}, Include("expr-keywords"), Include("builtins"), Include("magicfuncs"), Include("magicvars"), Include("name"), }, "expr-inside-fstring": { {`[{([]`, Punctuation, Push("expr-inside-fstring-inner")}, {`(=\s*)?(\![sraf])?\}`, LiteralStringInterpol, Pop(1)}, {`(=\s*)?(\![sraf])?:`, LiteralStringInterpol, Pop(1)}, {`\s+`, Text, nil}, Include("expr"), }, "expr-inside-fstring-inner": { {`[{([]`, Punctuation, Push("expr-inside-fstring-inner")}, {`[])}]`, Punctuation, Pop(1)}, {`\s+`, Text, nil}, Include("expr"), }, "expr-keywords": { {Words(``, `\b`, `async for`, `await`, `else`, `for`, `if`, `lambda`, `yield`, `yield from`), Keyword, nil}, {Words(``, `\b`, `True`, `False`, `None`), KeywordConstant, nil}, }, "keywords": { {Words(``, `\b`, `assert`, `async`, `await`, `break`, `continue`, `del`, `elif`, `else`, `except`, `finally`, `for`, `global`, `if`, `lambda`, `pass`, `raise`, `nonlocal`, `return`, `try`, `while`, `yield`, `yield from`, `as`, `with`), Keyword, nil}, {Words(``, `\b`, `True`, `False`, `None`), KeywordConstant, nil}, }, "builtins": { {Words(`(?=\^])?[-+ ]?#?0?(\d+)?,?(\.\d+)?[E-GXb-gnosx%]?)?\}`, LiteralStringInterpol, nil}, {`[^\\\'"%{\n]+`, LiteralStringSingle, nil}, {`[\'"\\]`, LiteralStringSingle, nil}, {`%|(\{{1,2})`, LiteralStringSingle, nil}, }, "strings-double": { {`%(\(\w+\))?[-#0 +]*([0-9]+|[*])?(\.([0-9]+|[*]))?[hlL]?[E-GXc-giorsaux%]`, LiteralStringInterpol, nil}, {`\{((\w+)((\.\w+)|(\[[^\]]+\]))*)?(\![sra])?(\:(.?[<>=\^])?[-+ ]?#?0?(\d+)?,?(\.\d+)?[E-GXb-gnosx%]?)?\}`, LiteralStringInterpol, nil}, {`[^\\\'"%{\n]+`, LiteralStringDouble, nil}, {`[\'"\\]`, LiteralStringDouble, nil}, {`%|(\{{1,2})`, LiteralStringDouble, nil}, }, "dqf": { {`"`, LiteralStringDouble, Pop(1)}, {`\\\\|\\"|\\\n`, LiteralStringEscape, nil}, Include("fstrings-double"), }, "sqf": { {`'`, LiteralStringSingle, Pop(1)}, {`\\\\|\\'|\\\n`, LiteralStringEscape, nil}, Include("fstrings-single"), }, "dqs": { {`"`, LiteralStringDouble, Pop(1)}, {`\\\\|\\"|\\\n`, LiteralStringEscape, nil}, Include("strings-double"), }, "sqs": { {`'`, LiteralStringSingle, Pop(1)}, {`\\\\|\\'|\\\n`, LiteralStringEscape, nil}, Include("strings-single"), }, "tdqf": { {`"""`, LiteralStringDouble, Pop(1)}, Include("fstrings-double"), {`\n`, LiteralStringDouble, nil}, }, "tsqf": { {`'''`, LiteralStringSingle, Pop(1)}, Include("fstrings-single"), {`\n`, LiteralStringSingle, nil}, }, "tdqs": { {`"""`, LiteralStringDouble, Pop(1)}, Include("strings-double"), {`\n`, LiteralStringDouble, nil}, }, "tsqs": { {`'''`, LiteralStringSingle, Pop(1)}, Include("strings-single"), {`\n`, LiteralStringSingle, nil}, }, } }