Class: OneOfPattern
- Inherits:
-
PatternBase
- Object
- PatternBase
- OneOfPattern
- Defined in:
- lib/ruby_grammar_builder/pattern_extensions/one_of.rb
Overview
Provides alternation when one of the passed in patterns is accepted, this pattern is accepted
Instance Attribute Summary
Attributes inherited from PatternBase
#arguments, #match, #next_pattern, #original_arguments
Instance Method Summary collapse
-
#do_collect_self_groups(next_group) ⇒ Array<Hash>
Collect group information about self.
-
#do_evaluate_self(groups) ⇒ String
evaluates @match.
-
#initialize(patterns, deep_clone = nil, original_arguments = nil) ⇒ OneOfPattern
constructor
Create a new OneOfPattern.
-
#map!(map_includes = false) {|self| ... } ⇒ self
Uses a block to transform all Patterns in the list.
- #single_entity? ⇒ true
-
#to_s(depth = 0, top_level = true) ⇒ String
Displays the PatternBase as you would write it in code.
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_get_to_s_name, #each, #eql?, #evaluate, #evaluate_operator, #fixup_regex_references, #groupless, #groupless?, #hash, #insert, #insert!, #inspect, #lookAheadFor, #lookAheadToAvoid, #lookAround, #lookBehindFor, #lookBehindToAvoid, #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_self_tests, #run_tests, #self_scramble_references, #start_pattern, #then, #to_r, #to_tag, #transform_includes, #transform_tag_as, #zeroOrMoreOf
Constructor Details
#initialize(patterns, deep_clone = nil, original_arguments = nil) ⇒ OneOfPattern
Create a new OneOfPattern
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/ruby_grammar_builder/pattern_extensions/one_of.rb', line 14 def initialize(patterns, deep_clone = nil, original_arguments = nil) if deep_clone == :deep_clone super(patterns, deep_clone, original_arguments) return end unless patterns.is_a? Array raise <<-HEREDOC.remove_indent oneOf() expects an array of patterns, the provided argument is not an array. The arguments to oneOf is below #{patterns} HEREDOC end super( match: "one of", patterns: patterns.map do |pattern| next pattern if pattern.is_a? PatternBase PatternBase.new(pattern) end ) end |
Instance Method Details
#do_collect_self_groups(next_group) ⇒ Array<Hash>
Collect group information about self
51 52 53 54 55 56 57 58 59 |
# File 'lib/ruby_grammar_builder/pattern_extensions/one_of.rb', line 51 def do_collect_self_groups(next_group) groups = [] @arguments[:patterns].each do |pattern| pat_groups = pattern.collect_group_attributes(next_group) groups.concat(pat_groups) next_group += pat_groups.length end groups end |
#do_evaluate_self(groups) ⇒ String
optionally override when inheriting
by default this optionally adds a capture group
evaluates @match
37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/ruby_grammar_builder/pattern_extensions/one_of.rb', line 37 def do_evaluate_self(groups) patterns_strings = @arguments[:patterns].map do |pattern| regex = pattern.evaluate(groups) next regex if pattern.single_entity? "(?:#{regex})" end return "(#{patterns_strings.join '|'})" if needs_to_capture? "(?:#{patterns_strings.join '|'})" end |
#map!(map_includes = false) {|self| ... } ⇒ self
Uses a block to transform all Patterns in the list
68 69 70 71 72 73 |
# File 'lib/ruby_grammar_builder/pattern_extensions/one_of.rb', line 68 def map!(map_includes = false, &block) @arguments[:patterns].map! { |p| p.map!(map_includes, &block) } @next_pattern.map!(map_includes, &block) if @next_pattern.is_a? PatternBase map_includes!(&block) if map_includes self end |
#single_entity? ⇒ true
63 64 65 |
# File 'lib/ruby_grammar_builder/pattern_extensions/one_of.rb', line 63 def single_entity? true end |
#to_s(depth = 0, top_level = true) ⇒ String
Displays the PatternBase as you would write it in code
76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/ruby_grammar_builder/pattern_extensions/one_of.rb', line 76 def to_s(depth = 0, top_level = true) indent = " " * depth output = top_level ? "oneOf([" : ".oneOf([" output += "\n#{indent} " output += (@arguments[:patterns].map do |pattern| pattern.to_s(depth + 1, true).lstrip end).join ",\n#{indent} " output += "\n#{indent}])" output += @next_pattern.to_s(depth, false).lstrip if @next_pattern output end |