improving the string according to kasper

This commit is contained in:
Torsten Ruger 2014-05-09 13:51:47 +03:00
parent e4dce2394c
commit 8e6297dcff
5 changed files with 29 additions and 18 deletions

View File

@ -10,6 +10,9 @@ module Parser
rule(:space?) { space.maybe }
rule(:newline){ str("\n") >> space?}
rule(:quote) { str('"') }
rule(:nonquote) { str('"').absent? >> any }
rule(:comment){ match('#') >> (newline.absent? >> any).repeat.as(:comment) >> newline }
rule(:eol) { newline | any.absent? }
@ -30,15 +33,22 @@ module Parser
# identifier must start with lower case
rule(:name) { (match['a-z'] >> match['a-zA-Z0-9'].repeat).as(:name) >> space? }
rule(:string_special) { match['\0\t\n\r"\\\\'] }
rule(:escaped_special) { str("\\") >> match['0tnr"\\\\'] }
rule(:escape) { str('\\') >> any.as(:esc) }
rule(:string) { quote >> (
escape |
nonquote.as(:char)
).repeat(1).as(:string) >> quote }
# rule(:string_special) { match['\0\t\n\r"\\\\'] }
# rule(:escaped_special) { str("\\") >> match['0tnr"\\\\'] }
#anything in double quotes
rule(:string){
double_quote >>
( escaped_special | string_special.absent? >> any ).repeat.as(:string) >>
double_quote >> space?
}
# rule(:string){
# double_quote >>
# ( escaped_special | string_special.absent? >> any ).repeat.as(:string) >>
# double_quote >> space?
# }
rule(:integer) { sign.maybe >> digit.repeat(1).as(:integer) >> space? }
rule(:float) { integer >> dot >> integer >>

View File

@ -3,9 +3,12 @@ require 'ast/expression'
module Parser
class Transform < Parslet::Transform
rule(:string => sequence(:chars)) { Ast::StringExpression.new chars.join }
rule(:esc => simple(:esc)) { '\\' + esc }
rule(char: simple(:char)) { char }
rule(:integer => simple(:value)) { Ast::IntegerExpression.new(value.to_i) }
rule(:name => simple(:name)) { Ast::NameExpression.new(name.to_s) }
rule(:string => simple(:string)) { Ast::StringExpression.new(string.to_s) }
rule(:argument => simple(:argument)) { argument }
rule(:argument_list => sequence(:argument_list)) { argument_list }

View File

@ -29,19 +29,16 @@ class TestBasic < MiniTest::Test
def test_string
@string_input = "\"hello\""
@parse_output = {:string=>"hello"}
@parse_output = {:string=>[{:char=>"h"}, {:char=>"e"}, {:char=>"l"}, {:char=>"l"}, {:char=>"o"}]}
@transform_output = Ast::StringExpression.new('hello')
@parser = @parser.string
end
def test_string_escapes
out = "hello nyou"
out[6] = '\\'
@string_input = "\"#{out}\""
# puts will show that this is a string with a \n in it.
# but he who knows the ruby string rules well enough to do this in the input may win a beer at the ...
# puts @string_input
@parse_output = {:string=>out} #chop quotes off
out = 'hello \nyou'
@string_input = '"' + out + '"'
@parse_output = {:string=>[{:char=>"h"}, {:char=>"e"}, {:char=>"l"}, {:char=>"l"}, {:char=>"o"},
{:char=>" "}, {:char=>" "}, {:esc=>"n"}, {:char=>"y"}, {:char=>"o"}, {:char=>"u"}]}
@transform_output = Ast::StringExpression.new(out)
@parser = @parser.string
end

View File

@ -24,7 +24,8 @@ class TestFunctionCall < MiniTest::Test
def test_function_call_string
@string_input = 'puts( "hello")'
@parse_output = {:function_call => {:name => 'puts' },
:argument_list => [{:argument => {:string => 'hello'}}]}
:argument_list => [{:argument =>
{:string=>[{:char=>"h"}, {:char=>"e"}, {:char=>"l"}, {:char=>"l"}, {:char=>"o"}]}}]}
@transform_output = Ast::FuncallExpression.new "puts", [Ast::StringExpression.new("hello")]
@parser = @parser.function_call
end

View File

@ -20,7 +20,7 @@ class TestRunner < MiniTest::Test
def execute file
string = File.read(file)
parser = Parser::Composed.new
syntax = parser.function_definition.parse_with_debug(string)
syntax = parser.parse_with_debug(string)
program = Vm::Program.new "Arm"
main = Parser::Transform.new.apply(syntax)