From bcd138a15a7ed51e9f1c80f923aa651ecc88bfa7 Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Tue, 26 Aug 2014 20:21:30 +0200 Subject: [PATCH] Lexing of explicit negative/positive XPath numbers --- lib/oga/xpath/lexer.rl | 4 ++-- spec/oga/xpath/lexer/floats_spec.rb | 13 +++++++++++++ spec/oga/xpath/lexer/integers_spec.rb | 13 +++++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 spec/oga/xpath/lexer/floats_spec.rb create mode 100644 spec/oga/xpath/lexer/integers_spec.rb diff --git a/lib/oga/xpath/lexer.rl b/lib/oga/xpath/lexer.rl index 5cdf58e..c2e7064 100644 --- a/lib/oga/xpath/lexer.rl +++ b/lib/oga/xpath/lexer.rl @@ -178,8 +178,8 @@ module Oga # instead lexes them separately so that we can convert the values to # the corresponding Ruby types (Fixnum and Float). - integer = digit+; - float = digit+ ('.' digit+)*; + integer = ('-' | '+')* digit+; + float = ('-' | '+')* digit+ ('.' digit+)*; action emit_integer { value = slice_input(ts, te).to_i diff --git a/spec/oga/xpath/lexer/floats_spec.rb b/spec/oga/xpath/lexer/floats_spec.rb new file mode 100644 index 0000000..3ff0999 --- /dev/null +++ b/spec/oga/xpath/lexer/floats_spec.rb @@ -0,0 +1,13 @@ +require 'spec_helper' + +describe Oga::XPath::Lexer do + context 'floats' do + example 'lex a float' do + lex_xpath('10.0').should == [[:T_FLOAT, 10.0]] + end + + example 'lex a negative float' do + lex_xpath('-10.0').should == [[:T_FLOAT, -10.0]] + end + end +end diff --git a/spec/oga/xpath/lexer/integers_spec.rb b/spec/oga/xpath/lexer/integers_spec.rb new file mode 100644 index 0000000..9a6d8e0 --- /dev/null +++ b/spec/oga/xpath/lexer/integers_spec.rb @@ -0,0 +1,13 @@ +require 'spec_helper' + +describe Oga::XPath::Lexer do + context 'integers' do + example 'lex an integer' do + lex_xpath('10').should == [[:T_INT, 10]] + end + + example 'lex a negative integer' do + lex_xpath('-10').should == [[:T_INT, -10]] + end + end +end