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

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

#initializeCompiler

Returns a new instance of Compiler



49
50
51
# File 'lib/oga/xpath/compiler.rb', line 49

def initialize
  reset
end

Class Method Details

.compile_with_cache(ast) ⇒ Object

Compiles and caches an AST.

See Also:

  • [[#compile]


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

def self.compile_with_cache(ast)
  CACHE.get_or_set(ast) { new.compile(ast) }
end

Instance Method Details

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

Parameters:

Returns:



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

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:



1362
1363
1364
# File 'lib/oga/xpath/compiler.rb', line 1362

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

#breakOga::Ruby::Node

Returns:



1527
1528
1529
# File 'lib/oga/xpath/compiler.rb', line 1527

def break
  send_message(:break)
end

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

Parameters:

  • name (Symbol)

Returns:



1511
1512
1513
1514
1515
1516
1517
# File 'lib/oga/xpath/compiler.rb', line 1511

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)


65
66
67
68
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
# File 'lib/oga/xpath/compiler.rb', line 65

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:



1368
1369
1370
# File 'lib/oga/xpath/compiler.rb', line 1368

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:



1356
1357
1358
# File 'lib/oga/xpath/compiler.rb', line 1356

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:



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

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:



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

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

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

Parameters:

  • ast (AST::Node)

Returns:

  • (TrueClass|FalseClass)


1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
# File 'lib/oga/xpath/compiler.rb', line 1539

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:



1294
1295
1296
# File 'lib/oga/xpath/compiler.rb', line 1294

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

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

Parameters:

Returns:



1493
1494
1495
# File 'lib/oga/xpath/compiler.rb', line 1493

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:



1400
1401
1402
1403
1404
1405
1406
# File 'lib/oga/xpath/compiler.rb', line 1400

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:



1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
# File 'lib/oga/xpath/compiler.rb', line 1375

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
    ns_match  = input.namespace_name.eq(string(ns))
    condition = condition ? condition.and(ns_match) : ns_match
  end

  condition
end

#matched_literalOga::Ruby::Node

Returns:



1470
1471
1472
# File 'lib/oga/xpath/compiler.rb', line 1470

def matched_literal
  literal(:matched)
end

#nilOga::Ruby::Node

Returns:



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

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

#number?(ast) ⇒ TrueClass|FalseClass

Parameters:

  • ast (AST::Node)

Returns:

  • (TrueClass|FalseClass)


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

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

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

Parameters:

Returns:



111
112
113
114
115
116
117
# File 'lib/oga/xpath/compiler.rb', line 111

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:



126
127
128
129
130
131
132
133
134
# File 'lib/oga/xpath/compiler.rb', line 126

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:



188
189
190
191
192
193
194
195
196
# File 'lib/oga/xpath/compiler.rb', line 188

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:



171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/oga/xpath/compiler.rb', line 171

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:



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/oga/xpath/compiler.rb', line 152

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:



139
140
141
142
143
144
145
146
147
# File 'lib/oga/xpath/compiler.rb', line 139

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:



218
219
220
221
222
223
224
225
226
# File 'lib/oga/xpath/compiler.rb', line 218

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:



201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/oga/xpath/compiler.rb', line 201

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:



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

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:



251
252
253
254
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
# File 'lib/oga/xpath/compiler.rb', line 251

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:



323
324
325
326
327
328
329
330
331
332
333
334
335
# File 'lib/oga/xpath/compiler.rb', line 323

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:



231
232
233
234
235
236
237
238
239
# File 'lib/oga/xpath/compiler.rb', line 231

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:



340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# File 'lib/oga/xpath/compiler.rb', line 340

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:



363
364
365
366
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
# File 'lib/oga/xpath/compiler.rb', line 363

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:



244
245
246
# File 'lib/oga/xpath/compiler.rb', line 244

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:



641
642
643
644
645
646
647
# File 'lib/oga/xpath/compiler.rb', line 641

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:



662
663
664
665
666
667
668
669
670
671
672
# File 'lib/oga/xpath/compiler.rb', line 662

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:



677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
# File 'lib/oga/xpath/compiler.rb', line 677

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:



734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
# File 'lib/oga/xpath/compiler.rb', line 734

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:



757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
# File 'lib/oga/xpath/compiler.rb', line 757

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:



777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
# File 'lib/oga/xpath/compiler.rb', line 777

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:



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

def on_call_false(*)
  self.false
end

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

Parameters:

Returns:



696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
# File 'lib/oga/xpath/compiler.rb', line 696

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:



809
810
811
812
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
# File 'lib/oga/xpath/compiler.rb', line 809

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:



855
856
857
858
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
# File 'lib/oga/xpath/compiler.rb', line 855

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:



1232
1233
1234
1235
1236
1237
1238
1239
1240
# File 'lib/oga/xpath/compiler.rb', line 1232

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:



903
904
905
906
907
908
909
910
911
912
# File 'lib/oga/xpath/compiler.rb', line 903

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:



917
918
919
920
921
922
923
924
925
926
# File 'lib/oga/xpath/compiler.rb', line 917

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:



931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
# File 'lib/oga/xpath/compiler.rb', line 931

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:



950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
# File 'lib/oga/xpath/compiler.rb', line 950

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:



971
972
973
974
975
976
977
978
979
980
981
# File 'lib/oga/xpath/compiler.rb', line 971

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:



986
987
988
989
990
991
992
993
994
995
996
997
998
999
# File 'lib/oga/xpath/compiler.rb', line 986

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:



1243
1244
1245
1246
1247
1248
1249
1250
1251
# File 'lib/oga/xpath/compiler.rb', line 1243

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:



715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
# File 'lib/oga/xpath/compiler.rb', line 715

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:



1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
# File 'lib/oga/xpath/compiler.rb', line 1005

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:



1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
# File 'lib/oga/xpath/compiler.rb', line 1050

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:



1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
# File 'lib/oga/xpath/compiler.rb', line 1031

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:



1129
1130
1131
1132
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
# File 'lib/oga/xpath/compiler.rb', line 1129

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:



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

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:



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

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:



1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
# File 'lib/oga/xpath/compiler.rb', line 1167

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:



1191
1192
1193
1194
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
# File 'lib/oga/xpath/compiler.rb', line 1191

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:



650
651
652
# File 'lib/oga/xpath/compiler.rb', line 650

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]


544
545
546
547
548
549
550
551
552
553
554
555
# File 'lib/oga/xpath/compiler.rb', line 544

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:



623
624
625
# File 'lib/oga/xpath/compiler.rb', line 623

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

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

Parameters:

  • ast (AST::Node)

Returns:



617
618
619
# File 'lib/oga/xpath/compiler.rb', line 617

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]


560
561
562
563
564
565
566
567
568
569
570
571
# File 'lib/oga/xpath/compiler.rb', line 560

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]


590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
# File 'lib/oga/xpath/compiler.rb', line 590

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:



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

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:



474
475
476
477
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
# File 'lib/oga/xpath/compiler.rb', line 474

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:



514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
# File 'lib/oga/xpath/compiler.rb', line 514

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:



429
430
431
432
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
# File 'lib/oga/xpath/compiler.rb', line 429

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:



611
612
613
# File 'lib/oga/xpath/compiler.rb', line 611

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

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

Parameters:

Returns:



534
535
536
537
538
539
# File 'lib/oga/xpath/compiler.rb', line 534

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:



1258
1259
1260
1261
1262
1263
1264
1265
1266
# File 'lib/oga/xpath/compiler.rb', line 1258

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:



1270
1271
1272
# File 'lib/oga/xpath/compiler.rb', line 1270

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

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

Parameters:

Returns:



1288
1289
1290
# File 'lib/oga/xpath/compiler.rb', line 1288

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:



1282
1283
1284
# File 'lib/oga/xpath/compiler.rb', line 1282

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

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

Parameters:

Returns:



1276
1277
1278
# File 'lib/oga/xpath/compiler.rb', line 1276

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

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

Parameters:

  • ast (AST::Node)

Returns:



629
630
631
632
633
634
# File 'lib/oga/xpath/compiler.rb', line 629

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:



1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
# File 'lib/oga/xpath/compiler.rb', line 1455

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:



1475
1476
1477
# File 'lib/oga/xpath/compiler.rb', line 1475

def original_input_literal
  literal(:original_input)
end

#predicate_indexOga::Ruby::Node

Returns:



1556
1557
1558
# File 'lib/oga/xpath/compiler.rb', line 1556

def predicate_index
  @predicate_indexes.last
end

#predicate_nodesetOga::Ruby::Node

Returns:



1561
1562
1563
# File 'lib/oga/xpath/compiler.rb', line 1561

def predicate_nodeset
  @predicate_nodesets.last
end

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

Processes a single XPath AST node.

Parameters:

Returns:



104
105
106
# File 'lib/oga/xpath/compiler.rb', line 104

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:



1568
1569
1570
1571
1572
1573
1574
# File 'lib/oga/xpath/compiler.rb', line 1568

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:



1335
1336
1337
# File 'lib/oga/xpath/compiler.rb', line 1335

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

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

Parameters:

Returns:



1301
1302
1303
# File 'lib/oga/xpath/compiler.rb', line 1301

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

#resetObject

Resets the internal state.



54
55
56
57
58
59
# File 'lib/oga/xpath/compiler.rb', line 54

def reset
  @literal_id = 0

  @predicate_nodesets = []
  @predicate_indexes  = []
end

#return_nodeset?(ast) ⇒ TrueClass|FalseClass

Parameters:

  • ast (AST::Node)

Returns:

  • (TrueClass|FalseClass)


1533
1534
1535
# File 'lib/oga/xpath/compiler.rb', line 1533

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

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

Parameters:

  • name (String)
  • args (Array)

Returns:



1328
1329
1330
# File 'lib/oga/xpath/compiler.rb', line 1328

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:



1315
1316
1317
# File 'lib/oga/xpath/compiler.rb', line 1315

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

#string?(ast) ⇒ TrueClass|FalseClass

Parameters:

  • ast (AST::Node)

Returns:

  • (TrueClass|FalseClass)


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

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

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

Parameters:

  • value (String)

Returns:



1321
1322
1323
# File 'lib/oga/xpath/compiler.rb', line 1321

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

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

Parameters:

  • name (Symbol)
  • args (Array)

Returns:



1522
1523
1524
# File 'lib/oga/xpath/compiler.rb', line 1522

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

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

Parameters:

  • ast (AST::Node)

Returns:



1486
1487
1488
# File 'lib/oga/xpath/compiler.rb', line 1486

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

#trueOga::Ruby::Node

Returns:



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

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]


1411
1412
1413
1414
1415
1416
1417
# File 'lib/oga/xpath/compiler.rb', line 1411

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:



1307
1308
1309
1310
1311
# File 'lib/oga/xpath/compiler.rb', line 1307

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

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

#variables_literalOga::Ruby::Node

Returns:



1480
1481
1482
# File 'lib/oga/xpath/compiler.rb', line 1480

def variables_literal
  literal(:variables)
end