Skip to content

Instantly share code, notes, and snippets.

@startling
Created February 16, 2012 02:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save startling/1841257 to your computer and use it in GitHub Desktop.
Save startling/1841257 to your computer and use it in GitHub Desktop.
toy lispy parser
#!/usr/bin/env ruby
require 'parslet'
class Lispy < Parslet::Parser
rule(:integer) { match('[0-9]').repeat(1) }
rule(:literal) { integer }
rule(:identifier) { match('[^0-9\'"]') >> match('.').repeat(0)}
rule(:atom) { identifier | literal }
rule(:spaces) { match('\s').repeat(1) }
rule(:spaces?) { spaces.maybe }
rule(:item) { spaces? >> (list | atom) >> spaces? }
rule(:list) { str('(') >> item.repeat(0) >> str(')') }
root(:list)
end
p Lispy.new.parse("(112)")
p Lispy.new.parse("((112))")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment