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