Created
September 7, 2011 21:17
-
-
Save whitequark/1201762 to your computer and use it in GitHub Desktop.
Treetop AST quirks
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 'treetop' | |
Treetop.load 'ast_quirks.treetop' | |
parser = SimpleExpressionParser.new | |
ast = parser.parse %q{42} | |
p ast.to_code |
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
grammar SimpleExpression | |
rule expression | |
( string / number ) { | |
def to_code | |
elements.first.to_code | |
end | |
} | |
end | |
rule string | |
'"' ( !'"' . / '\"' )* '"' { | |
def to_code; text_value; end | |
} | |
end | |
rule number | |
[0-9]+ { | |
def to_code; text_value.to_i; end | |
} | |
end | |
end |
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
grammar SimpleExpression | |
rule expression | |
( string / number ) 1..1 { | |
def to_code | |
elements.first.to_code | |
end | |
} | |
end | |
rule string | |
'"' ( !'"' . / '\"' )* '"' { | |
def to_code; text_value; end | |
} | |
end | |
rule number | |
[0-9]+ { | |
def to_code; text_value.to_i; end | |
} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment