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