aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/Pygments/py3/pygments/lexers/tact.py
blob: 8ede906f2135ddfb3301dffb4a9eaebbfe7a7f8d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
"""
    pygments.lexers.tact
    ~~~~~~~~~~~~~~~~~~~~

    Lexers for Tact.

    :copyright: Copyright 2006-2024 by the Pygments team, see AUTHORS.
    :license: BSD, see LICENSE for details.
"""

from pygments.lexer import RegexLexer, include, bygroups, words
from pygments.token import Comment, Operator, Keyword, Name, String, \
    Number, Whitespace, Punctuation

__all__ = ['TactLexer']


class TactLexer(RegexLexer):
    """For Tact source code."""

    name = 'Tact'
    aliases = ['tact']
    filenames = ['*.tact']
    url = "https://tact-lang.org"
    version_added = '2.18'

    tokens = {
        'root': [
            (r'\s+', Whitespace),
            (r'[.;(),\[\]{}]', Punctuation),
            (r'\?|!!', Operator),
            include('comments'),
            include('import-in'),
            include('struct-in'),
            include('contract-or-trait-in'),
            include('annotation-in'),
            include('fun-declaration-in'),
            include('const-declaration-in'),
            include('statements'),
        ],
        'import-in': [
            (r'((?<=\.\.\.)|(?<![.$]))\b(import)\b(\s*)', bygroups(Punctuation, Keyword, Whitespace), 'import'),
        ],
        'import': [
            (r';', Punctuation, '#pop'),
            include('comments'),
            include('string-in'),
            (r'\s+', Whitespace),
        ],
        'struct-in': [
            (r'((?<=\.\.\.)|(?<![.$]))\b(struct|message)\b', bygroups(Punctuation, Keyword), 'struct'),
        ],
        'struct': [
            include('comments'),
            include('struct-header'),
            include('struct-body-in'),
            (r'\s+', Whitespace),
        ],
        'struct-header': [
            include('comments'),
            (r'\b\w+', Name.Class),
            (r'(\()((?:\b0[xX])[0-9a-fA-F][0-9a-fA-F_]*\b)(\))', bygroups(Punctuation, Number.Hex, Punctuation)),
            (r'(\()((?:\b[0-9]+\b))(\))', bygroups(Punctuation, Number.Integer, Punctuation)),
        ],
        'struct-body-in': [
            (r'\{', Punctuation, 'struct-body'),
        ],
        'struct-body': [
            (r'\}', Punctuation, '#pop:2'),
            include('comments'),
            include('field-declaration-in'),
        ],
        'contract-or-trait-in': [
            (r'((?<=\.\.\.)|(?<![.$]))\b(contract|trait)\b', Keyword, 'contract-or-trait'),
        ],
        'contract-or-trait': [
            include('comments'),
            (r'with', Keyword),
            (r'\b\w+', Name.Class),
            include('contract-or-trait-body-in'),
            (r'\s+', Whitespace),
            (r',', Punctuation),
        ],
        'contract-or-trait-body-in': [
            (r'\{', Punctuation, 'contract-or-trait-body'),
        ],
        'contract-or-trait-body': [
            (r'\}', Punctuation, '#pop:2'),
            include('comments'),
            include('init-declaration-in'),
            include('receive-declaration-in'),
            include('bounce-declaration-in'),
            include('fun-declaration-in'),
            include('const-declaration-in'),
            include('field-declaration-in'),
            (r'\s+', Whitespace),
        ],
        'field-declaration-in': [
            (r'\b\w+', Name.Property, 'field-declaration'),
        ],
        'field-declaration': [
            (r';', Punctuation, '#pop'),
            include('comments'),
            include('type-annotation-in'),
            include('variable-init-in'),
        ],
        'const-declaration-in': [
            (r'(?=\b(?:(?:get|native|extends|mutates|virtual|override|inline|abstract)\s*)*const\b)', Keyword, 'const-declaration'),
        ],
        'const-declaration': [
            (r'(;)', Punctuation, '#pop'),
            (r'const', Keyword),
            (words(('get', 'native', 'extends', 'mutates', 'virtual', 'override', 'inline', 'abstract'), suffix=r'\b'), Keyword),
            (r'\b\w+\b', Name.Constant),
            include('comments'),
            include('type-annotation-in'),
            include('variable-init-in'),
            (r'\s+', Whitespace),
        ],
        'init-declaration-in': [
            (r'(init)', Keyword, 'init-declaration')
        ],
        'init-declaration': [
            (r'(?<=\})', Punctuation, '#pop'),
            include('comments'),
            include('fun-arguments-in'),
            include('block-declaration-in'),
            (r'\s+', Whitespace),
        ],
        'receive-declaration-in': [
            (r'(receive|exernal)', Keyword, 'receive-declaration')
        ],
        'receive-declaration': [
            (r'(?<=\})', Punctuation, '#pop'),
            include('comments'),
            include('fun-arguments-in'),
            include('block-declaration-in'),
        ],
        'bounce-declaration-in': [
            (r'(bounced)', Keyword, 'bounce-declaration')
        ],
        'bounce-declaration': [
            (r'(?<=\})', Punctuation, '#pop'),
            include('comments'),
            include('fun-arguments-in'),
            include('block-declaration-in'),
        ],
        'fun-declaration-in': [
            (r'(?=\b(?:(?:get|native|extends|mutates|virtual|override|inline|abstract)\s*)*fun\b)', Keyword, 'fun-declaration')
        ],
        'fun-declaration': [
            (r'(?<=\}|\;)', Punctuation, '#pop'),
            (r'fun', Keyword),
            (r'\b(get|native|extends|mutates|virtual|override|inline|abstract)\b', Keyword),
            (r'\b[\w]+', Name.Function),
            include('fun-declaration-body'),
            (r'[,;]', Punctuation),
        ],
        'fun-declaration-body': [
            include('comments'),
            include('fun-arguments-in'),
            include('type-annotation-in'),
            include('block-declaration-in'),
            (r'\s+', Whitespace),
        ],
        'fun-arguments-in': [
            (r'\(', Punctuation, 'fun-arguments'),
        ],
        'fun-arguments': [
            (r'\)', Punctuation, '#pop'),
            include('comments'),
            include('string-in'),
            include('type-annotation-in'),
            (r'(self)|(\b[\w]+\b)', bygroups(Name.Variable.Instance, Name.Variable)),
            (r',', Punctuation),
            (r'\s+', Whitespace),
        ],
        'block-declaration-in': [
            (r'\{', Punctuation, 'block-declaration')
        ],
        'block-declaration': [
            (r'\}', Punctuation, '#pop'),
            include('statements'),
        ],
        'statements': [
            include('comments'),
            include('block-declaration-in'),
            include('expressions'),
        ],
        'annotation-in': [
            (r'(@)(\w+)(\()', bygroups(Keyword.Pseudo, Keyword, Punctuation), 'annotation')
        ],
        'annotation': [
            (r'\)', Punctuation, '#pop'),
            include('annotation-argument'),
            (r'\s+', Whitespace),
        ],
        'annotation-argument': [
            (r'\w+', Name.Function.Magic),
        ],
        'expressions': [
            include('comments'),
            include('type-annotation-in'),
            include('keywords'),
            include('numeric'),
            include('string-in'),
            include('variable'),
            include('function-call'),
            include('struct-init-in'),
        ],
        'struct-init-in': [
            (r'(\b\w+)(\s*)(\{)', bygroups(Name.Class, Whitespace, Punctuation), 'struct-init')
        ],
        'struct-init': [
            (r'(\})', Punctuation, '#pop'),
            include('comments'),
            include('struct-property-in'),
            (r'\s+', Whitespace),
            (r',', Punctuation),
        ],
        'struct-property-in': [
            (r'(\b[\w]+)(\s*)(:)', bygroups(Name.Property, Whitespace, Punctuation), 'struct-property')
        ],
        'struct-property': [
            (r'(?=\}|\,)', Punctuation, '#pop'),
            include('comments'),
            include('expressions'),
            (r'\s+', Whitespace),
        ],
        'variable-init-in': [
            (r'(=)', Operator, 'variable-init')
        ],
        'variable-init': [
            (r'(?=\}|\{|\,|\;)',Punctuation, '#pop'),
            include('comments'),
            include('expressions'),
            (r'\s+', Whitespace),
        ],
        'type-annotation-in': [
            (r'(:)(\s+)', bygroups(Punctuation, Whitespace), 'type-annotation')
        ],
        'type-annotation': [
            (r'(?=\{|\;|\=|\,|\))', Punctuation, '#pop'),
            include('comments'),
            include('type-as-in'),
            include('type-generic-in'),
            (r'\?', Operator),
            (r'\b\w+', Keyword.Type),
            (r'\s+', Whitespace),
        ],
        'type-generic-in': [
            (r'<', Punctuation, 'type-generic'),
        ],
        'type-generic': [
            (r'>', Punctuation, '#pop'),
            include('comments'),
            include('type-as-in'),
            (r'\b\w+', Keyword.Type),
            (r'\s+', Whitespace),
            (r',', Punctuation),
        ],
        'type-as-in': [
            (r'\b(as)(\s+)', bygroups(Keyword, Whitespace), 'type-as'),
        ],
        'type-as': [
            (r'(?=\{|\;|\=|\,|\)|\>)', Punctuation, '#pop'),
            include('comments'),
            (r'\b\w+', Keyword.Type),
            (r'\s+', Whitespace),
        ],
        'keywords': [
            (words(('if', 'else', 'while', 'do', 'until', 'repeat', 'return', 'extends', 'mutates', 'virtual', 'override', 'inline', 'native', 'let', 'const', 'fun', 'self', 'is', 'initOf', 'map', 'bounced', 'get', 'as'), prefix=r'\b', suffix=r'\b'), Keyword),
            (r'(<=>|>=|<=|!=|==|\^>>|~>>|>>|<<|\/%|\^%|~%|\^\/|~\/|\+=|-=|\*=|\/=|~\/=|\^\/=|%=|\^%=|<<=|>>=|~>>=|\^>>=|&=|\|=|\^=|\^|=|~|\/|%|-|\*|\+|>|<|&|\||:|\?)', Operator),
            (words(('true', 'false'), prefix=r'\b', suffix=r'\b'), Keyword.Constant),
        ],
        'string-in': [
            (r'"', String, 'string'),
        ],
        'string': [
            (r'"', String, '#pop'),
            (r'\\.', String.Escape),
            (r'[^\\"]+', String.Double),
        ],
        'numeric': [
            (r'(?:\b0[xX])[0-9a-fA-F][0-9a-fA-F_]*\b', Number.Hex),
            (r'(?:\b[0-9]+\b)', Number.Integer),
        ],
        'comments': [
            (r'//.*', Comment.Single),
            (r'/\*', Comment.Multiline, 'comments-multiline'),
        ],
        'comments-multiline': [
            (r'\*/', Comment.Multiline, '#pop'),
            (r'[^*]+', Comment.Multiline),
            (r'[*]', Comment.Multiline),
        ],
        'variable': [
            (r'\b\w+\b(?!\s*\()(?!\s*\{)', Name.Variable)
        ],
        'function-call': [
            (r'\b\w+\b(?=\s*\()(?!\s*\{)', Name.Function)
        ],
    }