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