Class: Lexr::Rule

Inherits:
Object
  • Object
show all
Defined in:
lib/lexr.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pattern, symbol, opts = {}) ⇒ Rule

Returns a new instance of Rule.



74
75
76
# File 'lib/lexr.rb', line 74

def initialize(pattern, symbol, opts = {})
  @pattern, @symbol, @opts = pattern, symbol, opts
end

Instance Attribute Details

#patternObject (readonly)

Returns the value of attribute pattern.



69
70
71
# File 'lib/lexr.rb', line 69

def pattern
  @pattern
end

#symbolObject (readonly)

Returns the value of attribute symbol.



69
70
71
# File 'lib/lexr.rb', line 69

def symbol
  @symbol
end

Instance Method Details

#==(other) ⇒ Object



86
87
88
89
90
91
# File 'lib/lexr.rb', line 86

def ==(other)
  @pattern == other.pattern && 
    @symbol == other.symbol && 
    @opts[:convert_with] == other.converter && 
    @opts[:ignore] == other.ignore?
end

#converterObject



71
# File 'lib/lexr.rb', line 71

def converter ; @opts[:convert_with] ; end

#ignore?Boolean

Returns:

  • (Boolean)


72
# File 'lib/lexr.rb', line 72

def ignore? ; @opts[:ignore] ; end

#match(text, previous_token = nil) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/lexr.rb', line 78

def match(text, previous_token = nil)
  text_matched = self.send :"#{pattern.class.name.downcase}_matcher", text
  return nil unless text_matched
  return nil if predecate && !predecate[previous_token]
  value = converter ? converter[text_matched] : text_matched
  Lexr::MatchData.new(text_matched.length, Lexr::Token.new(value, symbol))
end