Skip to content

Instantly share code, notes, and snippets.

@Chubek
Last active September 19, 2024 14:47
Show Gist options
  • Select an option

  • Save Chubek/886580036f37bda5d6023595821afa51 to your computer and use it in GitHub Desktop.

Select an option

Save Chubek/886580036f37bda5d6023595821afa51 to your computer and use it in GitHub Desktop.

Revisions

  1. Chubek revised this gist Jan 10, 2024. 1 changed file with 37 additions and 23 deletions.
    60 changes: 37 additions & 23 deletions ebnf.vim
    Original file line number Diff line number Diff line change
    @@ -1,24 +1,38 @@
    " Vim syntax file for EBNF

    " Define syntax groups
    syntax region ebnfLocalDesc start=/{/ end=/}/ contains=@ebnf
    syntax region ebnfGlobalDesc start= '{{' end= '}}' contains=@ebnf
    syntax keyword ebnfKeyword ::= "|" "." "?" "*" "+"
    syntax match ebnfSpecial /[\?\*\+\|\.]/ contained
    syntax keyword ebnfBoolean true false
    syntax match ebnfIdentifier /\v[A-Za-z][\w-]*/
    syntax match ebnfCharacterLiteral /'.'/
    syntax match ebnfStringLiteral /".*"/
    syntax region ebnfGroup start="(" end=")" contains=@ebnf

    " Highlighting
    highlight link ebnfKeyword Keyword
    highlight link ebnfSpecial Special
    highlight link ebnfBoolean Boolean
    highlight link ebnfIdentifier Identifier
    highlight link ebnfCharacterLiteral Character
    highlight link ebnfStringLiteral String
    highlight link ebnfGroup Statement

    " Enable syntax highlighting
    " 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"
  2. Chubek created this gist Jan 8, 2024.
    41 changes: 41 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    The following grammar is based on this EBNF specifications for EBNF meta-grammar:

    ```ebnf
    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'
    ```
    24 changes: 24 additions & 0 deletions ebnf.vim
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    " Vim syntax file for EBNF

    " Define syntax groups
    syntax region ebnfLocalDesc start=/{/ end=/}/ contains=@ebnf
    syntax region ebnfGlobalDesc start= '{{' end= '}}' contains=@ebnf
    syntax keyword ebnfKeyword ::= "|" "." "?" "*" "+"
    syntax match ebnfSpecial /[\?\*\+\|\.]/ contained
    syntax keyword ebnfBoolean true false
    syntax match ebnfIdentifier /\v[A-Za-z][\w-]*/
    syntax match ebnfCharacterLiteral /'.'/
    syntax match ebnfStringLiteral /".*"/
    syntax region ebnfGroup start="(" end=")" contains=@ebnf

    " Highlighting
    highlight link ebnfKeyword Keyword
    highlight link ebnfSpecial Special
    highlight link ebnfBoolean Boolean
    highlight link ebnfIdentifier Identifier
    highlight link ebnfCharacterLiteral Character
    highlight link ebnfStringLiteral String
    highlight link ebnfGroup Statement

    " Enable syntax highlighting
    let b:current_syntax = "ebnf"