1"""
2 pygments.lexers.maple
3 ~~~~~~~~~~~~~~~~~~~~~
4
5 Lexers for Maple.
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 words, bygroups, ExtendedRegexLexer
12from pygments.token import Comment, Name, String, Whitespace, Operator, Punctuation, Number, Keyword
13
14__all__ = ['MapleLexer']
15
16
17class MapleLexer(ExtendedRegexLexer):
18 """
19 Lexer for Maple.
20 """
21
22 name = 'Maple'
23 aliases = ['maple']
24 filenames = ['*.mpl', '*.mi', '*.mm']
25 mimetypes = ['text/x-maple']
26 url = 'https://www.maplesoft.com/products/Maple/'
27 version_added = '2.19'
28
29 keywords = ('and',
30 'assuming',
31 'break',
32 'by',
33 'catch',
34 'description',
35 'do',
36 'done',
37 'elif',
38 'else',
39 'end',
40 'error',
41 'export',
42 'fi',
43 'finally',
44 'for',
45 'from',
46 'global',
47 'if',
48 'implies',
49 'in',
50 'intersect',
51 'local',
52 'minus',
53 'mod',
54 'module',
55 'next',
56 'not',
57 'od',
58 'option',
59 'options',
60 'or',
61 'proc',
62 'quit',
63 'read',
64 'return',
65 'save',
66 'stop',
67 'subset',
68 'then',
69 'to',
70 'try',
71 'union',
72 'use',
73 'uses',
74 'while',
75 'xor')
76
77 builtins = ('abs',
78 'add',
79 'addressof',
80 'anames',
81 'and',
82 'andmap',
83 'andseq',
84 'appendto',
85 'Array',
86 'array',
87 'ArrayOptions',
88 'assemble',
89 'ASSERT',
90 'assign',
91 'assigned',
92 'attributes',
93 'cat',
94 'ceil',
95 'coeff',
96 'coeffs',
97 'conjugate',
98 'convert',
99 'CopySign',
100 'DEBUG',
101 'debugopts',
102 'Default0',
103 'DefaultOverflow',
104 'DefaultUnderflow',
105 'degree',
106 'denom',
107 'diff',
108 'disassemble',
109 'divide',
110 'done',
111 'entries',
112 'EqualEntries',
113 'eval',
114 'evalb',
115 'evalf',
116 'evalhf',
117 'evalindets',
118 'evaln',
119 'expand',
120 'exports',
121 'factorial',
122 'floor',
123 'frac',
124 'frem',
125 'FromInert',
126 'frontend',
127 'gc',
128 'genpoly',
129 'has',
130 'hastype',
131 'hfarray',
132 'icontent',
133 'igcd',
134 'ilcm',
135 'ilog10',
136 'Im',
137 'implies',
138 'indets',
139 'indices',
140 'intersect',
141 'iolib',
142 'iquo',
143 'irem',
144 'iroot',
145 'iroot',
146 'isqrt',
147 'kernelopts',
148 'lcoeff',
149 'ldegree',
150 'length',
151 'lexorder',
152 'lhs',
153 'lowerbound',
154 'lprint',
155 'macro',
156 'map',
157 'max',
158 'maxnorm',
159 'member',
160 'membertype',
161 'min',
162 'minus',
163 'mod',
164 'modp',
165 'modp1',
166 'modp2',
167 'mods',
168 'mul',
169 'NextAfter',
170 'nops',
171 'normal',
172 'not',
173 'numboccur',
174 'numelems',
175 'numer',
176 'NumericClass',
177 'NumericEvent',
178 'NumericEventHandler',
179 'NumericStatus',
180 'op',
181 'or',
182 'order',
183 'OrderedNE',
184 'ormap',
185 'orseq',
186 'parse',
187 'piecewise',
188 'pointto',
189 'print',
190 'quit',
191 'Re',
192 'readlib',
193 'Record',
194 'remove',
195 'rhs',
196 'round',
197 'rtable',
198 'rtable_elems',
199 'rtable_eval',
200 'rtable_indfns',
201 'rtable_num_elems',
202 'rtable_options',
203 'rtable_redim',
204 'rtable_scanblock',
205 'rtable_set_indfn',
206 'rtable_split_unit',
207 'savelib',
208 'Scale10',
209 'Scale2',
210 'SDMPolynom',
211 'searchtext',
212 'SearchText',
213 'select',
214 'selectremove',
215 'seq',
216 'series',
217 'setattribute',
218 'SFloatExponent',
219 'SFloatMantissa',
220 'sign',
221 'sort',
222 'ssystem',
223 'stop',
224 'String',
225 'subs',
226 'subset',
227 'subsindets',
228 'subsop',
229 'substring',
230 'system',
231 'table',
232 'taylor',
233 'tcoeff',
234 'time',
235 'timelimit',
236 'ToInert',
237 'traperror',
238 'trunc',
239 'type',
240 'typematch',
241 'unames',
242 'unassign',
243 'union',
244 'Unordered',
245 'upperbound',
246 'userinfo',
247 'writeto',
248 'xor',
249 'xormap',
250 'xorseq')
251
252 def delayed_callback(self, match, ctx):
253 yield match.start(1), Punctuation, match.group(1) # quote
254
255 ctx.pos = match.start(2)
256 orig_end = ctx.end
257 ctx.end = match.end(2)
258
259 yield from self.get_tokens_unprocessed(context=ctx)
260 yield match.end(2), Punctuation, match.group(1) # quote
261
262 ctx.pos = match.end()
263 ctx.end = orig_end
264
265 tokens = {
266 'root': [
267 (r'#.*\n', Comment.Single),
268 (r'\(\*', Comment.Multiline, 'comment'),
269 (r'"(\\.|.|\s)*?"', String),
270 (r"('+)((.|\n)*?)\1", delayed_callback),
271 (r'`(\\`|.)*?`', Name),
272 (words(keywords, prefix=r'\b', suffix=r'\b'), Keyword),
273 (words(builtins, prefix=r'\b', suffix=r'\b'), Name.Builtin),
274 (r'[a-zA-Z_][a-zA-Z0-9_]*', Name),
275 (r'(:=|\*\*|@@|<=|>=|<>|->|::|\.\.|&\+|[\+\-\*\.\^\$/@&,:=<>%~])', Operator),
276 (r'[;^!@$\(\)\[\]{}|_\\#?]+', Punctuation),
277 (r'(\d+)(\.\.)', bygroups(Number.Integer, Punctuation)),
278 (r'(\d*\.\d+|\d+\.\d*)([eE][+-]?\d+)?', Number.Float),
279 (r'\d+', Number.Integer),
280 (r'\s+', Whitespace),
281 ],
282 'comment': [
283 (r'.*\(\*', Comment.Multiline, '#push'),
284 (r'.*\*\)', Comment.Multiline, '#pop'),
285 (r'.*\n', Comment.Multiline),
286 ]
287 }
288
289 def analyse_text(text):
290 if ':=' in text:
291 return 0.1