Class: Lexr
- Inherits:
-
Object
show all
- Defined in:
- lib/lexr.rb
Defined Under Namespace
Classes: Dsl, MatchData, Rule, Token, UnmatchableTextError
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(text, rules) ⇒ Lexr
Returns a new instance of Lexr.
8
9
10
11
12
|
# File 'lib/lexr.rb', line 8
def initialize(text, rules)
@text, @rules = text, rules
@current = nil
@position = 0
end
|
Class Method Details
.that(&block) ⇒ Object
2
3
4
5
6
|
# File 'lib/lexr.rb', line 2
def self.that(&block)
dsl = Lexr::Dsl.new
block.arity == 1 ? block[dsl] : dsl.instance_eval(&block)
dsl
end
|
Instance Method Details
#current ⇒ Object
34
35
36
|
# File 'lib/lexr.rb', line 34
def current
@current
end
|
#end? ⇒ Boolean
38
39
40
|
# File 'lib/lexr.rb', line 38
def end?
@current == Lexr::Token.end
end
|
#next ⇒ Object
14
15
16
17
18
19
20
21
22
23
|
# File 'lib/lexr.rb', line 14
def next
return @current = Lexr::Token.end if @position >= @text.length
@rules.each do |rule|
next unless result = rule.match(unprocessed_text)
@position += result.characters_matched
return self.next if rule.ignore?
return @current = result.token
end
raise Lexr::UnmatchableTextError.new(unprocessed_text[0..0], @position)
end
|
#peek ⇒ Object
25
26
27
28
29
30
31
32
|
# File 'lib/lexr.rb', line 25
def peek
pos = @position
cur = @current
result = self.send :next
@position = pos
@current = cur
result
end
|