lexr 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/lexr.rb +32 -17
  2. metadata +6 -6
data/lib/lexr.rb CHANGED
@@ -14,11 +14,10 @@ class Lexr
14
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) : literal_match(rule.pattern)
18
- result = rule.converter[result] if rule.converter
19
- return self.send :next if rule.ignore?
20
- return @current = Lexr::Token.new(result, rule.symbol)
21
- end
17
+ next unless result = rule.match(unprocessed_text)
18
+ @position += result.characters_matched
19
+ return self.next if rule.ignore?
20
+ return @current = result.token
22
21
  end
23
22
  raise Lexr::UnmatchableTextError.new(unprocessed_text[0..0], @position)
24
23
  end
@@ -46,18 +45,6 @@ class Lexr
46
45
  @text[@position..-1]
47
46
  end
48
47
 
49
- def regexp_match(regex)
50
- return nil unless m = unprocessed_text.match(/^#{regex}/)
51
- @position += m.end(0)
52
- m[0]
53
- end
54
-
55
- def literal_match(lit)
56
- return nil unless unprocessed_text[0..lit.length-1] == lit
57
- @position += lit.length
58
- lit
59
- end
60
-
61
48
  class Token
62
49
  attr_reader :value, :type
63
50
 
@@ -87,6 +74,13 @@ class Lexr
87
74
  def initialize(pattern, symbol, opts = {})
88
75
  @pattern, @symbol, @opts = pattern, symbol, opts
89
76
  end
77
+
78
+ def match(text)
79
+ text_matched = self.send :"#{pattern.class.name.downcase}_matcher", text
80
+ return nil unless text_matched
81
+ value = converter ? converter[text_matched] : text_matched
82
+ Lexr::MatchData.new(text_matched.length, Lexr::Token.new(value, symbol))
83
+ end
90
84
 
91
85
  def ==(other)
92
86
  @pattern == other.pattern &&
@@ -94,6 +88,18 @@ class Lexr
94
88
  @opts[:convert_with] == other.converter &&
95
89
  @opts[:ignore] == other.ignore?
96
90
  end
91
+
92
+ private
93
+
94
+ def string_matcher(text)
95
+ return nil unless text[0..pattern.length-1] == pattern
96
+ pattern
97
+ end
98
+
99
+ def regexp_matcher(text)
100
+ return nil unless m = text.match(/\A#{pattern}/)
101
+ m[0]
102
+ end
97
103
  end
98
104
 
99
105
  class Dsl
@@ -132,4 +138,13 @@ class Lexr
132
138
  message
133
139
  end
134
140
  end
141
+
142
+ class MatchData
143
+ attr_reader :characters_matched, :token
144
+
145
+ def initialize(characters_matched, token)
146
+ @characters_matched = characters_matched
147
+ @token = token
148
+ end
149
+ end
135
150
  end
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: 19
5
- prerelease: false
4
+ hash: 17
5
+ prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 2
10
- version: 0.2.2
9
+ - 3
10
+ version: 0.2.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Michael Baldry
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-11-24 00:00:00 +00:00
18
+ date: 2011-05-17 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -74,7 +74,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
74
74
  requirements: []
75
75
 
76
76
  rubyforge_project:
77
- rubygems_version: 1.3.7
77
+ rubygems_version: 1.6.2
78
78
  signing_key:
79
79
  specification_version: 3
80
80
  summary: A lightweight and pretty lexical analyser