felix.py 9.2 KB

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