Skip to content

Instantly share code, notes, and snippets.

@Chubek
Last active January 10, 2024 05:40
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"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment