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