Allow followed_by to take a block

This removes the need for a lot of local variables in the Compiler
class, at the cost of some extra indentation levels.
This commit is contained in:
Yorick Peterse 2015-08-11 21:04:27 +02:00
parent 7bcd462e22
commit 54473d9865
3 changed files with 198 additions and 230 deletions

View File

@ -165,7 +165,9 @@ module Oga
# @param [Oga::Ruby::Node] other # @param [Oga::Ruby::Node] other
# @return [Oga::Ruby::Node] # @return [Oga::Ruby::Node]
# #
def followed_by(other) def followed_by(other = nil)
other = yield if ::Kernel.block_given?
Node.new(:followed_by, [self, other]) Node.new(:followed_by, [self, other])
end end

View File

@ -199,13 +199,14 @@ module Oga
# @return [Oga::Ruby::Node] # @return [Oga::Ruby::Node]
def on_axis_ancestor_or_self(ast, input, &block) def on_axis_ancestor_or_self(ast, input, &block)
parent = node_literal parent = node_literal
self_test = process(ast, input, &block).if_true { yield input }
ancestors_test = input.each_ancestor.add_block(parent) do process(ast, input, &block)
.if_true { yield input }
.followed_by do
input.each_ancestor.add_block(parent) do
process(ast, parent, &block).if_true { yield parent } process(ast, parent, &block).if_true { yield parent }
end end
end
self_test.followed_by(ancestors_test)
end end
# @param [AST::Node] ast # @param [AST::Node] ast
@ -226,13 +227,13 @@ module Oga
node = node_literal node = node_literal
backup_variable(node, input) do backup_variable(node, input) do
self_test = process(ast, node, &block).if_true { yield node } process(ast, node, &block)
.if_true { yield node }
descendants_test = node.each_node.add_block(node) do .followed_by do
node.each_node.add_block(node) do
process(ast, node, &block).if_true { yield node } process(ast, node, &block).if_true { yield node }
end end
end
self_test.followed_by(descendants_test)
end end
end end
@ -284,39 +285,36 @@ module Oga
parent = literal(:parent) parent = literal(:parent)
root = literal(:root) root = literal(:root)
root_assign = orig_input.is_a?(XML::Node) 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) }
.followed_by do
parent_if = input.is_a?(XML::Node).and(input.parent) input.is_a?(XML::Node).and(input.parent)
.if_true { parent.assign(input.parent) } .if_true { parent.assign(input.parent) }
.else { parent.assign(self.nil) } .else { parent.assign(self.nil) }
end
check_assign = check.assign(self.false) .followed_by(check.assign(self.false))
.followed_by do
each_node = root.each_node.add_block(doc_node) do root.each_node.add_block(doc_node) do
doc_compare = doc_node.eq(input).if_true do doc_node.eq(input)
.if_true do
check.assign(self.true) check.assign(self.true)
.followed_by(throw_message(:skip_children)) .followed_by(throw_message(:skip_children))
end end
.followed_by do
next_check = check.not.or(parent != doc_node.parent).if_true do check.not.or(parent != doc_node.parent).if_true do
send_message(:next) send_message(:next)
end end
end
match_node = backup_variable(node, doc_node) do .followed_by do
backup_variable(node, doc_node) do
process(ast, node, &block).if_true do process(ast, node, &block).if_true do
yield(node).followed_by(throw_message(:skip_children)) yield(node).followed_by(throw_message(:skip_children))
end end
end end
doc_compare.followed_by(next_check)
.followed_by(match_node)
end end
end
root_assign.followed_by(parent_if) end
.followed_by(check_assign)
.followed_by(each_node)
end end
# @param [AST::Node] ast # @param [AST::Node] ast
@ -329,32 +327,29 @@ module Oga
check = literal(:check) check = literal(:check)
root = literal(:root) root = literal(:root)
root_assign = orig_input.is_a?(XML::Node) 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) }
.followed_by(check.assign(self.false))
check_assign = check.assign(self.false) .followed_by do
root.each_node.add_block(doc_node) do
each_node = root.each_node.add_block(doc_node) do doc_node.eq(input)
doc_compare = doc_node.eq(input).if_true do .if_true do
check.assign(self.true) check.assign(self.true)
.followed_by(throw_message(:skip_children)) .followed_by(throw_message(:skip_children))
end end
.followed_by do
next_check = check.not.if_true { send_message(:next) } check.not.if_true { send_message(:next) }
end
match_node = backup_variable(node, doc_node) do .followed_by do
backup_variable(node, doc_node) do
process(ast, node, &block).if_true do process(ast, node, &block).if_true do
yield node yield node
end end
end end
doc_compare.followed_by(next_check)
.followed_by(match_node)
end end
end
root_assign.followed_by(check_assign) end
.followed_by(each_node)
end end
# @param [AST::Node] ast # @param [AST::Node] ast
@ -383,21 +378,19 @@ module Oga
node = node_literal node = node_literal
doc_node = literal(:doc_node) doc_node = literal(:doc_node)
root_assign = orig_input.is_a?(XML::Node) 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) }
.followed_by do
each_node = root.each_node.add_block(doc_node) do root.each_node.add_block(doc_node) do
compare = doc_node.eq(input).if_true { send_message(:break) } doc_node.eq(input).if_true { send_message(:break) }
.followed_by do
match = backup_variable(node, doc_node) do backup_variable(node, doc_node) do
process(ast, node, &block).if_true { yield node } process(ast, node, &block).if_true { yield node }
end end
compare.followed_by(match)
end end
end
root_assign.followed_by(each_node) end
end end
# @param [AST::Node] ast # @param [AST::Node] ast
@ -411,31 +404,27 @@ module Oga
parent = literal(:parent) parent = literal(:parent)
doc_node = literal(:doc_node) doc_node = literal(:doc_node)
root_assign = orig_input.is_a?(XML::Node) 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) }
.followed_by(check.assign(self.false))
check_assign = check.assign(self.false) .followed_by do
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(self.nil) } .else { parent.assign(self.nil) }
end
each_node = root.each_node.add_block(doc_node) do .followed_by do
compare = doc_node.eq(input).if_true { send_message(:break) } root.each_node.add_block(doc_node) do
doc_node.eq(input).if_true { send_message(:break) }
match = doc_node.parent.eq(parent).if_true do .followed_by do
doc_node.parent.eq(parent).if_true do
backup_variable(node, doc_node) do backup_variable(node, doc_node) do
process(ast, node, &block).if_true { yield node } process(ast, node, &block).if_true { yield node }
end end
end end
compare.followed_by(match)
end end
end
root_assign.followed_by(check_assign) end
.followed_by(parent_if)
.followed_by(each_node)
end end
# @param [AST::Node] ast # @param [AST::Node] ast
@ -464,12 +453,14 @@ module Oga
index = to_int(predicate) index = to_int(predicate)
index_var = literal(:index) index_var = literal(:index)
inner = process(test, input) do |matched_test_node| index_var.assign(int1)
index_var.eq(index).if_true { yield matched_test_node } .followed_by do
process(test, input) do |matched_test_node|
index_var.eq(index)
.if_true { yield matched_test_node }
.followed_by(index_var.assign(index_var + int1)) .followed_by(index_var.assign(index_var + int1))
end end
end
index_var.assign(int1).followed_by(inner)
end end
## ##
@ -523,18 +514,15 @@ module Oga
# @see [#operator] # @see [#operator]
# #
def on_eq(ast, input, &block) def on_eq(ast, input, &block)
conversion = literal(XPath::Conversion) conv = literal(Conversion)
operator(ast, input) do |left, right| operator(ast, input) do |left, right|
compatible_assign = mass_assign( mass_assign([left, right], conv.to_compatible_types(left, right))
[left, right], .followed_by do
conversion.to_compatible_types(left, right)
)
operation = left.eq(right) operation = left.eq(right)
operation = operation.if_true(&block) if block # In a predicate
compatible_assign.followed_by(operation) block ? operation.if_true(&block) : operation
end
end end
end end
@ -544,18 +532,15 @@ module Oga
# @see [#operator] # @see [#operator]
# #
def on_neq(ast, input, &block) def on_neq(ast, input, &block)
conversion = literal(XPath::Conversion) conv = literal(Conversion)
operator(ast, input) do |left, right| operator(ast, input) do |left, right|
compatible_assign = mass_assign( mass_assign([left, right], conv.to_compatible_types(left, right))
[left, right], .followed_by do
conversion.to_compatible_types(left, right)
)
operation = left != right operation = left != right
operation = operation.if_true(&block) if block # In a predicate
compatible_assign.followed_by(operation) block ? operation.if_true(&block) : operation
end
end end
end end
@ -568,12 +553,7 @@ module Oga
rval = conversion.__send__(conv_method, right) rval = conversion.__send__(conv_method, right)
operation = lval.__send__(ruby_method, rval) operation = lval.__send__(ruby_method, rval)
# In a predicate block ? conversion.to_boolean(operation).if_true(&block) : operation
if block
operation = conversion.to_boolean(operation).if_true(&block)
end
operation
end end
end end
end end
@ -587,28 +567,15 @@ module Oga
left, right = *ast left, right = *ast
union = unique_literal(:union) union = unique_literal(:union)
conversion = literal(XPath::Conversion) conversion = literal(Conversion)
left_push = process(left, input) do |node| union.assign(literal(XML::NodeSet).new)
union << node .followed_by(process(left, input) { |node| union << node })
.followed_by(process(right, input) { |node| union << node })
.followed_by do
# block present means we're in a predicate
block ? conversion.to_boolean(union).if_true(&block) : union
end end
right_push = process(right, input) do |node|
union << node
end
push_ast = union.assign(literal(XML::NodeSet).new)
.followed_by(left_push)
.followed_by(right_push)
# In a predicate
if block
final = conversion.to_boolean(union).if_true(&block)
else
final = union
end
push_ast.followed_by(final)
end end
# @param [AST::Node] ast # @param [AST::Node] ast
@ -688,15 +655,13 @@ module Oga
call_arg = unique_literal(:call_arg) call_arg = unique_literal(:call_arg)
conversion = literal(Conversion) conversion = literal(Conversion)
initial_assign = call_arg.assign(arg_ast) call_arg.assign(arg_ast)
float_assign = call_arg.assign(conversion.to_float(call_arg)) .followed_by do
call_arg.assign(conversion.to_float(call_arg))
if_nan = call_arg.nan? end
.if_true { call_arg } .followed_by do
.else { call_arg.ceil.to_f } call_arg.nan?.if_true { call_arg }.else { call_arg.ceil.to_f }
end
initial_assign.followed_by(float_assign)
.followed_by(if_nan)
end end
# @param [Oga::Ruby::Node] input # @param [Oga::Ruby::Node] input
@ -707,15 +672,13 @@ module Oga
call_arg = unique_literal(:call_arg) call_arg = unique_literal(:call_arg)
conversion = literal(Conversion) conversion = literal(Conversion)
initial_assign = call_arg.assign(arg_ast) call_arg.assign(arg_ast)
float_assign = call_arg.assign(conversion.to_float(call_arg)) .followed_by do
call_arg.assign(conversion.to_float(call_arg))
if_nan = call_arg.nan? end
.if_true { call_arg } .followed_by do
.else { call_arg.floor.to_f } call_arg.nan?.if_true { call_arg }.else { call_arg.floor.to_f }
end
initial_assign.followed_by(float_assign)
.followed_by(if_nan)
end end
# @param [Oga::Ruby::Node] input # @param [Oga::Ruby::Node] input
@ -726,15 +689,13 @@ module Oga
call_arg = unique_literal(:call_arg) call_arg = unique_literal(:call_arg)
conversion = literal(Conversion) conversion = literal(Conversion)
initial_assign = call_arg.assign(arg_ast) call_arg.assign(arg_ast)
float_assign = call_arg.assign(conversion.to_float(call_arg)) .followed_by do
call_arg.assign(conversion.to_float(call_arg))
if_nan = call_arg.nan? end
.if_true { call_arg } .followed_by do
.else { call_arg.round.to_f } call_arg.nan?.if_true { call_arg }.else { call_arg.round.to_f }
end
initial_assign.followed_by(float_assign)
.followed_by(if_nan)
end end
# @param [Oga::Ruby::Node] input # @param [Oga::Ruby::Node] input
@ -766,15 +727,14 @@ module Oga
needle_lit = unique_literal(:needle) needle_lit = unique_literal(:needle)
conversion = literal(Conversion) conversion = literal(Conversion)
haystack_ast = try_match_first_node(haystack, input) haystack_lit.assign(try_match_first_node(haystack, input))
needle_ast = try_match_first_node(needle, input) .followed_by do
needle_lit.assign(try_match_first_node(needle, input))
include_call = conversion.to_string(haystack_lit) end
.followed_by do
conversion.to_string(haystack_lit)
.include?(conversion.to_string(needle_lit)) .include?(conversion.to_string(needle_lit))
end
haystack_lit.assign(haystack_ast)
.followed_by(needle_lit.assign(needle_ast))
.followed_by(include_call)
end end
# @param [Oga::Ruby::Node] input # @param [Oga::Ruby::Node] input
@ -782,17 +742,16 @@ module Oga
# @return [Oga::Ruby::Node] # @return [Oga::Ruby::Node]
def on_call_count(input, arg) def on_call_count(input, arg)
count = unique_literal(:count) count = unique_literal(:count)
assign = count.assign(literal('0.0')) assign =
unless return_nodeset?(arg) unless return_nodeset?(arg)
raise TypeError, 'count() can only operate on NodeSet instances' raise TypeError, 'count() can only operate on NodeSet instances'
end end
increment = process(arg, input) do count.assign(literal('0.0'))
count.assign(count + literal('1')) .followed_by do
process(arg, input) { count.assign(count + literal('1')) }
end end
assign.followed_by(increment)
.followed_by(count) .followed_by(count)
end end
@ -822,39 +781,36 @@ module Oga
id_str_var = unique_literal('id_string') id_str_var = unique_literal('id_string')
attr_var = unique_literal('attr') attr_var = unique_literal('attr')
matched_assign = matched.assign(literal(XML::NodeSet).new) matched.assign(literal(XML::NodeSet).new)
.followed_by do
# When using some sort of path we'll want the text of all matched nodes. # When using some sort of path we'll want the text of all matched
# nodes.
if return_nodeset?(arg) if return_nodeset?(arg)
id_assign = ids_var.assign(literal(:[])) ids_var.assign(literal(:[])).followed_by do
.followed_by(process(arg, input) { |node| ids_var << node.text }) process(arg, input) { |node| ids_var << node.text }
end
# For everything else we'll cast the value to a string and split it on # For everything else we'll cast the value to a string and split it
# every space. # on every space.
else else
conversion = literal(Conversion).to_string(ids_var).split(string(' ')) conversion = literal(Conversion).to_string(ids_var)
.split(string(' '))
id_assign = ids_var.assign(process(arg, input)) ids_var.assign(process(arg, input))
.followed_by(ids_var.assign(conversion)) .followed_by(ids_var.assign(conversion))
end end
end
id_str_assign = id_str_var.assign(string('id')) .followed_by(id_str_var.assign(string('id')))
.followed_by do
each_node = orig_input.each_node.add_block(node) do orig_input.each_node.add_block(node) do
node.is_a?(XML::Element).if_true do node.is_a?(XML::Element).if_true do
assign = attr_var.assign(node.attribute(id_str_var)) attr_var.assign(node.attribute(id_str_var)).followed_by do
attr_var.and(ids_var.include?(attr_var.value))
compare = attr_var.and(ids_var.include?(attr_var.value)).if_true do .if_true { matched << node }
matched << node end
end end
assign.followed_by(compare)
end end
end end
matched_assign.followed_by(id_assign)
.followed_by(id_str_assign)
.followed_by(each_node)
.followed_by(matched) .followed_by(matched)
end end
@ -1089,10 +1045,7 @@ module Oga
initial_assign = left_var.assign(left_ast.wrap) initial_assign = left_var.assign(left_ast.wrap)
.followed_by(right_var.assign(right_ast.wrap)) .followed_by(right_var.assign(right_ast.wrap))
.followed_by { yield left_var, right_var }
blockval = yield left_var, right_var
initial_assign.followed_by(blockval)
end end
## ##

View File

@ -143,6 +143,7 @@ describe Oga::Ruby::Node do
end end
describe '#followed_by' do describe '#followed_by' do
describe 'without a block' do
it 'returns a Node chaining two nodes together' do it 'returns a Node chaining two nodes together' do
node1 = described_class.new(:lit, %w{A}) node1 = described_class.new(:lit, %w{A})
node2 = described_class.new(:lit, %w{B}) node2 = described_class.new(:lit, %w{B})
@ -153,6 +154,18 @@ describe Oga::Ruby::Node do
end end
end end
describe 'with a block' do
it 'returns a Node chaining two nodes together' do
node1 = described_class.new(:lit, %w{A})
node2 = described_class.new(:lit, %w{B})
joined = node1.followed_by { node2 }
joined.type.should == :followed_by
joined.to_a.should == [node1, node2]
end
end
end
describe '#method_missing' do describe '#method_missing' do
it 'returns a send Node' do it 'returns a send Node' do
receiver = described_class.new(:lit, %w{foo}) receiver = described_class.new(:lit, %w{foo})