cython.go

  1package c
  2
  3import (
  4	. "github.com/alecthomas/chroma" // nolint
  5	"github.com/alecthomas/chroma/lexers/internal"
  6)
  7
  8// Cython lexer.
  9var Cython = internal.Register(MustNewLexer(
 10	&Config{
 11		Name:      "Cython",
 12		Aliases:   []string{"cython", "pyx", "pyrex"},
 13		Filenames: []string{"*.pyx", "*.pxd", "*.pxi"},
 14		MimeTypes: []string{"text/x-cython", "application/x-cython"},
 15	},
 16	Rules{
 17		"root": {
 18			{`\n`, Text, nil},
 19			{`^(\s*)("""(?:.|\n)*?""")`, ByGroups(Text, LiteralStringDoc), nil},
 20			{`^(\s*)('''(?:.|\n)*?''')`, ByGroups(Text, LiteralStringDoc), nil},
 21			{`[^\S\n]+`, Text, nil},
 22			{`#.*$`, Comment, nil},
 23			{`[]{}:(),;[]`, Punctuation, nil},
 24			{`\\\n`, Text, nil},
 25			{`\\`, Text, nil},
 26			{`(in|is|and|or|not)\b`, OperatorWord, nil},
 27			{`(<)([a-zA-Z0-9.?]+)(>)`, ByGroups(Punctuation, KeywordType, Punctuation), nil},
 28			{`!=|==|<<|>>|[-~+/*%=<>&^|.?]`, Operator, nil},
 29			{`(from)(\d+)(<=)(\s+)(<)(\d+)(:)`, ByGroups(Keyword, LiteralNumberInteger, Operator, Name, Operator, Name, Punctuation), nil},
 30			Include("keywords"),
 31			{`(def|property)(\s+)`, ByGroups(Keyword, Text), Push("funcname")},
 32			{`(cp?def)(\s+)`, ByGroups(Keyword, Text), Push("cdef")},
 33			{`(cdef)(:)`, ByGroups(Keyword, Punctuation), nil},
 34			{`(class|struct)(\s+)`, ByGroups(Keyword, Text), Push("classname")},
 35			{`(from)(\s+)`, ByGroups(Keyword, Text), Push("fromimport")},
 36			{`(c?import)(\s+)`, ByGroups(Keyword, Text), Push("import")},
 37			Include("builtins"),
 38			Include("backtick"),
 39			{`(?:[rR]|[uU][rR]|[rR][uU])"""`, LiteralString, Push("tdqs")},
 40			{`(?:[rR]|[uU][rR]|[rR][uU])'''`, LiteralString, Push("tsqs")},
 41			{`(?:[rR]|[uU][rR]|[rR][uU])"`, LiteralString, Push("dqs")},
 42			{`(?:[rR]|[uU][rR]|[rR][uU])'`, LiteralString, Push("sqs")},
 43			{`[uU]?"""`, LiteralString, Combined("stringescape", "tdqs")},
 44			{`[uU]?'''`, LiteralString, Combined("stringescape", "tsqs")},
 45			{`[uU]?"`, LiteralString, Combined("stringescape", "dqs")},
 46			{`[uU]?'`, LiteralString, Combined("stringescape", "sqs")},
 47			Include("name"),
 48			Include("numbers"),
 49		},
 50		"keywords": {
 51			{Words(``, `\b`, `assert`, `break`, `by`, `continue`, `ctypedef`, `del`, `elif`, `else`, `except`, `except?`, `exec`, `finally`, `for`, `fused`, `gil`, `global`, `if`, `include`, `lambda`, `nogil`, `pass`, `print`, `raise`, `return`, `try`, `while`, `yield`, `as`, `with`), Keyword, nil},
 52			{`(DEF|IF|ELIF|ELSE)\b`, CommentPreproc, nil},
 53		},
 54		"builtins": {
 55			{Words(`(?<!\.)`, `\b`, `__import__`, `abs`, `all`, `any`, `apply`, `basestring`, `bin`, `bool`, `buffer`, `bytearray`, `bytes`, `callable`, `chr`, `classmethod`, `cmp`, `coerce`, `compile`, `complex`, `delattr`, `dict`, `dir`, `divmod`, `enumerate`, `eval`, `execfile`, `exit`, `file`, `filter`, `float`, `frozenset`, `getattr`, `globals`, `hasattr`, `hash`, `hex`, `id`, `input`, `int`, `intern`, `isinstance`, `issubclass`, `iter`, `len`, `list`, `locals`, `long`, `map`, `max`, `min`, `next`, `object`, `oct`, `open`, `ord`, `pow`, `property`, `range`, `raw_input`, `reduce`, `reload`, `repr`, `reversed`, `round`, `set`, `setattr`, `slice`, `sorted`, `staticmethod`, `str`, `sum`, `super`, `tuple`, `type`, `unichr`, `unicode`, `unsigned`, `vars`, `xrange`, `zip`), NameBuiltin, nil},
 56			{`(?<!\.)(self|None|Ellipsis|NotImplemented|False|True|NULL)\b`, NameBuiltinPseudo, nil},
 57			{Words(`(?<!\.)`, `\b`, `ArithmeticError`, `AssertionError`, `AttributeError`, `BaseException`, `DeprecationWarning`, `EOFError`, `EnvironmentError`, `Exception`, `FloatingPointError`, `FutureWarning`, `GeneratorExit`, `IOError`, `ImportError`, `ImportWarning`, `IndentationError`, `IndexError`, `KeyError`, `KeyboardInterrupt`, `LookupError`, `MemoryError`, `NameError`, `NotImplemented`, `NotImplementedError`, `OSError`, `OverflowError`, `OverflowWarning`, `PendingDeprecationWarning`, `ReferenceError`, `RuntimeError`, `RuntimeWarning`, `StandardError`, `StopIteration`, `SyntaxError`, `SyntaxWarning`, `SystemError`, `SystemExit`, `TabError`, `TypeError`, `UnboundLocalError`, `UnicodeDecodeError`, `UnicodeEncodeError`, `UnicodeError`, `UnicodeTranslateError`, `UnicodeWarning`, `UserWarning`, `ValueError`, `Warning`, `ZeroDivisionError`), NameException, nil},
 58		},
 59		"numbers": {
 60			{`(\d+\.?\d*|\d*\.\d+)([eE][+-]?[0-9]+)?`, LiteralNumberFloat, nil},
 61			{`0\d+`, LiteralNumberOct, nil},
 62			{`0[xX][a-fA-F0-9]+`, LiteralNumberHex, nil},
 63			{`\d+L`, LiteralNumberIntegerLong, nil},
 64			{`\d+`, LiteralNumberInteger, nil},
 65		},
 66		"backtick": {
 67			{"`.*?`", LiteralStringBacktick, nil},
 68		},
 69		"name": {
 70			{`@\w+`, NameDecorator, nil},
 71			{`[a-zA-Z_]\w*`, Name, nil},
 72		},
 73		"funcname": {
 74			{`[a-zA-Z_]\w*`, NameFunction, Pop(1)},
 75		},
 76		"cdef": {
 77			{`(public|readonly|extern|api|inline)\b`, KeywordReserved, nil},
 78			{`(struct|enum|union|class)\b`, Keyword, nil},
 79			{`([a-zA-Z_]\w*)(\s*)(?=[(:#=]|$)`, ByGroups(NameFunction, Text), Pop(1)},
 80			{`([a-zA-Z_]\w*)(\s*)(,)`, ByGroups(NameFunction, Text, Punctuation), nil},
 81			{`from\b`, Keyword, Pop(1)},
 82			{`as\b`, Keyword, nil},
 83			{`:`, Punctuation, Pop(1)},
 84			{`(?=["\'])`, Text, Pop(1)},
 85			{`[a-zA-Z_]\w*`, KeywordType, nil},
 86			{`.`, Text, nil},
 87		},
 88		"classname": {
 89			{`[a-zA-Z_]\w*`, NameClass, Pop(1)},
 90		},
 91		"import": {
 92			{`(\s+)(as)(\s+)`, ByGroups(Text, Keyword, Text), nil},
 93			{`[a-zA-Z_][\w.]*`, NameNamespace, nil},
 94			{`(\s*)(,)(\s*)`, ByGroups(Text, Operator, Text), nil},
 95			Default(Pop(1)),
 96		},
 97		"fromimport": {
 98			{`(\s+)(c?import)\b`, ByGroups(Text, Keyword), Pop(1)},
 99			{`[a-zA-Z_.][\w.]*`, NameNamespace, nil},
100			Default(Pop(1)),
101		},
102		"stringescape": {
103			{`\\([\\abfnrtv"\']|\n|N\{.*?\}|u[a-fA-F0-9]{4}|U[a-fA-F0-9]{8}|x[a-fA-F0-9]{2}|[0-7]{1,3})`, LiteralStringEscape, nil},
104		},
105		"strings": {
106			{`%(\([a-zA-Z0-9]+\))?[-#0 +]*([0-9]+|[*])?(\.([0-9]+|[*]))?[hlL]?[E-GXc-giorsux%]`, LiteralStringInterpol, nil},
107			{`[^\\\'"%\n]+`, LiteralString, nil},
108			{`[\'"\\]`, LiteralString, nil},
109			{`%`, LiteralString, nil},
110		},
111		"nl": {
112			{`\n`, LiteralString, nil},
113		},
114		"dqs": {
115			{`"`, LiteralString, Pop(1)},
116			{`\\\\|\\"|\\\n`, LiteralStringEscape, nil},
117			Include("strings"),
118		},
119		"sqs": {
120			{`'`, LiteralString, Pop(1)},
121			{`\\\\|\\'|\\\n`, LiteralStringEscape, nil},
122			Include("strings"),
123		},
124		"tdqs": {
125			{`"""`, LiteralString, Pop(1)},
126			Include("strings"),
127			Include("nl"),
128		},
129		"tsqs": {
130			{`'''`, LiteralString, Pop(1)},
131			Include("strings"),
132			Include("nl"),
133		},
134	},
135))