Updated used ASTs for all XPath parser specs.

This commit is contained in:
Yorick Peterse 2014-06-17 18:51:33 +02:00
parent 2298ef618b
commit bb7af98257
4 changed files with 63 additions and 54 deletions

View File

@ -10,45 +10,45 @@ 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
options no_result_var options no_result_var
prechigh prechigh
left T_EQ left T_EQ T_AXIS
right T_OR right T_OR
preclow preclow
rule rule
xpath xpath
: T_SLASH expression { s(:absolute, val[1]) } : T_SLASH path { s(:absolute, val[1]) }
| expression { val[0] } | path { val[0] }
| /* none */ { nil } | /* none */ { nil }
;
path
: expression { s(:path, val[0]) }
| expression T_SLASH path { s(:path, val[0], val[2]) }
; ;
expression expression
: node_tests : node_test
| operator | operator
| axis | axis
| string | string
| number | number
; ;
node_tests
: node_test { val[0] }
| node_test T_SLASH node_tests { val[0].append(val[2]) }
;
node_test node_test
: node_name { s(:test, val[0]) } : node_name { s(:test, *val[0]) }
| node_name predicate { s(:test, val[0], val[1]) } | node_name predicate { s(:test, *val[0], val[1]) }
; ;
node_name node_name
# foo # foo
: T_IDENT { s(:name, nil, val[0]) } : T_IDENT { [nil, val[0]] }
# foo:bar # foo:bar
| T_IDENT T_COLON T_IDENT { s(:name, val[0], val[1]) } | T_IDENT T_COLON T_IDENT { [val[0], val[2]] }
; ;
predicate predicate
: T_LBRACK xpath T_RBRACK { s(:predicate, val[1]) } : T_LBRACK xpath T_RBRACK { val[1] }
; ;
operator operator
@ -57,7 +57,8 @@ rule
; ;
axis axis
: T_AXIS node_name { s(:axis, val[0], val[1]) } : T_AXIS expression { s(:axis, val[0], val[1]) }
| T_AXIS { s(:axis, val[0]) }
; ;
string string

View File

@ -5,91 +5,91 @@ describe Oga::XPath::Parser do
example 'parse the ancestor axis' do example 'parse the ancestor axis' do
parse_xpath('/ancestor::A').should == s( parse_xpath('/ancestor::A').should == s(
:absolute, :absolute,
s(:test, s(:axis, 'ancestor', s(:name, nil, 'A'))) s(:path, s(:axis, 'ancestor', s(:test, nil, 'A')))
) )
end end
example 'parse the ancestor-or-self axis' do example 'parse the ancestor-or-self axis' do
parse_xpath('/ancestor-or-self::A').should == s( parse_xpath('/ancestor-or-self::A').should == s(
:absolute, :absolute,
s(:test, s(:axis, 'ancestor-or-self', s(:name, nil, 'A'))) s(:path, s(:axis, 'ancestor-or-self', s(:test, nil, 'A')))
) )
end end
example 'parse the attribute axis' do example 'parse the attribute axis' do
parse_xpath('/attribute::A').should == s( parse_xpath('/attribute::A').should == s(
:absolute, :absolute,
s(:test, s(:axis, 'attribute', s(:name, nil, 'A'))) s(:path, s(:axis, 'attribute', s(:test, nil, 'A')))
) )
end end
example 'parse the child axis' do example 'parse the child axis' do
parse_xpath('/child::A').should == s( parse_xpath('/child::A').should == s(
:absolute, :absolute,
s(:test, s(:axis, 'child', s(:name, nil, 'A'))) s(:path, s(:axis, 'child', s(:test, nil, 'A')))
) )
end end
example 'parse the descendant axis' do example 'parse the descendant axis' do
parse_xpath('/descendant::A').should == s( parse_xpath('/descendant::A').should == s(
:absolute, :absolute,
s(:test, s(:axis, 'descendant', s(:name, nil, 'A'))) s(:path, s(:axis, 'descendant', s(:test, nil, 'A')))
) )
end end
example 'parse the descendant-or-self axis' do example 'parse the descendant-or-self axis' do
parse_xpath('/descendant-or-self::A').should == s( parse_xpath('/descendant-or-self::A').should == s(
:absolute, :absolute,
s(:test, s(:axis, 'descendant-or-self', s(:name, nil, 'A'))) s(:path, s(:axis, 'descendant-or-self', s(:test, nil, 'A')))
) )
end end
example 'parse the following axis' do example 'parse the following axis' do
parse_xpath('/following::A').should == s( parse_xpath('/following::A').should == s(
:absolute, :absolute,
s(:test, s(:axis, 'following', s(:name, nil, 'A'))) s(:path, s(:axis, 'following', s(:test, nil, 'A')))
) )
end end
example 'parse the following-sibling axis' do example 'parse the following-sibling axis' do
parse_xpath('/following-sibling::A').should == s( parse_xpath('/following-sibling::A').should == s(
:absolute, :absolute,
s(:test, s(:axis, 'following-sibling', s(:name, nil, 'A'))) s(:path, s(:axis, 'following-sibling', s(:test, nil, 'A')))
) )
end end
example 'parse the namespace axis' do example 'parse the namespace axis' do
parse_xpath('/namespace::A').should == s( parse_xpath('/namespace::A').should == s(
:absolute, :absolute,
s(:test, s(:axis, 'namespace', s(:name, nil, 'A'))) s(:path, s(:axis, 'namespace', s(:test, nil, 'A')))
) )
end end
example 'parse the parent axis' do example 'parse the parent axis' do
parse_xpath('/parent::A').should == s( parse_xpath('/parent::A').should == s(
:absolute, :absolute,
s(:test, s(:axis, 'parent', s(:name, nil, 'A'))) s(:path, s(:axis, 'parent', s(:test, nil, 'A')))
) )
end end
example 'parse the preceding axis' do example 'parse the preceding axis' do
parse_xpath('/preceding::A').should == s( parse_xpath('/preceding::A').should == s(
:absolute, :absolute,
s(:test, s(:axis, 'preceding', s(:name, nil, 'A'))) s(:path, s(:axis, 'preceding', s(:test, nil, 'A')))
) )
end end
example 'parse the preceding-sibling axis' do example 'parse the preceding-sibling axis' do
parse_xpath('/preceding-sibling::A').should == s( parse_xpath('/preceding-sibling::A').should == s(
:absolute, :absolute,
s(:test, s(:axis, 'preceding-sibling', s(:name, nil, 'A'))) s(:path, s(:axis, 'preceding-sibling', s(:test, nil, 'A')))
) )
end end
example 'parse the self axis' do example 'parse the self axis' do
parse_xpath('/self::A').should == s( parse_xpath('/self::A').should == s(
:absolute, :absolute,
s(:test, s(:axis, 'self', s(:name, nil, 'A'))) s(:path, s(:axis, 'self', s(:test, nil, 'A')))
) )
end end
end end
@ -98,28 +98,28 @@ describe Oga::XPath::Parser do
example 'parse the @attribute axis' do example 'parse the @attribute axis' do
parse_xpath('/@A').should == s( parse_xpath('/@A').should == s(
:absolute, :absolute,
s(:test, s(:axis, 'attribute', s(:name, nil, 'A'))) s(:path, s(:axis, 'attribute', s(:test, nil, 'A')))
) )
end end
example 'parse the // axis' do example 'parse the // axis' do
parse_xpath('//A').should == s( parse_xpath('//A').should == s(
:absolute, :absolute,
s(:test, s(:axis, 'descendant-or-self', s(:name, nil, 'A'))) s(:path, s(:axis, 'descendant-or-self', s(:test, nil, 'A')))
) )
end end
example 'parse the .. axis' do example 'parse the .. axis' do
parse_xpath('/..').should == s( parse_xpath('/..').should == s(
:absolute, :absolute,
s(:test, s(:axis, 'parent')) s(:path, s(:axis, 'parent'))
) )
end end
example 'parse the . axis' do example 'parse the . axis' do
parse_xpath('/.').should == s( parse_xpath('/.').should == s(
:absolute, :absolute,
s(:test, s(:axis, 'self')) s(:path, s(:axis, 'self'))
) )
end end
end end

View File

@ -3,17 +3,17 @@ require 'spec_helper'
describe Oga::XPath::Parser do describe Oga::XPath::Parser do
context 'paths' do context 'paths' do
example 'parse an absolute path' do example 'parse an absolute path' do
parse_xpath('/A').should == s(:absolute, s(:test, s(:name, nil, 'A'))) parse_xpath('/A').should == s(:absolute, s(:path, s(:test, nil, 'A')))
end end
example 'parse a relative path' do example 'parse a relative path' do
parse_xpath('A').should == s(:test, s(:name, nil, 'A')) parse_xpath('A').should == s(:path, s(:test, nil, 'A'))
end end
example 'parse an expression using two paths' do example 'parse an expression using two paths' do
parse_xpath('/A/B').should == s( parse_xpath('/A/B').should == s(
:absolute, :absolute,
s(:test, s(:name, nil, 'A'), s(:test, s(:name, nil, 'B'))) s(:path, s(:test, nil, 'A'), s(:path, s(:test, nil, 'B')))
) )
end end
end end

View File

@ -6,14 +6,18 @@ describe Oga::XPath::Parser do
parse_xpath('/foo[@class="bar"]').should == s( parse_xpath('/foo[@class="bar"]').should == s(
:absolute, :absolute,
s( s(
:test, :path,
s(:name, nil, 'foo'),
s( s(
:predicate, :test,
nil,
'foo',
s( s(
:eq, :path,
s(:axis, 'attribute', s(:name, nil, 'class')), s(
s(:string, 'bar') :eq,
s(:axis, 'attribute', s(:test, nil, 'class')),
s(:string, 'bar')
)
) )
) )
) )
@ -24,21 +28,25 @@ describe Oga::XPath::Parser do
parse_xpath('/foo[@class="bar" or @class="baz"]').should == s( parse_xpath('/foo[@class="bar" or @class="baz"]').should == s(
:absolute, :absolute,
s( s(
:test, :path,
s(:name, nil, 'foo'),
s( s(
:predicate, :test,
nil,
'foo',
s( s(
:or, :path,
s( s(
:eq, :or,
s(:axis, 'attribute', s(:name, nil, 'class')), s(
s(:string, 'bar') :eq,
), s(:axis, 'attribute', s(:test, nil, 'class')),
s( s(:string, 'bar')
:eq, ),
s(:axis, 'attribute', s(:name, nil, 'class')), s(
s(:string, 'baz') :eq,
s(:axis, 'attribute', s(:test, nil, 'class')),
s(:string, 'baz')
)
) )
) )
) )