From 514c342cab471b7cb99cf65dc53eb74505e96845 Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Fri, 20 Jun 2014 20:37:34 +0200 Subject: [PATCH] Lex wildcards as T_IDENT instead of T_STAR. --- lib/oga/xpath/lexer.rl | 5 ++--- lib/oga/xpath/parser.y | 2 +- spec/oga/xpath/lexer/general_spec.rb | 4 ++-- spec/oga/xpath/lexer/predicates_spec.rb | 2 +- spec/oga/xpath/parser/wildcard_spec.rb | 17 +++++++++++++++++ 5 files changed, 23 insertions(+), 7 deletions(-) create mode 100644 spec/oga/xpath/parser/wildcard_spec.rb diff --git a/lib/oga/xpath/lexer.rl b/lib/oga/xpath/lexer.rl index 7461d18..cf7d688 100644 --- a/lib/oga/xpath/lexer.rl +++ b/lib/oga/xpath/lexer.rl @@ -145,14 +145,13 @@ module Oga rparen = ')' @{ add_token(:T_RPAREN) }; comma = ',' @{ add_token(:T_COMMA) }; colon = ':' @{ add_token(:T_COLON) }; - star = '*' @{ add_token(:T_STAR) }; # Identifiers # # Identifiers are used for element names, namespaces, attribute names, # etc. Identifiers have to start with a letter. - identifier = [a-zA-Z]+ [a-zA-Z\-_0-9]*; + identifier = [a-zA-Z*]+ [a-zA-Z\-_0-9]*; action emit_identifier { emit(:T_IDENT, ts, te) @@ -281,7 +280,7 @@ module Oga main := |* operator; - whitespace | slash | lparen | rparen | comma | colon | star; + whitespace | slash | lparen | rparen | comma | colon; '[' => { add_token(:T_LBRACK) }; ']' => { add_token(:T_RBRACK) }; diff --git a/lib/oga/xpath/parser.y b/lib/oga/xpath/parser.y index c0e8fc0..900679c 100644 --- a/lib/oga/xpath/parser.y +++ b/lib/oga/xpath/parser.y @@ -3,7 +3,7 @@ # class Oga::XPath::Parser -token T_AXIS T_COLON T_COMMA T_FLOAT T_INT T_IDENT T_STAR +token T_AXIS T_COLON T_COMMA T_FLOAT T_INT T_IDENT token T_LBRACK T_RBRACK T_LPAREN T_RPAREN T_SLASH T_STRING token T_PIPE T_AND T_OR T_ADD T_DIV T_MOD T_EQ T_NEQ T_LT T_GT T_LTE T_GTE diff --git a/spec/oga/xpath/lexer/general_spec.rb b/spec/oga/xpath/lexer/general_spec.rb index d3e00af..bc44fbe 100644 --- a/spec/oga/xpath/lexer/general_spec.rb +++ b/spec/oga/xpath/lexer/general_spec.rb @@ -16,13 +16,13 @@ describe Oga::XPath::Lexer do end example 'lex a whildcard node test' do - lex_xpath('/*').should == [[:T_SLASH, nil], [:T_STAR, nil]] + lex_xpath('/*').should == [[:T_SLASH, nil], [:T_IDENT, '*']] end example 'lex a wildcard node test for a namespace' do lex_xpath('/*:foo').should == [ [:T_SLASH, nil], - [:T_STAR, nil], + [:T_IDENT, '*'], [:T_COLON, nil], [:T_IDENT, 'foo'] ] diff --git a/spec/oga/xpath/lexer/predicates_spec.rb b/spec/oga/xpath/lexer/predicates_spec.rb index 916b12c..2e0607e 100644 --- a/spec/oga/xpath/lexer/predicates_spec.rb +++ b/spec/oga/xpath/lexer/predicates_spec.rb @@ -108,7 +108,7 @@ describe Oga::XPath::Lexer do [:T_SLASH, nil], [:T_IDENT, 'foo'], [:T_SLASH, nil], - [:T_STAR, nil], + [:T_IDENT, '*'], [:T_RBRACK, nil] ] end diff --git a/spec/oga/xpath/parser/wildcard_spec.rb b/spec/oga/xpath/parser/wildcard_spec.rb new file mode 100644 index 0000000..8d0de40 --- /dev/null +++ b/spec/oga/xpath/parser/wildcard_spec.rb @@ -0,0 +1,17 @@ +require 'spec_helper' + +describe Oga::XPath::Parser do + context 'wildcards' do + example 'parse a wildcard name test' do + parse_xpath('*').should == s(:path, s(:test, nil, '*')) + end + + example 'parse a wildcard namespace test' do + parse_xpath('*:A').should == s(:path, s(:test, '*', 'A')) + end + + example 'parse a wildcard namespace and name test' do + parse_xpath('*:*').should == s(:path, s(:test, '*', '*')) + end + end +end