Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1 | " Vim syntax file |
Bram Moolenaar | 7ceefb3 | 2020-05-01 16:07:38 +0200 | [diff] [blame] | 2 | " Language: Lex and Flex |
Bram Moolenaar | 1d9215b | 2020-01-25 13:27:42 +0100 | [diff] [blame] | 3 | " Maintainer: Charles E. Campbell <NcampObell@SdrPchip.AorgM-NOSPAM> |
Bram Moolenaar | 7ceefb3 | 2020-05-01 16:07:38 +0200 | [diff] [blame] | 4 | " Contributor: Robert A. van Engelen <engelen@acm.org> |
| 5 | " Last Change: Apr 24, 2020 |
| 6 | " Version: 18 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7 | |
Bram Moolenaar | 89bcfda | 2016-08-30 23:26:57 +0200 | [diff] [blame] | 8 | " quit when a syntax file was already loaded |
| 9 | if exists("b:current_syntax") |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10 | finish |
| 11 | endif |
| 12 | |
Bram Moolenaar | 7ceefb3 | 2020-05-01 16:07:38 +0200 | [diff] [blame] | 13 | " Read the C++ syntax to start with |
| 14 | let s:Cpath= fnameescape(expand("<sfile>:p:h")."/cpp.vim") |
Bram Moolenaar | 97d6249 | 2012-11-15 21:28:22 +0100 | [diff] [blame] | 15 | if !filereadable(s:Cpath) |
Bram Moolenaar | 7ceefb3 | 2020-05-01 16:07:38 +0200 | [diff] [blame] | 16 | for s:Cpath in split(globpath(&rtp,"syntax/cpp.vim"),"\n") |
Bram Moolenaar | 97d6249 | 2012-11-15 21:28:22 +0100 | [diff] [blame] | 17 | if filereadable(fnameescape(s:Cpath)) |
| 18 | let s:Cpath= fnameescape(s:Cpath) |
| 19 | break |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 20 | endif |
Bram Moolenaar | 97d6249 | 2012-11-15 21:28:22 +0100 | [diff] [blame] | 21 | endfor |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 22 | endif |
Bram Moolenaar | 97d6249 | 2012-11-15 21:28:22 +0100 | [diff] [blame] | 23 | exe "syn include @lexCcode ".s:Cpath |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 24 | |
Bram Moolenaar | d4755bb | 2004-09-02 19:12:26 +0000 | [diff] [blame] | 25 | " --- ========= --- |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 26 | " --- Lex stuff --- |
Bram Moolenaar | d4755bb | 2004-09-02 19:12:26 +0000 | [diff] [blame] | 27 | " --- ========= --- |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 28 | |
Bram Moolenaar | 7ceefb3 | 2020-05-01 16:07:38 +0200 | [diff] [blame] | 29 | " 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 Moolenaar | 81af925 | 2010-12-10 20:35:50 +0100 | [diff] [blame] | 77 | |
Bram Moolenaar | 7ceefb3 | 2020-05-01 16:07:38 +0200 | [diff] [blame] | 78 | " Definitions Section with initial code blocks, abbreviations, options, states |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 79 | if has("folding") |
Bram Moolenaar | 7ceefb3 | 2020-05-01 16:07:38 +0200 | [diff] [blame] | 80 | syn region lexAbbrvBlock fold start="^\S" end="^\ze%%" skipnl nextgroup=lexPatBlock contains=lexOptions,lexAbbrv,lexInitialCodeBlock,lexInclude,lexAbbrvComment,lexStartState |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 81 | else |
Bram Moolenaar | 7ceefb3 | 2020-05-01 16:07:38 +0200 | [diff] [blame] | 82 | syn region lexAbbrvBlock start="^\S" end="^\ze%%" skipnl nextgroup=lexPatBlock contains=lexOptions,lexAbbrv,lexInitialCodeBlock,lexInclude,lexAbbrvComment,lexStartState |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 83 | endif |
Bram Moolenaar | 7ceefb3 | 2020-05-01 16:07:38 +0200 | [diff] [blame] | 84 | syn match lexOptions "^%\a\+\(\s.*\|[^{]*\)$" contains=lexOptionsEq,lexPatString,lexSlashQuote,lexBrace,lexSlashBrace |
| 85 | syn match lexOptionsEq "=" skipwhite contained |
| 86 | syn match lexAbbrv "^\I\i*\s"me=e-1 skipwhite contained nextgroup=lexAbbrvPat |
| 87 | syn match lexAbbrvPat "\s\S.*$"lc=1 contained contains=lexPatAbbrv,lexPatString,lexSlashQuote,lexBrace,lexSlashBrace nextgroup=lexAbbrv,lexInclude |
| 88 | syn match lexStartState "^%\(xs\?\|s\)\(t\(a\(t\(e\?\)\?\)\?\)\?\)\?\(\s\+\I\i*\)\+\s*$" contained contains=lexStartStateCmd |
| 89 | syn match lexStartStateCmd '^%\(xs\?\|s\)\(t\(a\(t\(e\?\)\?\)\?\)\?\)\?' contained |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 90 | if has("folding") |
Bram Moolenaar | 7ceefb3 | 2020-05-01 16:07:38 +0200 | [diff] [blame] | 91 | 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 Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 95 | else |
Bram Moolenaar | 7ceefb3 | 2020-05-01 16:07:38 +0200 | [diff] [blame] | 96 | 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 Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 100 | endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 101 | |
Bram Moolenaar | 7ceefb3 | 2020-05-01 16:07:38 +0200 | [diff] [blame] | 102 | " Rules Section with patterns and actions |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 103 | if has("folding") |
Bram Moolenaar | 7ceefb3 | 2020-05-01 16:07:38 +0200 | [diff] [blame] | 104 | 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 Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 109 | else |
Bram Moolenaar | 7ceefb3 | 2020-05-01 16:07:38 +0200 | [diff] [blame] | 110 | 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 Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 115 | endif |
Bram Moolenaar | 7ceefb3 | 2020-05-01 16:07:38 +0200 | [diff] [blame] | 116 | syn match lexPatAbbrv "{\I\i*}"hs=s+1,he=e-1 contained |
| 117 | syn match lexPatTag "^<\^\?\(\I\i*\|\*\)\(,\^\?\(\I\i*\|\*\)\)*>" contained nextgroup=lexPat,lexMorePat,lexPatSep,lexPatEnd |
| 118 | syn match lexPatTagZone "^<\^\?\(\I\i*\|\*\)\(,\^\?\(\I\i*\|\*\)\)*>\s*{$"me=e-1 contained nextgroup=lexPatTagZoneStart |
Bram Moolenaar | 97d6249 | 2012-11-15 21:28:22 +0100 | [diff] [blame] | 119 | |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 120 | if has("folding") |
Bram Moolenaar | 7ceefb3 | 2020-05-01 16:07:38 +0200 | [diff] [blame] | 121 | 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 Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 124 | else |
Bram Moolenaar | 7ceefb3 | 2020-05-01 16:07:38 +0200 | [diff] [blame] | 125 | syn region lexPatTagZoneStart matchgroup=lexPatTag start='{' end='^}' skipnl skipwhitecontained contains=lexPatTag,lexPatTagZone,lexPatComment,lexPat,lexPatSep,lexPatInclude |
| 126 | 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 Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 128 | endif |
Bram Moolenaar | 7ceefb3 | 2020-05-01 16:07:38 +0200 | [diff] [blame] | 129 | syn match lexPatEnd "\s*$" skipnl contained |
| 130 | syn match lexPatCodeLine "[^{\[].*" contained contains=@lexCcode,lexCFunctions |
| 131 | syn match lexMorePat "\s*|\s*$" skipnl contained nextgroup=lexPat,lexPatTag,lexPatComment |
| 132 | syn match lexPatSep "\s\+" contained nextgroup=lexMorePat,lexPatCode,lexPatCodeLine |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 133 | syn match lexSlashQuote +\(\\\\\)*\\"+ contained |
Bram Moolenaar | 7ceefb3 | 2020-05-01 16:07:38 +0200 | [diff] [blame] | 134 | syn match lexSlashBrace +\(\\\\\)*\\\[+ contained |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 135 | if has("folding") |
Bram Moolenaar | 7ceefb3 | 2020-05-01 16:07:38 +0200 | [diff] [blame] | 136 | syn region lexPatCode fold matchgroup=Delimiter start="{" end="}" skipnl contained contains=@lexCcode,lexCFunctions |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 137 | else |
Bram Moolenaar | 7ceefb3 | 2020-05-01 16:07:38 +0200 | [diff] [blame] | 138 | syn region lexPatCode matchgroup=Delimiter start="{" end="}" skipnl contained contains=@lexCcode,lexCFunctions |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 139 | endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 140 | |
Bram Moolenaar | 7ceefb3 | 2020-05-01 16:07:38 +0200 | [diff] [blame] | 141 | " User Code Section with final code block |
| 142 | syn region lexFinalCodeBlock matchgroup=Todo start="^%%" end="\%$" contained contains=@lexCcode |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 143 | |
Bram Moolenaar | 7ceefb3 | 2020-05-01 16:07:38 +0200 | [diff] [blame] | 144 | " Lex macros which may appear in C/C++ code blocks |
| 145 | syn keyword lexCFunctions BEGIN ECHO REJECT yytext YYText yyleng YYLeng yymore yyless yywrap yylook |
| 146 | syn keyword lexCFunctions yyrestart yyterminate yylineno yycolumno yyin yyout |
| 147 | syn keyword lexCFunctions input unput output winput wunput woutput |
| 148 | syn keyword lexCFunctions yyinput yyunput yyoutput yywinput yywunput yywoutput |
Bram Moolenaar | 97d6249 | 2012-11-15 21:28:22 +0100 | [diff] [blame] | 149 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 150 | " <c.vim> includes several ALLBUTs; these have to be treated so as to exclude lex* groups |
Bram Moolenaar | 7ceefb3 | 2020-05-01 16:07:38 +0200 | [diff] [blame] | 151 | syn cluster cParenGroup add=lex.* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 152 | syn cluster cDefineGroup add=lex.* |
| 153 | syn cluster cPreProcGroup add=lex.* |
Bram Moolenaar | 7ceefb3 | 2020-05-01 16:07:38 +0200 | [diff] [blame] | 154 | syn cluster cMultiGroup add=lex.* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 155 | |
| 156 | " Synchronization |
| 157 | syn sync clear |
Bram Moolenaar | 97d6249 | 2012-11-15 21:28:22 +0100 | [diff] [blame] | 158 | syn sync minlines=500 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 159 | syn sync match lexSyncPat grouphere lexPatBlock "^%[a-zA-Z]" |
| 160 | syn sync match lexSyncPat groupthere lexPatBlock "^<$" |
Bram Moolenaar | 7ceefb3 | 2020-05-01 16:07:38 +0200 | [diff] [blame] | 161 | syn sync match lexSyncPat groupthere lexPatBlock "^%%" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 162 | |
| 163 | " The default highlighting. |
Bram Moolenaar | f37506f | 2016-08-31 22:22:10 +0200 | [diff] [blame] | 164 | if !exists("skip_lex_syntax_inits") |
| 165 | hi def link lexAbbrvComment lexPatComment |
Bram Moolenaar | 7ceefb3 | 2020-05-01 16:07:38 +0200 | [diff] [blame] | 166 | 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 Moolenaar | f37506f | 2016-08-31 22:22:10 +0200 | [diff] [blame] | 173 | hi def link lexPatComment Comment |
| 174 | hi def link lexPat Function |
Bram Moolenaar | 7ceefb3 | 2020-05-01 16:07:38 +0200 | [diff] [blame] | 175 | hi def link lexPatString lexPat |
| 176 | hi def link lexPatAbbrv Special |
| 177 | hi def link lexPatTag Statement |
Bram Moolenaar | f37506f | 2016-08-31 22:22:10 +0200 | [diff] [blame] | 178 | hi def link lexPatTagZone lexPatTag |
| 179 | hi def link lexSep Delimiter |
| 180 | hi def link lexSlashQuote lexPat |
Bram Moolenaar | 7ceefb3 | 2020-05-01 16:07:38 +0200 | [diff] [blame] | 181 | hi def link lexSlashBrace lexPat |
| 182 | hi def link lexStartState lexPatTag |
| 183 | hi def link lexStartStateCmd Special |
Bram Moolenaar | f37506f | 2016-08-31 22:22:10 +0200 | [diff] [blame] | 184 | endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 185 | |
| 186 | let b:current_syntax = "lex" |
| 187 | |
Bram Moolenaar | 7ceefb3 | 2020-05-01 16:07:38 +0200 | [diff] [blame] | 188 | " vim:ts=8 |