Skip to content

Instantly share code, notes, and snippets.

@Chubek
Last active September 19, 2024 14:47
Show Gist options
  • Save Chubek/886580036f37bda5d6023595821afa51 to your computer and use it in GitHub Desktop.
Save Chubek/886580036f37bda5d6023595821afa51 to your computer and use it in GitHub Desktop.
VimScript specs for EBNF syntax

The following grammar is based on this EBNF specifications for EBNF meta-grammar:

ebnf            ::= [ global-desc ] { local-desc | rule | comment }

local-desc	::= '{' ? { any-character } ? '}'

global-desc	:: "{{" ? { any-character } ? "}}"

rule            ::= identifier '::=' expression [ '.' ]

expression      ::= term { '|' term }

term            ::= factor { factor }

factor          ::= identifier
                | string-literal
		| character-literal
                | '(' expression ')'
                | '[' expression ']'
                | '{' expression '}'
		| '?' expression '?'
                | option
                | repetition
                | group

option          ::= expression '?'

repetition      ::= expression '*'
                | expression '+'

group           ::= '(' expression ')'

character-literal ::= "'" ? any-character ? "'"

string-literal	::= '"' ? { any-character } ? '"'

identifier	::= letter { letter | '-' }

letter		::= 'A' | 'B' | ... | 'z' 
" ebnf.vim - Syntax highlighting for EBNF
if exists("b:current_syntax")
finish
endif
syntax clear
" Comments
syntax region ebnfComment start= "===" end= "===" contained
syntax region ebnfComment start= "#" end= "#" contained
" Identifiers
syntax match ebnfIdentifier /[a-z]\+\%(-[a-z]\+\)\?/
" Literals
syntax match ebnfCharacterLiteral /'\([^']\|\\.\)+'/
syntax match ebnfStringLiteral /"\([^"]\|\\.\)+"/
" Operators and special characters
syntax match ebnfOp "::="
syntax match ebnfOp "|"
syntax match ebnfOp "?"
syntax match ebnfOp "\["
syntax match ebnfOp "]"
syntax match ebnfOp "("
syntax match ebnfOp ")"
syntax match ebnfOp "{"
syntax match ebnfOp "}"
" Link groups to colors
hi link ebnfComment Comment
hi link ebnfIdentifier Identifier
hi link ebnfCharacterLiteral Constant
hi link ebnfStringLiteral Constant
hi link ebnfOp Operator
let b:current_syntax = "ebnf"
@egberts
Copy link

egberts commented Sep 19, 2024

Put your name on it, maybe a LICENSE too.

this is the best EBNF syntax by far (and I've reviewed nearly all the variants of EBNF syntax highlighters).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment