diff --git a/lib/oga/xpath/parser.y b/lib/oga/xpath/parser.y index 74bde1e..67c755f 100644 --- a/lib/oga/xpath/parser.y +++ b/lib/oga/xpath/parser.y @@ -16,31 +16,27 @@ preclow rule xpath - : expressions { s(:xpath, *val[0]) } - | /* none */ { s(:xpath) } - ; - - expressions - : expressions expression { val[0] << val[1] } - | expression { val } + : T_SLASH expression { s(:absolute, val[1]) } + | expression { val[0] } + | /* none */ { nil } ; expression - : path - | node_test + : node_tests | operator | axis | string | number ; - path - : T_SLASH node_test { s(:path, val[1]) } + node_tests + : node_test { val[0] } + | node_test T_SLASH node_tests { val[0].append(val[2]) } ; node_test - : node_name { s(:node_test, val[0]) } - | node_name predicate { s(:node_test, val[0], *val[1]) } + : node_name { s(:test, val[0]) } + | node_name predicate { s(:test, val[0], val[1]) } ; node_name @@ -52,7 +48,7 @@ rule ; predicate - : T_LBRACK expressions T_RBRACK { val[1] } + : T_LBRACK xpath T_RBRACK { s(:predicate, val[1]) } ; operator @@ -61,7 +57,7 @@ rule ; axis - : T_AXIS T_IDENT { s(:axis, val[0], val[1]) } + : T_AXIS node_name { s(:axis, val[0], val[1]) } ; string diff --git a/spec/oga/xpath/parser/axes_spec.rb b/spec/oga/xpath/parser/axes_spec.rb index faa5fca..35f27dd 100644 --- a/spec/oga/xpath/parser/axes_spec.rb +++ b/spec/oga/xpath/parser/axes_spec.rb @@ -4,104 +4,92 @@ describe Oga::XPath::Parser do context 'full axes' do example 'parse the ancestor axis' do parse_xpath('/ancestor::A').should == s( - :xpath, - s(:path, s(:node_test, s(:axis, 'ancestor', s(:name, nil, 'A')))) + :absolute, + s(:test, s(:axis, 'ancestor', s(:name, nil, 'A'))) ) end example 'parse the ancestor-or-self axis' do parse_xpath('/ancestor-or-self::A').should == s( - :xpath, - s( - :path, - s(:node_test, s(:axis, 'ancestor-or-self', s(:name, nil, 'A'))) - ) + :absolute, + s(:test, s(:axis, 'ancestor-or-self', s(:name, nil, 'A'))) ) end example 'parse the attribute axis' do parse_xpath('/attribute::A').should == s( - :xpath, - s(:path, s(:node_test, s(:axis, 'attribute', s(:name, nil, 'A')))) + :absolute, + s(:test, s(:axis, 'attribute', s(:name, nil, 'A'))) ) end example 'parse the child axis' do parse_xpath('/child::A').should == s( - :xpath, - s(:path, s(:node_test, s(:axis, 'child', s(:name, nil, 'A')))) + :absolute, + s(:test, s(:axis, 'child', s(:name, nil, 'A'))) ) end example 'parse the descendant axis' do parse_xpath('/descendant::A').should == s( - :xpath, - s(:path, s(:node_test, s(:axis, 'descendant', s(:name, nil, 'A')))) + :absolute, + s(:test, s(:axis, 'descendant', s(:name, nil, 'A'))) ) end example 'parse the descendant-or-self axis' do parse_xpath('/descendant-or-self::A').should == s( - :xpath, - s( - :path, - s(:node_test, s(:axis, 'descendant-or-self', s(:name, nil, 'A'))) - ) + :absolute, + s(:test, s(:axis, 'descendant-or-self', s(:name, nil, 'A'))) ) end example 'parse the following axis' do parse_xpath('/following::A').should == s( - :xpath, - s(:path, s(:node_test, s(:axis, 'following', s(:name, nil, 'A')))) + :absolute, + s(:test, s(:axis, 'following', s(:name, nil, 'A'))) ) end example 'parse the following-sibling axis' do parse_xpath('/following-sibling::A').should == s( - :xpath, - s( - :path, - s(:node_test, s(:axis, 'following-sibling', s(:name, nil, 'A'))) - ) + :absolute, + s(:test, s(:axis, 'following-sibling', s(:name, nil, 'A'))) ) end example 'parse the namespace axis' do parse_xpath('/namespace::A').should == s( - :xpath, - s(:path, s(:node_test, s(:axis, 'namespace', s(:name, nil, 'A')))) + :absolute, + s(:test, s(:axis, 'namespace', s(:name, nil, 'A'))) ) end example 'parse the parent axis' do parse_xpath('/parent::A').should == s( - :xpath, - s(:path, s(:node_test, s(:axis, 'parent', s(:name, nil, 'A')))) + :absolute, + s(:test, s(:axis, 'parent', s(:name, nil, 'A'))) ) end example 'parse the preceding axis' do parse_xpath('/preceding::A').should == s( - :xpath, - s(:path, s(:node_test, s(:axis, 'preceding', s(:name, nil, 'A')))) + :absolute, + s(:test, s(:axis, 'preceding', s(:name, nil, 'A'))) ) end example 'parse the preceding-sibling axis' do parse_xpath('/preceding-sibling::A').should == s( - :xpath, - s( - :path, - s(:node_test, s(:axis, 'preceding-sibling', s(:name, nil, 'A'))) - ) + :absolute, + s(:test, s(:axis, 'preceding-sibling', s(:name, nil, 'A'))) ) end example 'parse the self axis' do parse_xpath('/self::A').should == s( - :xpath, - s(:path, s(:node_test, s(:axis, 'self', s(:name, nil, 'A')))) + :absolute, + s(:test, s(:axis, 'self', s(:name, nil, 'A'))) ) end end @@ -109,32 +97,29 @@ describe Oga::XPath::Parser do context 'short axes' do example 'parse the @attribute axis' do parse_xpath('/@A').should == s( - :xpath, - s(:path, s(:node_test, s(:axis, 'attribute', s(:name, nil, 'A')))) + :absolute, + s(:test, s(:axis, 'attribute', s(:name, nil, 'A'))) ) end example 'parse the // axis' do parse_xpath('//A').should == s( - :xpath, - s( - :path, - s(:node_test, s(:axis, 'descendant-or-self', s(:name, nil, 'A'))) - ) + :absolute, + s(:test, s(:axis, 'descendant-or-self', s(:name, nil, 'A'))) ) end example 'parse the .. axis' do parse_xpath('/..').should == s( - :xpath, - s(:path, s(:node_test, s(:axis, 'parent'))) + :absolute, + s(:test, s(:axis, 'parent')) ) end example 'parse the . axis' do parse_xpath('/.').should == s( - :xpath, - s(:path, s(:node_test, s(:axis, 'self'))) + :absolute, + s(:test, s(:axis, 'self')) ) end end diff --git a/spec/oga/xpath/parser/paths_spec.rb b/spec/oga/xpath/parser/paths_spec.rb index 4f9fbb9..f6aafeb 100644 --- a/spec/oga/xpath/parser/paths_spec.rb +++ b/spec/oga/xpath/parser/paths_spec.rb @@ -3,21 +3,17 @@ require 'spec_helper' describe Oga::XPath::Parser do context 'paths' do example 'parse an absolute path' do - parse_xpath('/foo').should == s( - :xpath, - s(:path, s(:node_test, s(:name, nil, 'foo'))) - ) + parse_xpath('/A').should == s(:absolute, s(:test, s(:name, nil, 'A'))) end example 'parse a relative path' do - parse_xpath('foo').should == s(:xpath, s(:node_test, s(:name, nil, 'foo'))) + parse_xpath('A').should == s(:test, s(:name, nil, 'A')) end example 'parse an expression using two paths' do - parse_xpath('/foo/bar').should == s( - :xpath, - s(:path, s(:node_test, s(:name, nil, 'foo'))), - s(:path, s(:node_test, s(:name, nil, 'bar'))) + parse_xpath('/A/B').should == s( + :absolute, + s(:test, s(:name, nil, 'A'), s(:test, s(:name, nil, 'B'))) ) end end diff --git a/spec/oga/xpath/parser/predicates_spec.rb b/spec/oga/xpath/parser/predicates_spec.rb index 29d939b..9536a1d 100644 --- a/spec/oga/xpath/parser/predicates_spec.rb +++ b/spec/oga/xpath/parser/predicates_spec.rb @@ -4,12 +4,12 @@ describe Oga::XPath::Parser do context 'predicates' do example 'parse a single predicate' do parse_xpath('/foo[@class="bar"]').should == s( - :xpath, + :absolute, s( - :path, + :test, + s(:name, nil, 'foo'), s( - :node_test, - s(:name, nil, 'foo'), + :predicate, s( :eq, s(:axis, 'attribute', s(:name, nil, 'class')), @@ -22,12 +22,12 @@ describe Oga::XPath::Parser do example 'parse a predicate using the or operator' do parse_xpath('/foo[@class="bar" or @class="baz"]').should == s( - :xpath, + :absolute, s( - :path, + :test, + s(:name, nil, 'foo'), s( - :node_test, - s(:name, nil, 'foo'), + :predicate, s( :or, s(