You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cython.go 6.3KB

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