fixes missing string tests and expression/transformation

This commit is contained in:
Torsten Ruger 2014-04-29 11:17:19 +03:00
parent 9c16d81ad6
commit 7dfde45373
9 changed files with 53 additions and 3 deletions

View File

@ -16,7 +16,6 @@ module Parser
rule(:dot) { str('.') }
rule(:digit) { match('[0-9]') }
rule(:exponent) { (str('e')| str('E')) }
rule(:escaped_character) { str('\\') >> (match('["\\\\/bfnrt]') | (str('u') >> hexdigit.repeat(4,4))) }
rule(:true) { str('true').as(:true) >> space?}
rule(:false) { str('false').as(:false) >> space?}
@ -28,7 +27,7 @@ module Parser
#anything in double quotes
rule(:string){
double_quote >>
(escaped_character | double_quote.absent? >> any ).repeat.as(:string) >>
( double_quote.absent? >> any ).repeat.as(:string) >>
double_quote >> space?
}
rule(:integer) { sign.maybe >> digit.repeat(1).as(:integer) >> space? }

View File

@ -27,7 +27,7 @@ module Parser
rule(:assignment) { name.as(:asignee) >> equal_sign >> expression.as(:asigned) }
rule(:expression) { conditional | function_call | integer | (name >> space? >> equal_sign.absent?) }
rule(:expression) { conditional | function_call | integer | string | (name >> space? >> equal_sign.absent?) }
def delimited_expressions( delimit )
( space? >> (delimit.absent? >> (assignment | expression)).repeat(1)).as(:expressions) >> delimit

View File

@ -35,6 +35,16 @@ module Parser
end
end
class StringExpression < Expression
attr_reader :string
def initialize str
@string = str
end
def == other
compare other , [:string]
end
end
class FuncallExpression < Expression
attr_reader :name, :args
def initialize name, args

View File

@ -5,6 +5,7 @@ module Parser
class Transform < Parslet::Transform
rule(:integer => simple(:value)) { IntegerExpression.new(value.to_i) }
rule(:name => simple(:name)) { NameExpression.new(name.to_s) }
rule(:string => simple(:string)) { StringExpression.new(string.to_s) }
rule(:argument => simple(:argument)) { argument }
rule(:argument_list => sequence(:argument_list)) { argument_list }

1
test/runners/hello.rb Normal file
View File

@ -0,0 +1 @@
puts( "hello world" )

View File

@ -32,6 +32,15 @@ class TestAst < MiniTest::Test
check
end
def test_string
@input = <<HERE
"hello"
HERE
@expected = Parser::StringExpression.new('hello')
@parser = @parser.string
check
end
def test_one_argument
@input = '(42)'
@expected = { :argument_list => Parser::IntegerExpression.new(42) }

View File

@ -45,6 +45,15 @@ class ParserTest < MiniTest::Test
check
end
def test_string
@input = <<HERE
"hello"
HERE
@expected = {:string=>"hello"}
@parser = @parser.string
check
end
def test_one_argument
@input = '(42)'
@expected = {:argument_list => {:argument => {:integer => '42'}} }
@ -70,6 +79,18 @@ class ParserTest < MiniTest::Test
check
end
def test_function_call_string
@input = <<HERE
puts( "hello")
HERE
@expected = {:function_call => {:name => 'baz' },
:argument_list => [{:argument => {:integer => '42'}},
{:argument => {:name => 'foo'}}]}
@parser = @parser.function_call
check
end
def test_expression_else
@input = <<HERE
4

View File

@ -25,6 +25,8 @@ class TestRunner < MiniTest::Test
#link
# execute
# check result ?
puts string
puts " "
puts tree.inspect
end

View File

@ -26,6 +26,13 @@ class TransformTest < MiniTest::Test
check
end
def test_string
@input = {:string=>"hello"}
@expected = Parser::StringExpression.new('hello')
@parser = @parser.string
check
end
def test_argument_list
@input = {:argument_list => [{:argument => {:integer => '42'}},
{:argument => {:name => 'foo'}}]}