Class: LexicalRule
- Inherits:
-
Object
- Object
- LexicalRule
- Defined in:
- lib/lexical_analyzer/lexical_rule.rb
Overview
The RCTP class for lexical rules.
Instance Attribute Summary collapse
-
#regex ⇒ Object
readonly
Read access to the rule’s regex.
-
#symbol ⇒ Object
readonly
Read access to the rule’s symbol.
Instance Method Summary collapse
-
#call(value) ⇒ Object
The default rule action.
-
#initialize(symbol, regex, &action) ⇒ LexicalRule
constructor
Create a lexical rule.
-
#match(text) ⇒ Object
Does this rule match?.
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
#regex ⇒ Object (readonly)
Read access to the rule’s regex.
7 8 9 |
# File 'lib/lexical_analyzer/lexical_rule.rb', line 7 def regex @regex end |
#symbol ⇒ Object (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 |