Created
December 5, 2012 16:14
-
-
Save nelstrom/4217027 to your computer and use it in GitHub Desktop.
A proof-of-concept parser for making Vim keystrokes more readable.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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