diff --git a/lib/parser/basic_types.rb b/lib/parser/basic_types.rb index b1844d6b..4410a3cc 100644 --- a/lib/parser/basic_types.rb +++ b/lib/parser/basic_types.rb @@ -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? } diff --git a/lib/parser/composed.rb b/lib/parser/composed.rb index 1ede3b7c..143b89ac 100644 --- a/lib/parser/composed.rb +++ b/lib/parser/composed.rb @@ -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 diff --git a/lib/parser/nodes.rb b/lib/parser/nodes.rb index 478cf995..c0b56169 100644 --- a/lib/parser/nodes.rb +++ b/lib/parser/nodes.rb @@ -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 diff --git a/lib/parser/transform.rb b/lib/parser/transform.rb index 030e9303..3f40bc50 100644 --- a/lib/parser/transform.rb +++ b/lib/parser/transform.rb @@ -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 } diff --git a/test/runners/hello.rb b/test/runners/hello.rb new file mode 100644 index 00000000..f4e9ca1e --- /dev/null +++ b/test/runners/hello.rb @@ -0,0 +1 @@ +puts( "hello world" ) diff --git a/test/test_ast.rb b/test/test_ast.rb index 1ec9488e..c01c3708 100644 --- a/test/test_ast.rb +++ b/test/test_ast.rb @@ -32,6 +32,15 @@ class TestAst < MiniTest::Test check end + def test_string + @input = < Parser::IntegerExpression.new(42) } diff --git a/test/test_parser.rb b/test/test_parser.rb index 314b060f..c5ec0dea 100644 --- a/test/test_parser.rb +++ b/test/test_parser.rb @@ -45,6 +45,15 @@ class ParserTest < MiniTest::Test check end + def test_string + @input = <"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 = < {:name => 'baz' }, + :argument_list => [{:argument => {:integer => '42'}}, + {:argument => {:name => 'foo'}}]} + + @parser = @parser.function_call + check + end + def test_expression_else @input = <"hello"} + @expected = Parser::StringExpression.new('hello') + @parser = @parser.string + check + end + def test_argument_list @input = {:argument_list => [{:argument => {:integer => '42'}}, {:argument => {:name => 'foo'}}]}