Created
September 24, 2013 05:40
-
-
Save shelling/6680786 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| grammar Foo | |
| rule top | |
| value? { | |
| def to_ruby | |
| elements.to_ruby | |
| end | |
| } | |
| end | |
| rule value | |
| array / object / primitive | |
| end | |
| rule primitive | |
| null / true / false / string / float / integer | |
| end | |
| rule object | |
| "{" space head:pair space tail:("," space pair)* space "}" { | |
| def to_ruby | |
| Hash[tail.elements.map(&:pair).map(&:to_ruby).unshift(head.to_ruby)] | |
| end | |
| } | |
| / | |
| "{" space "}" { | |
| def to_ruby | |
| {} | |
| end | |
| } | |
| end | |
| rule pair | |
| k:primitive space ":" space v:value { | |
| def to_ruby | |
| [ k.to_ruby, v.to_ruby ] | |
| end | |
| } | |
| end | |
| rule array | |
| "[" space head:value space tail:("," space value)* space "]" { | |
| def to_ruby | |
| tail.elements.map(&:value).map(&:to_ruby).unshift(head.to_ruby) | |
| end | |
| } | |
| / | |
| "[" space "]" { | |
| def to_ruby | |
| [] | |
| end | |
| } | |
| end | |
| rule null | |
| space "null" space { | |
| def to_ruby | |
| nil | |
| end | |
| } | |
| end | |
| rule true | |
| space "true" space { | |
| def to_ruby | |
| true | |
| end | |
| } | |
| end | |
| rule false | |
| space "false" space { | |
| def to_ruby | |
| false | |
| end | |
| } | |
| end | |
| rule string | |
| '"' [a-zA-Z0-9 _-]* '"' { | |
| def to_ruby | |
| text_value.gsub(/\"/,"") | |
| end | |
| } | |
| end | |
| rule float | |
| [+-]? [0-9]* "." [0-9]* { | |
| def to_ruby | |
| text_value.to_f | |
| end | |
| } | |
| end | |
| rule integer | |
| [+-]? [0-9]+ { | |
| def to_ruby | |
| text_value.to_i | |
| end | |
| } | |
| end | |
| rule space | |
| [ \t\r\n]* | |
| end | |
| end |
This file contains hidden or 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
| #!/usr/bin/env ruby | |
| $:.unshift "." | |
| require "treetop" | |
| require "foo" | |
| require "pp" | |
| pp FooParser.new.parse('null').to_ruby | |
| pp FooParser.new.parse('true').to_ruby | |
| pp FooParser.new.parse('false').to_ruby | |
| pp FooParser.new.parse('"hello"').to_ruby | |
| pp FooParser.new.parse('1').to_ruby | |
| pp FooParser.new.parse('0').to_ruby | |
| pp FooParser.new.parse('-1').to_ruby | |
| pp FooParser.new.parse('.230').to_ruby | |
| pp FooParser.new.parse('023.').to_ruby | |
| pp FooParser.new.parse('023.230').to_ruby | |
| pp FooParser.new.parse('[ ]').to_ruby | |
| pp FooParser.new.parse('[1,2,3]').to_ruby | |
| pp FooParser.new.parse('[1.1,2.2,3.3]').to_ruby | |
| pp FooParser.new.parse('["hello"]').to_ruby | |
| pp FooParser.new.parse('[ "hello" , "world" ]').to_ruby | |
| pp FooParser.new.parse('[ true, false, null, 1, 1.2, "string" ]').to_ruby | |
| pp FooParser.new.parse('{ }').to_ruby | |
| pp FooParser.new.parse('{"hello":"world"}').to_ruby | |
| pp FooParser.new.parse('{"hello":"world", "foo":"bar", "shelling":"ford"}').to_ruby | |
| pp FooParser.new.parse('{ true : null, false : null }').to_ruby | |
| pp FooParser.new.parse('[{"hello":"world"}, {"foo":"bar"}, {"shelling":"ford"}]').to_ruby | |
| pp FooParser.new.parse('{"list" : [1,2,3]}').to_ruby |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment