Class: BabelBridge::PatternElement

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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, options={})
  @init_options = options.clone
  @rule_variant = options[:rule_variant]
  @parser_class = options[:parser_class]
  @delimiter = options[:delimiter]
  @name = options[: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_matchObject

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

#delimiterObject

true if this is a delimiter



18
19
20
# File 'lib/babel_bridge/pattern_element.rb', line 18

def delimiter
  @delimiter
end

#matchObject

Returns the value of attribute match.



15
16
17
# File 'lib/babel_bridge/pattern_element.rb', line 15

def match
  @match
end

#nameObject

Returns the value of attribute name.



14
15
16
# File 'lib/babel_bridge/pattern_element.rb', line 14

def name
  @name
end

#negativeObject

Returns the value of attribute negative.



14
15
16
# File 'lib/babel_bridge/pattern_element.rb', line 14

def negative
  @negative
end

#optionalObject

Returns the value of attribute optional.



14
15
16
# File 'lib/babel_bridge/pattern_element.rb', line 14

def optional
  @optional
end

#parserObject

Returns the value of attribute parser.



14
15
16
# File 'lib/babel_bridge/pattern_element.rb', line 14

def parser
  @parser
end

#parser_classObject

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_variantObject

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

#terminalObject

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

#inspectObject



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

#rulesObject



49
50
51
# File 'lib/babel_bridge/pattern_element.rb', line 49

def rules
  parser_class.rules
end

#to_sObject



41
42
43
# File 'lib/babel_bridge/pattern_element.rb', line 41

def to_s
  match.inspect
end