Class: OrPattern

Inherits:
PatternBase show all
Defined in:
lib/ruby_grammar_builder/pattern_extensions/or_pattern.rb

Overview

Note:

OneOfPattern is likely just as powerful and less confusing

Provides alternation Either the previous pattern or provided pattern is accepted

Instance Attribute Summary

Attributes inherited from PatternBase

#arguments, #match, #next_pattern, #original_arguments

Instance Method Summary collapse

Methods inherited from PatternBase

#==, #__deep_clone__, #__deep_clone_self__, #collect_group_attributes, #convert_group_attributes_to_captures, #convert_includes_to_patterns, #do_add_attributes, #do_collect_self_groups, #do_evaluate_self, #each, #eql?, #fixup_regex_references, #groupless, #groupless?, #hash, #initialize, #insert, #insert!, #inspect, #lookAheadFor, #lookAheadToAvoid, #lookAround, #lookBehindFor, #lookBehindToAvoid, #map, #map!, #map_includes!, #matchResultOf, #maybe, #name, #needs_to_capture?, #oneOf, #oneOrMoreOf, #optimize_outer_group?, #or, #placeholder, #raise_if_regex_has_capture_group, #reTag, #recursivelyMatch, #resolve, #run_tests, #self_scramble_references, #start_pattern, #then, #to_r, #to_s, #to_tag, #transform_includes, #transform_tag_as, #zeroOrMoreOf

Constructor Details

This class inherits a constructor from PatternBase

Instance Method Details

#do_get_to_s_name(top_level) ⇒ String

What is the name of the method that the user would call top_level is if a freestanding or chaining function is called called by #to_s

Parameters:

  • top_level (Boolean)

    is this top_level or chained

Returns:

  • (String)

    the name of the method



84
85
86
# File 'lib/ruby_grammar_builder/pattern_extensions/or_pattern.rb', line 84

def do_get_to_s_name(top_level)
    top_level ? "or(" : ".or("
end

#evaluate(*_ignored) ⇒ void

This method returns an undefined value.

Raises an error to prevent use as initial type

Parameters:

  • _ignored

    ignored



79
80
81
# File 'lib/ruby_grammar_builder/pattern_extensions/or_pattern.rb', line 79

def evaluate(*_ignored)
    raise "evaluate is not implemented for OrPattern"
end

#evaluate_operatorRegexOperator, AlternationOperator

Returns the operator to use when evaluating

Returns:



14
15
16
# File 'lib/ruby_grammar_builder/pattern_extensions/or_pattern.rb', line 14

def evaluate_operator
    AlternationOperator.new
end

#run_self_testsObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/ruby_grammar_builder/pattern_extensions/or_pattern.rb', line 18

def run_self_tests
    pass = [true]

    # some patterns are not able to be evaluated
    # do not attempt to unless required
    return true unless [
        :should_fully_match,
        :should_not_fully_match,
        :should_partially_match,
        :should_not_partially_match,
    ].any? { |k| @arguments.include? k }
    
    copy = @match.__deep_clone_self__
    test_regex = copy.to_r
    test_fully_regex = wrap_with_anchors(copy).to_r

    warn = lambda do |symbol|
        puts [
            "",
            "When testing the pattern #{test_regex.inspect}. The unit test for #{symbol} failed.",
            "The unit test has the following patterns:",
            "#{@arguments[symbol].to_yaml}",
            "The Failing pattern is below:",
            "#{self}",
        ].join("\n")
    end
    if @arguments[:should_fully_match].is_a? Array
        unless @arguments[:should_fully_match].all? { |test| test =~ test_fully_regex }
            warn.call :should_fully_match
            pass << false
        end
    end
    if @arguments[:should_not_fully_match].is_a? Array
        unless @arguments[:should_not_fully_match].none? { |test| test =~ test_fully_regex }
            warn.call :should_not_fully_match
            pass << false
        end
    end
    if @arguments[:should_partially_match].is_a? Array
        unless @arguments[:should_partially_match].all? { |test| test =~ test_regex }
            warn.call :should_partially_match
            pass << false
        end
    end
    if @arguments[:should_not_partially_match].is_a? Array
        unless @arguments[:should_not_partially_match].none? { |test| test =~ test_regex }
            warn.call :should_not_partially_match
            pass << false
        end
    end

    pass.none?(&:!)
end

#single_entity?true

Returns:

  • (true)


90
91
92
# File 'lib/ruby_grammar_builder/pattern_extensions/or_pattern.rb', line 90

def single_entity?
    true
end