factor.py 17 KB

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