Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # vim_maps.treetop - demo grammar for sparkup-y vim maps
- # Barry Arthur, Sat May 28, 2011
- # NOTE: The strange use of '' at the start of rules, like:
- #
- # '' (some match elements here) { code block}
- #
- # was necessary to force the code block to be module-level
- # and not syntax-node level. This seems to be a treetop
- # peculiarity.
- grammar VimMaps
- rule vimmap
- (string / keycode / keycombo)* {
- def value
- s = elements.map {|e| e.value}.join
- if $ctrl
- s += '>'
- end
- s
- end
- }
- end
- # handles both the double and single-char key combos.
- # The doubles are placed first so they can parse, otherwise
- # the single would greedily parse first and prevent the
- # double from being recognised.
- rule keycode
- '' (
- 'bu' / 'en' / 'ex' / 'ho' / 'lx' / 'lt' /
- 'pd' / 'pl' / 'pu' / 'sc' / 'sd' / 'si' /
- 'sp' / 'tb' / 'un' / 'xx' /
- [ _,.behjklr]
- ) {
- def value
- keycodes = {
- ' ' => " ",
- '_' => "",
- ',' => "",
- '.' => "<Space>",
- 'b' => "<Bar>",
- 'bu' => " <buffer> ",
- 'e' => "<Esc>",
- 'en' => "<End>",
- 'ex' => " <expr> ",
- 'h' => "<Left>",
- 'ho' => "<Home>",
- 'j' => "<Down>",
- 'k' => "<Up>",
- 'l' => "<Right>",
- 'lx' => "<LocalLeader>",
- 'lt' => "<lt>",
- 'pd' => "<PageDown>",
- 'pl' => "<Plug>",
- 'pu' => "<PageUp>",
- 'r' => "<CR>",
- 'sc' => " <script> ",
- 'sd' => "<SID>",
- 'si' => " <silent> ",
- 'sp' => " <special> ",
- 'tb' => "<Tab>",
- 'un' => " <unique> ",
- 'xx' => "<Leader>",
- }
- s = ''
- if $ctrl
- s = '>'
- $ctrl = nil
- end
- s + keycodes[text_value]
- end
- }
- end
- # handles the alt-, ctrl-, cmd-, meta-, and shift- key combos.
- # from: az,cx-n generates: <a-z><c-x><c-n>
- # uses a global, $ctrl to signal that we're currently inside
- # a combo - when we process a non-combo, or multi-combo (using
- # the '-' char) we can close the preceding combo with '>'.
- rule keycombo
- '' combo:(ctrl:[-acdms] key:.) {
- def value
- s = ''
- if combo.ctrl.text_value == '-'
- '><' + $ctrl + '-' + combo.key.text_value
- else
- if $ctrl
- s = '>'
- end
- $ctrl = combo.ctrl.text_value
- s + '<' + $ctrl + '-' + combo.key.text_value
- end
- end
- }
- end
- # Handles any other text in the input and passes
- # it through as is (with double-quotes stripped).
- rule string
- '' ('"' ('\"' / !'"' .)* '"') {
- def value
- text_value[1..-2]
- end
- }
- end
- end
- #
- #--8<--
- #
- #!/usr/bin/ruby
- # vm.rb - demo parser harness for sparkup-y vim maps
- # Barry Arthur, Sat May 28, 2011
- require 'treetop'
- Treetop.load File.expand_path("./vim_maps")
- parser = VimMapsParser.new
- puts parser.parse(ARGV.join).value
Advertisement
Add Comment
Please, Sign In to add comment