fixes missing string tests and expression/transformation
This commit is contained in:
parent
9c16d81ad6
commit
7dfde45373
@ -16,7 +16,6 @@ module Parser
|
|||||||
rule(:dot) { str('.') }
|
rule(:dot) { str('.') }
|
||||||
rule(:digit) { match('[0-9]') }
|
rule(:digit) { match('[0-9]') }
|
||||||
rule(:exponent) { (str('e')| str('E')) }
|
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(:true) { str('true').as(:true) >> space?}
|
||||||
rule(:false) { str('false').as(:false) >> space?}
|
rule(:false) { str('false').as(:false) >> space?}
|
||||||
@ -28,7 +27,7 @@ module Parser
|
|||||||
#anything in double quotes
|
#anything in double quotes
|
||||||
rule(:string){
|
rule(:string){
|
||||||
double_quote >>
|
double_quote >>
|
||||||
(escaped_character | double_quote.absent? >> any ).repeat.as(:string) >>
|
( double_quote.absent? >> any ).repeat.as(:string) >>
|
||||||
double_quote >> space?
|
double_quote >> space?
|
||||||
}
|
}
|
||||||
rule(:integer) { sign.maybe >> digit.repeat(1).as(:integer) >> space? }
|
rule(:integer) { sign.maybe >> digit.repeat(1).as(:integer) >> space? }
|
||||||
|
@ -27,7 +27,7 @@ module Parser
|
|||||||
|
|
||||||
rule(:assignment) { name.as(:asignee) >> equal_sign >> expression.as(:asigned) }
|
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 )
|
def delimited_expressions( delimit )
|
||||||
( space? >> (delimit.absent? >> (assignment | expression)).repeat(1)).as(:expressions) >> delimit
|
( space? >> (delimit.absent? >> (assignment | expression)).repeat(1)).as(:expressions) >> delimit
|
||||||
|
@ -35,6 +35,16 @@ module Parser
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class StringExpression < Expression
|
||||||
|
attr_reader :string
|
||||||
|
def initialize str
|
||||||
|
@string = str
|
||||||
|
end
|
||||||
|
def == other
|
||||||
|
compare other , [:string]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
class FuncallExpression < Expression
|
class FuncallExpression < Expression
|
||||||
attr_reader :name, :args
|
attr_reader :name, :args
|
||||||
def initialize name, args
|
def initialize name, args
|
||||||
|
@ -5,6 +5,7 @@ module Parser
|
|||||||
class Transform < Parslet::Transform
|
class Transform < Parslet::Transform
|
||||||
rule(:integer => simple(:value)) { IntegerExpression.new(value.to_i) }
|
rule(:integer => simple(:value)) { IntegerExpression.new(value.to_i) }
|
||||||
rule(:name => simple(:name)) { NameExpression.new(name.to_s) }
|
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 => simple(:argument)) { argument }
|
||||||
rule(:argument_list => sequence(:argument_list)) { argument_list }
|
rule(:argument_list => sequence(:argument_list)) { argument_list }
|
||||||
|
1
test/runners/hello.rb
Normal file
1
test/runners/hello.rb
Normal file
@ -0,0 +1 @@
|
|||||||
|
puts( "hello world" )
|
@ -32,6 +32,15 @@ class TestAst < MiniTest::Test
|
|||||||
check
|
check
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_string
|
||||||
|
@input = <<HERE
|
||||||
|
"hello"
|
||||||
|
HERE
|
||||||
|
@expected = Parser::StringExpression.new('hello')
|
||||||
|
@parser = @parser.string
|
||||||
|
check
|
||||||
|
end
|
||||||
|
|
||||||
def test_one_argument
|
def test_one_argument
|
||||||
@input = '(42)'
|
@input = '(42)'
|
||||||
@expected = { :argument_list => Parser::IntegerExpression.new(42) }
|
@expected = { :argument_list => Parser::IntegerExpression.new(42) }
|
||||||
|
@ -45,6 +45,15 @@ class ParserTest < MiniTest::Test
|
|||||||
check
|
check
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_string
|
||||||
|
@input = <<HERE
|
||||||
|
"hello"
|
||||||
|
HERE
|
||||||
|
@expected = {:string=>"hello"}
|
||||||
|
@parser = @parser.string
|
||||||
|
check
|
||||||
|
end
|
||||||
|
|
||||||
def test_one_argument
|
def test_one_argument
|
||||||
@input = '(42)'
|
@input = '(42)'
|
||||||
@expected = {:argument_list => {:argument => {:integer => '42'}} }
|
@expected = {:argument_list => {:argument => {:integer => '42'}} }
|
||||||
@ -70,6 +79,18 @@ class ParserTest < MiniTest::Test
|
|||||||
check
|
check
|
||||||
end
|
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
|
def test_expression_else
|
||||||
@input = <<HERE
|
@input = <<HERE
|
||||||
4
|
4
|
||||||
|
@ -25,6 +25,8 @@ class TestRunner < MiniTest::Test
|
|||||||
#link
|
#link
|
||||||
# execute
|
# execute
|
||||||
# check result ?
|
# check result ?
|
||||||
|
puts string
|
||||||
|
puts " "
|
||||||
puts tree.inspect
|
puts tree.inspect
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -26,6 +26,13 @@ class TransformTest < MiniTest::Test
|
|||||||
check
|
check
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_string
|
||||||
|
@input = {:string=>"hello"}
|
||||||
|
@expected = Parser::StringExpression.new('hello')
|
||||||
|
@parser = @parser.string
|
||||||
|
check
|
||||||
|
end
|
||||||
|
|
||||||
def test_argument_list
|
def test_argument_list
|
||||||
@input = {:argument_list => [{:argument => {:integer => '42'}},
|
@input = {:argument_list => [{:argument => {:integer => '42'}},
|
||||||
{:argument => {:name => 'foo'}}]}
|
{:argument => {:name => 'foo'}}]}
|
||||||
|
Loading…
Reference in New Issue
Block a user