Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- source ../plugin/vimpeg.vim
- let p = Vimpeg({'skip_white': 1})
- " # Hierarchical syntax {{{
- " Grammar <- Spacing Definition+ EndOfFile
- " Definition <- Identifier LEFTARROW Expression
- " Expression <- Sequence (SLASH Sequence)*
- " Sequence <- Prefix*
- " Prefix <- (AND / NOT)? Suffix
- " Suffix <- Primary (QUESTION / STAR / PLUS)?
- " Primary <- Identifier !LEFTARROW / OPEN Expression CLOSE / Literal / Class / DOT
- " # Lexical syntax
- " Identifier <- IdentStart IdentCont* Spacing
- " IdentStart <- [a-zA-Z_]
- " IdentCont <- IdentStart / [0-9]
- " Literal <- [’] (![’] Char)* [’] Spacing / ["] (!["] Char)* ["] Spacing
- " Class <- ’[’ (!’]’ Range)* ’]’ Spacing
- " Range <- Char ’-’ Char / Char
- " Char <- ’\\’ [nrt’"\[\]\\] / ’\\’ [0-2][0-7][0-7] / ’\\’ [0-7][0-7]? / !’\\’ .
- " LEFTARROW <- ’<-’ Spacing
- " SLASH <- ’/’ Spacing
- " AND <- ’&’ Spacing
- " NOT <- ’!’ Spacing
- " QUESTION <- ’?’ Spacing
- " STAR <- ’*’ Spacing
- " PLUS <- ’+’ Spacing
- " OPEN <- ’(’ Spacing
- " CLOSE <- ’)’ Spacing
- " DOT <- ’.’ Spacing
- " Spacing <- (Space / Comment)*
- " Comment <- ’#’ (!EndOfLine .)* EndOfLine
- " Space <- ’ ’ / ’\t’ / EndOfLine
- " EndOfLine <- ’\r\n’ / ’\n’ / ’\r’
- " EndOfFile <- !.
- " }}}
- " <definition> ::= <label> <mallet> <expression>
- " <expression> ::= <sequence> ( <or> <sequence> )*
- " <sequence> ::= <prefix>*
- " <prefix> ::= ( <and> | <not> )? <suffix>
- " <suffix> ::= <primary> ( <question> | <star> | <plus> )?
- " <primary> ::= <label> !<mallet> | <open> <expression> <close> | <regex>
- " <label> ::= <lt> <identifier> <gt>
- " <identifier> ::= <ident_start> <ident_cont>*
- " <ident_cont> ::= '\w'
- " <ident_start> ::= '\h'
- " <regex> ::= <squote> ( '[^']' | <double_squote> )* <squote>
- " <double-squote> ::= "''"
- " <squote> ::= "'"
- " <mallet> ::= '::='
- " <or> ::= '|'
- " <and> ::= '&'
- " <not> ::= '!'
- " <question> ::= '?'
- " <star> ::= '\*'
- " <plus> ::= '+'
- " <close> ::= ')'
- " <open> ::= '('
- " <gt> ::= '>'
- " <lt> ::= '<'
- " <spacing> ::= '\s'
- " Parser grammar {{{
- let expr = p.and(['label', 'mallet', 'expression'],
- \{'id': 'definition', 'on_match': 'Definition'})
- call p.and(['sequence', p.maybe_many(p.and(['or', 'sequence']))],
- \{'id': 'expression', 'on_match': 'Expression'})
- call p.maybe_many('prefix',
- \{'id': 'sequence', 'on_match': 'Sequence'})
- call p.and([p.maybe_one(p.or(['and', 'not'])), 'suffix'],
- \{'id': 'prefix', 'on_match': 'Prefix'})
- call p.and(['primary', p.maybe_one(p.or(['question', 'star', 'plus']))],
- \{'id': 'suffix', 'on_match': 'Suffix'})
- call p.or([p.and(['label', p.not_has('mallet')]), p.and(['open', 'expression', 'close']), 'regex'],
- \{'id': 'primary', 'on_match': 'Primary'})
- call p.and(['lt', 'identifier', 'gt'],
- \{'id': 'label', 'on_match': 'Label'})
- call p.and(['ident_start', p.many('ident_cont')],
- \{'id': 'identifier', 'on_match': 'Identifier'})
- call p.e('\w',
- \{'id': 'ident_cont'})
- call p.e('\h',
- \{'id': 'ident_start'})
- call p.and(['squote', p.maybe_many(p.or([p.e('[^'']'),'double_squote'])), 'squote'],
- \{'id': 'regex', 'on_match': 'Regex'})
- call p.e("''",
- \{'id': 'double_squote', 'on_match': 'Double_squote'})
- call p.e("'",
- \{'id': 'squote', 'on_match': 'Squote'})
- call p.e('::=',
- \{'id': 'mallet', 'on_match': 'Mallet'})
- call p.e('|',
- \{'id': 'or', 'on_match': 'Or'})
- call p.e('&',
- \{'id': 'and', 'on_match': 'And'})
- call p.e('!',
- \{'id': 'not', 'on_match': 'Not'})
- call p.e('?',
- \{'id': 'question', 'on_match': 'Question'})
- call p.e('\*',
- \{'id': 'star', 'on_match': 'Star'})
- call p.e('+',
- \{'id': 'plus', 'on_match': 'Plus'})
- call p.e(')',
- \{'id': 'close', 'on_match': 'Close'})
- call p.e('(',
- \{'id': 'open', 'on_match': 'Open'})
- call p.e('>',
- \{'id': 'gt', 'on_match': 'Gt'})
- call p.e('<',
- \{'id': 'lt', 'on_match': 'Lt'})
- " }}}
- " Callback functions {{{
- function! Definition(elems)
- echom string(a:elems)
- let def = 'call '.string(a:elems[2][0])."{'id':'".a:elems[0][0]."'})"
- echom 'Definition: ' . string(def)
- return def
- endfunction
- function! Expression(elems)
- echom 'Expression: ' . string(a:elems)
- return a:elems
- endfunction
- function! Sequence(elems)
- echom 'Sequence: ' . string(a:elems)
- return a:elems
- endfunction
- function! Prefix(elems)
- echom 'Prefix: ' . string(a:elems)
- return a:elems
- endfunction
- function! Suffix(elems)
- echom 'Suffix: ' . string(a:elems)
- return a:elems
- endfunction
- function! Primary(elems)
- echom 'Primary: ' . string(a:elems)
- return a:elems
- endfunction
- function! Label(elems)
- let res = a:elems[1]
- echom 'Label: ' . string(res)
- return res
- endfunction
- function! Identifier(elems)
- let id = [a:elems[0].join(a:elems[1], '')]
- echom 'Identifier: ' . string(id)
- return id
- endfunction
- function! Regex(elems)
- echom 'Regex: ' . string(a:elems)
- return a:elems
- endfunction
- function! Double_squote(elems)
- echom 'Double_squote: ' . string(a:elems)
- return a:elems
- endfunction
- function! Squote(elems)
- echom 'Squote: ' . string(a:elems)
- return a:elems
- endfunction
- function! Mallet(elems)
- echom 'Mallet: ' . string(a:elems)
- return a:elems
- endfunction
- function! Or(elems)
- echom 'Or: ' . string(a:elems)
- return a:elems
- endfunction
- function! And(elems)
- echom 'And: ' . string(a:elems)
- return a:elems
- endfunction
- function! Not(elems)
- echom 'Not: ' . string(a:elems)
- return a:elems
- endfunction
- function! Question(elems)
- echom 'Question: ' . string(a:elems)
- return a:elems
- endfunction
- function! Star(elems)
- echom 'Star: ' . string(a:elems)
- return a:elems
- endfunction
- function! Plus(elems)
- echom 'Plus: ' . string(a:elems)
- return a:elems
- endfunction
- function! Close(elems)
- echom 'Close: ' . string(a:elems)
- return a:elems
- endfunction
- function! Open(elems)
- echom 'Open: ' . string(a:elems)
- return a:elems
- endfunction
- function! Gt(elems)
- echom 'Gt: ' . string(a:elems)
- return a:elems
- endfunction
- function! Lt(elems)
- echom 'Lt: ' . string(a:elems)
- return a:elems
- endfunction
- " }}}
- function! Test(expr)
- let expr = substitute(a:expr, '^"\s*', '', '')
- echom expr
- return string(g:expr.match(expr).value)
- endfunction
- nore <leader><leader> :w<bar>so %<bar>echo Test(getline('.'))<CR>
- finish
- " <hola> ::= 'mundo'
Advertisement
Add Comment
Please, Sign In to add comment