unicon.py 18 KB

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