Class: LexicalRule

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

Overview

The RCTP class for lexical rules.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(symbol, regex, &action) ⇒ LexicalRule

Create a lexical rule.



10
11
12
13
14
15
# File 'lib/lexical_analyzer/lexical_rule.rb', line 10

def initialize(symbol, regex, &action)
  @symbol = symbol
  @regex  = regex

  define_singleton_method(:call, &action) if block_given?
end

Instance Attribute Details

#regexObject (readonly)

Read access to the rule’s regex.



7
8
9
# File 'lib/lexical_analyzer/lexical_rule.rb', line 7

def regex
  @regex
end

#symbolObject (readonly)

Read access to the rule’s symbol.



6
7
8
# File 'lib/lexical_analyzer/lexical_rule.rb', line 6

def symbol
  @symbol
end

Instance Method Details

#call(value) ⇒ Object

The default rule action.



23
24
25
# File 'lib/lexical_analyzer/lexical_rule.rb', line 23

def call(value)
  [symbol, value]
end

#match(text) ⇒ Object

Does this rule match?



18
19
20
# File 'lib/lexical_analyzer/lexical_rule.rb', line 18

def match(text)
  text.match(@regex)
end