Cleaned up literal usage in the XPath compiler
This commit is contained in:
parent
44bd0751bc
commit
f3f3c7d31c
|
@ -70,9 +70,9 @@ module Oga
|
||||||
ruby_ast = process(ast, document)
|
ruby_ast = process(ast, document)
|
||||||
end
|
end
|
||||||
|
|
||||||
vars = variables_literal.assign(literal('nil'))
|
vars = variables_literal.assign(self.nil)
|
||||||
|
|
||||||
proc_ast = literal('lambda').add_block(document, vars) do
|
proc_ast = literal(:lambda).add_block(document, vars) do
|
||||||
if return_nodeset?(ast)
|
if return_nodeset?(ast)
|
||||||
input_assign = original_input_literal.assign(document)
|
input_assign = original_input_literal.assign(document)
|
||||||
|
|
||||||
|
@ -178,7 +178,7 @@ module Oga
|
||||||
# @return [Oga::Ruby::Node]
|
# @return [Oga::Ruby::Node]
|
||||||
def on_axis_attribute(ast, input)
|
def on_axis_attribute(ast, input)
|
||||||
input.is_a?(XML::Element).if_true do
|
input.is_a?(XML::Element).if_true do
|
||||||
attribute = literal('attribute')
|
attribute = literal(:attribute)
|
||||||
|
|
||||||
input.attributes.each.add_block(attribute) do
|
input.attributes.each.add_block(attribute) do
|
||||||
name_match = match_name_and_namespace(ast, attribute)
|
name_match = match_name_and_namespace(ast, attribute)
|
||||||
|
@ -277,10 +277,10 @@ module Oga
|
||||||
def on_axis_following_sibling(ast, input, &block)
|
def on_axis_following_sibling(ast, input, &block)
|
||||||
node = node_literal
|
node = node_literal
|
||||||
orig_input = original_input_literal
|
orig_input = original_input_literal
|
||||||
doc_node = literal('doc_node')
|
doc_node = literal(:doc_node)
|
||||||
check = literal('check')
|
check = literal(:check)
|
||||||
parent = literal('parent')
|
parent = literal(:parent)
|
||||||
root = literal('root')
|
root = literal(:root)
|
||||||
|
|
||||||
root_assign = orig_input.is_a?(XML::Node)
|
root_assign = orig_input.is_a?(XML::Node)
|
||||||
.if_true { root.assign(orig_input.parent) }
|
.if_true { root.assign(orig_input.parent) }
|
||||||
|
@ -288,13 +288,13 @@ module Oga
|
||||||
|
|
||||||
parent_if = input.is_a?(XML::Node).and(input.parent)
|
parent_if = input.is_a?(XML::Node).and(input.parent)
|
||||||
.if_true { parent.assign(input.parent) }
|
.if_true { parent.assign(input.parent) }
|
||||||
.else { parent.assign(literal('nil')) }
|
.else { parent.assign(self.nil) }
|
||||||
|
|
||||||
check_assign = check.assign(literal('false'))
|
check_assign = check.assign(self.false)
|
||||||
|
|
||||||
each_node = root.each_node.add_block(doc_node) do
|
each_node = root.each_node.add_block(doc_node) do
|
||||||
doc_compare = doc_node.eq(input).if_true do
|
doc_compare = doc_node.eq(input).if_true do
|
||||||
check.assign(literal('true'))
|
check.assign(self.true)
|
||||||
.followed_by(throw_message(:skip_children))
|
.followed_by(throw_message(:skip_children))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -323,19 +323,19 @@ module Oga
|
||||||
def on_axis_following(ast, input, &block)
|
def on_axis_following(ast, input, &block)
|
||||||
node = node_literal
|
node = node_literal
|
||||||
orig_input = original_input_literal
|
orig_input = original_input_literal
|
||||||
doc_node = literal('doc_node')
|
doc_node = literal(:doc_node)
|
||||||
check = literal('check')
|
check = literal(:check)
|
||||||
root = literal('root')
|
root = literal(:root)
|
||||||
|
|
||||||
root_assign = orig_input.is_a?(XML::Node)
|
root_assign = orig_input.is_a?(XML::Node)
|
||||||
.if_true { root.assign(orig_input.root_node) }
|
.if_true { root.assign(orig_input.root_node) }
|
||||||
.else { root.assign(orig_input) }
|
.else { root.assign(orig_input) }
|
||||||
|
|
||||||
check_assign = check.assign(literal('false'))
|
check_assign = check.assign(self.false)
|
||||||
|
|
||||||
each_node = root.each_node.add_block(doc_node) do
|
each_node = root.each_node.add_block(doc_node) do
|
||||||
doc_compare = doc_node.eq(input).if_true do
|
doc_compare = doc_node.eq(input).if_true do
|
||||||
check.assign(literal('true'))
|
check.assign(self.true)
|
||||||
.followed_by(throw_message(:skip_children))
|
.followed_by(throw_message(:skip_children))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -359,7 +359,7 @@ module Oga
|
||||||
# @param [Oga::Ruby::Node] input
|
# @param [Oga::Ruby::Node] input
|
||||||
# @return [Oga::Ruby::Node]
|
# @return [Oga::Ruby::Node]
|
||||||
def on_axis_namespace(ast, input)
|
def on_axis_namespace(ast, input)
|
||||||
underscore = literal('_')
|
underscore = literal(:_)
|
||||||
node = node_literal
|
node = node_literal
|
||||||
|
|
||||||
name = string(ast.children[1])
|
name = string(ast.children[1])
|
||||||
|
@ -377,9 +377,9 @@ module Oga
|
||||||
# @return [Oga::Ruby::Node]
|
# @return [Oga::Ruby::Node]
|
||||||
def on_axis_preceding(ast, input, &block)
|
def on_axis_preceding(ast, input, &block)
|
||||||
orig_input = original_input_literal
|
orig_input = original_input_literal
|
||||||
root = literal('root')
|
root = literal(:root)
|
||||||
node = node_literal
|
node = node_literal
|
||||||
doc_node = literal('doc_node')
|
doc_node = literal(:doc_node)
|
||||||
|
|
||||||
root_assign = orig_input.is_a?(XML::Node)
|
root_assign = orig_input.is_a?(XML::Node)
|
||||||
.if_true { root.assign(orig_input.root_node) }
|
.if_true { root.assign(orig_input.root_node) }
|
||||||
|
@ -403,21 +403,21 @@ module Oga
|
||||||
# @return [Oga::Ruby::Node]
|
# @return [Oga::Ruby::Node]
|
||||||
def on_axis_preceding_sibling(ast, input, &block)
|
def on_axis_preceding_sibling(ast, input, &block)
|
||||||
orig_input = original_input_literal
|
orig_input = original_input_literal
|
||||||
check = literal('check')
|
check = literal(:check)
|
||||||
root = literal('root')
|
root = literal(:root)
|
||||||
node = node_literal
|
node = node_literal
|
||||||
parent = literal('parent')
|
parent = literal(:parent)
|
||||||
doc_node = literal('doc_node')
|
doc_node = literal(:doc_node)
|
||||||
|
|
||||||
root_assign = orig_input.is_a?(XML::Node)
|
root_assign = orig_input.is_a?(XML::Node)
|
||||||
.if_true { root.assign(orig_input.parent) }
|
.if_true { root.assign(orig_input.parent) }
|
||||||
.else { root.assign(orig_input) }
|
.else { root.assign(orig_input) }
|
||||||
|
|
||||||
check_assign = check.assign(literal('false'))
|
check_assign = check.assign(self.false)
|
||||||
|
|
||||||
parent_if = input.is_a?(XML::Node).and(input.parent)
|
parent_if = input.is_a?(XML::Node).and(input.parent)
|
||||||
.if_true { parent.assign(input.parent) }
|
.if_true { parent.assign(input.parent) }
|
||||||
.else { parent.assign(literal('nil')) }
|
.else { parent.assign(self.nil) }
|
||||||
|
|
||||||
each_node = root.each_node.add_block(doc_node) do
|
each_node = root.each_node.add_block(doc_node) do
|
||||||
compare = doc_node.eq(input).if_true { send_message(:break) }
|
compare = doc_node.eq(input).if_true { send_message(:break) }
|
||||||
|
@ -460,7 +460,7 @@ module Oga
|
||||||
def on_index_predicate(test, predicate, input)
|
def on_index_predicate(test, predicate, input)
|
||||||
int1 = literal('1')
|
int1 = literal('1')
|
||||||
index = to_int(predicate)
|
index = to_int(predicate)
|
||||||
index_var = literal('index')
|
index_var = literal(:index)
|
||||||
|
|
||||||
inner = process(test, input) do |matched_test_node|
|
inner = process(test, input) do |matched_test_node|
|
||||||
index_var.eq(index).if_true { yield matched_test_node }
|
index_var.eq(index).if_true { yield matched_test_node }
|
||||||
|
@ -497,7 +497,7 @@ module Oga
|
||||||
process(test, input) do |matched_test_node|
|
process(test, input) do |matched_test_node|
|
||||||
catch_block = catch_message(:predicate_matched) do
|
catch_block = catch_message(:predicate_matched) do
|
||||||
process(predicate, matched_test_node) do
|
process(predicate, matched_test_node) do
|
||||||
throw_message(:predicate_matched, literal('true'))
|
throw_message(:predicate_matched, self.true)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -521,7 +521,7 @@ module Oga
|
||||||
# @see [#operator]
|
# @see [#operator]
|
||||||
#
|
#
|
||||||
def on_eq(ast, input, &block)
|
def on_eq(ast, input, &block)
|
||||||
conversion = literal('Conversion')
|
conversion = literal(XPath::Conversion)
|
||||||
|
|
||||||
operator(ast, input) do |left, right|
|
operator(ast, input) do |left, right|
|
||||||
compatible_assign = mass_assign(
|
compatible_assign = mass_assign(
|
||||||
|
@ -542,7 +542,7 @@ module Oga
|
||||||
# @see [#operator]
|
# @see [#operator]
|
||||||
#
|
#
|
||||||
def on_neq(ast, input, &block)
|
def on_neq(ast, input, &block)
|
||||||
conversion = literal('Conversion')
|
conversion = literal(XPath::Conversion)
|
||||||
|
|
||||||
operator(ast, input) do |left, right|
|
operator(ast, input) do |left, right|
|
||||||
compatible_assign = mass_assign(
|
compatible_assign = mass_assign(
|
||||||
|
@ -559,7 +559,7 @@ module Oga
|
||||||
|
|
||||||
OPERATORS.each do |callback, (conv_method, ruby_method)|
|
OPERATORS.each do |callback, (conv_method, ruby_method)|
|
||||||
define_method(callback) do |ast, input, &block|
|
define_method(callback) do |ast, input, &block|
|
||||||
conversion = literal('Conversion')
|
conversion = literal(XPath::Conversion)
|
||||||
|
|
||||||
operator(ast, input) do |left, right|
|
operator(ast, input) do |left, right|
|
||||||
lval = conversion.__send__(conv_method, left)
|
lval = conversion.__send__(conv_method, left)
|
||||||
|
@ -584,8 +584,8 @@ module Oga
|
||||||
def on_pipe(ast, input, &block)
|
def on_pipe(ast, input, &block)
|
||||||
left, right = *ast
|
left, right = *ast
|
||||||
|
|
||||||
union = unique_literal('union')
|
union = unique_literal(:union)
|
||||||
conversion = literal('Conversion')
|
conversion = literal(XPath::Conversion)
|
||||||
|
|
||||||
left_push = process(left, input) do |node|
|
left_push = process(left, input) do |node|
|
||||||
union << node
|
union << node
|
||||||
|
@ -638,7 +638,7 @@ module Oga
|
||||||
name = ast.children[0]
|
name = ast.children[0]
|
||||||
|
|
||||||
variables_literal.and(variables_literal[string(name)])
|
variables_literal.and(variables_literal[string(name)])
|
||||||
.or(send_message('raise', string("Undefined XPath variable: #{name}")))
|
.or(send_message(:raise, string("Undefined XPath variable: #{name}")))
|
||||||
end
|
end
|
||||||
|
|
||||||
##
|
##
|
||||||
|
@ -662,7 +662,7 @@ module Oga
|
||||||
# @return [Oga::Ruby::Node]
|
# @return [Oga::Ruby::Node]
|
||||||
#
|
#
|
||||||
def on_call_true(*)
|
def on_call_true(*)
|
||||||
literal('true')
|
self.true
|
||||||
end
|
end
|
||||||
|
|
||||||
##
|
##
|
||||||
|
@ -671,7 +671,7 @@ module Oga
|
||||||
# @return [Oga::Ruby::Node]
|
# @return [Oga::Ruby::Node]
|
||||||
#
|
#
|
||||||
def on_call_false(*)
|
def on_call_false(*)
|
||||||
literal('false')
|
self.false
|
||||||
end
|
end
|
||||||
|
|
||||||
##
|
##
|
||||||
|
@ -729,8 +729,6 @@ module Oga
|
||||||
input.is_a?(XML::Node).or(input.is_a?(XML::Document))
|
input.is_a?(XML::Node).or(input.is_a?(XML::Document))
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
# @param [#to_s] value
|
# @param [#to_s] value
|
||||||
# @return [Oga::Ruby::Node]
|
# @return [Oga::Ruby::Node]
|
||||||
def literal(value)
|
def literal(value)
|
||||||
|
@ -761,7 +759,22 @@ module Oga
|
||||||
# @param [Array] args
|
# @param [Array] args
|
||||||
# @return [Oga::Ruby::Node]
|
# @return [Oga::Ruby::Node]
|
||||||
def send_message(name, *args)
|
def send_message(name, *args)
|
||||||
Ruby::Node.new(:send, [nil, name, *args])
|
Ruby::Node.new(:send, [nil, name.to_s, *args])
|
||||||
|
end
|
||||||
|
|
||||||
|
# @return [Oga::Ruby::Node]
|
||||||
|
def nil
|
||||||
|
@nil ||= literal(:nil)
|
||||||
|
end
|
||||||
|
|
||||||
|
# @return [Oga::Ruby::Node]
|
||||||
|
def true
|
||||||
|
@true ||= literal(:true)
|
||||||
|
end
|
||||||
|
|
||||||
|
# @return [Oga::Ruby::Node]
|
||||||
|
def false
|
||||||
|
@false ||= literal(:false)
|
||||||
end
|
end
|
||||||
|
|
||||||
# @param [Oga::Ruby::Node] node
|
# @param [Oga::Ruby::Node] node
|
||||||
|
@ -871,8 +884,8 @@ module Oga
|
||||||
def operator(ast, input, optimize_first = true)
|
def operator(ast, input, optimize_first = true)
|
||||||
left, right = *ast
|
left, right = *ast
|
||||||
|
|
||||||
left_var = unique_literal('op_left')
|
left_var = unique_literal(:op_left)
|
||||||
right_var = unique_literal('op_right')
|
right_var = unique_literal(:op_right)
|
||||||
|
|
||||||
if return_nodeset?(left) and optimize_first
|
if return_nodeset?(left) and optimize_first
|
||||||
left_ast = match_first_node(left, input)
|
left_ast = match_first_node(left, input)
|
||||||
|
@ -908,7 +921,7 @@ module Oga
|
||||||
# @return [Oga::Ruby::Node]
|
# @return [Oga::Ruby::Node]
|
||||||
#
|
#
|
||||||
def backup_variable(variable, new)
|
def backup_variable(variable, new)
|
||||||
backup = unique_literal('backup')
|
backup = unique_literal(:backup)
|
||||||
|
|
||||||
backup.assign(variable)
|
backup.assign(variable)
|
||||||
.followed_by(variable.assign(new))
|
.followed_by(variable.assign(new))
|
||||||
|
@ -918,22 +931,22 @@ module Oga
|
||||||
|
|
||||||
# @return [Oga::Ruby::Node]
|
# @return [Oga::Ruby::Node]
|
||||||
def matched_literal
|
def matched_literal
|
||||||
literal('matched')
|
literal(:matched)
|
||||||
end
|
end
|
||||||
|
|
||||||
# @return [Oga::Ruby::Node]
|
# @return [Oga::Ruby::Node]
|
||||||
def node_literal
|
def node_literal
|
||||||
literal('node')
|
literal(:node)
|
||||||
end
|
end
|
||||||
|
|
||||||
# @return [Oga::Ruby::Node]
|
# @return [Oga::Ruby::Node]
|
||||||
def original_input_literal
|
def original_input_literal
|
||||||
literal('original_input')
|
literal(:original_input)
|
||||||
end
|
end
|
||||||
|
|
||||||
# @return [Oga::Ruby::Node]
|
# @return [Oga::Ruby::Node]
|
||||||
def variables_literal
|
def variables_literal
|
||||||
literal('variables')
|
literal(:variables)
|
||||||
end
|
end
|
||||||
|
|
||||||
# @param [AST::Node] ast
|
# @param [AST::Node] ast
|
||||||
|
@ -966,10 +979,10 @@ module Oga
|
||||||
# @param [Symbol] name
|
# @param [Symbol] name
|
||||||
# @return [Oga::Ruby::Node]
|
# @return [Oga::Ruby::Node]
|
||||||
def catch_message(name)
|
def catch_message(name)
|
||||||
send_message('catch', symbol(name)).add_block do
|
send_message(:catch, symbol(name)).add_block do
|
||||||
# Ensure that the "catch" only returns a value when "throw" is
|
# Ensure that the "catch" only returns a value when "throw" is
|
||||||
# actually invoked.
|
# actually invoked.
|
||||||
yield.followed_by(literal('nil'))
|
yield.followed_by(self.nil)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -977,7 +990,7 @@ module Oga
|
||||||
# @param [Array] args
|
# @param [Array] args
|
||||||
# @return [Oga::Ruby::Node]
|
# @return [Oga::Ruby::Node]
|
||||||
def throw_message(name, *args)
|
def throw_message(name, *args)
|
||||||
send_message('throw', symbol(name), *args)
|
send_message(:throw, symbol(name), *args)
|
||||||
end
|
end
|
||||||
|
|
||||||
# @param [AST::Node] ast
|
# @param [AST::Node] ast
|
||||||
|
|
Loading…
Reference in New Issue