func.py 3.6 KB

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