1"""
2 pygments.lexers.monte
3 ~~~~~~~~~~~~~~~~~~~~~
4
5 Lexer for the Monte programming language.
6
7 :copyright: Copyright 2006-2025 by the Pygments team, see AUTHORS.
8 :license: BSD, see LICENSE for details.
9"""
10
11from pygments.token import Comment, Error, Keyword, Name, Number, Operator, \
12 Punctuation, String, Whitespace
13from pygments.lexer import RegexLexer, include, words
14
15__all__ = ['MonteLexer']
16
17
18# `var` handled separately
19# `interface` handled separately
20_declarations = ['bind', 'def', 'fn', 'object']
21_methods = ['method', 'to']
22_keywords = [
23 'as', 'break', 'catch', 'continue', 'else', 'escape', 'exit', 'exports',
24 'extends', 'finally', 'for', 'guards', 'if', 'implements', 'import',
25 'in', 'match', 'meta', 'pass', 'return', 'switch', 'try', 'via', 'when',
26 'while',
27]
28_operators = [
29 # Unary
30 '~', '!',
31 # Binary
32 '+', '-', '*', '/', '%', '**', '&', '|', '^', '<<', '>>',
33 # Binary augmented
34 '+=', '-=', '*=', '/=', '%=', '**=', '&=', '|=', '^=', '<<=', '>>=',
35 # Comparison
36 '==', '!=', '<', '<=', '>', '>=', '<=>',
37 # Patterns and assignment
38 ':=', '?', '=~', '!~', '=>',
39 # Calls and sends
40 '.', '<-', '->',
41]
42_escape_pattern = (
43 r'(?:\\x[0-9a-fA-F]{2}|\\u[0-9a-fA-F]{4}|\\U[0-9a-fA-F]{8}|'
44 r'\\["\'\\bftnr])')
45# _char = _escape_chars + [('.', String.Char)]
46_identifier = r'[_a-zA-Z]\w*'
47
48_constants = [
49 # Void constants
50 'null',
51 # Bool constants
52 'false', 'true',
53 # Double constants
54 'Infinity', 'NaN',
55 # Special objects
56 'M', 'Ref', 'throw', 'traceln',
57]
58
59_guards = [
60 'Any', 'Binding', 'Bool', 'Bytes', 'Char', 'DeepFrozen', 'Double',
61 'Empty', 'Int', 'List', 'Map', 'Near', 'NullOk', 'Same', 'Selfless',
62 'Set', 'Str', 'SubrangeGuard', 'Transparent', 'Void',
63]
64
65_safeScope = [
66 '_accumulateList', '_accumulateMap', '_auditedBy', '_bind',
67 '_booleanFlow', '_comparer', '_equalizer', '_iterForever', '_loop',
68 '_makeBytes', '_makeDouble', '_makeFinalSlot', '_makeInt', '_makeList',
69 '_makeMap', '_makeMessageDesc', '_makeOrderedSpace', '_makeParamDesc',
70 '_makeProtocolDesc', '_makeSourceSpan', '_makeString', '_makeVarSlot',
71 '_makeVerbFacet', '_mapExtract', '_matchSame', '_quasiMatcher',
72 '_slotToBinding', '_splitList', '_suchThat', '_switchFailed',
73 '_validateFor', 'b__quasiParser', 'eval', 'import', 'm__quasiParser',
74 'makeBrandPair', 'makeLazySlot', 'safeScope', 'simple__quasiParser',
75]
76
77
78class MonteLexer(RegexLexer):
79 """
80 Lexer for the Monte programming language.
81 """
82 name = 'Monte'
83 url = 'https://monte.readthedocs.io/'
84 aliases = ['monte']
85 filenames = ['*.mt']
86 version_added = '2.2'
87
88 tokens = {
89 'root': [
90 # Comments
91 (r'#[^\n]*\n', Comment),
92
93 # Docstrings
94 # Apologies for the non-greedy matcher here.
95 (r'/\*\*.*?\*/', String.Doc),
96
97 # `var` declarations
98 (r'\bvar\b', Keyword.Declaration, 'var'),
99
100 # `interface` declarations
101 (r'\binterface\b', Keyword.Declaration, 'interface'),
102
103 # method declarations
104 (words(_methods, prefix='\\b', suffix='\\b'),
105 Keyword, 'method'),
106
107 # All other declarations
108 (words(_declarations, prefix='\\b', suffix='\\b'),
109 Keyword.Declaration),
110
111 # Keywords
112 (words(_keywords, prefix='\\b', suffix='\\b'), Keyword),
113
114 # Literals
115 ('[+-]?0x[_0-9a-fA-F]+', Number.Hex),
116 (r'[+-]?[_0-9]+\.[_0-9]*([eE][+-]?[_0-9]+)?', Number.Float),
117 ('[+-]?[_0-9]+', Number.Integer),
118 ("'", String.Double, 'char'),
119 ('"', String.Double, 'string'),
120
121 # Quasiliterals
122 ('`', String.Backtick, 'ql'),
123
124 # Operators
125 (words(_operators), Operator),
126
127 # Verb operators
128 (_identifier + '=', Operator.Word),
129
130 # Safe scope constants
131 (words(_constants, prefix='\\b', suffix='\\b'),
132 Keyword.Pseudo),
133
134 # Safe scope guards
135 (words(_guards, prefix='\\b', suffix='\\b'), Keyword.Type),
136
137 # All other safe scope names
138 (words(_safeScope, prefix='\\b', suffix='\\b'),
139 Name.Builtin),
140
141 # Identifiers
142 (_identifier, Name),
143
144 # Punctuation
145 (r'\(|\)|\{|\}|\[|\]|:|,', Punctuation),
146
147 # Whitespace
148 (' +', Whitespace),
149
150 # Definite lexer errors
151 ('=', Error),
152 ],
153 'char': [
154 # It is definitely an error to have a char of width == 0.
155 ("'", Error, 'root'),
156 (_escape_pattern, String.Escape, 'charEnd'),
157 ('.', String.Char, 'charEnd'),
158 ],
159 'charEnd': [
160 ("'", String.Char, '#pop:2'),
161 # It is definitely an error to have a char of width > 1.
162 ('.', Error),
163 ],
164 # The state of things coming into an interface.
165 'interface': [
166 (' +', Whitespace),
167 (_identifier, Name.Class, '#pop'),
168 include('root'),
169 ],
170 # The state of things coming into a method.
171 'method': [
172 (' +', Whitespace),
173 (_identifier, Name.Function, '#pop'),
174 include('root'),
175 ],
176 'string': [
177 ('"', String.Double, 'root'),
178 (_escape_pattern, String.Escape),
179 (r'\n', String.Double),
180 ('.', String.Double),
181 ],
182 'ql': [
183 ('`', String.Backtick, 'root'),
184 (r'\$' + _escape_pattern, String.Escape),
185 (r'\$\$', String.Escape),
186 (r'@@', String.Escape),
187 (r'\$\{', String.Interpol, 'qlNest'),
188 (r'@\{', String.Interpol, 'qlNest'),
189 (r'\$' + _identifier, Name),
190 ('@' + _identifier, Name),
191 ('.', String.Backtick),
192 ],
193 'qlNest': [
194 (r'\}', String.Interpol, '#pop'),
195 include('root'),
196 ],
197 # The state of things immediately following `var`.
198 'var': [
199 (' +', Whitespace),
200 (_identifier, Name.Variable, '#pop'),
201 include('root'),
202 ],
203 }