Class: BabelBridge::PatternElement
- Inherits:
-
Object
- Object
- BabelBridge::PatternElement
- Defined in:
- lib/pattern_element.rb
Overview
PatternElement provides optimized parsing for each Element of a pattern PatternElement provides all the logic for parsing:
:many
:optional
Instance Attribute Summary collapse
-
#could_match ⇒ Object
Returns the value of attribute could_match.
-
#delimiter ⇒ Object
true if this is a delimiter.
-
#match ⇒ Object
Returns the value of attribute match.
-
#name ⇒ Object
Returns the value of attribute name.
-
#negative ⇒ Object
Returns the value of attribute negative.
-
#optional ⇒ Object
Returns the value of attribute optional.
-
#parser ⇒ Object
Returns the value of attribute parser.
-
#parser_class ⇒ Object
Returns the value of attribute parser_class.
-
#rule_variant ⇒ Object
Returns the value of attribute rule_variant.
-
#terminal ⇒ Object
Returns the value of attribute terminal.
Instance Method Summary collapse
-
#initialize(match, options = {}) ⇒ PatternElement
constructor
match can be: true, Hash, Symbol, String, Regexp options :rule_varient :parser.
- #inspect ⇒ Object
-
#parse(parent_node) ⇒ Object
attempt to match the pattern defined in self.parser in parent_node.src starting at offset parent_node.next.
- #rules ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(match, options = {}) ⇒ PatternElement
match can be: true, Hash, Symbol, String, Regexp options
:rule_varient
:parser
43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/pattern_element.rb', line 43 def initialize(match, ={}) @init_options = .clone @rule_variant = [:rule_variant] @parser_class = [:parser_class] @delimiter = [:delimiter] @name = [:name] raise "rule_variant or parser_class required" unless @rule_variant || @parser_class init match raise "pattern element cannot be both :dont and :optional" if negative && optional end |
Instance Attribute Details
#could_match ⇒ Object
Returns the value of attribute could_match.
32 33 34 |
# File 'lib/pattern_element.rb', line 32 def could_match @could_match end |
#delimiter ⇒ Object
true if this is a delimiter
36 37 38 |
# File 'lib/pattern_element.rb', line 36 def delimiter @delimiter end |
#match ⇒ Object
Returns the value of attribute match.
33 34 35 |
# File 'lib/pattern_element.rb', line 33 def match @match end |
#name ⇒ Object
Returns the value of attribute name.
32 33 34 |
# File 'lib/pattern_element.rb', line 32 def name @name end |
#negative ⇒ Object
Returns the value of attribute negative.
32 33 34 |
# File 'lib/pattern_element.rb', line 32 def negative @negative end |
#optional ⇒ Object
Returns the value of attribute optional.
32 33 34 |
# File 'lib/pattern_element.rb', line 32 def optional @optional end |
#parser ⇒ Object
Returns the value of attribute parser.
32 33 34 |
# File 'lib/pattern_element.rb', line 32 def parser @parser end |
#parser_class ⇒ Object
Returns the value of attribute parser_class.
33 34 35 |
# File 'lib/pattern_element.rb', line 33 def parser_class @parser_class end |
#rule_variant ⇒ Object
Returns the value of attribute rule_variant.
33 34 35 |
# File 'lib/pattern_element.rb', line 33 def rule_variant @rule_variant end |
#terminal ⇒ Object
Returns the value of attribute terminal.
32 33 34 |
# File 'lib/pattern_element.rb', line 32 def terminal @terminal end |
Instance Method Details
#inspect ⇒ Object
55 56 57 |
# File 'lib/pattern_element.rb', line 55 def inspect "<PatternElement #{rule_variant && "rule_variant=#{rule_variant.variant_node_class} "}match=#{match.inspect}#{" delimiter" if delimiter}>" end |
#parse(parent_node) ⇒ Object
attempt to match the pattern defined in self.parser in parent_node.src starting at offset parent_node.next
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 98 99 100 |
# File 'lib/pattern_element.rb', line 72 def parse(parent_node) # run element parser begin parent_node.parser.matching_negative if negative match = parser.call(parent_node) ensure parent_node.parser.unmatching_negative if negative end # Negative patterns (PEG: !element) match = match ? nil : EmptyNode.new(parent_node) if negative # Optional patterns (PEG: element?) match = EmptyNode.new(parent_node) if !match && optional # Could-match patterns (PEG: &element) match.match_length = 0 if match && could_match if !match && (terminal || negative) # log failures on Terminal patterns for debug output if overall parse fails parent_node.parser.log_parsing_failure parent_node.next, :pattern => self.match, :node => parent_node end match.delimiter = delimiter if match # return match match end |
#rules ⇒ Object
67 68 69 |
# File 'lib/pattern_element.rb', line 67 def rules parser_class.rules end |
#to_s ⇒ Object
59 60 61 |
# File 'lib/pattern_element.rb', line 59 def to_s match.inspect end |