lex.vim 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. " Vim syntax file
  2. " Language: Lex and Flex
  3. " Maintainer: Charles E. Campbell <NcampObell@SdrPchip.AorgM-NOSPAM>
  4. " Contributor: Robert A. van Engelen <engelen@acm.org>
  5. " Last Change: Apr 24, 2020
  6. " Version: 18
  7. " quit when a syntax file was already loaded
  8. if exists("b:current_syntax")
  9. finish
  10. endif
  11. " Read the C++ syntax to start with
  12. let s:Cpath= fnameescape(expand("<sfile>:p:h")."/cpp.vim")
  13. if !filereadable(s:Cpath)
  14. for s:Cpath in split(globpath(&rtp,"syntax/cpp.vim"),"\n")
  15. if filereadable(fnameescape(s:Cpath))
  16. let s:Cpath= fnameescape(s:Cpath)
  17. break
  18. endif
  19. endfor
  20. endif
  21. exe "syn include @lexCcode ".s:Cpath
  22. " --- ========= ---
  23. " --- Lex stuff ---
  24. " --- ========= ---
  25. " Definitions
  26. " %%
  27. " Rules
  28. " %%
  29. " User Code
  30. "
  31. " --- ======= ---
  32. " --- Example ---
  33. " --- ======= ---
  34. "
  35. " // this is a valid lex file
  36. " // indented initial code block
  37. " #include <stdlib.h>
  38. " %{
  39. " // initial code block
  40. " #include <stdio.h>
  41. " const char *sep = "";
  42. " %}
  43. " %option outfile="scanner.c" noyywrap nodefault
  44. " %x COMMENT
  45. " id [A-Za-z_][A-Za-z0-9_]*
  46. " %%
  47. " // indented initial action code block
  48. " printf("BEGIN");
  49. " {id} printf("%s%s", sep, yytext); sep = "";
  50. " . |
  51. " \n { sep = "\n"; }
  52. " "/*" { BEGIN COMMENT; }
  53. " "//".* { }
  54. " <COMMENT>{
  55. " "*/" { BEGIN INITIAL; }
  56. " .|\n
  57. " }
  58. " <*><<EOF>> { // end of file
  59. " printf("\nEND\n");
  60. " yyterminate();
  61. " }
  62. " %%
  63. " void scan()
  64. " {
  65. " while (yylex())
  66. " continue;
  67. " }
  68. " /* main program */
  69. " int main()
  70. " {
  71. " scan();
  72. " }
  73. " Definitions Section with initial code blocks, abbreviations, options, states
  74. if has("folding")
  75. syn region lexAbbrvBlock fold start="^\S" end="^\ze%%" skipnl nextgroup=lexPatBlock contains=lexOptions,lexAbbrv,lexInitialCodeBlock,lexInclude,lexAbbrvComment,lexStartState
  76. else
  77. syn region lexAbbrvBlock start="^\S" end="^\ze%%" skipnl nextgroup=lexPatBlock contains=lexOptions,lexAbbrv,lexInitialCodeBlock,lexInclude,lexAbbrvComment,lexStartState
  78. endif
  79. syn match lexOptions "^%\a\+\(\s.*\|[^{]*\)$" contains=lexOptionsEq,lexPatString,lexSlashQuote,lexBrace,lexSlashBrace
  80. syn match lexOptionsEq "=" skipwhite contained
  81. syn match lexAbbrv "^\I\i*\s"me=e-1 skipwhite contained nextgroup=lexAbbrvPat
  82. syn match lexAbbrvPat "\s\S.*$"lc=1 contained contains=lexPatAbbrv,lexPatString,lexSlashQuote,lexBrace,lexSlashBrace nextgroup=lexAbbrv,lexInclude
  83. syn match lexStartState "^%\(xs\?\|s\)\(t\(a\(t\(e\?\)\?\)\?\)\?\)\?\(\s\+\I\i*\)\+\s*$" contained contains=lexStartStateCmd
  84. syn match lexStartStateCmd '^%\(xs\?\|s\)\(t\(a\(t\(e\?\)\?\)\?\)\?\)\?' contained
  85. if has("folding")
  86. syn region lexInitialCodeBlock fold start="^\s\+" end="^\S"me=e-1 contains=@lexCcode
  87. syn region lexInclude fold matchgroup=lexSep start="^%\a*{" end="^%\?}" contained contains=@lexCcode,lexCFunctions
  88. syn region lexAbbrvComment fold start="^\s*//" end="$" contains=@Spell
  89. syn region lexAbbrvComment fold start="^\s*/\*" end="\*/" contains=@Spell
  90. else
  91. syn region lexInitialCodeBlock start="^\s\+" end="^\S"me=e-1 contains=@lexCcode
  92. syn region lexInclude matchgroup=lexSep start="^%\a*{" end="^%\?}" contained contains=@lexCcode,lexCFunctions
  93. syn region lexAbbrvComment start="^\s*//" end="$" contains=@Spell
  94. syn region lexAbbrvComment start="^\s*/\*" end="\*/" contains=@Spell
  95. endif
  96. " Rules Section with patterns and actions
  97. if has("folding")
  98. syn region lexPatBlock fold matchgroup=Todo start="^%%" matchgroup=Todo end="^\ze%%" skipnl skipwhite nextgroup=lexFinalCodeBlock contains=lexPatTag,lexPatTagZone,lexPatComment,lexPat,lexPatSep,lexPatInclude
  99. 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
  100. syn region lexPatInclude fold matchgroup=lexSep start="^%{" end="^%}" contained contains=@lexCcode
  101. syn region lexBrace fold matchgroup=Character start="\[" skip="\\.\|\[:\a\+:\]\|\[\.\a\+\.\]\|\[=.=\]" end="\]" contained
  102. syn region lexPatString fold matchgroup=String start=+"+ skip=+\\\\\|\\"+ matchgroup=String end=+"+ contained
  103. else
  104. syn region lexPatBlock matchgroup=Todo start="^%%" matchgroup=Todo end="^\ze%%" skipnl skipwhite nextgroup=lexFinalCodeBlock contains=lexPatTag,lexPatTagZone,lexPatComment,lexPat,lexPatSep,lexPatInclude
  105. 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
  106. syn region lexPatInclude matchgroup=lexSep start="^%{" end="^%}" contained contains=@lexCcode
  107. syn region lexBrace matchgroup=Character start="\[" skip="\\.\|\[:\a\+:\]\|\[\.\a\+\.\]\|\[=.=\]" end="\]" contained
  108. syn region lexPatString matchgroup=String start=+"+ skip=+\\\\\|\\"+ matchgroup=String end=+"+ contained
  109. endif
  110. syn match lexPatAbbrv "{\I\i*}"hs=s+1,he=e-1 contained
  111. syn match lexPatTag "^<\^\?\(\I\i*\|\*\)\(,\^\?\(\I\i*\|\*\)\)*>" contained nextgroup=lexPat,lexMorePat,lexPatSep,lexPatEnd
  112. syn match lexPatTagZone "^<\^\?\(\I\i*\|\*\)\(,\^\?\(\I\i*\|\*\)\)*>\s*{$"me=e-1 contained nextgroup=lexPatTagZoneStart
  113. if has("folding")
  114. syn region lexPatTagZoneStart fold matchgroup=lexPatTag start='{$' end='^}' skipnl skipwhite contained contains=lexPatTag,lexPatTagZone,lexPatComment,lexPat,lexPatSep,lexPatInclude
  115. syn region lexPatComment fold start="//" end="$" skipnl contained contains=cTodo skipwhite nextgroup=lexPatComment,lexPat,@Spell
  116. syn region lexPatComment fold start="/\*" end="\*/" skipnl contained contains=cTodo skipwhite nextgroup=lexPatComment,lexPat,@Spell
  117. else
  118. syn region lexPatTagZoneStart matchgroup=lexPatTag start='{' end='^}' skipnl skipwhite contained contains=lexPatTag,lexPatTagZone,lexPatComment,lexPat,lexPatSep,lexPatInclude
  119. syn region lexPatComment start="//" end="$" skipnl contained contains=cTodo skipwhite nextgroup=lexPatComment,lexPat,@Spell
  120. syn region lexPatComment start="/\*" end="\*/" skipnl contained contains=cTodo skipwhite nextgroup=lexPatComment,lexPat,@Spell
  121. endif
  122. syn match lexPatEnd "\s*$" skipnl contained
  123. syn match lexPatCodeLine "[^{\[].*" contained contains=@lexCcode,lexCFunctions
  124. syn match lexMorePat "\s*|\s*$" skipnl contained nextgroup=lexPat,lexPatTag,lexPatComment
  125. syn match lexPatSep "\s\+" contained nextgroup=lexMorePat,lexPatCode,lexPatCodeLine
  126. syn match lexSlashQuote +\(\\\\\)*\\"+ contained
  127. syn match lexSlashBrace +\(\\\\\)*\\\[+ contained
  128. if has("folding")
  129. syn region lexPatCode fold matchgroup=Delimiter start="{" end="}" skipnl contained contains=@lexCcode,lexCFunctions
  130. else
  131. syn region lexPatCode matchgroup=Delimiter start="{" end="}" skipnl contained contains=@lexCcode,lexCFunctions
  132. endif
  133. " User Code Section with final code block
  134. syn region lexFinalCodeBlock matchgroup=Todo start="^%%" end="\%$" contained contains=@lexCcode
  135. " Lex macros which may appear in C/C++ code blocks
  136. syn keyword lexCFunctions BEGIN ECHO REJECT yytext YYText yyleng YYLeng yymore yyless yywrap yylook
  137. syn keyword lexCFunctions yyrestart yyterminate yylineno yycolumno yyin yyout
  138. syn keyword lexCFunctions input unput output winput wunput woutput
  139. syn keyword lexCFunctions yyinput yyunput yyoutput yywinput yywunput yywoutput
  140. " <c.vim> includes several ALLBUTs; these have to be treated so as to exclude lex* groups
  141. syn cluster cParenGroup add=lex.*
  142. syn cluster cDefineGroup add=lex.*
  143. syn cluster cPreProcGroup add=lex.*
  144. syn cluster cMultiGroup add=lex.*
  145. " Synchronization
  146. syn sync clear
  147. syn sync minlines=500
  148. syn sync match lexSyncPat grouphere lexPatBlock "^%[a-zA-Z]"
  149. syn sync match lexSyncPat groupthere lexPatBlock "^<$"
  150. syn sync match lexSyncPat groupthere lexPatBlock "^%%"
  151. " The default highlighting.
  152. if !exists("skip_lex_syntax_inits")
  153. hi def link lexAbbrvComment lexPatComment
  154. hi def link lexAbbrvPat lexPat
  155. hi def link lexAbbrv Special
  156. hi def link lexBrace lexPat
  157. hi def link lexCFunctions PreProc
  158. hi def link lexMorePat Special
  159. hi def link lexOptions PreProc
  160. hi def link lexOptionsEq Operator
  161. hi def link lexPatComment Comment
  162. hi def link lexPat Function
  163. hi def link lexPatString lexPat
  164. hi def link lexPatAbbrv Special
  165. hi def link lexPatTag Statement
  166. hi def link lexPatTagZone lexPatTag
  167. hi def link lexSep Delimiter
  168. hi def link lexSlashQuote lexPat
  169. hi def link lexSlashBrace lexPat
  170. hi def link lexStartState lexPatTag
  171. hi def link lexStartStateCmd Special
  172. endif
  173. let b:current_syntax = "lex"
  174. " vim:ts=8