1" Vim syntax file
2" Language:    FORTH
3" Maintainer:  Christian V. J. Br�ssow <cvjb@cvjb.de>
4" Last Change: Di 07 Jul 2009 21:38:45 CEST
5" Filenames:   *.fs,*.ft
6" URL:	       http://www.cvjb.de/comp/vim/forth.vim
7
8" $Id: forth.vim,v 1.12 2008/07/07 21:39:12 bruessow Exp $
9
10" The list of keywords is incomplete, compared with the offical ANS
11" wordlist. If you use this language, please improve it, and send me
12" the patches.
13"
14" Before sending me patches, please download the newest version of this file
15" from http://www.cvjb.de/comp/vim/forth.vim or http://www.vim.org/ (search
16" for forth.vim).
17
18" Many Thanks to...
19"
20" 2009-06-28:
21" Josh Grams send a patch to allow the parenthesis comments at the
22" beginning of a line. That patch also fixed a typo in one of the
23" comments.
24"
25" 2008-02-09:
26" Shawn K. Quinn <sjquinn at speakeasy dot net> send a big patch with
27" new words commonly used in Forth programs or defined by GNU Forth.
28"
29" 2007-07-11:
30" Benjamin Krill <ben at codiert dot org> send me a patch
31" to highlight space errors.
32" You can toggle this feature on through setting the
33" flag forth_space_errors in you vimrc. If you have switched it on,
34" you can turn off highlighting of trailing spaces in comments by
35" setting forth_no_trail_space_error in your vimrc. If you do not want
36" the highlighting of a tabulator following a space in comments, you
37" can turn this off by setting forth_no_tab_space_error.
38"
39" 2006-05-25:
40" Bill McCarthy <WJMc@...> and Ilya Sher <ilya-vim@...>
41" Who found a bug in the ccomment line in 2004!!!
42" I'm really very sorry, that it has taken two years to fix that
43" in the offical version of this file. Shame on me.
44" I think my face will be red the next ten years...
45"
46" 2006-05-21:
47" Thomas E. Vaughan <tevaugha at ball dot com> send me a patch
48" for the parenthesis comment word, so words with a trailing
49" parenthesis will not start the highlighting for such comments.
50" 
51" 2003-05-10:
52" Andrew Gaul <andrew at gaul.org> send me a patch for
53" forthOperators.
54"
55" 2003-04-03:
56" Ron Aaron <ron at ronware dot org> made updates for an
57" improved Win32Forth support.
58"
59" 2002-04-22:
60" Charles Shattuck <charley at forth dot org> helped me to settle up with the
61" binary and hex number highlighting.
62"
63" 2002-04-20:
64" Charles Shattuck <charley at forth dot org> send me some code for correctly
65" highlighting char and [char] followed by an opening paren. He also added
66" some words for operators, conditionals, and definitions; and added the
67" highlighting for s" and c".
68"
69" 2000-03-28:
70" John Providenza <john at probo dot com> made improvements for the
71" highlighting of strings, and added the code for highlighting hex numbers.
72"
73
74
75" For version 5.x: Clear all syntax items
76" For version 6.x: Quit when a syntax file was already loaded
77if version < 600
78    syntax clear
79elseif exists("b:current_syntax")
80    finish
81endif
82
83" Synchronization method
84syn sync ccomment
85syn sync maxlines=200
86
87" I use gforth, so I set this to case ignore
88syn case ignore
89
90" Some special, non-FORTH keywords
91syn keyword forthTodo contained TODO FIXME XXX
92syn match forthTodo contained 'Copyright\(\s([Cc])\)\=\(\s[0-9]\{2,4}\)\='
93
94" Characters allowed in keywords
95" I don't know if 128-255 are allowed in ANS-FORTH
96if version >= 600
97    setlocal iskeyword=!,@,33-35,%,$,38-64,A-Z,91-96,a-z,123-126,128-255
98else
99    set iskeyword=!,@,33-35,%,$,38-64,A-Z,91-96,a-z,123-126,128-255
100endif
101
102" when wanted, highlight trailing white space
103if exists("forth_space_errors")
104    if !exists("forth_no_trail_space_error")
105        syn match forthSpaceError display excludenl "\s\+$"
106    endif
107    if !exists("forth_no_tab_space_error")
108        syn match forthSpaceError display " \+\t"me=e-1
109    endif
110endif
111
112" Keywords
113
114" basic mathematical and logical operators
115syn keyword forthOperators + - * / MOD /MOD NEGATE ABS MIN MAX
116syn keyword forthOperators AND OR XOR NOT LSHIFT RSHIFT INVERT 2* 2/ 1+
117syn keyword forthOperators 1- 2+ 2- 8* UNDER+
118syn keyword forthOperators M+ */ */MOD M* UM* M*/ UM/MOD FM/MOD SM/REM
119syn keyword forthOperators D+ D- DNEGATE DABS DMIN DMAX D2* D2/
120syn keyword forthOperators F+ F- F* F/ FNEGATE FABS FMAX FMIN FLOOR FROUND
121syn keyword forthOperators F** FSQRT FEXP FEXPM1 FLN FLNP1 FLOG FALOG FSIN
122syn keyword forthOperators FCOS FSINCOS FTAN FASIN FACOS FATAN FATAN2 FSINH
123syn keyword forthOperators FCOSH FTANH FASINH FACOSH FATANH F2* F2/ 1/F
124syn keyword forthOperators F~REL F~ABS F~
125syn keyword forthOperators 0< 0<= 0<> 0= 0> 0>= < <= <> = > >= U< U<=
126syn keyword forthOperators U> U>= D0< D0<= D0<> D0= D0> D0>= D< D<= D<>
127syn keyword forthOperators D= D> D>= DU< DU<= DU> DU>= WITHIN ?NEGATE
128syn keyword forthOperators ?DNEGATE 
129
130" stack manipulations
131syn keyword forthStack DROP NIP DUP OVER TUCK SWAP ROT -ROT ?DUP PICK ROLL
132syn keyword forthStack 2DROP 2NIP 2DUP 2OVER 2TUCK 2SWAP 2ROT 2-ROT
133syn keyword forthStack 3DUP 4DUP 5DUP 3DROP 4DROP 5DROP 8DROP 4SWAP 4ROT
134syn keyword forthStack 4-ROT 4TUCK 8SWAP 8DUP
135syn keyword forthRStack >R R> R@ RDROP 2>R 2R> 2R@ 2RDROP
136syn keyword forthRstack 4>R 4R> 4R@ 4RDROP
137syn keyword forthFStack FDROP FNIP FDUP FOVER FTUCK FSWAP FROT
138
139" stack pointer manipulations
140syn keyword forthSP SP@ SP! FP@ FP! RP@ RP! LP@ LP!
141
142" address operations
143syn keyword forthMemory @ ! +! C@ C! 2@ 2! F@ F! SF@ SF! DF@ DF!
144syn keyword forthAdrArith CHARS CHAR+ CELLS CELL+ CELL ALIGN ALIGNED FLOATS
145syn keyword forthAdrArith FLOAT+ FLOAT FALIGN FALIGNED SFLOATS SFLOAT+
146syn keyword forthAdrArith SFALIGN SFALIGNED DFLOATS DFLOAT+ DFALIGN DFALIGNED
147syn keyword forthAdrArith MAXALIGN MAXALIGNED CFALIGN CFALIGNED
148syn keyword forthAdrArith ADDRESS-UNIT-BITS ALLOT ALLOCATE HERE
149syn keyword forthMemBlks MOVE ERASE CMOVE CMOVE> FILL BLANK
150
151" conditionals
152syn keyword forthCond IF ELSE ENDIF THEN CASE OF ENDOF ENDCASE ?DUP-IF
153syn keyword forthCond ?DUP-0=-IF AHEAD CS-PICK CS-ROLL CATCH THROW WITHIN
154
155" iterations
156syn keyword forthLoop BEGIN WHILE REPEAT UNTIL AGAIN
157syn keyword forthLoop ?DO LOOP I J K +DO U+DO -DO U-DO DO +LOOP -LOOP
158syn keyword forthLoop UNLOOP LEAVE ?LEAVE EXIT DONE FOR NEXT
159
160" new words
161syn match forthClassDef '\<:class\s*[^ \t]\+\>'
162syn match forthObjectDef '\<:object\s*[^ \t]\+\>'
163syn match forthColonDef '\<:m\?\s*[^ \t]\+\>'
164syn keyword forthEndOfColonDef ; ;M ;m
165syn keyword forthEndOfClassDef ;class
166syn keyword forthEndOfObjectDef ;object
167syn keyword forthDefine CONSTANT 2CONSTANT FCONSTANT VARIABLE 2VARIABLE
168syn keyword forthDefine FVARIABLE CREATE USER VALUE TO DEFER IS DOES> IMMEDIATE
169syn keyword forthDefine COMPILE-ONLY COMPILE RESTRICT INTERPRET POSTPONE EXECUTE
170syn keyword forthDefine LITERAL CREATE-INTERPRET/COMPILE INTERPRETATION>
171syn keyword forthDefine <INTERPRETATION COMPILATION> <COMPILATION ] LASTXT
172syn keyword forthDefine COMP' POSTPONE, FIND-NAME NAME>INT NAME?INT NAME>COMP
173syn keyword forthDefine NAME>STRING STATE C; CVARIABLE
174syn keyword forthDefine , 2, F, C, 
175syn match forthDefine "\[IFDEF]"
176syn match forthDefine "\[IFUNDEF]"
177syn match forthDefine "\[THEN]"
178syn match forthDefine "\[ENDIF]"
179syn match forthDefine "\[ELSE]"
180syn match forthDefine "\[?DO]"
181syn match forthDefine "\[DO]"
182syn match forthDefine "\[LOOP]"
183syn match forthDefine "\[+LOOP]"
184syn match forthDefine "\[NEXT]"
185syn match forthDefine "\[BEGIN]"
186syn match forthDefine "\[UNTIL]"
187syn match forthDefine "\[AGAIN]"
188syn match forthDefine "\[WHILE]"
189syn match forthDefine "\[REPEAT]"
190syn match forthDefine "\[COMP']"
191syn match forthDefine "'"
192syn match forthDefine '\<\[\>'
193syn match forthDefine "\[']"
194syn match forthDefine '\[COMPILE]'
195
196" debugging
197syn keyword forthDebug PRINTDEBUGDATA PRINTDEBUGLINE
198syn match forthDebug "\<\~\~\>"
199
200" Assembler
201syn keyword forthAssembler ASSEMBLER CODE END-CODE ;CODE FLUSH-ICACHE C,
202
203" basic character operations
204syn keyword forthCharOps (.) CHAR EXPECT FIND WORD TYPE -TRAILING EMIT KEY
205syn keyword forthCharOps KEY? TIB CR
206" recognize 'char (' or '[char] (' correctly, so it doesn't
207" highlight everything after the paren as a comment till a closing ')'
208syn match forthCharOps '\<char\s\S\s'
209syn match forthCharOps '\<\[char\]\s\S\s'
210syn region forthCharOps start=+."\s+ skip=+\\"+ end=+"+
211
212" char-number conversion
213syn keyword forthConversion <<# <# # #> #>> #S (NUMBER) (NUMBER?) CONVERT D>F 
214syn keyword forthConversion D>S DIGIT DPL F>D HLD HOLD NUMBER S>D SIGN >NUMBER
215syn keyword forthConversion F>S S>F
216
217" interpreter, wordbook, compiler
218syn keyword forthForth (LOCAL) BYE COLD ABORT >BODY >NEXT >LINK CFA >VIEW HERE
219syn keyword forthForth PAD WORDS VIEW VIEW> N>LINK NAME> LINK> L>NAME FORGET
220syn keyword forthForth BODY> ASSERT( ASSERT0( ASSERT1( ASSERT2( ASSERT3( )
221syn region forthForth start=+ABORT"\s+ skip=+\\"+ end=+"+
222
223" vocabularies
224syn keyword forthVocs ONLY FORTH ALSO ROOT SEAL VOCS ORDER CONTEXT #VOCS
225syn keyword forthVocs VOCABULARY DEFINITIONS
226
227" File keywords
228syn keyword forthFileMode R/O R/W W/O BIN 
229syn keyword forthFileWords OPEN-FILE CREATE-FILE CLOSE-FILE DELETE-FILE
230syn keyword forthFileWords RENAME-FILE READ-FILE READ-LINE KEY-FILE
231syn keyword forthFileWords KEY?-FILE WRITE-FILE WRITE-LINE EMIT-FILE
232syn keyword forthFileWords FLUSH-FILE FILE-STATUS FILE-POSITION
233syn keyword forthFileWords REPOSITION-FILE FILE-SIZE RESIZE-FILE
234syn keyword forthFileWords SLURP-FILE SLURP-FID STDIN STDOUT STDERR
235syn keyword forthBlocks OPEN-BLOCKS USE LOAD --> BLOCK-OFFSET
236syn keyword forthBlocks GET-BLOCK-FID BLOCK-POSITION LIST SCR BLOCK
237syn keyword forthBlocks BUFER EMPTY-BUFFERS EMPTY-BUFFER UPDATE UPDATED?
238syn keyword forthBlocks SAVE-BUFFERS SAVE-BUFFER FLUSH THRU +LOAD +THRU
239syn keyword forthBlocks BLOCK-INCLUDED
240
241" numbers
242syn keyword forthMath DECIMAL HEX BASE
243syn match forthInteger '\<-\=[0-9.]*[0-9.]\+\>'
244syn match forthInteger '\<&-\=[0-9.]*[0-9.]\+\>'
245" recognize hex and binary numbers, the '$' and '%' notation is for gforth
246syn match forthInteger '\<\$\x*\x\+\>' " *1* --- dont't mess
247syn match forthInteger '\<\x*\d\x*\>'  " *2* --- this order!
248syn match forthInteger '\<%[0-1]*[0-1]\+\>'
249syn match forthFloat '\<-\=\d*[.]\=\d\+[DdEe]\d\+\>'
250syn match forthFloat '\<-\=\d*[.]\=\d\+[DdEe][-+]\d\+\>'
251
252" XXX If you find this overkill you can remove it. this has to come after the
253" highlighting for numbers otherwise it has no effect.
254syn region forthComment start='0 \[if\]' end='\[endif\]' end='\[then\]' contains=forthTodo
255
256" Strings
257syn region forthString start=+\.*\"+ end=+"+ end=+$+
258" XXX
259syn region forthString start=+s\"+ end=+"+ end=+$+
260syn region forthString start=+c\"+ end=+"+ end=+$+
261
262" Comments
263syn match forthComment '\\\s.*$' contains=forthTodo,forthSpaceError
264syn region forthComment start='\\S\s' end='.*' contains=forthTodo,forthSpaceError
265syn match forthComment '\.(\s[^)]*)' contains=forthTodo,forthSpaceError
266syn region forthComment start='\(^\|\s\)\zs(\s' skip='\\)' end=')' contains=forthTodo,forthSpaceError
267syn region forthComment start='/\*' end='\*/' contains=forthTodo,forthSpaceError
268
269" Include files
270syn match forthInclude '^INCLUDE\s\+\k\+'
271syn match forthInclude '^require\s\+\k\+'
272syn match forthInclude '^fload\s\+'
273syn match forthInclude '^needs\s\+'
274
275" Locals definitions
276syn region forthLocals start='{\s' start='{$' end='\s}' end='^}'
277syn match forthLocals '{ }' " otherwise, at least two spaces between
278syn region forthDeprecated start='locals|' end='|'
279
280" Define the default highlighting.
281" For version 5.7 and earlier: only when not done already
282" For version 5.8 and later: only when an item doesn't have highlighting yet
283if version >= 508 || !exists("did_forth_syn_inits")
284    if version < 508
285	let did_forth_syn_inits = 1
286	command -nargs=+ HiLink hi link <args>
287    else
288	command -nargs=+ HiLink hi def link <args>
289    endif
290
291    " The default methods for highlighting. Can be overriden later.
292    HiLink forthTodo Todo
293    HiLink forthOperators Operator
294    HiLink forthMath Number
295    HiLink forthInteger Number
296    HiLink forthFloat Float
297    HiLink forthStack Special
298    HiLink forthRstack Special
299    HiLink forthFStack Special
300    HiLink forthSP Special
301    HiLink forthMemory Function
302    HiLink forthAdrArith Function
303    HiLink forthMemBlks Function
304    HiLink forthCond Conditional
305    HiLink forthLoop Repeat
306    HiLink forthColonDef Define
307    HiLink forthEndOfColonDef Define
308    HiLink forthDefine Define
309    HiLink forthDebug Debug
310    HiLink forthAssembler Include
311    HiLink forthCharOps Character
312    HiLink forthConversion String
313    HiLink forthForth Statement
314    HiLink forthVocs Statement
315    HiLink forthString String
316    HiLink forthComment Comment
317    HiLink forthClassDef Define
318    HiLink forthEndOfClassDef Define
319    HiLink forthObjectDef Define
320    HiLink forthEndOfObjectDef Define
321    HiLink forthInclude Include
322    HiLink forthLocals Type " nothing else uses type and locals must stand out
323    HiLink forthDeprecated Error " if you must, change to Type
324    HiLink forthFileMode Function
325    HiLink forthFileWords Statement
326    HiLink forthBlocks Statement
327    HiLink forthSpaceError Error
328
329    delcommand HiLink
330endif
331
332let b:current_syntax = "forth"
333
334" vim:ts=8:sw=4:nocindent:smartindent:
335