Skip to content

Instantly share code, notes, and snippets.

@nelstrom
Created December 5, 2012 16:14
Show Gist options
  • Save nelstrom/4217027 to your computer and use it in GitHub Desktop.
Save nelstrom/4217027 to your computer and use it in GitHub Desktop.
A proof-of-concept parser for making Vim keystrokes more readable.
require 'parslet'
class Mini < Parslet::Parser
rule(:start) { match('[iIaAoOsS]').as(:switch) }
rule(:typing) { match('[^\e]').repeat.as(:typing) }
rule(:terminate) { match('\e').as(:escape) }
rule(:insertion) { start >> typing >> terminate }
root(:insertion)
end
class Trans < Parslet::Transform
rule(
:switch => simple(:s),
:typing => simple(:t),
:escape => simple(:term)
) { s+"{"+t+"}"+term }
end
begin
tree = Mini.new.parse("OHello, World!\e")
puts tree
# Output the parsed result as O{Hello, World!}
result = Trans.new.apply(tree)
puts result
rescue Parslet::ParseFailed => error
puts error.cause.ascii_tree
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment