From 034ae4f7ca6f91a90b3439c26d42f26863dcbc51 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Thu, 8 May 2014 18:38:49 +0300 Subject: [PATCH] add comment and remove newline from space (starting a long whitespace battle) --- lib/parser/basic_types.rb | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/lib/parser/basic_types.rb b/lib/parser/basic_types.rb index 4410a3cc..2e6e54da 100644 --- a/lib/parser/basic_types.rb +++ b/lib/parser/basic_types.rb @@ -4,9 +4,15 @@ module Parser # floats ? module BasicTypes include Parslet - rule(:space) { match('\s').repeat(1) } + # space really is just space. ruby is newline sensitive, so there is more whitespace footwork + # rule of thumb is that anything eats space behind it, but only space, no newlines + rule(:space) { (str('\t') | str(' ')).repeat(1) } rule(:space?) { space.maybe } - rule(:eol) { (str("\n") >> space?) | any.absent? } + rule(:newline){ str('\n') } + + rule(:comment){ match('#') >> (newline.absent? >> any).repeat.as(:comment) >> newline } + + rule(:eol) { (newline >> space?) | any.absent? } rule(:double_quote){ str('"') } rule(:minus) { str('-') } @@ -24,15 +30,18 @@ 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"\\\\'] } + #anything in double quotes rule(:string){ double_quote >> - ( double_quote.absent? >> any ).repeat.as(:string) >> + ( 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 >> - (exponent >> sign.maybe >> digit.repeat).maybe >> space?} + (exponent >> sign.maybe >> digit.repeat(1,3)).maybe >> space?} end end \ No newline at end of file