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