Class: Spectre::Operators::Union

Inherits:
Object
  • Object
show all
Includes:
Op
Defined in:
lib/spectre/base/operators.rb

Overview

Matches either the first or the second Node it receives as parameters Union.new p1, p2 is equal to p1 | p2. This operator applies the tactics of short-circuiting, i.e. if p1 matches, p2 will not be tried at all.

Instance Attribute Summary

Attributes included from Parser

#node

Instance Method Summary collapse

Methods included from Op

#pre_skip?

Methods included from Parser

#backtrack, #create_match, from_POD, #pre_skip?, #to_p

Instance Method Details

#inspectObject



122
123
124
# File 'lib/spectre/base/operators.rb', line 122

def inspect
    '(' + @node.left.inspect + ' | ' + @node.right.inspect + ')'
end

#scan(iter) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/spectre/base/operators.rb', line 87

def scan iter
    first = @node.left.parse iter

    if @node.policy[:union] == :normal
        if first
            create_match iter, first
        else
            backtrack iter
            create_match iter, @node.right.parse(iter)
        end
    else
        fiter = iter.dup
        backtrack iter
        second = @node.right.parse iter
        siter = iter.dup
        backtrack iter

        if first and second
            mm = [[first,fiter], [second,siter]].minmax_by { |x| x[0].length }
            winner = @node.policy[:union] == :longest ? mm[1] : mm[0]

            iter.to winner[1].pos
            create_match iter, winner[0]
        elsif first
            iter.to fiter.pos
            create_match iter, first
        elsif second
            iter.to siter.pos
            create_match iter, second
        else
            nil
        end
    end
end