func.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. """
  2. pygments.lexers.func
  3. ~~~~~~~~~~~~~~~~~~~~
  4. Lexers for FunC.
  5. :copyright: Copyright 2006-2023 by the Pygments team, see AUTHORS.
  6. :license: BSD, see LICENSE for details.
  7. """
  8. from pygments.lexer import RegexLexer, include, words
  9. from pygments.token import Text, Comment, Operator, Keyword, Name, String, \
  10. Number, Whitespace, Punctuation
  11. __all__ = ['FuncLexer']
  12. class FuncLexer(RegexLexer):
  13. """
  14. For FunC source code.
  15. """
  16. name = 'FunC'
  17. aliases = ['func', 'fc']
  18. filenames = ['*.fc', '*.func']
  19. # 1. Does not start from "
  20. # 2. Can start from ` and end with `, containing any character
  21. # 3. Starts with underscore or { or } and have more than 1 character after it
  22. # 4. Starts with letter, contains letters, numbers and underscores
  23. identifier = r'(?!")(`([^`]+)`|((?=_)_|(?=\{)\{|(?=\})\}|(?![_`{}]))([^;,\[\]\(\)\s~.]+))'
  24. tokens = {
  25. 'root': [
  26. (r'\n', Whitespace),
  27. (r'\s+', Whitespace),
  28. include('keywords'),
  29. include('strings'),
  30. include('directives'),
  31. include('numeric'),
  32. include('comments'),
  33. include('storage'),
  34. include('functions'),
  35. include('variables'),
  36. (r'[.;(),\[\]~{}]', Punctuation)
  37. ],
  38. 'keywords': [
  39. (words((
  40. '<=>', '>=', '<=', '!=', '==', '^>>', '~>>',
  41. '>>', '<<', '/%', '^%', '~%', '^/', '~/', '+=',
  42. '-=', '*=', '/=', '~/=', '^/=', '%=', '^%=', '<<=',
  43. '>>=', '~>>=', '^>>=', '&=', '|=', '^=', '^', '=',
  44. '~', '/', '%', '-', '*', '+','>',
  45. '<', '&', '|', ':', '?'), prefix=r'(?<=\s)', suffix=r'(?=\s)'),
  46. Operator),
  47. (words((
  48. 'if', 'ifnot',
  49. 'else', 'elseif', 'elseifnot',
  50. 'while', 'do', 'until', 'repeat',
  51. 'return', 'impure', 'method_id',
  52. 'forall', 'asm', 'inline', 'inline_ref'), prefix=r'\b', suffix=r'\b'),
  53. Keyword),
  54. (words(('true', 'false'), prefix=r'\b', suffix=r'\b'), Keyword.Constant),
  55. ],
  56. 'directives': [
  57. (r'#include|#pragma', Keyword, 'directive'),
  58. ],
  59. 'directive': [
  60. include('strings'),
  61. (r'\s+', Whitespace),
  62. (r'version|not-version', Keyword),
  63. (r'(>=|<=|=|>|<|\^)?([0-9]+)(.[0-9]+)?(.[0-9]+)?', Number), # version
  64. (r';', Text, '#pop')
  65. ],
  66. 'strings': [
  67. (r'\"([^\n\"]+)\"[Hhcusa]?', String),
  68. ],
  69. 'numeric': [
  70. (r'\b(-?(?!_)([\d_]+|0x[\d_a-fA-F]+)|0b[1_0]+)(?<!_)(?=[\s\)\],;])', Number)
  71. ],
  72. 'comments': [
  73. (r';;([^\n]*)', Comment.Singleline),
  74. (r'\{-', Comment.Multiline, 'comment'),
  75. ],
  76. 'comment': [
  77. (r'[^-}{]+', Comment.Multiline),
  78. (r'\{-', Comment.Multiline, '#push'),
  79. (r'-\}', Comment.Multiline, '#pop'),
  80. (r'[-}{]', Comment.Multiline),
  81. ],
  82. 'storage': [
  83. (words((
  84. 'var', 'int', 'slice', 'tuple',
  85. 'cell', 'builder', 'cont', '_'),
  86. prefix=r'\b', suffix=r'(?=[\s\(\),\[\]])'),
  87. Keyword.Type),
  88. (words(('global', 'const'), prefix=r'\b', suffix=r'\b'), Keyword.Constant),
  89. ],
  90. 'variables': [
  91. (identifier, Name.Variable),
  92. ],
  93. 'functions': [
  94. # identifier followed by (
  95. (identifier + r'(?=[\(])', Name.Function),
  96. ]
  97. }