Class: Oga::XPath::Compiler

Inherits:
Object
  • Object
show all
Defined in:
lib/oga/xpath/compiler.rb

Overview

Compiling of XPath ASTs into Ruby code.

The Compiler class can be used to turn an XPath AST into Ruby source code that can be executed to match XML nodes in a given input document/element. Compiled source code is cached per expression, removing the need for recompiling the same expression over and over again.

Constant Summary collapse

CACHE =

Returns:

LRU.new
CONTEXT =

Context for compiled Procs. As compiled Procs do not mutate the enclosing environment we can just re-use the same instance without synchronization.

Context.new
STAR =

Wildcard for node names/namespace prefixes.

'*'
RETURN_NODESET =

Node types that require a NodeSet to push nodes into.

[:path, :absolute_path, :axis, :predicate]
OPERATORS =

Hash containing all operator callbacks, the conversion methods and the Ruby methods to use.

{
  :on_add => [:to_float, :+],
  :on_sub => [:to_float, :-],
  :on_div => [:to_float, :/],
  :on_gt  => [:to_float, :>],
  :on_gte => [:to_float, :>=],
  :on_lt  => [:to_float, :<],
  :on_lte => [:to_float, :<=],
  :on_mul => [:to_float, :*],
  :on_mod => [:to_float, :%],
  :on_and => [:to_boolean, :and],
  :on_or  => [:to_boolean, :or]
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(namespaces: nil) ⇒ Compiler

Returns a new instance of Compiler

Parameters:

  • namespaces (Hash)


51
52
53
54
55
# File 'lib/oga/xpath/compiler.rb', line 51

def initialize(namespaces: nil)
  reset

  @namespaces = namespaces
end

Class Method Details

.compile_with_cache(ast, namespaces: nil) ⇒ Object

Compiles and caches an AST.

See Also:

  • [[#compile]


45
46
47
48
# File 'lib/oga/xpath/compiler.rb', line 45

def self.compile_with_cache(ast, namespaces: nil)
  cache_key = namespaces ? [ast, namespaces] : ast
  CACHE.get_or_set(cache_key) { new(namespaces: namespaces).compile(ast) }
end

Instance Method Details

#argument_or_first_node(input, arg = nil) ⇒ Oga::Ruby::Node

Parameters:

Returns:



1450
1451
1452
1453
1454
1455
# File 'lib/oga/xpath/compiler.rb', line 1450

def argument_or_first_node(input, arg = nil)
  arg_ast = arg ? try_match_first_node(arg, input) : input
  arg_var = unique_literal(:argument_or_first_node)

  arg_var.assign(arg_ast).followed_by { yield arg_var }
end

#attribute_or_node(node) ⇒ Oga::Ruby::Node

Parameters:

Returns:



1366
1367
1368
# File 'lib/oga/xpath/compiler.rb', line 1366

def attribute_or_node(node)
  node.is_a?(XML::Attribute).or(node.is_a?(XML::Node))
end

#breakOga::Ruby::Node

Returns:



1547
1548
1549
# File 'lib/oga/xpath/compiler.rb', line 1547

def break
  send_message(:break)
end

#catch_message(name) ⇒ Oga::Ruby::Node

Parameters:

  • name (Symbol)

Returns:



1531
1532
1533
1534
1535
1536
1537
# File 'lib/oga/xpath/compiler.rb', line 1531

def catch_message(name)
  send_message(:catch, symbol(name)).add_block do
    # Ensure that the "catch" only returns a value when "throw" is
    # actually invoked.
    yield.followed_by(self.nil)
  end
end

#compile(ast) ⇒ Proc

Compiles an XPath AST into a Ruby Proc.

Parameters:

  • ast (AST::Node)

Returns:

  • (Proc)


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/oga/xpath/compiler.rb', line 69

def compile(ast)
  document = literal(:node)
  matched  = matched_literal

  if return_nodeset?(ast)
    ruby_ast = process(ast, document) { |node| matched.push(node) }
  else
    ruby_ast = process(ast, document)
  end

  vars = variables_literal.assign(self.nil)

  proc_ast = literal(:lambda).add_block(document, vars) do
    input_assign = original_input_literal.assign(document)

    if return_nodeset?(ast)
      body = matched.assign(literal(XML::NodeSet).new)
        .followed_by(ruby_ast)
        .followed_by(matched)
    else
      body = ruby_ast
    end

    input_assign.followed_by(body)
  end

  generator = Ruby::Generator.new
  source    = generator.process(proc_ast)

  CONTEXT.evaluate(source)
ensure
  reset
end

#document_or_node(node) ⇒ Oga::Ruby::Node

Parameters:

Returns:



1372
1373
1374
# File 'lib/oga/xpath/compiler.rb', line 1372

def document_or_node(node)
  node.is_a?(XML::Document).or(node.is_a?(XML::Node))
end

#element_or_attribute(node) ⇒ Oga::Ruby::Node

Parameters:

Returns:



1360
1361
1362
# File 'lib/oga/xpath/compiler.rb', line 1360

def element_or_attribute(node)
  node.is_a?(XML::Element).or(node.is_a?(XML::Attribute))
end

#ensure_element_or_attribute(input) ⇒ Oga::Ruby::Node

Parameters:

Returns:



1441
1442
1443
1444
1445
# File 'lib/oga/xpath/compiler.rb', line 1441

def ensure_element_or_attribute(input)
  element_or_attribute(input).if_false do
    raise_message(TypeError, 'argument is not an Element or Attribute')
  end
end

#falseOga::Ruby::Node

Returns:



1354
1355
1356
# File 'lib/oga/xpath/compiler.rb', line 1354

def false
  @false ||= literal(:false)
end

#has_call_node?(ast, name) ⇒ TrueClass|FalseClass

Parameters:

  • ast (AST::Node)

Returns:

  • (TrueClass|FalseClass)


1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
# File 'lib/oga/xpath/compiler.rb', line 1559

def has_call_node?(ast, name)
  visit = [ast]

  until visit.empty?
    current = visit.pop

    return true if current.type == :call && current.children[0] == name

    current.children.each do |child|
      visit << child if child.is_a?(AST::Node)
    end
  end

  false
end

#literal(value) ⇒ Oga::Ruby::Node

Parameters:

  • value (#to_s)

Returns:



1298
1299
1300
# File 'lib/oga/xpath/compiler.rb', line 1298

def literal(value)
  Ruby::Node.new(:lit, [value.to_s])
end

#mass_assign(vars, value) ⇒ Oga::Ruby::Node

Parameters:

Returns:



1513
1514
1515
# File 'lib/oga/xpath/compiler.rb', line 1513

def mass_assign(vars, value)
  Ruby::Node.new(:massign, [vars, value])
end

#match_first_node(ast, input) ⇒ Oga::Ruby::Node

Returns an AST matching the first node of a node set.

Parameters:

Returns:



1420
1421
1422
1423
1424
1425
1426
# File 'lib/oga/xpath/compiler.rb', line 1420

def match_first_node(ast, input)
  catch_message(:value) do
    process(ast, input) do |node|
      throw_message(:value, node)
    end
  end
end

#match_name_and_namespace(ast, input) ⇒ Oga::Ruby::Node

Parameters:

Returns:



1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
# File 'lib/oga/xpath/compiler.rb', line 1379

def match_name_and_namespace(ast, input)
  ns, name = *ast

  condition = nil
  name_str  = string(name)
  zero      = literal(0)

  if name != STAR
    condition = input.name.eq(name_str)
      .or(input.name.casecmp(name_str).eq(zero))
  end

  if ns and ns != STAR
    if @namespaces
      ns_uri = @namespaces[ns]
      ns_match =
        if ns_uri
          input.namespace.and(input.namespace.uri.eq(string(ns_uri)))
        else
          self.false
        end
    else
      ns_match =
        if ns == XML::Element::XMLNS_PREFIX
          input
        else
          input.namespace_name.eq(string(ns))
        end
    end

    condition = condition ? condition.and(ns_match) : ns_match
  end

  condition
end

#matched_literalOga::Ruby::Node

Returns:



1490
1491
1492
# File 'lib/oga/xpath/compiler.rb', line 1490

def matched_literal
  literal(:matched)
end

#nilOga::Ruby::Node

Returns:



1344
1345
1346
# File 'lib/oga/xpath/compiler.rb', line 1344

def nil
  @nil ||= literal(:nil)
end

#number?(ast) ⇒ TrueClass|FalseClass

Parameters:

  • ast (AST::Node)

Returns:

  • (TrueClass|FalseClass)


1519
1520
1521
# File 'lib/oga/xpath/compiler.rb', line 1519

def number?(ast)
  ast.type == :int || ast.type == :float
end

#on_absolute_path(ast, input, &block) ⇒ Oga::Ruby::Node

Parameters:

Returns:



115
116
117
118
119
120
121
# File 'lib/oga/xpath/compiler.rb', line 115

def on_absolute_path(ast, input, &block)
  if ast.children.empty?
    matched_literal.push(input.root_node)
  else
    process(ast.children[0], input.root_node, &block)
  end
end

#on_axis(ast, input, &block) ⇒ Oga::Ruby::Node

Dispatches the processing of axes to dedicated methods. This works similar to #process except the handler names are “on_axis_X” with “X” being the axis name.

Parameters:

Returns:



130
131
132
133
134
135
136
137
138
# File 'lib/oga/xpath/compiler.rb', line 130

def on_axis(ast, input, &block)
  name, test, following = *ast

  handler = name.gsub('-', '_')

  send(:"on_axis_#{handler}", test, input) do |matched|
    process_following_or_yield(following, matched, &block)
  end
end

#on_axis_ancestor(ast, input) ⇒ Oga::Ruby::Node

Parameters:

Returns:



192
193
194
195
196
197
198
199
200
# File 'lib/oga/xpath/compiler.rb', line 192

def on_axis_ancestor(ast, input)
  parent = unique_literal(:parent)

  attribute_or_node(input).if_true do
    input.each_ancestor.add_block(parent) do
      process(ast, parent).if_true { yield parent }
    end
  end
end

#on_axis_ancestor_or_self(ast, input) ⇒ Oga::Ruby::Node

Parameters:

Returns:



175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/oga/xpath/compiler.rb', line 175

def on_axis_ancestor_or_self(ast, input)
  parent = unique_literal(:parent)

  process(ast, input).and(input.is_a?(XML::Node))
    .if_true { yield input }
    .followed_by do
      attribute_or_node(input).if_true do
        input.each_ancestor.add_block(parent) do
          process(ast, parent).if_true { yield parent }
        end
      end
    end
end

#on_axis_attribute(ast, input) ⇒ Oga::Ruby::Node

Parameters:

Returns:



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/oga/xpath/compiler.rb', line 156

def on_axis_attribute(ast, input)
  input.is_a?(XML::Element).if_true do
    attribute = unique_literal(:attribute)

    input.attributes.each.add_block(attribute) do
      name_match = match_name_and_namespace(ast, attribute)

      if name_match
        name_match.if_true { yield attribute }
      else
        yield attribute
      end
    end
  end
end

#on_axis_child(ast, input) ⇒ Oga::Ruby::Node

Parameters:

Returns:



143
144
145
146
147
148
149
150
151
# File 'lib/oga/xpath/compiler.rb', line 143

def on_axis_child(ast, input)
  child = unique_literal(:child)

  document_or_node(input).if_true do
    input.children.each.add_block(child) do
      process(ast, child).if_true { yield child }
    end
  end
end

#on_axis_descendant(ast, input) ⇒ Oga::Ruby::Node

Parameters:

Returns:



222
223
224
225
226
227
228
229
230
# File 'lib/oga/xpath/compiler.rb', line 222

def on_axis_descendant(ast, input)
  node = unique_literal(:descendant)

  document_or_node(input).if_true do
    input.each_node.add_block(node) do
      process(ast, node).if_true { yield node }
    end
  end
end

#on_axis_descendant_or_self(ast, input) ⇒ Oga::Ruby::Node

Parameters:

Returns:



205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/oga/xpath/compiler.rb', line 205

def on_axis_descendant_or_self(ast, input)
  node = unique_literal(:descendant)

  document_or_node(input).if_true do
    process(ast, input)
      .if_true { yield input }
      .followed_by do
        input.each_node.add_block(node) do
          process(ast, node).if_true { yield node }
        end
      end
  end
end

#on_axis_following(ast, input) ⇒ Oga::Ruby::Node

Parameters:

Returns:



295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'lib/oga/xpath/compiler.rb', line 295

def on_axis_following(ast, input)
  orig_input = original_input_literal
  doc_node   = literal(:doc_node)
  check      = literal(:check)
  root       = literal(:root)

  orig_input.is_a?(XML::Node)
    .if_true { root.assign(orig_input.root_node) }
    .else    { root.assign(orig_input) }
    .followed_by(check.assign(self.false))
    .followed_by do
      document_or_node(root).if_true do
        root.each_node.add_block(doc_node) do
          doc_node.eq(input)
            .if_true do
              check.assign(self.true)
                .followed_by(throw_message(:skip_children))
            end
            .followed_by do
              check.if_false { send_message(:next) }
            end
            .followed_by do
              process(ast, doc_node).if_true { yield doc_node }
            end
        end
      end
    end
end

#on_axis_following_sibling(ast, input) ⇒ Oga::Ruby::Node

Parameters:

Returns:



255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/oga/xpath/compiler.rb', line 255

def on_axis_following_sibling(ast, input)
  orig_input = original_input_literal
  doc_node   = literal(:doc_node)
  check      = literal(:check)
  parent     = literal(:parent)
  root       = literal(:root)

  orig_input.is_a?(XML::Node)
    .if_true { root.assign(orig_input.parent) }
    .else    { root.assign(orig_input) }
    .followed_by do
      input.is_a?(XML::Node).and(input.parent)
        .if_true { parent.assign(input.parent) }
        .else    { parent.assign(self.nil) }
    end
    .followed_by(check.assign(self.false))
    .followed_by do
      document_or_node(root).if_true do
        root.each_node.add_block(doc_node) do
          doc_node.eq(input)
            .if_true do
              check.assign(self.true)
                .followed_by(throw_message(:skip_children))
            end
            .followed_by do
              check.not.or(parent != doc_node.parent).if_true do
                send_message(:next)
              end
            end
            .followed_by do
              process(ast, doc_node).if_true { yield doc_node }
            end
        end
      end
    end
end

#on_axis_namespace(ast, input) ⇒ Oga::Ruby::Node

Parameters:

Returns:



327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'lib/oga/xpath/compiler.rb', line 327

def on_axis_namespace(ast, input)
  underscore = literal(:_)
  node       = unique_literal(:namespace)

  name = string(ast.children[1])
  star = string(STAR)

  input.is_a?(XML::Element).if_true do
    input.available_namespaces.each.add_block(underscore, node) do
      node.name.eq(name).or(name.eq(star)).if_true { yield node }
    end
  end
end

#on_axis_parent(ast, input) ⇒ Oga::Ruby::Node

Parameters:

Returns:



235
236
237
238
239
240
241
242
243
# File 'lib/oga/xpath/compiler.rb', line 235

def on_axis_parent(ast, input)
  parent = unique_literal(:parent)

  attribute_or_node(input).if_true do
    parent.assign(input.parent).followed_by do
      process(ast, parent).if_true { yield parent }
    end
  end
end

#on_axis_preceding(ast, input) ⇒ Oga::Ruby::Node

Parameters:

Returns:



344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# File 'lib/oga/xpath/compiler.rb', line 344

def on_axis_preceding(ast, input)
  root     = literal(:root)
  doc_node = literal(:doc_node)

  input.is_a?(XML::Node).if_true do
    root.assign(input.root_node)
      .followed_by do
        document_or_node(root).if_true do
          root.each_node.add_block(doc_node) do
            doc_node.eq(input)
              .if_true { self.break }
              .followed_by do
                process(ast, doc_node).if_true { yield doc_node }
              end
          end
        end
      end
  end
end

#on_axis_preceding_sibling(ast, input) ⇒ Oga::Ruby::Node

Parameters:

Returns:



367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
# File 'lib/oga/xpath/compiler.rb', line 367

def on_axis_preceding_sibling(ast, input)
  orig_input = original_input_literal
  check      = literal(:check)
  root       = literal(:root)
  parent     = literal(:parent)
  doc_node   = literal(:doc_node)

  orig_input.is_a?(XML::Node)
    .if_true { root.assign(orig_input.parent) }
    .else    { root.assign(orig_input) }
    .followed_by(check.assign(self.false))
    .followed_by do
      input.is_a?(XML::Node).and(input.parent)
        .if_true { parent.assign(input.parent) }
        .else    { parent.assign(self.nil) }
    end
    .followed_by do
      document_or_node(root).if_true do
        root.each_node.add_block(doc_node) do
          doc_node.eq(input)
            .if_true { self.break }
            .followed_by do
              doc_node.parent.eq(parent).if_true do
                process(ast, doc_node).if_true { yield doc_node }
              end
            end
        end
      end
    end
end

#on_axis_self(ast, input) ⇒ Oga::Ruby::Node

Parameters:

Returns:



248
249
250
# File 'lib/oga/xpath/compiler.rb', line 248

def on_axis_self(ast, input)
  process(ast, input).if_true { yield input }
end

#on_call(ast, input, &block) ⇒ Oga::Ruby::Node

Delegates function calls to specific handlers.

Parameters:

Returns:



645
646
647
648
649
650
651
# File 'lib/oga/xpath/compiler.rb', line 645

def on_call(ast, input, &block)
  name, *args = *ast

  handler = name.gsub('-', '_')

  send(:"on_call_#{handler}", input, *args, &block)
end

#on_call_boolean(input, arg) ⇒ Oga::Ruby::Node

Parameters:

Returns:



666
667
668
669
670
671
672
673
674
675
676
# File 'lib/oga/xpath/compiler.rb', line 666

def on_call_boolean(input, arg)
  arg_ast    = try_match_first_node(arg, input)
  call_arg   = unique_literal(:call_arg)
  conversion = literal(Conversion)

  call_arg.assign(arg_ast).followed_by do
    converted = conversion.to_boolean(call_arg)

    block_given? ? converted.if_true { yield } : converted
  end
end

#on_call_ceiling(input, arg) ⇒ Oga::Ruby::Node

Parameters:

Returns:



681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
# File 'lib/oga/xpath/compiler.rb', line 681

def on_call_ceiling(input, arg)
  arg_ast    = try_match_first_node(arg, input)
  call_arg   = unique_literal(:call_arg)
  conversion = literal(Conversion)

  call_arg.assign(arg_ast)
    .followed_by do
      call_arg.assign(conversion.to_float(call_arg))
    end
    .followed_by do
      call_arg.nan?
        .if_true { call_arg }
        .else    { block_given? ? yield : call_arg.ceil.to_f }
    end
end

#on_call_concat(input, *args) ⇒ Oga::Ruby::Node

Parameters:

Returns:



738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
# File 'lib/oga/xpath/compiler.rb', line 738

def on_call_concat(input, *args)
  conversion  = literal(Conversion)
  assigns     = []
  conversions = []

  args.each do |arg|
    arg_var = unique_literal(:concat_arg)
    arg_ast = try_match_first_node(arg, input)

    assigns     << arg_var.assign(arg_ast)
    conversions << conversion.to_string(arg_var)
  end

  concatted = assigns.inject(:followed_by)
    .followed_by(conversions.inject(:+))

  block_given? ? concatted.empty?.if_false { yield } : concatted
end

#on_call_contains(input, haystack, needle) ⇒ Oga::Ruby::Node

Parameters:

Returns:



761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
# File 'lib/oga/xpath/compiler.rb', line 761

def on_call_contains(input, haystack, needle)
  haystack_lit = unique_literal(:haystack)
  needle_lit   = unique_literal(:needle)
  conversion   = literal(Conversion)

  haystack_lit.assign(try_match_first_node(haystack, input))
    .followed_by do
      needle_lit.assign(try_match_first_node(needle, input))
    end
    .followed_by do
      converted = conversion.to_string(haystack_lit)
        .include?(conversion.to_string(needle_lit))

      block_given? ? converted.if_true { yield } : converted
    end
end

#on_call_count(input, arg) ⇒ Oga::Ruby::Node

Parameters:

Returns:



781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
# File 'lib/oga/xpath/compiler.rb', line 781

def on_call_count(input, arg)
  count = unique_literal(:count)

  unless return_nodeset?(arg)
    raise TypeError, 'count() can only operate on NodeSet instances'
  end

  count.assign(literal(0.0))
    .followed_by do
      process(arg, input) { count.assign(count + literal(1)) }
    end
    .followed_by do
      block_given? ? count.zero?.if_false { yield } : count
    end
end

#on_call_falseOga::Ruby::Node

Returns:



659
660
661
# File 'lib/oga/xpath/compiler.rb', line 659

def on_call_false(*)
  self.false
end

#on_call_floor(input, arg) ⇒ Oga::Ruby::Node

Parameters:

Returns:



700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
# File 'lib/oga/xpath/compiler.rb', line 700

def on_call_floor(input, arg)
  arg_ast    = try_match_first_node(arg, input)
  call_arg   = unique_literal(:call_arg)
  conversion = literal(Conversion)

  call_arg.assign(arg_ast)
    .followed_by do
      call_arg.assign(conversion.to_float(call_arg))
    end
    .followed_by do
      call_arg.nan?
        .if_true { call_arg }
        .else    { block_given? ? yield : call_arg.floor.to_f }
    end
end

#on_call_id(input, arg) ⇒ Oga::Ruby::Node

Processes the id() function call.

The XPath specification states that this function’s behaviour should be controlled by a DTD. If a DTD were to specify that the ID attribute for a certain element would be “foo” then this function should use said attribute.

Oga does not support DTD parsing/evaluation and as such always uses the “id” attribute.

This function searches the entire document for a matching node, regardless of the current position.

Parameters:

Returns:



813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
# File 'lib/oga/xpath/compiler.rb', line 813

def on_call_id(input, arg)
  orig_input = original_input_literal
  node       = unique_literal(:node)
  ids_var    = unique_literal('ids')
  matched    = unique_literal('id_matched')
  id_str_var = unique_literal('id_string')
  attr_var   = unique_literal('attr')

  matched.assign(literal(XML::NodeSet).new)
    .followed_by do
      # When using some sort of path we'll want the text of all matched
      # nodes.
      if return_nodeset?(arg)
        ids_var.assign(literal(:[])).followed_by do
          process(arg, input) { |element| ids_var << element.text }
        end

      # For everything else we'll cast the value to a string and split it
      # on every space.
      else
        conversion = literal(Conversion).to_string(ids_var)
          .split(string(' '))

        ids_var.assign(process(arg, input))
          .followed_by(ids_var.assign(conversion))
      end
    end
    .followed_by do
      id_str_var.assign(string('id'))
    end
    .followed_by do
      orig_input.each_node.add_block(node) do
        node.is_a?(XML::Element).if_true do
          attr_var.assign(node.attribute(id_str_var)).followed_by do
            attr_var.and(ids_var.include?(attr_var.value))
              .if_true { block_given? ? yield : matched << node }
          end
        end
      end
    end
    .followed_by(matched)
end

#on_call_lang(input, arg) ⇒ Oga::Ruby::Node

Parameters:

Returns:



859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
# File 'lib/oga/xpath/compiler.rb', line 859

def on_call_lang(input, arg)
  lang_var = unique_literal('lang')
  node     = unique_literal('node')
  found    = unique_literal('found')
  xml_lang = unique_literal('xml_lang')
  matched  = unique_literal('matched')

  conversion = literal(Conversion)

  ast = lang_var.assign(try_match_first_node(arg, input))
    .followed_by do
      lang_var.assign(conversion.to_string(lang_var))
    end
    .followed_by do
      matched.assign(self.false)
    end
    .followed_by do
      node.assign(input)
    end
    .followed_by do
      xml_lang.assign(string('xml:lang'))
    end
    .followed_by do
      node.respond_to?(symbol(:attribute)).while_true do
        found.assign(node.get(xml_lang))
          .followed_by do
            found.if_true do
              found.eq(lang_var)
                .if_true do
                  if block_given?
                    yield
                  else
                    matched.assign(self.true).followed_by(self.break)
                  end
                end
                .else { self.break }
            end
          end
          .followed_by(node.assign(node.parent))
      end
    end

  block_given? ? ast : ast.followed_by(matched)
end

#on_call_lastOga::Ruby::Node

Returns:



1236
1237
1238
1239
1240
1241
1242
1243
1244
# File 'lib/oga/xpath/compiler.rb', line 1236

def on_call_last(*)
  set = predicate_nodeset

  unless set
    raise 'last() can only be used in a predicate'
  end

  set.length.to_f
end

#on_call_local_name(input, arg = nil) ⇒ Oga::Ruby::Node

Parameters:

Returns:



907
908
909
910
911
912
913
914
915
916
# File 'lib/oga/xpath/compiler.rb', line 907

def on_call_local_name(input, arg = nil)
  argument_or_first_node(input, arg) do |arg_var|
    arg_var
      .if_true do
        ensure_element_or_attribute(arg_var)
          .followed_by { block_given? ? yield : arg_var.name }
      end
      .else { string('') }
  end
end

#on_call_name(input, arg = nil) ⇒ Oga::Ruby::Node

Parameters:

Returns:



921
922
923
924
925
926
927
928
929
930
# File 'lib/oga/xpath/compiler.rb', line 921

def on_call_name(input, arg = nil)
  argument_or_first_node(input, arg) do |arg_var|
    arg_var
      .if_true do
        ensure_element_or_attribute(arg_var)
          .followed_by { block_given? ? yield : arg_var.expanded_name }
      end
      .else { string('') }
  end
end

#on_call_namespace_uri(input, arg = nil) ⇒ Oga::Ruby::Node

Parameters:

Returns:



935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
# File 'lib/oga/xpath/compiler.rb', line 935

def on_call_namespace_uri(input, arg = nil)
  default = string('')

  argument_or_first_node(input, arg) do |arg_var|
    arg_var
      .if_true do
        ensure_element_or_attribute(arg_var).followed_by do
          arg_var.namespace
            .if_true { block_given? ? yield : arg_var.namespace.uri }
            .else    { default } # no yield so predicates aren't matched
        end
      end
      .else { default }
  end
end

#on_call_normalize_space(input, arg = nil) ⇒ Oga::Ruby::Node

Parameters:

Returns:



954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
# File 'lib/oga/xpath/compiler.rb', line 954

def on_call_normalize_space(input, arg = nil)
  conversion = literal(Conversion)
  norm_var   = unique_literal(:normalized)

  find    = literal('/\s+/')
  replace = string(' ')

  argument_or_first_node(input, arg) do |arg_var|
    norm_var
      .assign(conversion.to_string(arg_var).strip.gsub(find, replace))
      .followed_by do
        norm_var.empty?
          .if_true { string('') }
          .else    { block_given? ? yield : norm_var }
      end
  end
end

#on_call_not(input, arg) ⇒ Oga::Ruby::Node

Parameters:

Returns:



975
976
977
978
979
980
981
982
983
984
985
# File 'lib/oga/xpath/compiler.rb', line 975

def on_call_not(input, arg)
  arg_ast    = try_match_first_node(arg, input)
  call_arg   = unique_literal(:call_arg)
  conversion = literal(Conversion)

  call_arg.assign(arg_ast).followed_by do
    converted = conversion.to_boolean(call_arg).not

    block_given? ? converted.if_true { yield } : converted
  end
end

#on_call_number(input, arg = nil) ⇒ Oga::Ruby::Node

Parameters:

Returns:



990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
# File 'lib/oga/xpath/compiler.rb', line 990

def on_call_number(input, arg = nil)
  convert_var = unique_literal(:convert)
  conversion  = literal(Conversion)

  argument_or_first_node(input, arg) do |arg_var|
    convert_var.assign(conversion.to_float(arg_var)).followed_by do
      if block_given?
        convert_var.zero?.if_false { yield }
      else
        convert_var
      end
    end
  end
end

#on_call_positionOga::Ruby::Node

Returns:



1247
1248
1249
1250
1251
1252
1253
1254
1255
# File 'lib/oga/xpath/compiler.rb', line 1247

def on_call_position(*)
  index = predicate_index

  unless index
    raise 'position() can only be used in a predicate'
  end

  index.to_f
end

#on_call_round(input, arg) ⇒ Oga::Ruby::Node

Parameters:

Returns:



719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
# File 'lib/oga/xpath/compiler.rb', line 719

def on_call_round(input, arg)
  arg_ast    = try_match_first_node(arg, input)
  call_arg   = unique_literal(:call_arg)
  conversion = literal(Conversion)

  call_arg.assign(arg_ast)
    .followed_by do
      call_arg.assign(conversion.to_float(call_arg))
    end
    .followed_by do
      call_arg.nan?
        .if_true { call_arg }
        .else    { block_given? ? yield : call_arg.round.to_f }
    end
end

#on_call_starts_with(input, haystack, needle) ⇒ Oga::Ruby::Node

Parameters:

Returns:



1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
# File 'lib/oga/xpath/compiler.rb', line 1009

def on_call_starts_with(input, haystack, needle)
  haystack_var = unique_literal(:haystack)
  needle_var   = unique_literal(:needle)
  conversion   = literal(Conversion)

  haystack_var.assign(try_match_first_node(haystack, input))
    .followed_by do
      needle_var.assign(try_match_first_node(needle, input))
    end
    .followed_by do
      haystack_var.assign(conversion.to_string(haystack_var))
        .followed_by do
          needle_var.assign(conversion.to_string(needle_var))
        end
        .followed_by do
          equal = needle_var.empty?
            .or(haystack_var.start_with?(needle_var))

          block_given? ? equal.if_true { yield } : equal
        end
    end
end

#on_call_string(input, arg = nil) ⇒ Oga::Ruby::Node

Parameters:

Returns:



1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
# File 'lib/oga/xpath/compiler.rb', line 1054

def on_call_string(input, arg = nil)
  convert_var = unique_literal(:convert)
  conversion  = literal(Conversion)

  argument_or_first_node(input, arg) do |arg_var|
    convert_var.assign(conversion.to_string(arg_var))
      .followed_by do
        if block_given?
          convert_var.empty?.if_false { yield }
        else
          convert_var
        end
      end
  end
end

#on_call_string_length(input, arg = nil) ⇒ Oga::Ruby::Node

Parameters:

Returns:



1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
# File 'lib/oga/xpath/compiler.rb', line 1035

def on_call_string_length(input, arg = nil)
  convert_var = unique_literal(:convert)
  conversion  = literal(Conversion)

  argument_or_first_node(input, arg) do |arg_var|
    convert_var.assign(conversion.to_string(arg_var).length)
      .followed_by do
        if block_given?
          convert_var.zero?.if_false { yield }
        else
          convert_var.to_f
        end
      end
  end
end

#on_call_substring(input, haystack, start, length = nil) ⇒ Oga::Ruby::Node

Parameters:

  • input (Oga::Ruby::Node)
  • haystack (AST::Node)
  • start (AST::Node)
  • length (AST::Node) (defaults to: nil)

Returns:



1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
# File 'lib/oga/xpath/compiler.rb', line 1133

def on_call_substring(input, haystack, start, length = nil)
  haystack_var = unique_literal(:haystack)
  start_var    = unique_literal(:start)
  stop_var     = unique_literal(:stop)
  length_var   = unique_literal(:length)
  conversion   = literal(Conversion)

  haystack_var.assign(try_match_first_node(haystack, input))
    .followed_by do
      start_var.assign(try_match_first_node(start, input))
        .followed_by do
          start_var.assign(start_var - literal(1))
        end
    end
    .followed_by do
      if length
        length_var.assign(try_match_first_node(length, input))
          .followed_by do
            length_int = conversion.to_float(length_var)
              .to_i - literal(1)

            stop_var.assign(start_var + length_int)
          end
      else
        stop_var.assign(literal(-1))
      end
    end
    .followed_by do
      substring = conversion
        .to_string(haystack_var)[range(start_var, stop_var)]

      block_given? ? substring.empty?.if_false { yield } : substring
    end
end

#on_call_substring_after(input, haystack, needle) ⇒ Oga::Ruby::Node

Parameters:

Returns:



1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
# File 'lib/oga/xpath/compiler.rb', line 1103

def on_call_substring_after(input, haystack, needle)
  haystack_var = unique_literal(:haystack)
  needle_var   = unique_literal(:needle)
  conversion   = literal(Conversion)

  before = unique_literal(:before)
  sep    = unique_literal(:sep)
  after  = unique_literal(:after)

  haystack_var.assign(try_match_first_node(haystack, input))
    .followed_by do
      needle_var.assign(try_match_first_node(needle, input))
    end
    .followed_by do
      converted = conversion.to_string(haystack_var)
        .partition(conversion.to_string(needle_var))

      mass_assign([before, sep, after], converted).followed_by do
        sep.empty?
          .if_true { sep }
          .else    { block_given? ? yield : after }
      end
    end
end

#on_call_substring_before(input, haystack, needle) ⇒ Oga::Ruby::Node

Parameters:

Returns:



1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
# File 'lib/oga/xpath/compiler.rb', line 1074

def on_call_substring_before(input, haystack, needle)
  haystack_var = unique_literal(:haystack)
  needle_var   = unique_literal(:needle)
  conversion   = literal(Conversion)

  before = unique_literal(:before)
  sep    = unique_literal(:sep)
  after  = unique_literal(:after)

  haystack_var.assign(try_match_first_node(haystack, input))
    .followed_by do
      needle_var.assign(try_match_first_node(needle, input))
    end
    .followed_by do
      converted = conversion.to_string(haystack_var)
        .partition(conversion.to_string(needle_var))

      mass_assign([before, sep, after], converted).followed_by do
        sep.empty?
          .if_true { sep }
          .else    { block_given? ? yield : before }
      end
    end
end

#on_call_sum(input, arg) ⇒ Oga::Ruby::Node

Parameters:

Returns:



1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
# File 'lib/oga/xpath/compiler.rb', line 1171

def on_call_sum(input, arg)
  unless return_nodeset?(arg)
    raise TypeError, 'sum() can only operate on a path, axis or predicate'
  end

  sum_var    = unique_literal(:sum)
  conversion = literal(Conversion)

  sum_var.assign(literal(0.0))
    .followed_by do
      process(arg, input) do |matched_node|
        sum_var.assign(sum_var + conversion.to_float(matched_node.text))
      end
    end
    .followed_by do
      block_given? ? sum_var.zero?.if_false { yield } : sum_var
    end
end

#on_call_translate(input, source, find, replace) ⇒ Oga::Ruby::Node

Parameters:

  • input (Oga::Ruby::Node)
  • source (AST::Node)
  • find (AST::Node)
  • replace (AST::Node)

Returns:



1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
# File 'lib/oga/xpath/compiler.rb', line 1195

def on_call_translate(input, source, find, replace)
  source_var   = unique_literal(:source)
  find_var     = unique_literal(:find)
  replace_var  = unique_literal(:replace)
  replaced_var = unique_literal(:replaced)
  conversion   = literal(Conversion)

  char  = unique_literal(:char)
  index = unique_literal(:index)

  source_var.assign(try_match_first_node(source, input))
    .followed_by do
      replaced_var.assign(conversion.to_string(source_var))
    end
    .followed_by do
      find_var.assign(try_match_first_node(find, input))
    end
    .followed_by do
      find_var.assign(conversion.to_string(find_var).chars.to_array)
    end
    .followed_by do
      replace_var.assign(try_match_first_node(replace, input))
    end
    .followed_by do
      replace_var.assign(conversion.to_string(replace_var).chars.to_array)
    end
    .followed_by do
      find_var.each_with_index.add_block(char, index) do
        replace_with = replace_var[index]
          .if_true { replace_var[index] }
          .else    { string('') }

        replaced_var.assign(replaced_var.gsub(char, replace_with))
      end
    end
    .followed_by do
      replaced_var
    end
end

#on_call_trueOga::Ruby::Node

Returns:



654
655
656
# File 'lib/oga/xpath/compiler.rb', line 654

def on_call_true(*)
  block_given? ? yield : self.true
end

#on_eq(ast, input, &block) ⇒ Object

Processes the = operator.

See Also:

  • Oga::XPath::Compiler.[[#operator]


548
549
550
551
552
553
554
555
556
557
558
559
# File 'lib/oga/xpath/compiler.rb', line 548

def on_eq(ast, input, &block)
  conv = literal(Conversion)

  operator(ast, input) do |left, right|
    mass_assign([left, right], conv.to_compatible_types(left, right))
      .followed_by do
        operation = left.eq(right)

        block ? operation.if_true(&block) : operation
      end
  end
end

#on_float(ast) ⇒ Oga::Ruby::Node

Parameters:

  • ast (AST::Node)

Returns:



627
628
629
# File 'lib/oga/xpath/compiler.rb', line 627

def on_float(ast, *)
  literal(ast.children[0].to_s)
end

#on_int(ast) ⇒ Oga::Ruby::Node

Parameters:

  • ast (AST::Node)

Returns:



621
622
623
# File 'lib/oga/xpath/compiler.rb', line 621

def on_int(ast, *)
  literal(ast.children[0].to_f.to_s)
end

#on_neq(ast, input, &block) ⇒ Object

Processes the != operator.

See Also:

  • Oga::XPath::Compiler.[[#operator]


564
565
566
567
568
569
570
571
572
573
574
575
# File 'lib/oga/xpath/compiler.rb', line 564

def on_neq(ast, input, &block)
  conv = literal(Conversion)

  operator(ast, input) do |left, right|
    mass_assign([left, right], conv.to_compatible_types(left, right))
      .followed_by do
        operation = left != right

        block ? operation.if_true(&block) : operation
      end
  end
end

#on_pipe(ast, input, &block) ⇒ Object

Processes the | operator.

See Also:

  • Oga::XPath::Compiler.[[#operator]


594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
# File 'lib/oga/xpath/compiler.rb', line 594

def on_pipe(ast, input, &block)
  left, right = *ast

  union = unique_literal(:union)

  # Expressions such as "a | b | c"
  if left.type == :pipe
    union.assign(process(left, input))
      .followed_by(process(right, input) { |node| union << node })
      .followed_by(union)
  # Expressions such as "a | b"
  else
    union.assign(literal(XML::NodeSet).new)
      .followed_by(process(left, input) { |node| union << node })
      .followed_by(process(right, input) { |node| union << node })
      .followed_by(union)
  end
end

#on_predicate(ast, input, &block) ⇒ Oga::Ruby::Node

Parameters:

Returns:



401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
# File 'lib/oga/xpath/compiler.rb', line 401

def on_predicate(ast, input, &block)
  test, predicate, following = *ast

  index_var = unique_literal(:index)

  if number?(predicate)
    method = :on_predicate_index
  elsif has_call_node?(predicate, 'last')
    method = :on_predicate_temporary
  else
    method = :on_predicate_direct
  end

  @predicate_indexes << index_var

  ast = index_var.assign(literal(1)).followed_by do
    send(method, input, test, predicate) do |matched|
      process_following_or_yield(following, matched, &block)
    end
  end

  @predicate_indexes.pop

  ast
end

#on_predicate_direct(input, test, predicate) ⇒ Oga::Ruby::Node

Processes a predicate that doesn’t require temporary NodeSet.

Parameters:

Returns:



478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
# File 'lib/oga/xpath/compiler.rb', line 478

def on_predicate_direct(input, test, predicate)
  pred_var   = unique_literal(:pred_var)
  index_var  = predicate_index
  index_step = literal(1)
  conversion = literal(Conversion)

  process(test, input) do |matched_test_node|
    if return_nodeset?(predicate)
      pred_ast = catch_message(:predicate_matched) do
        process(predicate, matched_test_node) do
          throw_message(:predicate_matched, self.true)
        end
      end
    else
      pred_ast = process(predicate, matched_test_node)
    end

    pred_var.assign(pred_ast)
      .followed_by do
        pred_var.is_a?(Numeric).if_true do
          pred_var.assign(pred_var.to_i.eq(index_var))
        end
      end
      .followed_by do
        conversion.to_boolean(pred_var).if_true do
          yield matched_test_node
        end
      end
      .followed_by do
        index_var.assign(index_var + index_step)
      end
  end
end

#on_predicate_index(input, test, predicate) ⇒ Oga::Ruby::Node

Processes a predicate that uses a literal index.

Parameters:

Returns:



518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
# File 'lib/oga/xpath/compiler.rb', line 518

def on_predicate_index(input, test, predicate)
  index_var  = predicate_index
  index_step = literal(1)

  index = process(predicate, input).to_i

  process(test, input) do |matched_test_node|
    index_var.eq(index)
      .if_true do
        yield matched_test_node
      end
      .followed_by do
        index_var.assign(index_var + index_step)
      end
  end
end

#on_predicate_temporary(input, test, predicate) ⇒ Oga::Ruby::Node

Processes a predicate that requires a temporary NodeSet.

Parameters:

Returns:



433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
# File 'lib/oga/xpath/compiler.rb', line 433

def on_predicate_temporary(input, test, predicate)
  temp_set   = unique_literal(:temp_set)
  pred_node  = unique_literal(:pred_node)
  pred_var   = unique_literal(:pred_var)
  conversion = literal(Conversion)

  index_var  = predicate_index
  index_step = literal(1)

  @predicate_nodesets << temp_set

  ast = temp_set.assign(literal(XML::NodeSet).new)
    .followed_by do
      process(test, input) { |node| temp_set << node }
    end
    .followed_by do
      temp_set.each.add_block(pred_node) do
        pred_ast = process(predicate, pred_node)

        pred_var.assign(pred_ast)
          .followed_by do
            pred_var.is_a?(Numeric).if_true do
              pred_var.assign(pred_var.to_i.eq(index_var))
            end
          end
          .followed_by do
            conversion.to_boolean(pred_var).if_true { yield pred_node }
          end
          .followed_by do
            index_var.assign(index_var + index_step)
          end
      end
    end

  @predicate_nodesets.pop

  ast
end

#on_string(ast) ⇒ Oga::Ruby::Node

Parameters:

  • ast (AST::Node)

Returns:



615
616
617
# File 'lib/oga/xpath/compiler.rb', line 615

def on_string(ast, *)
  string(ast.children[0])
end

#on_test(ast, input) ⇒ Oga::Ruby::Node

Parameters:

Returns:



538
539
540
541
542
543
# File 'lib/oga/xpath/compiler.rb', line 538

def on_test(ast, input)
  condition  = element_or_attribute(input)
  name_match = match_name_and_namespace(ast, input)

  name_match ? condition.and(name_match) : condition
end

#on_type_test(ast, input, &block) ⇒ Oga::Ruby::Node

Delegates type tests to specific handlers.

Parameters:

Returns:



1262
1263
1264
1265
1266
1267
1268
1269
1270
# File 'lib/oga/xpath/compiler.rb', line 1262

def on_type_test(ast, input, &block)
  name, following = *ast

  handler = name.gsub('-', '_')

  send(:"on_type_test_#{handler}", input) do |matched|
    process_following_or_yield(following, matched, &block)
  end
end

#on_type_test_comment(input) ⇒ Oga::Ruby::Node

Parameters:

Returns:



1274
1275
1276
# File 'lib/oga/xpath/compiler.rb', line 1274

def on_type_test_comment(input)
  input.is_a?(XML::Comment)
end

#on_type_test_node(input) ⇒ Oga::Ruby::Node

Parameters:

Returns:



1292
1293
1294
# File 'lib/oga/xpath/compiler.rb', line 1292

def on_type_test_node(input)
  document_or_node(input).or(input.is_a?(XML::Attribute))
end

#on_type_test_processing_instruction(input) ⇒ Oga::Ruby::Node

Parameters:

Returns:



1286
1287
1288
# File 'lib/oga/xpath/compiler.rb', line 1286

def on_type_test_processing_instruction(input)
  input.is_a?(XML::ProcessingInstruction)
end

#on_type_test_text(input) ⇒ Oga::Ruby::Node

Parameters:

Returns:



1280
1281
1282
# File 'lib/oga/xpath/compiler.rb', line 1280

def on_type_test_text(input)
  input.is_a?(XML::Text)
end

#on_var(ast) ⇒ Oga::Ruby::Node

Parameters:

  • ast (AST::Node)

Returns:



633
634
635
636
637
638
# File 'lib/oga/xpath/compiler.rb', line 633

def on_var(ast, *)
  name = ast.children[0]

  variables_literal.and(variables_literal[string(name)])
    .or(send_message(:raise, string("Undefined XPath variable: #{name}")))
end

#operator(ast, input, optimize_first = true) ⇒ Oga::Ruby::Node

Generates the code for an operator.

The generated code is optimized so that expressions such as a/b = c only match the first node in both arms instead of matching all available nodes first. Because numeric operators only ever operates on the first node in a set we can simply ditch the rest, possibly speeding things up quite a bit. This only works if one of the arms is:

  • a path
  • an axis
  • a predicate

Everything else is processed the usual (and possibly slower) way.

Parameters:

  • ast (AST::Node)
  • input (Oga::Ruby::Node)
  • optimize_first (TrueClass|FalseClass) (defaults to: true)

Returns:



1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
# File 'lib/oga/xpath/compiler.rb', line 1475

def operator(ast, input, optimize_first = true)
  left, right = *ast

  left_var  = unique_literal(:op_left)
  right_var = unique_literal(:op_right)

  left_ast  = try_match_first_node(left, input, optimize_first)
  right_ast = try_match_first_node(right, input, optimize_first)

  left_var.assign(left_ast)
    .followed_by(right_var.assign(right_ast))
    .followed_by { yield left_var, right_var }
end

#original_input_literalOga::Ruby::Node

Returns:



1495
1496
1497
# File 'lib/oga/xpath/compiler.rb', line 1495

def original_input_literal
  literal(:original_input)
end

#predicate_indexOga::Ruby::Node

Returns:



1576
1577
1578
# File 'lib/oga/xpath/compiler.rb', line 1576

def predicate_index
  @predicate_indexes.last
end

#predicate_nodesetOga::Ruby::Node

Returns:



1581
1582
1583
# File 'lib/oga/xpath/compiler.rb', line 1581

def predicate_nodeset
  @predicate_nodesets.last
end

#process(ast, input, &block) ⇒ Oga::Ruby::Node

Processes a single XPath AST node.

Parameters:

Returns:



108
109
110
# File 'lib/oga/xpath/compiler.rb', line 108

def process(ast, input, &block)
  send("on_#{ast.type}", ast, input, &block)
end

#process_following_or_yield(following, matched, &block) ⇒ Oga::Ruby::Node

Parameters:

Returns:



1588
1589
1590
1591
1592
1593
1594
# File 'lib/oga/xpath/compiler.rb', line 1588

def process_following_or_yield(following, matched, &block)
  if following
    process(following, matched, &block)
  else
    yield matched
  end
end

#raise_message(klass, message) ⇒ Oga::Ruby::Node

Parameters:

  • klass (Class)
  • message (String)

Returns:



1339
1340
1341
# File 'lib/oga/xpath/compiler.rb', line 1339

def raise_message(klass, message)
  send_message(:raise, literal(klass), string(message))
end

#range(start, stop) ⇒ Oga::Ruby::Node

Parameters:

Returns:



1305
1306
1307
# File 'lib/oga/xpath/compiler.rb', line 1305

def range(start, stop)
  Ruby::Node.new(:range, [start, stop])
end

#resetObject

Resets the internal state.



58
59
60
61
62
63
# File 'lib/oga/xpath/compiler.rb', line 58

def reset
  @literal_id = 0

  @predicate_nodesets = []
  @predicate_indexes  = []
end

#return_nodeset?(ast) ⇒ TrueClass|FalseClass

Parameters:

  • ast (AST::Node)

Returns:

  • (TrueClass|FalseClass)


1553
1554
1555
# File 'lib/oga/xpath/compiler.rb', line 1553

def return_nodeset?(ast)
  RETURN_NODESET.include?(ast.type)
end

#send_message(name, *args) ⇒ Oga::Ruby::Node

Parameters:

  • name (String)
  • args (Array)

Returns:



1332
1333
1334
# File 'lib/oga/xpath/compiler.rb', line 1332

def send_message(name, *args)
  Ruby::Node.new(:send, [nil, name.to_s, *args])
end

#string(value) ⇒ Oga::Ruby::Node

Parameters:

  • value (#to_s)

Returns:



1319
1320
1321
# File 'lib/oga/xpath/compiler.rb', line 1319

def string(value)
  Ruby::Node.new(:string, [value.to_s])
end

#string?(ast) ⇒ TrueClass|FalseClass

Parameters:

  • ast (AST::Node)

Returns:

  • (TrueClass|FalseClass)


1525
1526
1527
# File 'lib/oga/xpath/compiler.rb', line 1525

def string?(ast)
  ast.type == :string
end

#symbol(value) ⇒ Oga::Ruby::Node

Parameters:

  • value (String)

Returns:



1325
1326
1327
# File 'lib/oga/xpath/compiler.rb', line 1325

def symbol(value)
  Ruby::Node.new(:symbol, [value.to_sym])
end

#throw_message(name, *args) ⇒ Oga::Ruby::Node

Parameters:

  • name (Symbol)
  • args (Array)

Returns:



1542
1543
1544
# File 'lib/oga/xpath/compiler.rb', line 1542

def throw_message(name, *args)
  send_message(:throw, symbol(name), *args)
end

#to_int(ast) ⇒ Oga::Ruby::Node

Parameters:

  • ast (AST::Node)

Returns:



1506
1507
1508
# File 'lib/oga/xpath/compiler.rb', line 1506

def to_int(ast)
  literal(ast.children[0].to_i.to_s)
end

#trueOga::Ruby::Node

Returns:



1349
1350
1351
# File 'lib/oga/xpath/compiler.rb', line 1349

def true
  @true ||= literal(:true)
end

#try_match_first_node(ast, input, optimize_first = true) ⇒ Object

Tries to match the first node in a set, otherwise processes it as usual.

See Also:

  • Oga::XPath::Compiler.[[#match_first_node]


1431
1432
1433
1434
1435
1436
1437
# File 'lib/oga/xpath/compiler.rb', line 1431

def try_match_first_node(ast, input, optimize_first = true)
  if return_nodeset?(ast) and optimize_first
    match_first_node(ast, input)
  else
    process(ast, input)
  end
end

#unique_literal(name) ⇒ Oga::Ruby::Node

Parameters:

  • name (String)

Returns:



1311
1312
1313
1314
1315
# File 'lib/oga/xpath/compiler.rb', line 1311

def unique_literal(name)
  new_id = @literal_id += 1

  literal("#{name}#{new_id}")
end

#variables_literalOga::Ruby::Node

Returns:



1500
1501
1502
# File 'lib/oga/xpath/compiler.rb', line 1500

def variables_literal
  literal(:variables)
end