Permalink
Browse files

Clean up string before parsing it

  • Loading branch information...
namelessjon committed Jun 8, 2010
1 parent 7957328 commit 621f30afd8ecc73dcbefb033470275cc23dfba7d
Showing with 17 additions and 36 deletions.
  1. +4 −1 lib/exalted_math.rb
  2. +11 −33 lib/exalted_math/math.rb
  3. +1 −1 lib/exalted_math/math.treetop
  4. +1 −1 spec/parser_spec.rb
View
@@ -7,7 +7,10 @@
module Exalted
class MathsParser
def ast(text)
- result = parse(text.strip)
+ txt = text.dup
+ txt.strip!
+ txt.gsub!(/\s+/," ")
+ result = parse(txt)
if result
[true, result.ast]
else
View
@@ -928,40 +928,18 @@ def _nt_space
return cached
end
- s0, i0 = [], index
- loop do
- i1 = index
- if has_terminal?(" ", false, index)
- r2 = instantiate_node(SyntaxNode,input, index...(index + 1))
- @index += 1
- else
- terminal_parse_failure(" ")
- r2 = nil
- end
- if r2
- r1 = r2
- else
- if has_terminal?("\t", false, index)
- r3 = instantiate_node(SyntaxNode,input, index...(index + 1))
- @index += 1
- else
- terminal_parse_failure("\t")
- r3 = nil
- end
- if r3
- r1 = r3
- else
- @index = i1
- r1 = nil
- end
- end
- if r1
- s0 << r1
- else
- break
- end
+ if has_terminal?(" ", false, index)
+ r1 = instantiate_node(SyntaxNode,input, index...(index + 1))
+ @index += 1
+ else
+ terminal_parse_failure(" ")
+ r1 = nil
+ end
+ if r1
+ r0 = r1
+ else
+ r0 = instantiate_node(SyntaxNode,input, index...index)
end
- r0 = instantiate_node(SyntaxNode,input, i0...index, s0)
node_cache[:space][start_index] = r0
@@ -101,7 +101,7 @@ grammar Maths
end
rule space
- ( " " / "\t" )*
+ " "?
end
rule number
View
@@ -37,7 +37,7 @@ def initialize(failure)
end
it "Doesn't care about spaces" do
- success, result = @parser.ast(' ( Essence * 4 ) + Willpower + highest[2]( compassion , conviction ) ')
+ success, result = @parser.ast(' ( Essence * 4 ) + Willpower + highest[2]( compassion , conviction ) ')
raise ParserFailure, result unless success
success.should.be.true
end

0 comments on commit 621f30a

Please sign in to comment.