macks-ruby_protobuf 0.3.2.2 → 0.3.2.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,4 @@
1
1
  class Protobuf::ProtoParser
2
- #options no_result_var
3
2
  rule
4
3
  proto : proto_item
5
4
  { result = Protobuf::Node::ProtoNode.new val }
@@ -162,42 +161,61 @@ end
162
161
 
163
162
  ---- inner
164
163
 
164
+ require 'strscan'
165
+
165
166
  def parse(f)
166
- @q = []
167
- f.each do |line|
168
- until line.empty? do
169
- case line
170
- when /\A\s+/, /\A\/\/.*/
171
- ;
172
- when /\A(required|optional|repeated|import|package|option|message|extend|enum|service|rpc|returns|group|default|extensions|to|max|double|float|int32|int64|uint32|uint64|sint32|sint64|fixed32|fixed64|sfixed32|sfixed64|bool|string|bytes)\b/
173
- @q.push [$&, $&.to_sym]
174
- when /\A[1-9]\d*(?!\.)/, /\A0(?![.xX0-9])/
175
- @q.push [:DEC_INTEGER, $&.to_i]
176
- when /\A0[xX]([A-Fa-f0-9])+/
177
- @q.push [:HEX_INTEGER, $&.to_i(0)]
178
- when /\A0[0-7]+/
179
- @q.push [:OCT_INTEGER, $&.to_i(0)]
180
- when /\A\d+(\.\d+)?([Ee][\+-]?\d+)?/
181
- @q.push [:FLOAT_LITERAL, $&.to_f]
182
- when /\A(true|false)/
183
- @q.push [:BOOLEAN_LITERAL, $& == 'true']
184
- when /\A"(?:[^"\\]+|\\.)*"/, /\A'(?:[^'\\]+|\\.)*'/
185
- @q.push [:STRING_LITERAL, eval($&)]
186
- when /\A[a-zA-Z_][\w_]*/
187
- @q.push [:IDENT, $&.to_sym]
188
- when /\A[A-Z][\w_]*/
189
- @q.push [:CAMEL_IDENT, $&.to_sym]
190
- when /\A./
191
- @q.push [$&, $&]
192
- else
193
- raise ArgumentError.new(line)
194
- end
195
- line = $'
167
+ @scanner = StringScanner.new(f.read)
168
+ yyparse(self, :scan)
169
+ end
170
+
171
+ def scan_debug
172
+ scan do |token, value|
173
+ p [token, value]
174
+ yield [token, value]
175
+ end
176
+ end
177
+
178
+ def scan
179
+ until @scanner.eos?
180
+ case
181
+ when match(/\s+/, /\/\/.*/)
182
+ # skip
183
+ when match(/\/\*/)
184
+ # C-like comment
185
+ raise 'EOF inside block comment' until @scanner.scan_until(/\*\//)
186
+ when match(/(?:required|optional|repeated|import|package|option|message|extend|enum|service|rpc|returns|group|default|extensions|to|max|double|float|int32|int64|uint32|uint64|sint32|sint64|fixed32|fixed64|sfixed32|sfixed64|bool|string|bytes)\b/)
187
+ yield [@token, @token.to_sym]
188
+ when match(/[1-9]\d*(?!\.)/, /0(?![.xX0-9])/)
189
+ yield [:DEC_INTEGER, @token.to_i]
190
+ when match(/0[xX]([A-Fa-f0-9])+/)
191
+ yield [:HEX_INTEGER, @token.to_i(0)]
192
+ when match(/0[0-7]+/)
193
+ yield [:OCT_INTEGER, @token.to_i(0)]
194
+ when match(/\d+(\.\d+)?([Ee][\+-]?\d+)?/)
195
+ yield [:FLOAT_LITERAL, @token.to_f]
196
+ when match(/(true|false)\b/)
197
+ yield [:BOOLEAN_LITERAL, @token == 'true']
198
+ when match(/"(?:[^"\\]+|\\.)*"/, /'(?:[^'\\]+|\\.)*'/)
199
+ yield [:STRING_LITERAL, eval(@token)]
200
+ when match(/[a-zA-Z_][\w_]*/)
201
+ yield [:IDENT, @token.to_sym]
202
+ when match(/[A-Z][\w_]*/)
203
+ yield [:CAMEL_IDENT, @token.to_sym]
204
+ when match(/./)
205
+ yield [@token, @token]
206
+ else
207
+ raise "parse error around #{@scanner.string[@scanner.pos, 32].inspect}"
196
208
  end
197
209
  end
198
- do_parse
210
+ yield [false, nil]
199
211
  end
200
212
 
201
- def next_token
202
- @q.shift
213
+ def match(*regular_expressions)
214
+ regular_expressions.each do |re|
215
+ if @scanner.scan(re)
216
+ @token = @scanner[0]
217
+ return true
218
+ end
219
+ end
220
+ false
203
221
  end