bairui

sparkup-y vim maps treetop grammar

May 27th, 2011
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VIM 3.17 KB | None | 0 0
  1. # vim_maps.treetop - demo grammar for sparkup-y vim maps
  2. # Barry Arthur, Sat May 28, 2011
  3.  
  4. # NOTE: The strange use of   ''   at the start of rules, like:
  5. #
  6. #  '' (some match elements here) { code block}
  7. #
  8. # was necessary to force the code block to be module-level
  9. # and not syntax-node level. This seems to be a treetop
  10. # peculiarity.
  11.  
  12. grammar VimMaps
  13.   rule vimmap
  14.     (string / keycode / keycombo)* {
  15.       def value
  16.         s = elements.map {|e| e.value}.join
  17.         if $ctrl
  18.           s += '>'
  19.         end
  20.         s
  21.       end
  22.     }
  23.   end
  24.  
  25.   # handles both the double and single-char key combos.
  26.   # The doubles are placed first so they can parse, otherwise
  27.   # the single would greedily parse first and prevent the
  28.   # double from being recognised.
  29.   rule keycode
  30.     '' (
  31.          'bu' / 'en' / 'ex' / 'ho' / 'lx' / 'lt' /
  32.          'pd' / 'pl' / 'pu' / 'sc' / 'sd' / 'si' /
  33.          'sp' / 'tb' / 'un' / 'xx' /
  34.          [ _,.behjklr]
  35.        ) {
  36.         def value
  37.           keycodes = {
  38.             ' '  => " ",
  39.             '_'  => "",
  40.             ','  => "",
  41.             '.'  => "<Space>",
  42.             'b'  => "<Bar>",
  43.             'bu'  => " <buffer> ",
  44.             'e'  => "<Esc>",
  45.             'en' => "<End>",
  46.             'ex' => " <expr> ",
  47.             'h'  => "<Left>",
  48.             'ho' => "<Home>",
  49.             'j'  => "<Down>",
  50.             'k'  => "<Up>",
  51.             'l'  => "<Right>",
  52.             'lx' => "<LocalLeader>",
  53.             'lt' => "<lt>",
  54.             'pd' => "<PageDown>",
  55.             'pl' => "<Plug>",
  56.             'pu' => "<PageUp>",
  57.             'r'  => "<CR>",
  58.             'sc' => " <script> ",
  59.             'sd' => "<SID>",
  60.             'si' => " <silent> ",
  61.             'sp' => " <special> ",
  62.             'tb' => "<Tab>",
  63.             'un' => " <unique> ",
  64.             'xx' => "<Leader>",
  65.           }
  66.           s = ''
  67.           if $ctrl
  68.             s = '>'
  69.             $ctrl = nil
  70.           end
  71.           s + keycodes[text_value]
  72.         end
  73.       }
  74.   end
  75.  
  76.   # handles the alt-, ctrl-, cmd-, meta-, and shift- key combos.
  77.   # from: az,cx-n   generates: <a-z><c-x><c-n>
  78.   # uses a global, $ctrl to signal that we're currently inside
  79.  # a combo - when we process a non-combo, or multi-combo (using
  80.  # the '-' char) we can close the preceding combo with '>'.
  81.  rule keycombo
  82.    '' combo:(ctrl:[-acdms] key:.) {
  83.      def value
  84.        s = ''
  85.        if combo.ctrl.text_value == '-'
  86.          '><' + $ctrl + '-' + combo.key.text_value
  87.        else
  88.          if $ctrl
  89.            s = '>'
  90.          end
  91.          $ctrl = combo.ctrl.text_value
  92.          s + '<' + $ctrl + '-' + combo.key.text_value
  93.        end
  94.      end
  95.    }
  96.  end
  97.  
  98.  # Handles any other text in the input and passes
  99.  # it through as is (with double-quotes stripped).
  100.  rule string
  101.    '' ('"' ('\"' / !'"' .)* '"') {
  102.       def value
  103.         text_value[1..-2]
  104.       end
  105.     }
  106.   end
  107. end
  108.  
  109. #
  110. #--8<--
  111. #
  112.  
  113. #!/usr/bin/ruby
  114.  
  115. # vm.rb - demo parser harness for sparkup-y vim maps
  116. # Barry Arthur, Sat May 28, 2011
  117.  
  118. require 'treetop'
  119. Treetop.load File.expand_path("./vim_maps")
  120.  
  121. parser = VimMapsParser.new
  122. puts parser.parse(ARGV.join).value
Advertisement
Add Comment
Please, Sign In to add comment