1 # -*- coding: utf-8 -*- |
|
2 """ |
|
3 pygments.lexers.forth |
|
4 ~~~~~~~~~~~~~~~~~~~~~ |
|
5 |
|
6 Lexer for the Forth language. |
|
7 |
|
8 :copyright: Copyright 2006-2021 by the Pygments team, see AUTHORS. |
|
9 :license: BSD, see LICENSE for details. |
|
10 """ |
|
11 |
|
12 import re |
|
13 |
|
14 from pygments.lexer import RegexLexer, bygroups |
|
15 from pygments.token import Text, Comment, Keyword, Name, String, Number |
|
16 |
|
17 |
|
18 __all__ = ['ForthLexer'] |
|
19 |
|
20 |
|
21 class ForthLexer(RegexLexer): |
|
22 """ |
|
23 Lexer for Forth files. |
|
24 |
|
25 .. versionadded:: 2.2 |
|
26 """ |
|
27 name = 'Forth' |
|
28 aliases = ['forth'] |
|
29 filenames = ['*.frt', '*.fs'] |
|
30 mimetypes = ['application/x-forth'] |
|
31 |
|
32 flags = re.IGNORECASE | re.MULTILINE |
|
33 |
|
34 tokens = { |
|
35 'root': [ |
|
36 (r'\s+', Text), |
|
37 # All comment types |
|
38 (r'\\.*?\n', Comment.Single), |
|
39 (r'\([\s].*?\)', Comment.Single), |
|
40 # defining words. The next word is a new command name |
|
41 (r'(:|variable|constant|value|buffer:)(\s+)', |
|
42 bygroups(Keyword.Namespace, Text), 'worddef'), |
|
43 # strings are rather simple |
|
44 (r'([.sc]")(\s+?)', bygroups(String, Text), 'stringdef'), |
|
45 # keywords from the various wordsets |
|
46 # *** Wordset BLOCK |
|
47 (r'(blk|block|buffer|evaluate|flush|load|save-buffers|update|' |
|
48 # *** Wordset BLOCK-EXT |
|
49 r'empty-buffers|list|refill|scr|thru|' |
|
50 # *** Wordset CORE |
|
51 r'\#s|\*\/mod|\+loop|\/mod|0<|0=|1\+|1-|2!|' |
|
52 r'2\*|2\/|2@|2drop|2dup|2over|2swap|>body|' |
|
53 r'>in|>number|>r|\?dup|abort|abort\"|abs|' |
|
54 r'accept|align|aligned|allot|and|base|begin|' |
|
55 r'bl|c!|c,|c@|cell\+|cells|char|char\+|' |
|
56 r'chars|constant|count|cr|create|decimal|' |
|
57 r'depth|do|does>|drop|dup|else|emit|environment\?|' |
|
58 r'evaluate|execute|exit|fill|find|fm\/mod|' |
|
59 r'here|hold|i|if|immediate|invert|j|key|' |
|
60 r'leave|literal|loop|lshift|m\*|max|min|' |
|
61 r'mod|move|negate|or|over|postpone|quit|' |
|
62 r'r>|r@|recurse|repeat|rot|rshift|s\"|s>d|' |
|
63 r'sign|sm\/rem|source|space|spaces|state|swap|' |
|
64 r'then|type|u\.|u\<|um\*|um\/mod|unloop|until|' |
|
65 r'variable|while|word|xor|\[char\]|\[\'\]|' |
|
66 r'@|!|\#|<\#|\#>|:|;|\+|-|\*|\/|,|<|>|\|1\+|1-|\.|' |
|
67 # *** Wordset CORE-EXT |
|
68 r'\.r|0<>|' |
|
69 r'0>|2>r|2r>|2r@|:noname|\?do|again|c\"|' |
|
70 r'case|compile,|endcase|endof|erase|false|' |
|
71 r'hex|marker|nip|of|pad|parse|pick|refill|' |
|
72 r'restore-input|roll|save-input|source-id|to|' |
|
73 r'true|tuck|u\.r|u>|unused|value|within|' |
|
74 r'\[compile\]|' |
|
75 # *** Wordset CORE-EXT-obsolescent |
|
76 r'\#tib|convert|expect|query|span|' |
|
77 r'tib|' |
|
78 # *** Wordset DOUBLE |
|
79 r'2constant|2literal|2variable|d\+|d-|' |
|
80 r'd\.|d\.r|d0<|d0=|d2\*|d2\/|d<|d=|d>s|' |
|
81 r'dabs|dmax|dmin|dnegate|m\*\/|m\+|' |
|
82 # *** Wordset DOUBLE-EXT |
|
83 r'2rot|du<|' |
|
84 # *** Wordset EXCEPTION |
|
85 r'catch|throw|' |
|
86 # *** Wordset EXCEPTION-EXT |
|
87 r'abort|abort\"|' |
|
88 # *** Wordset FACILITY |
|
89 r'at-xy|key\?|page|' |
|
90 # *** Wordset FACILITY-EXT |
|
91 r'ekey|ekey>char|ekey\?|emit\?|ms|time&date|' |
|
92 # *** Wordset FILE |
|
93 r'BIN|CLOSE-FILE|CREATE-FILE|DELETE-FILE|FILE-POSITION|' |
|
94 r'FILE-SIZE|INCLUDE-FILE|INCLUDED|OPEN-FILE|R\/O|' |
|
95 r'R\/W|READ-FILE|READ-LINE|REPOSITION-FILE|RESIZE-FILE|' |
|
96 r'S\"|SOURCE-ID|W/O|WRITE-FILE|WRITE-LINE|' |
|
97 # *** Wordset FILE-EXT |
|
98 r'FILE-STATUS|FLUSH-FILE|REFILL|RENAME-FILE|' |
|
99 # *** Wordset FLOAT |
|
100 r'>float|d>f|' |
|
101 r'f!|f\*|f\+|f-|f\/|f0<|f0=|f<|f>d|f@|' |
|
102 r'falign|faligned|fconstant|fdepth|fdrop|fdup|' |
|
103 r'fliteral|float\+|floats|floor|fmax|fmin|' |
|
104 r'fnegate|fover|frot|fround|fswap|fvariable|' |
|
105 r'represent|' |
|
106 # *** Wordset FLOAT-EXT |
|
107 r'df!|df@|dfalign|dfaligned|dfloat\+|' |
|
108 r'dfloats|f\*\*|f\.|fabs|facos|facosh|falog|' |
|
109 r'fasin|fasinh|fatan|fatan2|fatanh|fcos|fcosh|' |
|
110 r'fe\.|fexp|fexpm1|fln|flnp1|flog|fs\.|fsin|' |
|
111 r'fsincos|fsinh|fsqrt|ftan|ftanh|f~|precision|' |
|
112 r'set-precision|sf!|sf@|sfalign|sfaligned|sfloat\+|' |
|
113 r'sfloats|' |
|
114 # *** Wordset LOCAL |
|
115 r'\(local\)|to|' |
|
116 # *** Wordset LOCAL-EXT |
|
117 r'locals\||' |
|
118 # *** Wordset MEMORY |
|
119 r'allocate|free|resize|' |
|
120 # *** Wordset SEARCH |
|
121 r'definitions|find|forth-wordlist|get-current|' |
|
122 r'get-order|search-wordlist|set-current|set-order|' |
|
123 r'wordlist|' |
|
124 # *** Wordset SEARCH-EXT |
|
125 r'also|forth|only|order|previous|' |
|
126 # *** Wordset STRING |
|
127 r'-trailing|\/string|blank|cmove|cmove>|compare|' |
|
128 r'search|sliteral|' |
|
129 # *** Wordset TOOLS |
|
130 r'.s|dump|see|words|' |
|
131 # *** Wordset TOOLS-EXT |
|
132 r';code|' |
|
133 r'ahead|assembler|bye|code|cs-pick|cs-roll|' |
|
134 r'editor|state|\[else\]|\[if\]|\[then\]|' |
|
135 # *** Wordset TOOLS-EXT-obsolescent |
|
136 r'forget|' |
|
137 # Forth 2012 |
|
138 r'defer|defer@|defer!|action-of|begin-structure|field:|buffer:|' |
|
139 r'parse-name|buffer:|traverse-wordlist|n>r|nr>|2value|fvalue|' |
|
140 r'name>interpret|name>compile|name>string|' |
|
141 r'cfield:|end-structure)(?!\S)', Keyword), |
|
142 |
|
143 # Numbers |
|
144 (r'(\$[0-9A-F]+)', Number.Hex), |
|
145 (r'(\#|%|&|\-|\+)?[0-9]+', Number.Integer), |
|
146 (r'(\#|%|&|\-|\+)?[0-9.]+', Keyword.Type), |
|
147 # amforth specific |
|
148 (r'(@i|!i|@e|!e|pause|noop|turnkey|sleep|' |
|
149 r'itype|icompare|sp@|sp!|rp@|rp!|up@|up!|' |
|
150 r'>a|a>|a@|a!|a@+|a@-|>b|b>|b@|b!|b@+|b@-|' |
|
151 r'find-name|1ms|' |
|
152 r'sp0|rp0|\(evaluate\)|int-trap|int!)(?!\S)', |
|
153 Name.Constant), |
|
154 # a proposal |
|
155 (r'(do-recognizer|r:fail|recognizer:|get-recognizers|' |
|
156 r'set-recognizers|r:float|r>comp|r>int|r>post|' |
|
157 r'r:name|r:word|r:dnum|r:num|recognizer|forth-recognizer|' |
|
158 r'rec:num|rec:float|rec:word)(?!\S)', Name.Decorator), |
|
159 # defining words. The next word is a new command name |
|
160 (r'(Evalue|Rvalue|Uvalue|Edefer|Rdefer|Udefer)(\s+)', |
|
161 bygroups(Keyword.Namespace, Text), 'worddef'), |
|
162 |
|
163 (r'\S+', Name.Function), # Anything else is executed |
|
164 |
|
165 ], |
|
166 'worddef': [ |
|
167 (r'\S+', Name.Class, '#pop'), |
|
168 ], |
|
169 'stringdef': [ |
|
170 (r'[^"]+', String, '#pop'), |
|
171 ], |
|
172 } |
|
173 |
|
174 def analyse_text(text): |
|
175 """Forth uses : COMMAND ; quite a lot in a single line, so we're trying |
|
176 to find that.""" |
|
177 if re.search('\n:[^\n]+;\n', text): |
|
178 return 0.1 |
|