Class: AlternationOperator

Inherits:
RegexOperator show all
Defined in:
lib/ruby_grammar_builder/regex_operators/alternation.rb

Overview

Provides alternation as described by OrPattern

Instance Attribute Summary

Attributes inherited from RegexOperator

#association, #precedence

Instance Method Summary collapse

Methods inherited from RegexOperator

#==, evaluate, #fold_left, #fold_right

Constructor Details

#initializeAlternationOperator

Returns a new instance of AlternationOperator.



9
10
11
12
# File 'lib/ruby_grammar_builder/regex_operators/alternation.rb', line 9

def initialize
    @precedence = 1
    @association = :right
end

Instance Method Details

#do_evaluate_self(arr_left, arr_right) ⇒ Array<RegexOperator,String>

This method is abstract.

override to provide evaluate the operator

Note:

arr_left and arr_right contain the entire parse array use RegexOperator#fold_left and RegexOperator#fold_right to collect only the portions that this operator is responsible for

<Description>

Parameters:

Returns:



15
16
17
18
19
20
21
22
23
# File 'lib/ruby_grammar_builder/regex_operators/alternation.rb', line 15

def do_evaluate_self(arr_left, arr_right)
    left = fold_left(arr_left)
    # fold right is not applied as only the immediate right is a part of the alternation
    # (?:#{foo}) is not needed as alternation has the lowest precedence (in regex)
    # that could be generated (anything lower is required to be closed)
    self_string = "(?:#{left[0]}|#{arr_right[0]})"

    [left[1], self_string, arr_right[1..-1]].flatten
end