Class: BabelBridge::PatternElement
- Inherits:
-
Object
- Object
- BabelBridge::PatternElement
- Defined in:
- lib/babel_bridge/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
25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/babel_bridge/pattern_element.rb', line 25 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.
14 15 16 |
# File 'lib/babel_bridge/pattern_element.rb', line 14 def could_match @could_match end |
#delimiter ⇒ Object
true if this is a delimiter
18 19 20 |
# File 'lib/babel_bridge/pattern_element.rb', line 18 def delimiter @delimiter end |
#match ⇒ Object
Returns the value of attribute match.
15 16 17 |
# File 'lib/babel_bridge/pattern_element.rb', line 15 def match @match end |
#name ⇒ Object
Returns the value of attribute name.
14 15 16 |
# File 'lib/babel_bridge/pattern_element.rb', line 14 def name @name end |
#negative ⇒ Object
Returns the value of attribute negative.
14 15 16 |
# File 'lib/babel_bridge/pattern_element.rb', line 14 def negative @negative end |
#optional ⇒ Object
Returns the value of attribute optional.
14 15 16 |
# File 'lib/babel_bridge/pattern_element.rb', line 14 def optional @optional end |
#parser ⇒ Object
Returns the value of attribute parser.
14 15 16 |
# File 'lib/babel_bridge/pattern_element.rb', line 14 def parser @parser end |
#parser_class ⇒ Object
Returns the value of attribute parser_class.
15 16 17 |
# File 'lib/babel_bridge/pattern_element.rb', line 15 def parser_class @parser_class end |
#rule_variant ⇒ Object
Returns the value of attribute rule_variant.
15 16 17 |
# File 'lib/babel_bridge/pattern_element.rb', line 15 def rule_variant @rule_variant end |
#terminal ⇒ Object
Returns the value of attribute terminal.
14 15 16 |
# File 'lib/babel_bridge/pattern_element.rb', line 14 def terminal @terminal end |
Instance Method Details
#inspect ⇒ Object
37 38 39 |
# File 'lib/babel_bridge/pattern_element.rb', line 37 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
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/babel_bridge/pattern_element.rb', line 54 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.to_s, :node => parent_node end match.delimiter = delimiter if match # return match match end |
#rules ⇒ Object
49 50 51 |
# File 'lib/babel_bridge/pattern_element.rb', line 49 def rules parser_class.rules end |
#to_s ⇒ Object
41 42 43 |
# File 'lib/babel_bridge/pattern_element.rb', line 41 def to_s match.inspect end |