Class: LexicalAnalyzer

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

Overview

The RCTP class for lexical analysis.

Constant Summary collapse

VERSION =
"0.3.5".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text: "", rules: []) ⇒ LexicalAnalyzer

Set things up.



13
14
15
16
# File 'lib/lexical_analyzer.rb', line 13

def initialize(text: "", rules: [])
  @text  = text
  @rules = rules
end

Instance Attribute Details

#rulesObject (readonly)

Access the array of lexical rules.



10
11
12
# File 'lib/lexical_analyzer.rb', line 10

def rules
  @rules
end

#textObject (readonly)

Access the text in the analyzer.



9
10
11
# File 'lib/lexical_analyzer.rb', line 9

def text
  @text
end

Instance Method Details

#get(extra = nil) ⇒ Object

Get the next lexical token



26
27
28
29
30
31
32
33
34
35
# File 'lib/lexical_analyzer.rb', line 26

def get(extra=nil)
  (extra ? rules + extra : rules).each do |rule|
    if match_data = rule.match(text)
      @text = match_data.post_match
      return rule.call(match_data.to_s) || get
    end
  end

  false
end

#renew(text: @text, rules: @rules) ⇒ Object

Reuse an existing lexical analyzer.



19
20
21
22
23
# File 'lib/lexical_analyzer.rb', line 19

def renew(text: @text, rules: @rules)
  @text  = text
  @rules = rules
  self
end