1"""
2 pygments.lexers.unicon
3 ~~~~~~~~~~~~~~~~~~~~~~
4
5 Lexers for the Icon and Unicon languages, including ucode VM.
6
7 :copyright: Copyright 2006-2025 by the Pygments team, see AUTHORS.
8 :license: BSD, see LICENSE for details.
9"""
10
11import re
12
13from pygments.lexer import RegexLexer, include, bygroups, words, using, this
14from pygments.token import Text, Comment, Operator, Keyword, Name, String, \
15 Number, Punctuation
16
17__all__ = ['IconLexer', 'UcodeLexer', 'UniconLexer']
18
19
20class UniconLexer(RegexLexer):
21 """
22 For Unicon source code.
23 """
24
25 name = 'Unicon'
26 aliases = ['unicon']
27 filenames = ['*.icn']
28 mimetypes = ['text/unicon']
29 url = 'https://www.unicon.org'
30 version_added = '2.4'
31
32 flags = re.MULTILINE
33
34 tokens = {
35 'root': [
36 (r'[^\S\n]+', Text),
37 (r'#.*?\n', Comment.Single),
38 (r'[^\S\n]+', Text),
39 (r'class|method|procedure', Keyword.Declaration, 'subprogram'),
40 (r'(record)(\s+)(\w+)',
41 bygroups(Keyword.Declaration, Text, Keyword.Type), 'type_def'),
42 (r'(#line|\$C|\$Cend|\$define|\$else|\$endif|\$error|\$ifdef|'
43 r'\$ifndef|\$include|\$line|\$undef)\b', Keyword.PreProc),
44 (r'(&null|&fail)\b', Keyword.Constant),
45 (r'&allocated|&ascii|&clock|&collections|&column|&col|&control|'
46 r'&cset|¤t|&dateline|&date|&digits|&dump|'
47 r'&errno|&errornumber|&errortext|&errorvalue|&error|&errout|'
48 r'&eventcode|&eventvalue|&eventsource|&e|'
49 r'&features|&file|&host|&input|&interval|&lcase|&letters|'
50 r'&level|&line|&ldrag|&lpress|&lrelease|'
51 r'&main|&mdrag|&meta|&mpress|&mrelease|&now|&output|'
52 r'&phi|&pick|&pi|&pos|&progname|'
53 r'&random|&rdrag|®ions|&resize|&row|&rpress|&rrelease|'
54 r'&shift|&source|&storage|&subject|'
55 r'&time|&trace|&ucase|&version|'
56 r'&window|&x|&y', Keyword.Reserved),
57 (r'(by|of|not|to)\b', Keyword.Reserved),
58 (r'(global|local|static|abstract)\b', Keyword.Reserved),
59 (r'package|link|import', Keyword.Declaration),
60 (words((
61 'break', 'case', 'create', 'critical', 'default', 'end', 'all',
62 'do', 'else', 'every', 'fail', 'if', 'import', 'initial',
63 'initially', 'invocable', 'next',
64 'repeat', 'return', 'suspend',
65 'then', 'thread', 'until', 'while'), prefix=r'\b', suffix=r'\b'),
66 Keyword.Reserved),
67 (words((
68 'Abort', 'abs', 'acos', 'Active', 'Alert', 'any', 'Any', 'Arb',
69 'Arbno', 'args', 'array', 'asin', 'atan', 'atanh', 'Attrib',
70 'Bal', 'bal', 'Bg', 'Break', 'Breakx',
71 'callout', 'center', 'char', 'chdir', 'chmod', 'chown', 'chroot',
72 'classname', 'Clip', 'Clone', 'close', 'cofail', 'collect',
73 'Color', 'ColorValue', 'condvar', 'constructor', 'copy',
74 'CopyArea', 'cos', 'Couple', 'crypt', 'cset', 'ctime',
75 'dbcolumns', 'dbdriver', 'dbkeys', 'dblimits', 'dbproduct',
76 'dbtables', 'delay', 'delete', 'detab', 'display', 'DrawArc',
77 'DrawCircle', 'DrawCube', 'DrawCurve', 'DrawCylinder',
78 'DrawDisk', 'DrawImage', 'DrawLine', 'DrawPoint', 'DrawPolygon',
79 'DrawRectangle', 'DrawSegment', 'DrawSphere', 'DrawString',
80 'DrawTorus', 'dtor',
81 'entab', 'EraseArea', 'errorclear', 'Event', 'eventmask',
82 'EvGet', 'EvSend', 'exec', 'exit', 'exp', 'Eye',
83 'Fail', 'fcntl', 'fdup', 'Fence', 'fetch', 'Fg', 'fieldnames',
84 'filepair', 'FillArc', 'FillCircle', 'FillPolygon',
85 'FillRectangle', 'find', 'flock', 'flush', 'Font', 'fork',
86 'FreeColor', 'FreeSpace', 'function',
87 'get', 'getch', 'getche', 'getegid', 'getenv', 'geteuid',
88 'getgid', 'getgr', 'gethost', 'getpgrp', 'getpid', 'getppid',
89 'getpw', 'getrusage', 'getserv', 'GetSpace', 'gettimeofday',
90 'getuid', 'globalnames', 'GotoRC', 'GotoXY', 'gtime', 'hardlink',
91 'iand', 'icom', 'IdentityMatrix', 'image', 'InPort', 'insert',
92 'Int86', 'integer', 'ioctl', 'ior', 'ishift', 'istate', 'ixor',
93 'kbhit', 'key', 'keyword', 'kill',
94 'left', 'Len', 'list', 'load', 'loadfunc', 'localnames',
95 'lock', 'log', 'Lower', 'lstat',
96 'many', 'map', 'match', 'MatrixMode', 'max', 'member',
97 'membernames', 'methodnames', 'methods', 'min', 'mkdir', 'move',
98 'MultMatrix', 'mutex',
99 'name', 'NewColor', 'Normals', 'NotAny', 'numeric',
100 'open', 'opencl', 'oprec', 'ord', 'OutPort',
101 'PaletteChars', 'PaletteColor', 'PaletteKey', 'paramnames',
102 'parent', 'Pattern', 'Peek', 'Pending', 'pipe', 'Pixel',
103 'PlayAudio', 'Poke', 'pop', 'PopMatrix', 'Pos', 'pos',
104 'proc', 'pull', 'push', 'PushMatrix', 'PushRotate', 'PushScale',
105 'PushTranslate', 'put',
106 'QueryPointer',
107 'Raise', 'read', 'ReadImage', 'readlink', 'reads', 'ready',
108 'real', 'receive', 'Refresh', 'Rem', 'remove', 'rename',
109 'repl', 'reverse', 'right', 'rmdir', 'Rotate', 'Rpos',
110 'Rtab', 'rtod', 'runerr',
111 'save', 'Scale', 'seek', 'select', 'send', 'seq',
112 'serial', 'set', 'setenv', 'setgid', 'setgrent',
113 'sethostent', 'setpgrp', 'setpwent', 'setservent',
114 'setuid', 'signal', 'sin', 'sort', 'sortf', 'Span',
115 'spawn', 'sql', 'sqrt', 'stat', 'staticnames', 'stop',
116 'StopAudio', 'string', 'structure', 'Succeed', 'Swi',
117 'symlink', 'sys_errstr', 'system', 'syswrite',
118 'Tab', 'tab', 'table', 'tan',
119 'Texcoord', 'Texture', 'TextWidth', 'Translate',
120 'trap', 'trim', 'truncate', 'trylock', 'type',
121 'umask', 'Uncouple', 'unlock', 'upto', 'utime',
122 'variable', 'VAttrib',
123 'wait', 'WAttrib', 'WDefault', 'WFlush', 'where',
124 'WinAssociate', 'WinButton', 'WinColorDialog', 'WindowContents',
125 'WinEditRegion', 'WinFontDialog', 'WinMenuBar', 'WinOpenDialog',
126 'WinPlayMedia', 'WinSaveDialog', 'WinScrollBar', 'WinSelectDialog',
127 'write', 'WriteImage', 'writes', 'WSection',
128 'WSync'), prefix=r'\b', suffix=r'\b'),
129 Name.Function),
130 include('numbers'),
131 (r'<@|<<@|>@|>>@|\.>|->|===|~===|\*\*|\+\+|--|\.|~==|~=|<=|>=|==|'
132 r'=|<<=|<<|>>=|>>|:=:|:=|->|<->|\+:=|\|', Operator),
133 (r'"(?:[^\\"]|\\.)*"', String),
134 (r"'(?:[^\\']|\\.)*'", String.Character),
135 (r'[*<>+=/&!?@~\\-]', Operator),
136 (r'\^', Operator),
137 (r'(\w+)(\s*|[(,])', bygroups(Name, using(this))),
138 (r"[\[\]]", Punctuation),
139 (r"<>|=>|[()|:;,.'`{}%&?]", Punctuation),
140 (r'\n+', Text),
141 ],
142 'numbers': [
143 (r'\b([+-]?([2-9]|[12][0-9]|3[0-6])[rR][0-9a-zA-Z]+)\b', Number.Hex),
144 (r'[+-]?[0-9]*\.([0-9]*)([Ee][+-]?[0-9]*)?', Number.Float),
145 (r'\b([+-]?[0-9]+[KMGTPkmgtp]?)\b', Number.Integer),
146 ],
147 'subprogram': [
148 (r'\(', Punctuation, ('#pop', 'formal_part')),
149 (r';', Punctuation, '#pop'),
150 (r'"[^"]+"|\w+', Name.Function),
151 include('root'),
152 ],
153 'type_def': [
154 (r'\(', Punctuation, 'formal_part'),
155 ],
156 'formal_part': [
157 (r'\)', Punctuation, '#pop'),
158 (r'\w+', Name.Variable),
159 (r',', Punctuation),
160 (r'(:string|:integer|:real)\b', Keyword.Reserved),
161 include('root'),
162 ],
163 }
164
165
166class IconLexer(RegexLexer):
167 """
168 Lexer for Icon.
169 """
170 name = 'Icon'
171 aliases = ['icon']
172 filenames = ['*.icon', '*.ICON']
173 mimetypes = []
174 url = 'https://www2.cs.arizona.edu/icon'
175 version_added = '1.6'
176
177 flags = re.MULTILINE
178
179 tokens = {
180 'root': [
181 (r'[^\S\n]+', Text),
182 (r'#.*?\n', Comment.Single),
183 (r'[^\S\n]+', Text),
184 (r'class|method|procedure', Keyword.Declaration, 'subprogram'),
185 (r'(record)(\s+)(\w+)',
186 bygroups(Keyword.Declaration, Text, Keyword.Type), 'type_def'),
187 (r'(#line|\$C|\$Cend|\$define|\$else|\$endif|\$error|\$ifdef|'
188 r'\$ifndef|\$include|\$line|\$undef)\b', Keyword.PreProc),
189 (r'(&null|&fail)\b', Keyword.Constant),
190 (r'&allocated|&ascii|&clock|&collections|&column|&col|&control|'
191 r'&cset|¤t|&dateline|&date|&digits|&dump|'
192 r'&errno|&errornumber|&errortext|&errorvalue|&error|&errout|'
193 r'&eventcode|&eventvalue|&eventsource|&e|'
194 r'&features|&file|&host|&input|&interval|&lcase|&letters|'
195 r'&level|&line|&ldrag|&lpress|&lrelease|'
196 r'&main|&mdrag|&meta|&mpress|&mrelease|&now|&output|'
197 r'&phi|&pick|&pi|&pos|&progname|'
198 r'&random|&rdrag|®ions|&resize|&row|&rpress|&rrelease|'
199 r'&shift|&source|&storage|&subject|'
200 r'&time|&trace|&ucase|&version|'
201 r'&window|&x|&y', Keyword.Reserved),
202 (r'(by|of|not|to)\b', Keyword.Reserved),
203 (r'(global|local|static)\b', Keyword.Reserved),
204 (r'link', Keyword.Declaration),
205 (words((
206 'break', 'case', 'create', 'default', 'end', 'all',
207 'do', 'else', 'every', 'fail', 'if', 'initial',
208 'invocable', 'next',
209 'repeat', 'return', 'suspend',
210 'then', 'until', 'while'), prefix=r'\b', suffix=r'\b'),
211 Keyword.Reserved),
212 (words((
213 'abs', 'acos', 'Active', 'Alert', 'any',
214 'args', 'array', 'asin', 'atan', 'atanh', 'Attrib',
215 'bal', 'Bg',
216 'callout', 'center', 'char', 'chdir', 'chmod', 'chown', 'chroot',
217 'Clip', 'Clone', 'close', 'cofail', 'collect',
218 'Color', 'ColorValue', 'condvar', 'copy',
219 'CopyArea', 'cos', 'Couple', 'crypt', 'cset', 'ctime',
220 'delay', 'delete', 'detab', 'display', 'DrawArc',
221 'DrawCircle', 'DrawCube', 'DrawCurve', 'DrawCylinder',
222 'DrawDisk', 'DrawImage', 'DrawLine', 'DrawPoint', 'DrawPolygon',
223 'DrawRectangle', 'DrawSegment', 'DrawSphere', 'DrawString',
224 'DrawTorus', 'dtor',
225 'entab', 'EraseArea', 'errorclear', 'Event', 'eventmask',
226 'EvGet', 'EvSend', 'exec', 'exit', 'exp', 'Eye',
227 'fcntl', 'fdup', 'fetch', 'Fg', 'fieldnames',
228 'FillArc', 'FillCircle', 'FillPolygon',
229 'FillRectangle', 'find', 'flock', 'flush', 'Font',
230 'FreeColor', 'FreeSpace', 'function',
231 'get', 'getch', 'getche', 'getenv',
232 'GetSpace', 'gettimeofday',
233 'getuid', 'globalnames', 'GotoRC', 'GotoXY', 'gtime', 'hardlink',
234 'iand', 'icom', 'IdentityMatrix', 'image', 'InPort', 'insert',
235 'Int86', 'integer', 'ioctl', 'ior', 'ishift', 'istate', 'ixor',
236 'kbhit', 'key', 'keyword', 'kill',
237 'left', 'Len', 'list', 'load', 'loadfunc', 'localnames',
238 'lock', 'log', 'Lower', 'lstat',
239 'many', 'map', 'match', 'MatrixMode', 'max', 'member',
240 'membernames', 'methodnames', 'methods', 'min', 'mkdir', 'move',
241 'MultMatrix', 'mutex',
242 'name', 'NewColor', 'Normals', 'numeric',
243 'open', 'opencl', 'oprec', 'ord', 'OutPort',
244 'PaletteChars', 'PaletteColor', 'PaletteKey', 'paramnames',
245 'parent', 'Pattern', 'Peek', 'Pending', 'pipe', 'Pixel',
246 'Poke', 'pop', 'PopMatrix', 'Pos', 'pos',
247 'proc', 'pull', 'push', 'PushMatrix', 'PushRotate', 'PushScale',
248 'PushTranslate', 'put',
249 'QueryPointer',
250 'Raise', 'read', 'ReadImage', 'readlink', 'reads', 'ready',
251 'real', 'receive', 'Refresh', 'Rem', 'remove', 'rename',
252 'repl', 'reverse', 'right', 'rmdir', 'Rotate', 'Rpos',
253 'rtod', 'runerr',
254 'save', 'Scale', 'seek', 'select', 'send', 'seq',
255 'serial', 'set', 'setenv',
256 'setuid', 'signal', 'sin', 'sort', 'sortf',
257 'spawn', 'sql', 'sqrt', 'stat', 'staticnames', 'stop',
258 'string', 'structure', 'Swi',
259 'symlink', 'sys_errstr', 'system', 'syswrite',
260 'tab', 'table', 'tan',
261 'Texcoord', 'Texture', 'TextWidth', 'Translate',
262 'trap', 'trim', 'truncate', 'trylock', 'type',
263 'umask', 'Uncouple', 'unlock', 'upto', 'utime',
264 'variable',
265 'wait', 'WAttrib', 'WDefault', 'WFlush', 'where',
266 'WinAssociate', 'WinButton', 'WinColorDialog', 'WindowContents',
267 'WinEditRegion', 'WinFontDialog', 'WinMenuBar', 'WinOpenDialog',
268 'WinPlayMedia', 'WinSaveDialog', 'WinScrollBar', 'WinSelectDialog',
269 'write', 'WriteImage', 'writes', 'WSection',
270 'WSync'), prefix=r'\b', suffix=r'\b'),
271 Name.Function),
272 include('numbers'),
273 (r'===|~===|\*\*|\+\+|--|\.|==|~==|<=|>=|=|~=|<<=|<<|>>=|>>|'
274 r':=:|:=|<->|<-|\+:=|\|\||\|', Operator),
275 (r'"(?:[^\\"]|\\.)*"', String),
276 (r"'(?:[^\\']|\\.)*'", String.Character),
277 (r'[*<>+=/&!?@~\\-]', Operator),
278 (r'(\w+)(\s*|[(,])', bygroups(Name, using(this))),
279 (r"[\[\]]", Punctuation),
280 (r"<>|=>|[()|:;,.'`{}%\^&?]", Punctuation),
281 (r'\n+', Text),
282 ],
283 'numbers': [
284 (r'\b([+-]?([2-9]|[12][0-9]|3[0-6])[rR][0-9a-zA-Z]+)\b', Number.Hex),
285 (r'[+-]?[0-9]*\.([0-9]*)([Ee][+-]?[0-9]*)?', Number.Float),
286 (r'\b([+-]?[0-9]+[KMGTPkmgtp]?)\b', Number.Integer),
287 ],
288 'subprogram': [
289 (r'\(', Punctuation, ('#pop', 'formal_part')),
290 (r';', Punctuation, '#pop'),
291 (r'"[^"]+"|\w+', Name.Function),
292 include('root'),
293 ],
294 'type_def': [
295 (r'\(', Punctuation, 'formal_part'),
296 ],
297 'formal_part': [
298 (r'\)', Punctuation, '#pop'),
299 (r'\w+', Name.Variable),
300 (r',', Punctuation),
301 (r'(:string|:integer|:real)\b', Keyword.Reserved),
302 include('root'),
303 ],
304 }
305
306
307class UcodeLexer(RegexLexer):
308 """
309 Lexer for Icon ucode files.
310 """
311 name = 'ucode'
312 aliases = ['ucode']
313 filenames = ['*.u', '*.u1', '*.u2']
314 mimetypes = []
315 url = 'http://www.unicon.org'
316 version_added = '2.4'
317
318 flags = re.MULTILINE
319
320 tokens = {
321 'root': [
322 (r'(#.*\n)', Comment),
323 (words((
324 'con', 'declend', 'end',
325 'global',
326 'impl', 'invocable',
327 'lab', 'link', 'local',
328 'record',
329 'uid', 'unions',
330 'version'),
331 prefix=r'\b', suffix=r'\b'),
332 Name.Function),
333 (words((
334 'colm', 'filen', 'line', 'synt'),
335 prefix=r'\b', suffix=r'\b'),
336 Comment),
337 (words((
338 'asgn',
339 'bang', 'bscan',
340 'cat', 'ccase', 'chfail',
341 'coact', 'cofail', 'compl',
342 'coret', 'create', 'cset',
343 'diff', 'div', 'dup',
344 'efail', 'einit', 'end', 'eqv', 'eret',
345 'error', 'escan', 'esusp',
346 'field',
347 'goto',
348 'init', 'int', 'inter',
349 'invoke',
350 'keywd',
351 'lconcat', 'lexeq', 'lexge',
352 'lexgt', 'lexle', 'lexlt', 'lexne',
353 'limit', 'llist', 'lsusp',
354 'mark', 'mark0', 'minus', 'mod', 'mult',
355 'neg', 'neqv', 'nonnull', 'noop', 'null',
356 'number', 'numeq', 'numge', 'numgt',
357 'numle', 'numlt', 'numne',
358 'pfail', 'plus', 'pnull', 'pop', 'power',
359 'pret', 'proc', 'psusp', 'push1', 'pushn1',
360 'random', 'rasgn', 'rcv', 'rcvbk', 'real',
361 'refresh', 'rswap',
362 'sdup', 'sect', 'size', 'snd', 'sndbk',
363 'str', 'subsc', 'swap',
364 'tabmat', 'tally', 'toby', 'trace',
365 'unmark',
366 'value', 'var'), prefix=r'\b', suffix=r'\b'),
367 Keyword.Declaration),
368 (words((
369 'any',
370 'case',
371 'endcase', 'endevery', 'endif',
372 'endifelse', 'endrepeat', 'endsuspend',
373 'enduntil', 'endwhile', 'every',
374 'if', 'ifelse',
375 'repeat',
376 'suspend',
377 'until',
378 'while'),
379 prefix=r'\b', suffix=r'\b'),
380 Name.Constant),
381 (r'\d+(\s*|\.$|$)', Number.Integer),
382 (r'[+-]?\d*\.\d+(E[-+]?\d+)?', Number.Float),
383 (r'[+-]?\d+\.\d*(E[-+]?\d+)?', Number.Float),
384 (r"(<>|=>|[()|:;,.'`]|[{}]|[%^]|[&?])", Punctuation),
385 (r'\s+\b', Text),
386 (r'[\w-]+', Text),
387 ],
388 }
389
390 def analyse_text(text):
391 """endsuspend and endrepeat are unique to this language, and
392 \\self, /self doesn't seem to get used anywhere else either."""
393 result = 0
394
395 if 'endsuspend' in text:
396 result += 0.1
397
398 if 'endrepeat' in text:
399 result += 0.1
400
401 if ':=' in text:
402 result += 0.01
403
404 if 'procedure' in text and 'end' in text:
405 result += 0.01
406
407 # This seems quite unique to unicon -- doesn't appear in any other
408 # example source we have (A quick search reveals that \SELF appears in
409 # Perl/Raku code)
410 if r'\self' in text and r'/self' in text:
411 result += 0.5
412
413 return result