Raimondi

Untitled

Oct 20th, 2011
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VIM 10.56 KB | None | 0 0
  1. source ../plugin/vimpeg.vim
  2. let p = Vimpeg({'skip_white': 1})
  3. " Ideas {{{
  4. " - For later, maybe create callback functions populated with vars
  5. "   corresponding to each element? Check Definition(). Not sure if all vars
  6. "   would be used, though.
  7. " - Add support for double quoted strings.
  8. " - Should VimPEG's function have the abort flag?
  9. " }}}
  10.  
  11. " Original PEG syntax {{{
  12. " # Hierarchical syntax
  13. " Grammar <- Spacing Definition+ EndOfFile
  14. " Definition <- Identifier LEFTARROW Expression
  15. " Expression <- Sequence (SLASH Sequence)*
  16. " Sequence <- Prefix*
  17. " Prefix <- (AND / NOT)? Suffix
  18. " Suffix <- Primary (QUESTION / STAR / PLUS)?
  19. " Primary <- Identifier !LEFTARROW / OPEN Expression CLOSE / Literal / Class / DOT
  20. " # Lexical syntax
  21. " Identifier <- IdentStart IdentCont* Spacing
  22. " IdentStart <- [a-zA-Z_]
  23. " IdentCont <- IdentStart / [0-9]
  24. " Literal <- [’] (![’] Char)* [’] Spacing / ["] (!["] Char)* ["] Spacing
  25. " Class <- ’[’ (!’]’ Range)* ’]’ Spacing
  26. " Range <- Char ’-’ Char / Char
  27. " Char <- ’\\’ [nrt’"\[\]\\] / ’\\’ [0-2][0-7][0-7] / ’\\’ [0-7][0-7]?  / !’\\’ .
  28. " LEFTARROW <- ’<-’ Spacing
  29. " SLASH <- ’/’ Spacing
  30. " AND <- ’&’ Spacing
  31. " NOT <- ’!’ Spacing
  32. " QUESTION <- ’?’ Spacing
  33. " STAR <- ’*’ Spacing
  34. " PLUS <- ’+’ Spacing
  35. " OPEN <- ’(’ Spacing
  36. " CLOSE <- ’)’ Spacing
  37. " DOT <- ’.’ Spacing
  38. " Spacing <- (Space / Comment)*
  39. " Comment <- ’#’ (!EndOfLine .)* EndOfLine
  40. " Space <- ’ ’ / ’\t’ / EndOfLine
  41. " EndOfLine <- ’\r\n’ / ’\n’ / ’\r’
  42. " EndOfFile <- !.
  43. " }}}
  44.  
  45. " VimPEG syntax {{{
  46. " <definition>       ::= <label> <mallet> <expression> <callback>?
  47. " <expression>       ::= <sequence> ( <or> <sequence> )*
  48. " <sequence>         ::= <prefix>*
  49. " <prefix>           ::= <not>? <suffix>
  50. " <suffix>           ::= <primary> ( <question> | <star> | <plus> )?
  51. " <primary>          ::= <label> !<mallet> | <open> <expression> <close> | <regex>
  52. " <callback>         ::= <right_arrow> <identifier>
  53. " <label>            ::= <lt> <identifier> <gt>
  54. " <identifier>       ::= <ident_start> <ident_cont>*
  55. " <ident_cont>       ::= '\w'
  56. " <ident_start>      ::= '\h'
  57. " <regex>            ::= <dquoted_string> | <squoted_string>
  58. " <dquoted_string>   ::= <dquote> ( <double_backslash> | <escaped_dquote> | '[^"]' )* <dquote>
  59. " <squoted_string>   ::= <squote> ( "[^']" | <double_squote> )* <squote>
  60. " <escaped_dquote>   ::= <backslash> <dquote>
  61. " <double_backslash> ::= <backslash> <backslash>
  62. " <backslash>        ::= '\'
  63. " <dquote>           ::= '"'
  64. " <double_squote>    ::= ''''''
  65. " <squote>           ::= "'"
  66. " <right_arrow>      ::= '->'
  67. " <mallet>           ::= '::='
  68. " <or>               ::= '|'
  69. " <not>              ::= '!'
  70. " <question>         ::= '?'
  71. " <star>             ::= '\*'
  72. " <plus>             ::= '+'
  73. " <close>            ::= ')'
  74. " <open>             ::= '('
  75. " <gt>               ::= '>'
  76. " <lt>               ::= '<'
  77. " }}}
  78.  
  79. " Parser grammar {{{
  80. let expr = p.and(['label', 'mallet', 'expression', p.maybe_one('callback')],
  81.       \{'id': 'definition', 'on_match': 'Definition'})
  82. call p.and(['sequence', p.maybe_many(p.and(['or', 'sequence']))],
  83.       \{'id': 'expression', 'on_match': 'Expression'})
  84. call p.maybe_many('prefix',
  85.       \{'id': 'sequence', 'on_match': 'Sequence'})
  86. call p.and([p.maybe_one('not'), 'suffix'],
  87.       \{'id': 'prefix', 'on_match': 'Prefix'})
  88. call p.and(['primary', p.maybe_one(p.or(['question', 'star', 'plus']))],
  89.       \{'id': 'suffix', 'on_match': 'Suffix'})
  90. call p.or([p.and(['label', p.not_has('mallet')]), p.and(['open', 'expression', 'close']), 'regex'],
  91.       \{'id': 'primary', 'on_match': 'Primary'})
  92. call p.and(['right_arrow', 'identifier'],
  93.       \{'id': 'callback', 'on_match': 'Callback'})
  94. call p.and(['lt', 'identifier', 'gt'],
  95.       \{'id': 'label', 'on_match': 'Label'})
  96. call p.and(['ident_start', p.many('ident_cont')],
  97.       \{'id': 'identifier', 'on_match': 'Identifier'})
  98. call p.e('\w',
  99.       \{'id': 'ident_cont'})
  100. call p.e('\h',
  101.       \{'id': 'ident_start'})
  102. call p.or(['squoted_string', 'dquoted_string'],
  103.       \{'id': 'regex', 'on_match': 'Regex'})
  104. call p.and(['dquote', p.maybe_many(p.or(['double_backslash', 'escaped_dquote', p.e('[^"]')])), 'dquote'],
  105.       \{'id': 'dquoted_string', 'on_match': 'Dquoted_string'})
  106. call p.and(['squote', p.maybe_many(p.or([p.e('[^'']'),'double_squote'])), 'squote'],
  107.       \{'id': 'squoted_string', 'on_match': 'Squoted_string'})
  108. call p.and(['backslash', 'dquote'],
  109.       \{'id': 'escaped_dquote', 'on_match': 'Escaped_dquote'})
  110. call p.and(['backslash', 'backslash'],
  111.       \{'id': 'double_backslash', 'on_match': 'Double_backslash'})
  112. call p.e('\',
  113.      \{'id': 'backslash', 'on_match': 'Backslash'})
  114. call p.e('"',
  115.       \{'id': 'dquote', 'on_match': 'Dquote'})
  116. call p.e("''",
  117.       \{'id': 'double_squote', 'on_match': 'Double_squote'})
  118. call p.e("'",
  119.       \{'id': 'squote', 'on_match': 'Squote'})
  120. call p.e('->',
  121.       \{'id': 'right_arrow', 'on_match': 'Right_arrow'})
  122. call p.e('::=',
  123.       \{'id': 'mallet', 'on_match': 'Mallet'})
  124. call p.e('|',
  125.       \{'id': 'or', 'on_match': 'Or'})
  126. call p.e('!',
  127.       \{'id': 'not', 'on_match': 'Not'})
  128. call p.e('?',
  129.       \{'id': 'question', 'on_match': 'Question'})
  130. call p.e('\*',
  131.       \{'id': 'star', 'on_match': 'Star'})
  132. call p.e('+',
  133.       \{'id': 'plus', 'on_match': 'Plus'})
  134. call p.e(')',
  135.       \{'id': 'close', 'on_match': 'Close'})
  136. call p.e('(',
  137.       \{'id': 'open', 'on_match': 'Open'})
  138. call p.e('>',
  139.       \{'id': 'gt', 'on_match': 'Gt'})
  140. call p.e('<',
  141.       \{'id': 'lt', 'on_match': 'Lt'})
  142. " }}}
  143.  
  144. " Callback functions {{{
  145. function! Definition(elems)
  146.   echom string(a:elems)
  147.   let label = a:elems[0]
  148.   let mallet = a:elems[1]
  149.   let expression = a:elems[2]
  150.   let callback = len(a:elems[3]) > 0 ? a:elems[3][0] : ''
  151.   let result = 'call '.expression[:-2].",\n        \\{'id': ".label.
  152.         \(callback != '' ? ", 'on_match': ".string(callback) : '') . "})"
  153.   echom 'Definition: ' . result
  154.   return result
  155. endfunction
  156. function! Expression(elems)
  157.   echom string(a:elems)
  158.   if len(a:elems[1]) > 0
  159.     echom 1
  160.     let result = 'p.or(['.a:elems[0]. ', '. join(a:elems[1][0][1:], ', ').'])'
  161.   else
  162.     echom 2
  163.     let result = a:elems[0]
  164.   endif
  165.   echom 'Expression: ' . result
  166.   return result
  167. endfunction
  168. function! Sequence(elems)
  169.   echom string(a:elems)
  170.   let sequence = a:elems
  171.   if len(sequence) > 1
  172.     let result = 'p.and(['.join(sequence, ', ').'])'
  173.   else
  174.     let result = sequence[0]
  175.   endif
  176.   echom 'Sequence: ' . result
  177.   return result
  178. endfunction
  179. function! Prefix(elems)
  180.   let suffix = a:elems[1]
  181.   if len(a:elems[0]) > 0
  182.     let prefix = a:elems[1][0]
  183.     let result = 'p.or('.suffix.')'
  184.   else
  185.     let result = suffix
  186.   endif
  187.   echom 'Prefix: ' . result
  188.   return result
  189. endfunction
  190. function! Suffix(elems)
  191.   let primary = a:elems[0]
  192.   if len(a:elems[1]) > 0
  193.     let suffix = a:elems[1][0]
  194.     let result = 'p.'.(suffix == '*' ? 'maybe_many' : (suffix == '+' ? 'many' : 'maybe_one')) . '('.primary.')'
  195.   else
  196.     let result = primary
  197.   endif
  198.   echom 'Suffix: ' . result
  199.   return result
  200. endfunction
  201. function! Primary(elems)
  202.   echom 'Primary: '.string(a:elems)
  203.   let len = len(a:elems)
  204.   if type(a:elems) == type('')
  205.     let result = a:elems
  206.   elseif len == 2
  207.     let result = a:elems[0]
  208.   else
  209.     let result = a:elems[1]
  210.   endif
  211.   echom 'Primary: ' . result
  212.   return result
  213. endfunction
  214. function! Callback(elems)
  215.   let callback = a:elems[1]
  216.   echom 'Callback: ' . callback
  217.   return callback
  218. endfunction
  219. function! Label(elems)
  220.   echo a:elems
  221.   let result = "'".a:elems[1]."'"
  222.   echom 'Label: ' . result
  223.   return result
  224. endfunction
  225. function! Identifier(elems)
  226.   let id = a:elems[0] . join(a:elems[1], '')
  227.   echom 'Identifier: ' . id
  228.   return id
  229. endfunction
  230. function! Regex(elems)
  231.   echom string(a:elems)
  232.   let regex = 'p.e('.a:elems.')'
  233.   echom 'Regex: ' . regex
  234.   return regex
  235. endfunction
  236. function! Dquoted_string(elems)
  237.   echom string(a:elems)
  238.   let dquoted_string = a:elems[0].join(a:elems[1], '').a:elems[2]
  239.   echom 'Dquoted_string: ' . dquoted_string
  240.   return dquoted_string
  241. endfunction
  242. function! Squoted_string(elems)
  243.   echom string(a:elems)
  244.   let squoted_string = a:elems[0]
  245.   echom 'Squoted_string: ' . squoted_string
  246.   return squoted_string
  247. endfunction
  248. function! Escaped_dquote(elems)
  249.   echom string(a:elems)
  250.   let escaped_dquote = a:elems[0]
  251.   echom 'Escaped_dquote: ' . escaped_dquote
  252.   return escaped_dquote
  253. endfunction
  254. function! Double_backslash(elems)
  255.   echom string(a:elems)
  256.   let double_backslash = a:elems[0]
  257.   echom 'Double_backslash: ' . double_backslash
  258.   return double_backslash
  259. endfunction
  260. function! Backslash(elems)
  261.   echom string(a:elems)
  262.   let backslash = a:elems[0]
  263.   echom 'Backslash: ' . backslash
  264.   return backslash
  265. endfunction
  266. function! Dquote(elems)
  267.   echom string(a:elems)
  268.   let dquote = a:elems[0]
  269.   echom 'Dquote: ' . dquote
  270.   return dquote
  271. endfunction
  272. function! Double_squote(elems)
  273.   let double_squote = a:elems[0]
  274.   echom 'Double_squote: ' . double_squote
  275.   return double_squote
  276. endfunction
  277. function! Squote(elems)
  278.   let squote = a:elems[0]
  279.   "echom 'Squote: ' . squote
  280.   return squote
  281. endfunction
  282. function! Right_arrow(elems)
  283.   let right_arrow = a:elems[0]
  284.   "echom 'Right_arrow: ' . right_arrow
  285.   return right_arrow
  286. endfunction
  287. function! Mallet(elems)
  288.   let mallet = a:elems
  289.   "echom 'Mallet: ' . mallet
  290.   return mallet
  291. endfunction
  292. function! Or(elems)
  293.   let or = a:elems[0]
  294.   "echom 'Or: ' . or
  295.   return or
  296. endfunction
  297. function! Not(elems)
  298.   let not = a:elems[0]
  299.   "echom 'Not: ' . not
  300.   return not
  301. endfunction
  302. function! Question(elems)
  303.   let question = a:elems[0]
  304.   "echom 'Question: ' . question
  305.   return question
  306. endfunction
  307. function! Star(elems)
  308.   let star = a:elems[0]
  309.   "echom 'Star: ' . star
  310.   return star
  311. endfunction
  312. function! Plus(elems)
  313.   let plus = a:elems[0]
  314.   "echom 'Plus: ' . plus
  315.   return plus
  316. endfunction
  317. function! Close(elems)
  318.   let close = a:elems[0]
  319.   "echom 'Close: ' . close
  320.   return close
  321. endfunction
  322. function! Open(elems)
  323.   let open = a:elems[0]
  324.   "echom 'Open: ' . open
  325.   return open
  326. endfunction
  327. function! Gt(elems)
  328.   let gt = a:elems[0]
  329.   "echom 'Gt: ' . gt
  330.   return gt
  331. endfunction
  332. function! Lt(elems)
  333.   let lt = a:elems[0]
  334.   "echom 'Lt: ' . lt
  335.   return lt
  336. endfunction
  337. " }}}
  338.  
  339. function! Test(expr)
  340.   let expr = substitute(a:expr, '^"\s*', '', '')
  341.   echom expr
  342.   let result = g:expr.match(expr).value
  343.   echom expr
  344.   return result
  345. endfunction
  346.  
  347. nore <leader><leader> :w<bar>so %<bar>echo Test(getline('.'))<CR>
  348. finish
  349.  
  350. " <hola> ::= <id1> <id2> | 'dido' -> Dado
  351.  
  352.  
Advertisement
Add Comment
Please, Sign In to add comment