lexr 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/lexr.rb +14 -11
- metadata +3 -3
data/lib/lexr.rb
CHANGED
@@ -11,22 +11,25 @@ class Lexr
|
|
11
11
|
@position = 0
|
12
12
|
end
|
13
13
|
|
14
|
-
def next
|
14
|
+
def next
|
15
15
|
return @current = Lexr::Token.end if @position >= @text.length
|
16
16
|
@rules.each do |rule|
|
17
|
-
if result = rule.pattern.instance_of?(Regexp) ? regexp_match(rule.pattern
|
17
|
+
if result = rule.pattern.instance_of?(Regexp) ? regexp_match(rule.pattern) : literal_match(rule.pattern)
|
18
18
|
result = rule.converter[result] if rule.converter
|
19
|
-
return self.send
|
20
|
-
|
21
|
-
return @current = token unless peeking
|
22
|
-
return token
|
19
|
+
return self.send :next if rule.ignore?
|
20
|
+
return @current = Lexr::Token.new(result, rule.symbol)
|
23
21
|
end
|
24
22
|
end
|
25
23
|
raise Lexr::UnmatchableTextError.new(unprocessed_text[0..0], @position)
|
26
24
|
end
|
27
25
|
|
28
26
|
def peek
|
29
|
-
|
27
|
+
pos = @position
|
28
|
+
cur = @current
|
29
|
+
result = self.send :next
|
30
|
+
@position = pos
|
31
|
+
@current = cur
|
32
|
+
result
|
30
33
|
end
|
31
34
|
|
32
35
|
def current
|
@@ -43,15 +46,15 @@ class Lexr
|
|
43
46
|
@text[@position..-1]
|
44
47
|
end
|
45
48
|
|
46
|
-
def regexp_match(regex
|
49
|
+
def regexp_match(regex)
|
47
50
|
return nil unless m = unprocessed_text.match(/^#{regex}/)
|
48
|
-
@position += m.end(0)
|
51
|
+
@position += m.end(0)
|
49
52
|
m[0]
|
50
53
|
end
|
51
54
|
|
52
|
-
def literal_match(lit
|
55
|
+
def literal_match(lit)
|
53
56
|
return nil unless unprocessed_text[0..lit.length-1] == lit
|
54
|
-
@position += lit.length
|
57
|
+
@position += lit.length
|
55
58
|
lit
|
56
59
|
end
|
57
60
|
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lexr
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 19
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 2
|
9
|
-
-
|
10
|
-
version: 0.2.
|
9
|
+
- 2
|
10
|
+
version: 0.2.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Michael Baldry
|