felix.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. """
  2. pygments.lexers.felix
  3. ~~~~~~~~~~~~~~~~~~~~~
  4. Lexer for the Felix language.
  5. :copyright: Copyright 2006-2021 by the Pygments team, see AUTHORS.
  6. :license: BSD, see LICENSE for details.
  7. """
  8. from pygments.lexer import RegexLexer, include, bygroups, default, words, \
  9. combined
  10. from pygments.token import Text, Comment, Operator, Keyword, Name, String, \
  11. Number, Punctuation
  12. __all__ = ['FelixLexer']
  13. class FelixLexer(RegexLexer):
  14. """
  15. For `Felix <http://www.felix-lang.org>`_ source code.
  16. .. versionadded:: 1.2
  17. """
  18. name = 'Felix'
  19. aliases = ['felix', 'flx']
  20. filenames = ['*.flx', '*.flxh']
  21. mimetypes = ['text/x-felix']
  22. preproc = (
  23. 'elif', 'else', 'endif', 'if', 'ifdef', 'ifndef',
  24. )
  25. keywords = (
  26. '_', '_deref', 'all', 'as',
  27. 'assert', 'attempt', 'call', 'callback', 'case', 'caseno', 'cclass',
  28. 'code', 'compound', 'ctypes', 'do', 'done', 'downto', 'elif', 'else',
  29. 'endattempt', 'endcase', 'endif', 'endmatch', 'enum', 'except',
  30. 'exceptions', 'expect', 'finally', 'for', 'forall', 'forget', 'fork',
  31. 'functor', 'goto', 'ident', 'if', 'incomplete', 'inherit', 'instance',
  32. 'interface', 'jump', 'lambda', 'loop', 'match', 'module', 'namespace',
  33. 'new', 'noexpand', 'nonterm', 'obj', 'of', 'open', 'parse', 'raise',
  34. 'regexp', 'reglex', 'regmatch', 'rename', 'return', 'the', 'then',
  35. 'to', 'type', 'typecase', 'typedef', 'typematch', 'typeof', 'upto',
  36. 'when', 'whilst', 'with', 'yield',
  37. )
  38. keyword_directives = (
  39. '_gc_pointer', '_gc_type', 'body', 'comment', 'const', 'export',
  40. 'header', 'inline', 'lval', 'macro', 'noinline', 'noreturn',
  41. 'package', 'private', 'pod', 'property', 'public', 'publish',
  42. 'requires', 'todo', 'virtual', 'use',
  43. )
  44. keyword_declarations = (
  45. 'def', 'let', 'ref', 'val', 'var',
  46. )
  47. keyword_types = (
  48. 'unit', 'void', 'any', 'bool',
  49. 'byte', 'offset',
  50. 'address', 'caddress', 'cvaddress', 'vaddress',
  51. 'tiny', 'short', 'int', 'long', 'vlong',
  52. 'utiny', 'ushort', 'vshort', 'uint', 'ulong', 'uvlong',
  53. 'int8', 'int16', 'int32', 'int64',
  54. 'uint8', 'uint16', 'uint32', 'uint64',
  55. 'float', 'double', 'ldouble',
  56. 'complex', 'dcomplex', 'lcomplex',
  57. 'imaginary', 'dimaginary', 'limaginary',
  58. 'char', 'wchar', 'uchar',
  59. 'charp', 'charcp', 'ucharp', 'ucharcp',
  60. 'string', 'wstring', 'ustring',
  61. 'cont',
  62. 'array', 'varray', 'list',
  63. 'lvalue', 'opt', 'slice',
  64. )
  65. keyword_constants = (
  66. 'false', 'true',
  67. )
  68. operator_words = (
  69. 'and', 'not', 'in', 'is', 'isin', 'or', 'xor',
  70. )
  71. name_builtins = (
  72. '_svc', 'while',
  73. )
  74. name_pseudo = (
  75. 'root', 'self', 'this',
  76. )
  77. decimal_suffixes = '([tTsSiIlLvV]|ll|LL|([iIuU])(8|16|32|64))?'
  78. tokens = {
  79. 'root': [
  80. include('whitespace'),
  81. # Keywords
  82. (words(('axiom', 'ctor', 'fun', 'gen', 'proc', 'reduce',
  83. 'union'), suffix=r'\b'),
  84. Keyword, 'funcname'),
  85. (words(('class', 'cclass', 'cstruct', 'obj', 'struct'), suffix=r'\b'),
  86. Keyword, 'classname'),
  87. (r'(instance|module|typeclass)\b', Keyword, 'modulename'),
  88. (words(keywords, suffix=r'\b'), Keyword),
  89. (words(keyword_directives, suffix=r'\b'), Name.Decorator),
  90. (words(keyword_declarations, suffix=r'\b'), Keyword.Declaration),
  91. (words(keyword_types, suffix=r'\b'), Keyword.Type),
  92. (words(keyword_constants, suffix=r'\b'), Keyword.Constant),
  93. # Operators
  94. include('operators'),
  95. # Float Literal
  96. # -- Hex Float
  97. (r'0[xX]([0-9a-fA-F_]*\.[0-9a-fA-F_]+|[0-9a-fA-F_]+)'
  98. r'[pP][+\-]?[0-9_]+[lLfFdD]?', Number.Float),
  99. # -- DecimalFloat
  100. (r'[0-9_]+(\.[0-9_]+[eE][+\-]?[0-9_]+|'
  101. r'\.[0-9_]*|[eE][+\-]?[0-9_]+)[lLfFdD]?', Number.Float),
  102. (r'\.(0|[1-9][0-9_]*)([eE][+\-]?[0-9_]+)?[lLfFdD]?',
  103. Number.Float),
  104. # IntegerLiteral
  105. # -- Binary
  106. (r'0[Bb][01_]+%s' % decimal_suffixes, Number.Bin),
  107. # -- Octal
  108. (r'0[0-7_]+%s' % decimal_suffixes, Number.Oct),
  109. # -- Hexadecimal
  110. (r'0[xX][0-9a-fA-F_]+%s' % decimal_suffixes, Number.Hex),
  111. # -- Decimal
  112. (r'(0|[1-9][0-9_]*)%s' % decimal_suffixes, Number.Integer),
  113. # Strings
  114. ('([rR][cC]?|[cC][rR])"""', String, 'tdqs'),
  115. ("([rR][cC]?|[cC][rR])'''", String, 'tsqs'),
  116. ('([rR][cC]?|[cC][rR])"', String, 'dqs'),
  117. ("([rR][cC]?|[cC][rR])'", String, 'sqs'),
  118. ('[cCfFqQwWuU]?"""', String, combined('stringescape', 'tdqs')),
  119. ("[cCfFqQwWuU]?'''", String, combined('stringescape', 'tsqs')),
  120. ('[cCfFqQwWuU]?"', String, combined('stringescape', 'dqs')),
  121. ("[cCfFqQwWuU]?'", String, combined('stringescape', 'sqs')),
  122. # Punctuation
  123. (r'[\[\]{}:(),;?]', Punctuation),
  124. # Labels
  125. (r'[a-zA-Z_]\w*:>', Name.Label),
  126. # Identifiers
  127. (r'(%s)\b' % '|'.join(name_builtins), Name.Builtin),
  128. (r'(%s)\b' % '|'.join(name_pseudo), Name.Builtin.Pseudo),
  129. (r'[a-zA-Z_]\w*', Name),
  130. ],
  131. 'whitespace': [
  132. (r'\n', Text),
  133. (r'\s+', Text),
  134. include('comment'),
  135. # Preprocessor
  136. (r'#\s*if\s+0', Comment.Preproc, 'if0'),
  137. (r'#', Comment.Preproc, 'macro'),
  138. ],
  139. 'operators': [
  140. (r'(%s)\b' % '|'.join(operator_words), Operator.Word),
  141. (r'!=|==|<<|>>|\|\||&&|[-~+/*%=<>&^|.$]', Operator),
  142. ],
  143. 'comment': [
  144. (r'//(.*?)\n', Comment.Single),
  145. (r'/[*]', Comment.Multiline, 'comment2'),
  146. ],
  147. 'comment2': [
  148. (r'[^/*]', Comment.Multiline),
  149. (r'/[*]', Comment.Multiline, '#push'),
  150. (r'[*]/', Comment.Multiline, '#pop'),
  151. (r'[/*]', Comment.Multiline),
  152. ],
  153. 'if0': [
  154. (r'^\s*#if.*?(?<!\\)\n', Comment, '#push'),
  155. (r'^\s*#endif.*?(?<!\\)\n', Comment, '#pop'),
  156. (r'.*?\n', Comment),
  157. ],
  158. 'macro': [
  159. include('comment'),
  160. (r'(import|include)(\s+)(<[^>]*?>)',
  161. bygroups(Comment.Preproc, Text, String), '#pop'),
  162. (r'(import|include)(\s+)("[^"]*?")',
  163. bygroups(Comment.Preproc, Text, String), '#pop'),
  164. (r"(import|include)(\s+)('[^']*?')",
  165. bygroups(Comment.Preproc, Text, String), '#pop'),
  166. (r'[^/\n]+', Comment.Preproc),
  167. # (r'/[*](.|\n)*?[*]/', Comment),
  168. # (r'//.*?\n', Comment, '#pop'),
  169. (r'/', Comment.Preproc),
  170. (r'(?<=\\)\n', Comment.Preproc),
  171. (r'\n', Comment.Preproc, '#pop'),
  172. ],
  173. 'funcname': [
  174. include('whitespace'),
  175. (r'[a-zA-Z_]\w*', Name.Function, '#pop'),
  176. # anonymous functions
  177. (r'(?=\()', Text, '#pop'),
  178. ],
  179. 'classname': [
  180. include('whitespace'),
  181. (r'[a-zA-Z_]\w*', Name.Class, '#pop'),
  182. # anonymous classes
  183. (r'(?=\{)', Text, '#pop'),
  184. ],
  185. 'modulename': [
  186. include('whitespace'),
  187. (r'\[', Punctuation, ('modulename2', 'tvarlist')),
  188. default('modulename2'),
  189. ],
  190. 'modulename2': [
  191. include('whitespace'),
  192. (r'([a-zA-Z_]\w*)', Name.Namespace, '#pop:2'),
  193. ],
  194. 'tvarlist': [
  195. include('whitespace'),
  196. include('operators'),
  197. (r'\[', Punctuation, '#push'),
  198. (r'\]', Punctuation, '#pop'),
  199. (r',', Punctuation),
  200. (r'(with|where)\b', Keyword),
  201. (r'[a-zA-Z_]\w*', Name),
  202. ],
  203. 'stringescape': [
  204. (r'\\([\\abfnrtv"\']|\n|N\{.*?\}|u[a-fA-F0-9]{4}|'
  205. r'U[a-fA-F0-9]{8}|x[a-fA-F0-9]{2}|[0-7]{1,3})', String.Escape)
  206. ],
  207. 'strings': [
  208. (r'%(\([a-zA-Z0-9]+\))?[-#0 +]*([0-9]+|[*])?(\.([0-9]+|[*]))?'
  209. '[hlL]?[E-GXc-giorsux%]', String.Interpol),
  210. (r'[^\\\'"%\n]+', String),
  211. # quotes, percents and backslashes must be parsed one at a time
  212. (r'[\'"\\]', String),
  213. # unhandled string formatting sign
  214. (r'%', String)
  215. # newlines are an error (use "nl" state)
  216. ],
  217. 'nl': [
  218. (r'\n', String)
  219. ],
  220. 'dqs': [
  221. (r'"', String, '#pop'),
  222. # included here again for raw strings
  223. (r'\\\\|\\"|\\\n', String.Escape),
  224. include('strings')
  225. ],
  226. 'sqs': [
  227. (r"'", String, '#pop'),
  228. # included here again for raw strings
  229. (r"\\\\|\\'|\\\n", String.Escape),
  230. include('strings')
  231. ],
  232. 'tdqs': [
  233. (r'"""', String, '#pop'),
  234. include('strings'),
  235. include('nl')
  236. ],
  237. 'tsqs': [
  238. (r"'''", String, '#pop'),
  239. include('strings'),
  240. include('nl')
  241. ],
  242. }