Skip to content

Instantly share code, notes, and snippets.

@egberts
Forked from Chubek/README.md
Created September 19, 2024 14:47
Show Gist options
  • Save egberts/7eb34e97feaa6e9d644b525e7ad32a58 to your computer and use it in GitHub Desktop.
Save egberts/7eb34e97feaa6e9d644b525e7ad32a58 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