...

Source file src/github.com/alecthomas/chroma/lexers/c/cython.go

Documentation: github.com/alecthomas/chroma/lexers/c

     1  package c
     2  
     3  import (
     4  	. "github.com/alecthomas/chroma" // nolint
     5  	"github.com/alecthomas/chroma/lexers/internal"
     6  )
     7  
     8  // Cython lexer.
     9  var Cython = internal.Register(MustNewLazyLexer(
    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  	cythonRules,
    17  ))
    18  
    19  func cythonRules() Rules {
    20  	return Rules{
    21  		"root": {
    22  			{`\n`, Text, nil},
    23  			{`^(\s*)("""(?:.|\n)*?""")`, ByGroups(Text, LiteralStringDoc), nil},
    24  			{`^(\s*)('''(?:.|\n)*?''')`, ByGroups(Text, LiteralStringDoc), nil},
    25  			{`[^\S\n]+`, Text, nil},
    26  			{`#.*$`, Comment, nil},
    27  			{`[]{}:(),;[]`, Punctuation, nil},
    28  			{`\\\n`, Text, nil},
    29  			{`\\`, Text, nil},
    30  			{`(in|is|and|or|not)\b`, OperatorWord, nil},
    31  			{`(<)([a-zA-Z0-9.?]+)(>)`, ByGroups(Punctuation, KeywordType, Punctuation), nil},
    32  			{`!=|==|<<|>>|[-~+/*%=<>&^|.?]`, Operator, nil},
    33  			{`(from)(\d+)(<=)(\s+)(<)(\d+)(:)`, ByGroups(Keyword, LiteralNumberInteger, Operator, Name, Operator, Name, Punctuation), nil},
    34  			Include("keywords"),
    35  			{`(def|property)(\s+)`, ByGroups(Keyword, Text), Push("funcname")},
    36  			{`(cp?def)(\s+)`, ByGroups(Keyword, Text), Push("cdef")},
    37  			{`(cdef)(:)`, ByGroups(Keyword, Punctuation), nil},
    38  			{`(class|struct)(\s+)`, ByGroups(Keyword, Text), Push("classname")},
    39  			{`(from)(\s+)`, ByGroups(Keyword, Text), Push("fromimport")},
    40  			{`(c?import)(\s+)`, ByGroups(Keyword, Text), Push("import")},
    41  			Include("builtins"),
    42  			Include("backtick"),
    43  			{`(?:[rR]|[uU][rR]|[rR][uU])"""`, LiteralString, Push("tdqs")},
    44  			{`(?:[rR]|[uU][rR]|[rR][uU])'''`, LiteralString, Push("tsqs")},
    45  			{`(?:[rR]|[uU][rR]|[rR][uU])"`, LiteralString, Push("dqs")},
    46  			{`(?:[rR]|[uU][rR]|[rR][uU])'`, LiteralString, Push("sqs")},
    47  			{`[uU]?"""`, LiteralString, Combined("stringescape", "tdqs")},
    48  			{`[uU]?'''`, LiteralString, Combined("stringescape", "tsqs")},
    49  			{`[uU]?"`, LiteralString, Combined("stringescape", "dqs")},
    50  			{`[uU]?'`, LiteralString, Combined("stringescape", "sqs")},
    51  			Include("name"),
    52  			Include("numbers"),
    53  		},
    54  		"keywords": {
    55  			{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},
    56  			{`(DEF|IF|ELIF|ELSE)\b`, CommentPreproc, nil},
    57  		},
    58  		"builtins": {
    59  			{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},
    60  			{`(?<!\.)(self|None|Ellipsis|NotImplemented|False|True|NULL)\b`, NameBuiltinPseudo, nil},
    61  			{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},
    62  		},
    63  		"numbers": {
    64  			{`(\d+\.?\d*|\d*\.\d+)([eE][+-]?[0-9]+)?`, LiteralNumberFloat, nil},
    65  			{`0\d+`, LiteralNumberOct, nil},
    66  			{`0[xX][a-fA-F0-9]+`, LiteralNumberHex, nil},
    67  			{`\d+L`, LiteralNumberIntegerLong, nil},
    68  			{`\d+`, LiteralNumberInteger, nil},
    69  		},
    70  		"backtick": {
    71  			{"`.*?`", LiteralStringBacktick, nil},
    72  		},
    73  		"name": {
    74  			{`@\w+`, NameDecorator, nil},
    75  			{`[a-zA-Z_]\w*`, Name, nil},
    76  		},
    77  		"funcname": {
    78  			{`[a-zA-Z_]\w*`, NameFunction, Pop(1)},
    79  		},
    80  		"cdef": {
    81  			{`(public|readonly|extern|api|inline)\b`, KeywordReserved, nil},
    82  			{`(struct|enum|union|class)\b`, Keyword, nil},
    83  			{`([a-zA-Z_]\w*)(\s*)(?=[(:#=]|$)`, ByGroups(NameFunction, Text), Pop(1)},
    84  			{`([a-zA-Z_]\w*)(\s*)(,)`, ByGroups(NameFunction, Text, Punctuation), nil},
    85  			{`from\b`, Keyword, Pop(1)},
    86  			{`as\b`, Keyword, nil},
    87  			{`:`, Punctuation, Pop(1)},
    88  			{`(?=["\'])`, Text, Pop(1)},
    89  			{`[a-zA-Z_]\w*`, KeywordType, nil},
    90  			{`.`, Text, nil},
    91  		},
    92  		"classname": {
    93  			{`[a-zA-Z_]\w*`, NameClass, Pop(1)},
    94  		},
    95  		"import": {
    96  			{`(\s+)(as)(\s+)`, ByGroups(Text, Keyword, Text), nil},
    97  			{`[a-zA-Z_][\w.]*`, NameNamespace, nil},
    98  			{`(\s*)(,)(\s*)`, ByGroups(Text, Operator, Text), nil},
    99  			Default(Pop(1)),
   100  		},
   101  		"fromimport": {
   102  			{`(\s+)(c?import)\b`, ByGroups(Text, Keyword), Pop(1)},
   103  			{`[a-zA-Z_.][\w.]*`, NameNamespace, nil},
   104  			Default(Pop(1)),
   105  		},
   106  		"stringescape": {
   107  			{`\\([\\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},
   108  		},
   109  		"strings": {
   110  			{`%(\([a-zA-Z0-9]+\))?[-#0 +]*([0-9]+|[*])?(\.([0-9]+|[*]))?[hlL]?[E-GXc-giorsux%]`, LiteralStringInterpol, nil},
   111  			{`[^\\\'"%\n]+`, LiteralString, nil},
   112  			{`[\'"\\]`, LiteralString, nil},
   113  			{`%`, LiteralString, nil},
   114  		},
   115  		"nl": {
   116  			{`\n`, LiteralString, nil},
   117  		},
   118  		"dqs": {
   119  			{`"`, LiteralString, Pop(1)},
   120  			{`\\\\|\\"|\\\n`, LiteralStringEscape, nil},
   121  			Include("strings"),
   122  		},
   123  		"sqs": {
   124  			{`'`, LiteralString, Pop(1)},
   125  			{`\\\\|\\'|\\\n`, LiteralStringEscape, nil},
   126  			Include("strings"),
   127  		},
   128  		"tdqs": {
   129  			{`"""`, LiteralString, Pop(1)},
   130  			Include("strings"),
   131  			Include("nl"),
   132  		},
   133  		"tsqs": {
   134  			{`'''`, LiteralString, Pop(1)},
   135  			Include("strings"),
   136  			Include("nl"),
   137  		},
   138  	}
   139  }
   140  

View as plain text