blob: 82197adbedb520c439f489cea3a193272f696d3c [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001" Vim syntax file
Bram Moolenaar7ceefb32020-05-01 16:07:38 +02002" Language: Lex and Flex
Bram Moolenaar1d9215b2020-01-25 13:27:42 +01003" Maintainer: Charles E. Campbell <NcampObell@SdrPchip.AorgM-NOSPAM>
Bram Moolenaar7ceefb32020-05-01 16:07:38 +02004" Contributor: Robert A. van Engelen <engelen@acm.org>
5" Last Change: Apr 24, 2020
6" Version: 18
Bram Moolenaar071d4272004-06-13 20:20:40 +00007
Bram Moolenaar89bcfda2016-08-30 23:26:57 +02008" quit when a syntax file was already loaded
9if exists("b:current_syntax")
Bram Moolenaar071d4272004-06-13 20:20:40 +000010 finish
11endif
12
Bram Moolenaar7ceefb32020-05-01 16:07:38 +020013" Read the C++ syntax to start with
14let s:Cpath= fnameescape(expand("<sfile>:p:h")."/cpp.vim")
Bram Moolenaar97d62492012-11-15 21:28:22 +010015if !filereadable(s:Cpath)
Bram Moolenaar7ceefb32020-05-01 16:07:38 +020016 for s:Cpath in split(globpath(&rtp,"syntax/cpp.vim"),"\n")
Bram Moolenaar97d62492012-11-15 21:28:22 +010017 if filereadable(fnameescape(s:Cpath))
18 let s:Cpath= fnameescape(s:Cpath)
19 break
Bram Moolenaar071d4272004-06-13 20:20:40 +000020 endif
Bram Moolenaar97d62492012-11-15 21:28:22 +010021 endfor
Bram Moolenaar071d4272004-06-13 20:20:40 +000022endif
Bram Moolenaar97d62492012-11-15 21:28:22 +010023exe "syn include @lexCcode ".s:Cpath
Bram Moolenaar071d4272004-06-13 20:20:40 +000024
Bram Moolenaard4755bb2004-09-02 19:12:26 +000025" --- ========= ---
Bram Moolenaar071d4272004-06-13 20:20:40 +000026" --- Lex stuff ---
Bram Moolenaard4755bb2004-09-02 19:12:26 +000027" --- ========= ---
Bram Moolenaar071d4272004-06-13 20:20:40 +000028
Bram Moolenaar7ceefb32020-05-01 16:07:38 +020029" Definitions
30" %%
31" Rules
32" %%
33" User Code
34"
35" --- ======= ---
36" --- Example ---
37" --- ======= ---
38"
39" // this is a valid lex file
40" // indented initial code block
41" #include <stdlib.h>
42" %{
43" // initial code block
44" #include <stdio.h>
45" const char *sep = "";
46" %}
47" %option outfile="scanner.c" noyywrap nodefault
48" %x COMMENT
49" id [A-Za-z_][A-Za-z0-9_]*
50" %%
51" // indented initial action code block
52" printf("BEGIN");
53" {id} printf("%s%s", sep, yytext); sep = "";
54" . |
55" \n { sep = "\n"; }
56" "/*" { BEGIN COMMENT; }
57" "//".* { }
58" <COMMENT>{
59" "*/" { BEGIN INITIAL; }
60" .|\n
61" }
62" <*><<EOF>> { // end of file
63" printf("\nEND\n");
64" yyterminate();
65" }
66" %%
67" void scan()
68" {
69" while (yylex())
70" continue;
71" }
72" /* main program */
73" int main()
74" {
75" scan();
76" }
Bram Moolenaar81af9252010-12-10 20:35:50 +010077
Bram Moolenaar7ceefb32020-05-01 16:07:38 +020078" Definitions Section with initial code blocks, abbreviations, options, states
Bram Moolenaar5c736222010-01-06 20:54:52 +010079if has("folding")
Bram Moolenaar7ceefb32020-05-01 16:07:38 +020080 syn region lexAbbrvBlock fold start="^\S" end="^\ze%%" skipnl nextgroup=lexPatBlock contains=lexOptions,lexAbbrv,lexInitialCodeBlock,lexInclude,lexAbbrvComment,lexStartState
Bram Moolenaar5c736222010-01-06 20:54:52 +010081else
Bram Moolenaar7ceefb32020-05-01 16:07:38 +020082 syn region lexAbbrvBlock start="^\S" end="^\ze%%" skipnl nextgroup=lexPatBlock contains=lexOptions,lexAbbrv,lexInitialCodeBlock,lexInclude,lexAbbrvComment,lexStartState
Bram Moolenaar5c736222010-01-06 20:54:52 +010083endif
Bram Moolenaar7ceefb32020-05-01 16:07:38 +020084syn match lexOptions "^%\a\+\(\s.*\|[^{]*\)$" contains=lexOptionsEq,lexPatString,lexSlashQuote,lexBrace,lexSlashBrace
85syn match lexOptionsEq "=" skipwhite contained
86syn match lexAbbrv "^\I\i*\s"me=e-1 skipwhite contained nextgroup=lexAbbrvPat
87syn match lexAbbrvPat "\s\S.*$"lc=1 contained contains=lexPatAbbrv,lexPatString,lexSlashQuote,lexBrace,lexSlashBrace nextgroup=lexAbbrv,lexInclude
88syn match lexStartState "^%\(xs\?\|s\)\(t\(a\(t\(e\?\)\?\)\?\)\?\)\?\(\s\+\I\i*\)\+\s*$" contained contains=lexStartStateCmd
89syn match lexStartStateCmd '^%\(xs\?\|s\)\(t\(a\(t\(e\?\)\?\)\?\)\?\)\?' contained
Bram Moolenaar5c736222010-01-06 20:54:52 +010090if has("folding")
Bram Moolenaar7ceefb32020-05-01 16:07:38 +020091 syn region lexInitialCodeBlock fold start="^\s\+" end="^\S"me=e-1 contains=@lexCcode
92 syn region lexInclude fold matchgroup=lexSep start="^%\a*{" end="^%\?}" contained contains=@lexCcode,lexCFunctions
93 syn region lexAbbrvComment fold start="^\s*//" end="$" contains=@Spell
94 syn region lexAbbrvComment fold start="^\s*/\*" end="\*/" contains=@Spell
Bram Moolenaar5c736222010-01-06 20:54:52 +010095else
Bram Moolenaar7ceefb32020-05-01 16:07:38 +020096 syn region lexInitialCodeBlock start="^\s\+" end="^\S"me=e-1 contains=@lexCcode
97 syn region lexInclude matchgroup=lexSep start="^%\a*{" end="^%\?}" contained contains=@lexCcode,lexCFunctions
98 syn region lexAbbrvComment start="^\s*//" end="$" contains=@Spell
99 syn region lexAbbrvComment start="^\s*/\*" end="\*/" contains=@Spell
Bram Moolenaar5c736222010-01-06 20:54:52 +0100100endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000101
Bram Moolenaar7ceefb32020-05-01 16:07:38 +0200102" Rules Section with patterns and actions
Bram Moolenaar5c736222010-01-06 20:54:52 +0100103if has("folding")
Bram Moolenaar7ceefb32020-05-01 16:07:38 +0200104 syn region lexPatBlock fold matchgroup=Todo start="^%%" matchgroup=Todo end="^\ze%%" skipnl skipwhite nextgroup=lexFinalCodeBlock contains=lexPatTag,lexPatTagZone,lexPatComment,lexPat,lexPatSep,lexPatInclude
105 syn region lexPat fold start="\S" skip="\\\\\|\\\s" end="\ze\(\s*$\|\s\+\(\h\|{\W\|{$\|[-+*]\|//\|/\*\)\)" skipwhite contained nextgroup=lexMorePat,lexPatSep,lexPatEnd contains=lexPatTag,lexPatString,lexSlashQuote,lexPatAbbrv,lexBrace,lexSlashBrace
106 syn region lexPatInclude fold matchgroup=lexSep start="^%{" end="^%}" contained contains=@lexCcode
107 syn region lexBrace fold matchgroup=Character start="\[" skip="\\.\|\[:\a\+:\]\|\[\.\a\+\.\]\|\[=.=\]" end="\]" contained
108 syn region lexPatString fold matchgroup=String start=+"+ skip=+\\\\\|\\"+ matchgroup=String end=+"+ contained
Bram Moolenaar5c736222010-01-06 20:54:52 +0100109else
Bram Moolenaar7ceefb32020-05-01 16:07:38 +0200110 syn region lexPatBlock matchgroup=Todo start="^%%" matchgroup=Todo end="^\ze%%" skipnl skipwhite nextgroup=lexFinalCodeBlock contains=lexPatTag,lexPatTagZone,lexPatComment,lexPat,lexPatSep,lexPatInclude
111 syn region lexPat start="\S" skip="\\\\\|\\\s" end="\ze\(\s*$\|\s\+\(\h\|{\W\|{$\|[-+*]\|//\|/\*\)\)" skipwhite contained nextgroup=lexMorePat,lexPatSep,lexPatEnd contains=lexPatTag,lexPatString,lexSlashQuote,lexPatAbbrv,lexBrace,lexSlashBrace
112 syn region lexPatInclude matchgroup=lexSep start="^%{" end="^%}" contained contains=@lexCcode
113 syn region lexBrace matchgroup=Character start="\[" skip="\\.\|\[:\a\+:\]\|\[\.\a\+\.\]\|\[=.=\]" end="\]" contained
114 syn region lexPatString matchgroup=String start=+"+ skip=+\\\\\|\\"+ matchgroup=String end=+"+ contained
Bram Moolenaar5c736222010-01-06 20:54:52 +0100115endif
Bram Moolenaar7ceefb32020-05-01 16:07:38 +0200116syn match lexPatAbbrv "{\I\i*}"hs=s+1,he=e-1 contained
117syn match lexPatTag "^<\^\?\(\I\i*\|\*\)\(,\^\?\(\I\i*\|\*\)\)*>" contained nextgroup=lexPat,lexMorePat,lexPatSep,lexPatEnd
118syn match lexPatTagZone "^<\^\?\(\I\i*\|\*\)\(,\^\?\(\I\i*\|\*\)\)*>\s*{$"me=e-1 contained nextgroup=lexPatTagZoneStart
Bram Moolenaar97d62492012-11-15 21:28:22 +0100119
Bram Moolenaar5c736222010-01-06 20:54:52 +0100120if has("folding")
Bram Moolenaar7ceefb32020-05-01 16:07:38 +0200121 syn region lexPatTagZoneStart fold matchgroup=lexPatTag start='{$' end='^}' skipnl skipwhite contained contains=lexPatTag,lexPatTagZone,lexPatComment,lexPat,lexPatSep,lexPatInclude
122 syn region lexPatComment fold start="//" end="$" skipnl contained contains=cTodo skipwhite nextgroup=lexPatComment,lexPat,@Spell
123 syn region lexPatComment fold start="/\*" end="\*/" skipnl contained contains=cTodo skipwhite nextgroup=lexPatComment,lexPat,@Spell
Bram Moolenaar5c736222010-01-06 20:54:52 +0100124else
Bram Moolenaar23515b42020-11-29 14:36:24 +0100125 syn region lexPatTagZoneStart matchgroup=lexPatTag start='{' end='^}' skipnl skipwhite contained contains=lexPatTag,lexPatTagZone,lexPatComment,lexPat,lexPatSep,lexPatInclude
Bram Moolenaar7ceefb32020-05-01 16:07:38 +0200126 syn region lexPatComment start="//" end="$" skipnl contained contains=cTodo skipwhite nextgroup=lexPatComment,lexPat,@Spell
127 syn region lexPatComment start="/\*" end="\*/" skipnl contained contains=cTodo skipwhite nextgroup=lexPatComment,lexPat,@Spell
Bram Moolenaar5c736222010-01-06 20:54:52 +0100128endif
Bram Moolenaar7ceefb32020-05-01 16:07:38 +0200129syn match lexPatEnd "\s*$" skipnl contained
130syn match lexPatCodeLine "[^{\[].*" contained contains=@lexCcode,lexCFunctions
131syn match lexMorePat "\s*|\s*$" skipnl contained nextgroup=lexPat,lexPatTag,lexPatComment
132syn match lexPatSep "\s\+" contained nextgroup=lexMorePat,lexPatCode,lexPatCodeLine
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133syn match lexSlashQuote +\(\\\\\)*\\"+ contained
Bram Moolenaar7ceefb32020-05-01 16:07:38 +0200134syn match lexSlashBrace +\(\\\\\)*\\\[+ contained
Bram Moolenaar5c736222010-01-06 20:54:52 +0100135if has("folding")
Bram Moolenaar7ceefb32020-05-01 16:07:38 +0200136 syn region lexPatCode fold matchgroup=Delimiter start="{" end="}" skipnl contained contains=@lexCcode,lexCFunctions
Bram Moolenaar5c736222010-01-06 20:54:52 +0100137else
Bram Moolenaar7ceefb32020-05-01 16:07:38 +0200138 syn region lexPatCode matchgroup=Delimiter start="{" end="}" skipnl contained contains=@lexCcode,lexCFunctions
Bram Moolenaar5c736222010-01-06 20:54:52 +0100139endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140
Bram Moolenaar7ceefb32020-05-01 16:07:38 +0200141" User Code Section with final code block
142syn region lexFinalCodeBlock matchgroup=Todo start="^%%" end="\%$" contained contains=@lexCcode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143
Bram Moolenaar7ceefb32020-05-01 16:07:38 +0200144" Lex macros which may appear in C/C++ code blocks
145syn keyword lexCFunctions BEGIN ECHO REJECT yytext YYText yyleng YYLeng yymore yyless yywrap yylook
146syn keyword lexCFunctions yyrestart yyterminate yylineno yycolumno yyin yyout
147syn keyword lexCFunctions input unput output winput wunput woutput
148syn keyword lexCFunctions yyinput yyunput yyoutput yywinput yywunput yywoutput
Bram Moolenaar97d62492012-11-15 21:28:22 +0100149
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150" <c.vim> includes several ALLBUTs; these have to be treated so as to exclude lex* groups
Bram Moolenaar7ceefb32020-05-01 16:07:38 +0200151syn cluster cParenGroup add=lex.*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152syn cluster cDefineGroup add=lex.*
153syn cluster cPreProcGroup add=lex.*
Bram Moolenaar7ceefb32020-05-01 16:07:38 +0200154syn cluster cMultiGroup add=lex.*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155
156" Synchronization
157syn sync clear
Bram Moolenaar97d62492012-11-15 21:28:22 +0100158syn sync minlines=500
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159syn sync match lexSyncPat grouphere lexPatBlock "^%[a-zA-Z]"
160syn sync match lexSyncPat groupthere lexPatBlock "^<$"
Bram Moolenaar7ceefb32020-05-01 16:07:38 +0200161syn sync match lexSyncPat groupthere lexPatBlock "^%%"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162
163" The default highlighting.
Bram Moolenaarf37506f2016-08-31 22:22:10 +0200164if !exists("skip_lex_syntax_inits")
165 hi def link lexAbbrvComment lexPatComment
Bram Moolenaar7ceefb32020-05-01 16:07:38 +0200166 hi def link lexAbbrvPat lexPat
167 hi def link lexAbbrv Special
168 hi def link lexBrace lexPat
169 hi def link lexCFunctions PreProc
170 hi def link lexMorePat Special
171 hi def link lexOptions PreProc
172 hi def link lexOptionsEq Operator
Bram Moolenaarf37506f2016-08-31 22:22:10 +0200173 hi def link lexPatComment Comment
174 hi def link lexPat Function
Bram Moolenaar7ceefb32020-05-01 16:07:38 +0200175 hi def link lexPatString lexPat
176 hi def link lexPatAbbrv Special
177 hi def link lexPatTag Statement
Bram Moolenaarf37506f2016-08-31 22:22:10 +0200178 hi def link lexPatTagZone lexPatTag
179 hi def link lexSep Delimiter
180 hi def link lexSlashQuote lexPat
Bram Moolenaar7ceefb32020-05-01 16:07:38 +0200181 hi def link lexSlashBrace lexPat
182 hi def link lexStartState lexPatTag
183 hi def link lexStartStateCmd Special
Bram Moolenaarf37506f2016-08-31 22:22:10 +0200184endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185
186let b:current_syntax = "lex"
187
Bram Moolenaar7ceefb32020-05-01 16:07:38 +0200188" vim:ts=8