factor.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. # -*- coding: utf-8 -*-
  2. """
  3. pygments.lexers.factor
  4. ~~~~~~~~~~~~~~~~~~~~~~
  5. Lexers for the Factor language.
  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, bygroups, default, words
  11. from pygments.token import Text, Comment, Keyword, Name, String, Number
  12. __all__ = ['FactorLexer']
  13. class FactorLexer(RegexLexer):
  14. """
  15. Lexer for the `Factor <http://factorcode.org>`_ language.
  16. .. versionadded:: 1.4
  17. """
  18. name = 'Factor'
  19. aliases = ['factor']
  20. filenames = ['*.factor']
  21. mimetypes = ['text/x-factor']
  22. flags = re.MULTILINE | re.UNICODE
  23. builtin_kernel = words((
  24. '-rot', '2bi', '2bi@', '2bi*', '2curry', '2dip', '2drop', '2dup', '2keep', '2nip',
  25. '2over', '2tri', '2tri@', '2tri*', '3bi', '3curry', '3dip', '3drop', '3dup', '3keep',
  26. '3tri', '4dip', '4drop', '4dup', '4keep', '<wrapper>', '=', '>boolean', 'clone',
  27. '?', '?execute', '?if', 'and', 'assert', 'assert=', 'assert?', 'bi', 'bi-curry',
  28. 'bi-curry@', 'bi-curry*', 'bi@', 'bi*', 'boa', 'boolean', 'boolean?', 'both?',
  29. 'build', 'call', 'callstack', 'callstack>array', 'callstack?', 'clear', '(clone)',
  30. 'compose', 'compose?', 'curry', 'curry?', 'datastack', 'die', 'dip', 'do', 'drop',
  31. 'dup', 'dupd', 'either?', 'eq?', 'equal?', 'execute', 'hashcode', 'hashcode*',
  32. 'identity-hashcode', 'identity-tuple', 'identity-tuple?', 'if', 'if*',
  33. 'keep', 'loop', 'most', 'new', 'nip', 'not', 'null', 'object', 'or', 'over',
  34. 'pick', 'prepose', 'retainstack', 'rot', 'same?', 'swap', 'swapd', 'throw',
  35. 'tri', 'tri-curry', 'tri-curry@', 'tri-curry*', 'tri@', 'tri*', 'tuple',
  36. 'tuple?', 'unless', 'unless*', 'until', 'when', 'when*', 'while', 'with',
  37. 'wrapper', 'wrapper?', 'xor'), suffix=r'\s')
  38. builtin_assocs = words((
  39. '2cache', '<enum>', '>alist', '?at', '?of', 'assoc', 'assoc-all?',
  40. 'assoc-any?', 'assoc-clone-like', 'assoc-combine', 'assoc-diff',
  41. 'assoc-diff!', 'assoc-differ', 'assoc-each', 'assoc-empty?',
  42. 'assoc-filter', 'assoc-filter!', 'assoc-filter-as', 'assoc-find',
  43. 'assoc-hashcode', 'assoc-intersect', 'assoc-like', 'assoc-map',
  44. 'assoc-map-as', 'assoc-partition', 'assoc-refine', 'assoc-size',
  45. 'assoc-stack', 'assoc-subset?', 'assoc-union', 'assoc-union!',
  46. 'assoc=', 'assoc>map', 'assoc?', 'at', 'at+', 'at*', 'cache', 'change-at',
  47. 'clear-assoc', 'delete-at', 'delete-at*', 'enum', 'enum?', 'extract-keys',
  48. 'inc-at', 'key?', 'keys', 'map>assoc', 'maybe-set-at', 'new-assoc', 'of',
  49. 'push-at', 'rename-at', 'set-at', 'sift-keys', 'sift-values', 'substitute',
  50. 'unzip', 'value-at', 'value-at*', 'value?', 'values', 'zip'), suffix=r'\s')
  51. builtin_combinators = words((
  52. '2cleave', '2cleave>quot', '3cleave', '3cleave>quot', '4cleave',
  53. '4cleave>quot', 'alist>quot', 'call-effect', 'case', 'case-find',
  54. 'case>quot', 'cleave', 'cleave>quot', 'cond', 'cond>quot', 'deep-spread>quot',
  55. 'execute-effect', 'linear-case-quot', 'no-case', 'no-case?', 'no-cond',
  56. 'no-cond?', 'recursive-hashcode', 'shallow-spread>quot', 'spread',
  57. 'to-fixed-point', 'wrong-values', 'wrong-values?'), suffix=r'\s')
  58. builtin_math = words((
  59. '-', '/', '/f', '/i', '/mod', '2/', '2^', '<', '<=', '<fp-nan>', '>',
  60. '>=', '>bignum', '>fixnum', '>float', '>integer', '(all-integers?)',
  61. '(each-integer)', '(find-integer)', '*', '+', '?1+',
  62. 'abs', 'align', 'all-integers?', 'bignum', 'bignum?', 'bit?', 'bitand',
  63. 'bitnot', 'bitor', 'bits>double', 'bits>float', 'bitxor', 'complex',
  64. 'complex?', 'denominator', 'double>bits', 'each-integer', 'even?',
  65. 'find-integer', 'find-last-integer', 'fixnum', 'fixnum?', 'float',
  66. 'float>bits', 'float?', 'fp-bitwise=', 'fp-infinity?', 'fp-nan-payload',
  67. 'fp-nan?', 'fp-qnan?', 'fp-sign', 'fp-snan?', 'fp-special?',
  68. 'if-zero', 'imaginary-part', 'integer', 'integer>fixnum',
  69. 'integer>fixnum-strict', 'integer?', 'log2', 'log2-expects-positive',
  70. 'log2-expects-positive?', 'mod', 'neg', 'neg?', 'next-float',
  71. 'next-power-of-2', 'number', 'number=', 'number?', 'numerator', 'odd?',
  72. 'out-of-fixnum-range', 'out-of-fixnum-range?', 'power-of-2?',
  73. 'prev-float', 'ratio', 'ratio?', 'rational', 'rational?', 'real',
  74. 'real-part', 'real?', 'recip', 'rem', 'sgn', 'shift', 'sq', 'times',
  75. 'u<', 'u<=', 'u>', 'u>=', 'unless-zero', 'unordered?', 'when-zero',
  76. 'zero?'), suffix=r'\s')
  77. builtin_sequences = words((
  78. '1sequence', '2all?', '2each', '2map', '2map-as', '2map-reduce', '2reduce',
  79. '2selector', '2sequence', '3append', '3append-as', '3each', '3map', '3map-as',
  80. '3sequence', '4sequence', '<repetition>', '<reversed>', '<slice>', '?first',
  81. '?last', '?nth', '?second', '?set-nth', 'accumulate', 'accumulate!',
  82. 'accumulate-as', 'all?', 'any?', 'append', 'append!', 'append-as',
  83. 'assert-sequence', 'assert-sequence=', 'assert-sequence?',
  84. 'binary-reduce', 'bounds-check', 'bounds-check?', 'bounds-error',
  85. 'bounds-error?', 'but-last', 'but-last-slice', 'cartesian-each',
  86. 'cartesian-map', 'cartesian-product', 'change-nth', 'check-slice',
  87. 'check-slice-error', 'clone-like', 'collapse-slice', 'collector',
  88. 'collector-for', 'concat', 'concat-as', 'copy', 'count', 'cut', 'cut-slice',
  89. 'cut*', 'delete-all', 'delete-slice', 'drop-prefix', 'each', 'each-from',
  90. 'each-index', 'empty?', 'exchange', 'filter', 'filter!', 'filter-as', 'find',
  91. 'find-from', 'find-index', 'find-index-from', 'find-last', 'find-last-from',
  92. 'first', 'first2', 'first3', 'first4', 'flip', 'follow', 'fourth', 'glue', 'halves',
  93. 'harvest', 'head', 'head-slice', 'head-slice*', 'head*', 'head?',
  94. 'if-empty', 'immutable', 'immutable-sequence', 'immutable-sequence?',
  95. 'immutable?', 'index', 'index-from', 'indices', 'infimum', 'infimum-by',
  96. 'insert-nth', 'interleave', 'iota', 'iota-tuple', 'iota-tuple?', 'join',
  97. 'join-as', 'last', 'last-index', 'last-index-from', 'length', 'lengthen',
  98. 'like', 'longer', 'longer?', 'longest', 'map', 'map!', 'map-as', 'map-find',
  99. 'map-find-last', 'map-index', 'map-integers', 'map-reduce', 'map-sum',
  100. 'max-length', 'member-eq?', 'member?', 'midpoint@', 'min-length',
  101. 'mismatch', 'move', 'new-like', 'new-resizable', 'new-sequence',
  102. 'non-negative-integer-expected', 'non-negative-integer-expected?',
  103. 'nth', 'nths', 'pad-head', 'pad-tail', 'padding', 'partition', 'pop', 'pop*',
  104. 'prefix', 'prepend', 'prepend-as', 'produce', 'produce-as', 'product', 'push',
  105. 'push-all', 'push-either', 'push-if', 'reduce', 'reduce-index', 'remove',
  106. 'remove!', 'remove-eq', 'remove-eq!', 'remove-nth', 'remove-nth!', 'repetition',
  107. 'repetition?', 'replace-slice', 'replicate', 'replicate-as', 'rest',
  108. 'rest-slice', 'reverse', 'reverse!', 'reversed', 'reversed?', 'second',
  109. 'selector', 'selector-for', 'sequence', 'sequence-hashcode', 'sequence=',
  110. 'sequence?', 'set-first', 'set-fourth', 'set-last', 'set-length', 'set-nth',
  111. 'set-second', 'set-third', 'short', 'shorten', 'shorter', 'shorter?',
  112. 'shortest', 'sift', 'slice', 'slice-error', 'slice-error?', 'slice?',
  113. 'snip', 'snip-slice', 'start', 'start*', 'subseq', 'subseq?', 'suffix',
  114. 'suffix!', 'sum', 'sum-lengths', 'supremum', 'supremum-by', 'surround', 'tail',
  115. 'tail-slice', 'tail-slice*', 'tail*', 'tail?', 'third', 'trim',
  116. 'trim-head', 'trim-head-slice', 'trim-slice', 'trim-tail', 'trim-tail-slice',
  117. 'unclip', 'unclip-last', 'unclip-last-slice', 'unclip-slice', 'unless-empty',
  118. 'virtual-exemplar', 'virtual-sequence', 'virtual-sequence?', 'virtual@',
  119. 'when-empty'), suffix=r'\s')
  120. builtin_namespaces = words((
  121. '+@', 'change', 'change-global', 'counter', 'dec', 'get', 'get-global',
  122. 'global', 'inc', 'init-namespaces', 'initialize', 'is-global', 'make-assoc',
  123. 'namespace', 'namestack', 'off', 'on', 'set', 'set-global', 'set-namestack',
  124. 'toggle', 'with-global', 'with-scope', 'with-variable', 'with-variables'),
  125. suffix=r'\s')
  126. builtin_arrays = words((
  127. '1array', '2array', '3array', '4array', '<array>', '>array', 'array',
  128. 'array?', 'pair', 'pair?', 'resize-array'), suffix=r'\s')
  129. builtin_io = words((
  130. '(each-stream-block-slice)', '(each-stream-block)',
  131. '(stream-contents-by-block)', '(stream-contents-by-element)',
  132. '(stream-contents-by-length-or-block)',
  133. '(stream-contents-by-length)', '+byte+', '+character+',
  134. 'bad-seek-type', 'bad-seek-type?', 'bl', 'contents', 'each-block',
  135. 'each-block-size', 'each-block-slice', 'each-line', 'each-morsel',
  136. 'each-stream-block', 'each-stream-block-slice', 'each-stream-line',
  137. 'error-stream', 'flush', 'input-stream', 'input-stream?',
  138. 'invalid-read-buffer', 'invalid-read-buffer?', 'lines', 'nl',
  139. 'output-stream', 'output-stream?', 'print', 'read', 'read-into',
  140. 'read-partial', 'read-partial-into', 'read-until', 'read1', 'readln',
  141. 'seek-absolute', 'seek-absolute?', 'seek-end', 'seek-end?',
  142. 'seek-input', 'seek-output', 'seek-relative', 'seek-relative?',
  143. 'stream-bl', 'stream-contents', 'stream-contents*', 'stream-copy',
  144. 'stream-copy*', 'stream-element-type', 'stream-flush',
  145. 'stream-length', 'stream-lines', 'stream-nl', 'stream-print',
  146. 'stream-read', 'stream-read-into', 'stream-read-partial',
  147. 'stream-read-partial-into', 'stream-read-partial-unsafe',
  148. 'stream-read-unsafe', 'stream-read-until', 'stream-read1',
  149. 'stream-readln', 'stream-seek', 'stream-seekable?', 'stream-tell',
  150. 'stream-write', 'stream-write1', 'tell-input', 'tell-output',
  151. 'with-error-stream', 'with-error-stream*', 'with-error>output',
  152. 'with-input-output+error-streams',
  153. 'with-input-output+error-streams*', 'with-input-stream',
  154. 'with-input-stream*', 'with-output-stream', 'with-output-stream*',
  155. 'with-output>error', 'with-output+error-stream',
  156. 'with-output+error-stream*', 'with-streams', 'with-streams*',
  157. 'write', 'write1'), suffix=r'\s')
  158. builtin_strings = words((
  159. '1string', '<string>', '>string', 'resize-string', 'string',
  160. 'string?'), suffix=r'\s')
  161. builtin_vectors = words((
  162. '1vector', '<vector>', '>vector', '?push', 'vector', 'vector?'),
  163. suffix=r'\s')
  164. builtin_continuations = words((
  165. '<condition>', '<continuation>', '<restart>', 'attempt-all',
  166. 'attempt-all-error', 'attempt-all-error?', 'callback-error-hook',
  167. 'callcc0', 'callcc1', 'cleanup', 'compute-restarts', 'condition',
  168. 'condition?', 'continuation', 'continuation?', 'continue',
  169. 'continue-restart', 'continue-with', 'current-continuation',
  170. 'error', 'error-continuation', 'error-in-thread', 'error-thread',
  171. 'ifcc', 'ignore-errors', 'in-callback?', 'original-error', 'recover',
  172. 'restart', 'restart?', 'restarts', 'rethrow', 'rethrow-restarts',
  173. 'return', 'return-continuation', 'thread-error-hook', 'throw-continue',
  174. 'throw-restarts', 'with-datastack', 'with-return'), suffix=r'\s')
  175. tokens = {
  176. 'root': [
  177. # factor allows a file to start with a shebang
  178. (r'#!.*$', Comment.Preproc),
  179. default('base'),
  180. ],
  181. 'base': [
  182. (r'\s+', Text),
  183. # defining words
  184. (r'((?:MACRO|MEMO|TYPED)?:[:]?)(\s+)(\S+)',
  185. bygroups(Keyword, Text, Name.Function)),
  186. (r'(M:[:]?)(\s+)(\S+)(\s+)(\S+)',
  187. bygroups(Keyword, Text, Name.Class, Text, Name.Function)),
  188. (r'(C:)(\s+)(\S+)(\s+)(\S+)',
  189. bygroups(Keyword, Text, Name.Function, Text, Name.Class)),
  190. (r'(GENERIC:)(\s+)(\S+)',
  191. bygroups(Keyword, Text, Name.Function)),
  192. (r'(HOOK:|GENERIC#)(\s+)(\S+)(\s+)(\S+)',
  193. bygroups(Keyword, Text, Name.Function, Text, Name.Function)),
  194. (r'\(\s', Name.Function, 'stackeffect'),
  195. (r';\s', Keyword),
  196. # imports and namespaces
  197. (r'(USING:)(\s+)',
  198. bygroups(Keyword.Namespace, Text), 'vocabs'),
  199. (r'(USE:|UNUSE:|IN:|QUALIFIED:)(\s+)(\S+)',
  200. bygroups(Keyword.Namespace, Text, Name.Namespace)),
  201. (r'(QUALIFIED-WITH:)(\s+)(\S+)(\s+)(\S+)',
  202. bygroups(Keyword.Namespace, Text, Name.Namespace, Text, Name.Namespace)),
  203. (r'(FROM:|EXCLUDE:)(\s+)(\S+)(\s+=>\s)',
  204. bygroups(Keyword.Namespace, Text, Name.Namespace, Text), 'words'),
  205. (r'(RENAME:)(\s+)(\S+)(\s+)(\S+)(\s+=>\s+)(\S+)',
  206. bygroups(Keyword.Namespace, Text, Name.Function, Text, Name.Namespace, Text, Name.Function)),
  207. (r'(ALIAS:|TYPEDEF:)(\s+)(\S+)(\s+)(\S+)',
  208. bygroups(Keyword.Namespace, Text, Name.Function, Text, Name.Function)),
  209. (r'(DEFER:|FORGET:|POSTPONE:)(\s+)(\S+)',
  210. bygroups(Keyword.Namespace, Text, Name.Function)),
  211. # tuples and classes
  212. (r'(TUPLE:|ERROR:)(\s+)(\S+)(\s+<\s+)(\S+)',
  213. bygroups(Keyword, Text, Name.Class, Text, Name.Class), 'slots'),
  214. (r'(TUPLE:|ERROR:|BUILTIN:)(\s+)(\S+)',
  215. bygroups(Keyword, Text, Name.Class), 'slots'),
  216. (r'(MIXIN:|UNION:|INTERSECTION:)(\s+)(\S+)',
  217. bygroups(Keyword, Text, Name.Class)),
  218. (r'(PREDICATE:)(\s+)(\S+)(\s+<\s+)(\S+)',
  219. bygroups(Keyword, Text, Name.Class, Text, Name.Class)),
  220. (r'(C:)(\s+)(\S+)(\s+)(\S+)',
  221. bygroups(Keyword, Text, Name.Function, Text, Name.Class)),
  222. (r'(INSTANCE:)(\s+)(\S+)(\s+)(\S+)',
  223. bygroups(Keyword, Text, Name.Class, Text, Name.Class)),
  224. (r'(SLOT:)(\s+)(\S+)', bygroups(Keyword, Text, Name.Function)),
  225. (r'(SINGLETON:)(\s+)(\S+)', bygroups(Keyword, Text, Name.Class)),
  226. (r'SINGLETONS:', Keyword, 'classes'),
  227. # other syntax
  228. (r'(CONSTANT:|SYMBOL:|MAIN:|HELP:)(\s+)(\S+)',
  229. bygroups(Keyword, Text, Name.Function)),
  230. (r'SYMBOLS:\s', Keyword, 'words'),
  231. (r'SYNTAX:\s', Keyword),
  232. (r'ALIEN:\s', Keyword),
  233. (r'(STRUCT:)(\s+)(\S+)', bygroups(Keyword, Text, Name.Class)),
  234. (r'(FUNCTION:)(\s+\S+\s+)(\S+)(\s+\(\s+[^)]+\)\s)',
  235. bygroups(Keyword.Namespace, Text, Name.Function, Text)),
  236. (r'(FUNCTION-ALIAS:)(\s+)(\S+)(\s+\S+\s+)(\S+)(\s+\(\s+[^)]+\)\s)',
  237. bygroups(Keyword.Namespace, Text, Name.Function, Text, Name.Function, Text)),
  238. # vocab.private
  239. (r'(?:<PRIVATE|PRIVATE>)\s', Keyword.Namespace),
  240. # strings
  241. (r'"""\s+(?:.|\n)*?\s+"""', String),
  242. (r'"(?:\\\\|\\"|[^"])*"', String),
  243. (r'\S+"\s+(?:\\\\|\\"|[^"])*"', String),
  244. (r'CHAR:\s+(?:\\[\\abfnrstv]|[^\\]\S*)\s', String.Char),
  245. # comments
  246. (r'!\s+.*$', Comment),
  247. (r'#!\s+.*$', Comment),
  248. (r'/\*\s+(?:.|\n)*?\s\*/\s', Comment),
  249. # boolean constants
  250. (r'[tf]\s', Name.Constant),
  251. # symbols and literals
  252. (r'[\\$]\s+\S+', Name.Constant),
  253. (r'M\\\s+\S+\s+\S+', Name.Constant),
  254. # numbers
  255. (r'[+-]?(?:[\d,]*\d)?\.(?:\d([\d,]*\d)?)?(?:[eE][+-]?\d+)?\s', Number),
  256. (r'[+-]?\d(?:[\d,]*\d)?(?:[eE][+-]?\d+)?\s', Number),
  257. (r'0x[a-fA-F\d](?:[a-fA-F\d,]*[a-fA-F\d])?(?:p\d([\d,]*\d)?)?\s', Number),
  258. (r'NAN:\s+[a-fA-F\d](?:[a-fA-F\d,]*[a-fA-F\d])?(?:p\d([\d,]*\d)?)?\s', Number),
  259. (r'0b[01]+\s', Number.Bin),
  260. (r'0o[0-7]+\s', Number.Oct),
  261. (r'(?:\d([\d,]*\d)?)?\+\d(?:[\d,]*\d)?/\d(?:[\d,]*\d)?\s', Number),
  262. (r'(?:\-\d([\d,]*\d)?)?\-\d(?:[\d,]*\d)?/\d(?:[\d,]*\d)?\s', Number),
  263. # keywords
  264. (r'(?:deprecated|final|foldable|flushable|inline|recursive)\s',
  265. Keyword),
  266. # builtins
  267. (builtin_kernel, Name.Builtin),
  268. (builtin_assocs, Name.Builtin),
  269. (builtin_combinators, Name.Builtin),
  270. (builtin_math, Name.Builtin),
  271. (builtin_sequences, Name.Builtin),
  272. (builtin_namespaces, Name.Builtin),
  273. (builtin_arrays, Name.Builtin),
  274. (builtin_io, Name.Builtin),
  275. (builtin_strings, Name.Builtin),
  276. (builtin_vectors, Name.Builtin),
  277. (builtin_continuations, Name.Builtin),
  278. # everything else is text
  279. (r'\S+', Text),
  280. ],
  281. 'stackeffect': [
  282. (r'\s+', Text),
  283. (r'\(\s+', Name.Function, 'stackeffect'),
  284. (r'\)\s', Name.Function, '#pop'),
  285. (r'--\s', Name.Function),
  286. (r'\S+', Name.Variable),
  287. ],
  288. 'slots': [
  289. (r'\s+', Text),
  290. (r';\s', Keyword, '#pop'),
  291. (r'(\{\s+)(\S+)(\s+[^}]+\s+\}\s)',
  292. bygroups(Text, Name.Variable, Text)),
  293. (r'\S+', Name.Variable),
  294. ],
  295. 'vocabs': [
  296. (r'\s+', Text),
  297. (r';\s', Keyword, '#pop'),
  298. (r'\S+', Name.Namespace),
  299. ],
  300. 'classes': [
  301. (r'\s+', Text),
  302. (r';\s', Keyword, '#pop'),
  303. (r'\S+', Name.Class),
  304. ],
  305. 'words': [
  306. (r'\s+', Text),
  307. (r';\s', Keyword, '#pop'),
  308. (r'\S+', Name.Function),
  309. ],
  310. }