From 894de7f909a2e88613e0038869e484089a937d1e Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Tue, 17 Jun 2014 19:56:38 +0200 Subject: [PATCH] Lex all XPath expressions in a single machine. This allows literal values such as strings and numbers to be used as function arguments. --- lib/oga/xpath/lexer.rl | 28 +++++----------------------- spec/oga/xpath/lexer/calls_spec.rb | 5 +++-- 2 files changed, 8 insertions(+), 25 deletions(-) diff --git a/lib/oga/xpath/lexer.rl b/lib/oga/xpath/lexer.rl index f3df977..7461d18 100644 --- a/lib/oga/xpath/lexer.rl +++ b/lib/oga/xpath/lexer.rl @@ -279,34 +279,16 @@ module Oga | op_sub ; - # Machine that handles the lexing of data inside an XPath predicate. - # When bumping into a "]" the lexer jumps back to the `main` machine. - predicate := |* - operator; - whitespace | slash | lparen | rparen | comma | colon | star; - - string => emit_string; - integer => emit_integer; - float => emit_float; - axis_full => emit_axis_full; - axis_short => emit_axis_short; - identifier => emit_identifier; - - ']' => { - add_token(:T_RBRACK) - fnext main; - }; - *|; - main := |* operator; whitespace | slash | lparen | rparen | comma | colon | star; - '[' => { - add_token(:T_LBRACK) - fnext predicate; - }; + '[' => { add_token(:T_LBRACK) }; + ']' => { add_token(:T_RBRACK) }; + string => emit_string; + integer => emit_integer; + float => emit_float; axis_full => emit_axis_full; axis_short => emit_axis_short; identifier => emit_identifier; diff --git a/spec/oga/xpath/lexer/calls_spec.rb b/spec/oga/xpath/lexer/calls_spec.rb index de9c780..bbe83cf 100644 --- a/spec/oga/xpath/lexer/calls_spec.rb +++ b/spec/oga/xpath/lexer/calls_spec.rb @@ -20,12 +20,13 @@ describe Oga::XPath::Lexer do end example 'lex a function call with two arguments' do - lex_xpath('count(foo, bar)').should == [ + lex_xpath('count(/foo, "bar")').should == [ [:T_IDENT, 'count'], [:T_LPAREN, nil], + [:T_SLASH, nil], [:T_IDENT, 'foo'], [:T_COMMA, nil], - [:T_IDENT, 'bar'], + [:T_STRING, 'bar'], [:T_RPAREN, nil] ] end