Raimondi

Untitled

Oct 19th, 2011
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VIM 6.51 KB | None | 0 0
  1. source ../plugin/vimpeg.vim
  2. let p = Vimpeg({'skip_white': 1})
  3.  
  4. " # Hierarchical syntax {{{
  5. " Grammar <- Spacing Definition+ EndOfFile
  6. " Definition <- Identifier LEFTARROW Expression
  7. " Expression <- Sequence (SLASH Sequence)*
  8. " Sequence <- Prefix*
  9. " Prefix <- (AND / NOT)? Suffix
  10. " Suffix <- Primary (QUESTION / STAR / PLUS)?
  11. " Primary <- Identifier !LEFTARROW / OPEN Expression CLOSE / Literal / Class / DOT
  12. " # Lexical syntax
  13. " Identifier <- IdentStart IdentCont* Spacing
  14. " IdentStart <- [a-zA-Z_]
  15. " IdentCont <- IdentStart / [0-9]
  16. " Literal <- [’] (![’] Char)* [’] Spacing / ["] (!["] Char)* ["] Spacing
  17. " Class <- ’[’ (!’]’ Range)* ’]’ Spacing
  18. " Range <- Char ’-’ Char / Char
  19. " Char <- ’\\’ [nrt’"\[\]\\] / ’\\’ [0-2][0-7][0-7] / ’\\’ [0-7][0-7]?  / !’\\’ .
  20. " LEFTARROW <- ’<-’ Spacing
  21. " SLASH <- ’/’ Spacing
  22. " AND <- ’&’ Spacing
  23. " NOT <- ’!’ Spacing
  24. " QUESTION <- ’?’ Spacing
  25. " STAR <- ’*’ Spacing
  26. " PLUS <- ’+’ Spacing
  27. " OPEN <- ’(’ Spacing
  28. " CLOSE <- ’)’ Spacing
  29. " DOT <- ’.’ Spacing
  30. " Spacing <- (Space / Comment)*
  31. " Comment <- ’#’ (!EndOfLine .)* EndOfLine
  32. " Space <- ’ ’ / ’\t’ / EndOfLine
  33. " EndOfLine <- ’\r\n’ / ’\n’ / ’\r’
  34. " EndOfFile <- !.
  35. " }}}
  36.  
  37. " <definition>    ::= <label> <mallet> <expression>
  38. " <expression>    ::= <sequence> ( <or> <sequence> )*
  39. " <sequence>      ::= <prefix>*
  40. " <prefix>        ::= ( <and> | <not> )? <suffix>
  41. " <suffix>        ::= <primary> ( <question> | <star> | <plus> )?
  42. " <primary>       ::= <label> !<mallet> | <open> <expression> <close> | <regex>
  43. " <label>         ::= <lt> <identifier> <gt>
  44. " <identifier>    ::= <ident_start> <ident_cont>*
  45. " <ident_cont>    ::= '\w'
  46. " <ident_start>   ::= '\h'
  47. " <regex>         ::= <squote> ( '[^']' | <double_squote> )* <squote>
  48. " <double-squote> ::= "''"
  49. " <squote>        ::= "'"
  50. " <mallet>        ::= '::='
  51. " <or>            ::= '|'
  52. " <and>           ::= '&'
  53. " <not>           ::= '!'
  54. " <question>      ::= '?'
  55. " <star>          ::= '\*'
  56. " <plus>          ::= '+'
  57. " <close>         ::= ')'
  58. " <open>          ::= '('
  59. " <gt>            ::= '>'
  60. " <lt>            ::= '<'
  61. " <spacing>       ::= '\s'
  62.  
  63. " Parser grammar {{{
  64. let expr = p.and(['label', 'mallet', 'expression'],
  65.      \{'id': 'definition', 'on_match': 'Definition'})
  66. call p.and(['sequence', p.maybe_many(p.and(['or', 'sequence']))],
  67.      \{'id': 'expression', 'on_match': 'Expression'})
  68. call p.maybe_many('prefix',
  69.      \{'id': 'sequence', 'on_match': 'Sequence'})
  70. call p.and([p.maybe_one(p.or(['and', 'not'])), 'suffix'],
  71.      \{'id': 'prefix', 'on_match': 'Prefix'})
  72. call p.and(['primary', p.maybe_one(p.or(['question', 'star', 'plus']))],
  73.      \{'id': 'suffix', 'on_match': 'Suffix'})
  74. call p.or([p.and(['label', p.not_has('mallet')]), p.and(['open', 'expression', 'close']), 'regex'],
  75.      \{'id': 'primary', 'on_match': 'Primary'})
  76. call p.and(['lt', 'identifier', 'gt'],
  77.      \{'id': 'label', 'on_match': 'Label'})
  78. call p.and(['ident_start', p.many('ident_cont')],
  79.      \{'id': 'identifier', 'on_match': 'Identifier'})
  80. call p.e('\w',
  81.      \{'id': 'ident_cont'})
  82. call p.e('\h',
  83.      \{'id': 'ident_start'})
  84. call p.and(['squote', p.maybe_many(p.or([p.e('[^'']'),'double_squote'])), 'squote'],
  85.      \{'id': 'regex', 'on_match': 'Regex'})
  86. call p.e("''",
  87.      \{'id': 'double_squote', 'on_match': 'Double_squote'})
  88. call p.e("'",
  89.       \{'id': 'squote', 'on_match': 'Squote'})
  90. call p.e('::=',
  91.       \{'id': 'mallet', 'on_match': 'Mallet'})
  92. call p.e('|',
  93.       \{'id': 'or', 'on_match': 'Or'})
  94. call p.e('&',
  95.       \{'id': 'and', 'on_match': 'And'})
  96. call p.e('!',
  97.       \{'id': 'not', 'on_match': 'Not'})
  98. call p.e('?',
  99.       \{'id': 'question', 'on_match': 'Question'})
  100. call p.e('\*',
  101.       \{'id': 'star', 'on_match': 'Star'})
  102. call p.e('+',
  103.       \{'id': 'plus', 'on_match': 'Plus'})
  104. call p.e(')',
  105.       \{'id': 'close', 'on_match': 'Close'})
  106. call p.e('(',
  107.       \{'id': 'open', 'on_match': 'Open'})
  108. call p.e('>',
  109.       \{'id': 'gt', 'on_match': 'Gt'})
  110. call p.e('<',
  111.       \{'id': 'lt', 'on_match': 'Lt'})
  112. " }}}
  113.  
  114. " Callback functions {{{
  115. function! Definition(elems)
  116.   echom string(a:elems)
  117.   let def = 'call '.string(a:elems[2][0])."{'id':'".a:elems[0][0]."'})"
  118.   echom 'Definition: ' . string(def)
  119.   return def
  120. endfunction
  121. function! Expression(elems)
  122.   echom 'Expression: ' . string(a:elems)
  123.   return a:elems
  124. endfunction
  125. function! Sequence(elems)
  126.   echom 'Sequence: ' . string(a:elems)
  127.   return a:elems
  128. endfunction
  129. function! Prefix(elems)
  130.   echom 'Prefix: ' . string(a:elems)
  131.   return a:elems
  132. endfunction
  133. function! Suffix(elems)
  134.   echom 'Suffix: ' . string(a:elems)
  135.   return a:elems
  136. endfunction
  137. function! Primary(elems)
  138.   echom 'Primary: ' . string(a:elems)
  139.   return a:elems
  140. endfunction
  141. function! Label(elems)
  142.   let res = a:elems[1]
  143.   echom 'Label: ' . string(res)
  144.   return res
  145. endfunction
  146. function! Identifier(elems)
  147.   let id = [a:elems[0].join(a:elems[1], '')]
  148.   echom 'Identifier: ' . string(id)
  149.   return id
  150. endfunction
  151. function! Regex(elems)
  152.   echom 'Regex: ' . string(a:elems)
  153.   return a:elems
  154. endfunction
  155. function! Double_squote(elems)
  156.   echom 'Double_squote: ' . string(a:elems)
  157.   return a:elems
  158. endfunction
  159. function! Squote(elems)
  160.   echom 'Squote: ' . string(a:elems)
  161.   return a:elems
  162. endfunction
  163. function! Mallet(elems)
  164.   echom 'Mallet: ' . string(a:elems)
  165.   return a:elems
  166. endfunction
  167. function! Or(elems)
  168.   echom 'Or: ' . string(a:elems)
  169.   return a:elems
  170. endfunction
  171. function! And(elems)
  172.   echom 'And: ' . string(a:elems)
  173.   return a:elems
  174. endfunction
  175. function! Not(elems)
  176.   echom 'Not: ' . string(a:elems)
  177.   return a:elems
  178. endfunction
  179. function! Question(elems)
  180.   echom 'Question: ' . string(a:elems)
  181.   return a:elems
  182. endfunction
  183. function! Star(elems)
  184.   echom 'Star: ' . string(a:elems)
  185.   return a:elems
  186. endfunction
  187. function! Plus(elems)
  188.   echom 'Plus: ' . string(a:elems)
  189.   return a:elems
  190. endfunction
  191. function! Close(elems)
  192.   echom 'Close: ' . string(a:elems)
  193.   return a:elems
  194. endfunction
  195. function! Open(elems)
  196.   echom 'Open: ' . string(a:elems)
  197.   return a:elems
  198. endfunction
  199. function! Gt(elems)
  200.   echom 'Gt: ' . string(a:elems)
  201.   return a:elems
  202. endfunction
  203. function! Lt(elems)
  204.   echom 'Lt: ' . string(a:elems)
  205.   return a:elems
  206. endfunction
  207. " }}}
  208.  
  209. function! Test(expr)
  210.   let expr = substitute(a:expr, '^"\s*', '', '')
  211.   echom expr
  212.   return string(g:expr.match(expr).value)
  213. endfunction
  214.  
  215. nore <leader><leader> :w<bar>so %<bar>echo Test(getline('.'))<CR>
  216. finish
  217.  
  218. " <hola> ::= 'mundo'
  219.  
  220.  
Advertisement
Add Comment
Please, Sign In to add comment