ruby-ll 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,582 @@
1
+
2
+ # line 1 "lib/ll/lexer.rl"
3
+
4
+ # line 3 "lib/ll/lexer.rl"
5
+ module LL
6
+ ##
7
+ # Ragel lexer for LL grammar files.
8
+ #
9
+ class Lexer
10
+
11
+ # line 12 "lib/ll/lexer.rb"
12
+ class << self
13
+ attr_accessor :_ll_lexer_trans_keys
14
+ private :_ll_lexer_trans_keys, :_ll_lexer_trans_keys=
15
+ end
16
+ self._ll_lexer_trans_keys = [
17
+ 0, 0, 104, 116, 101, 101,
18
+ 97, 97, 100, 100, 101,
19
+ 101, 114, 114, 110, 110,
20
+ 110, 110, 101, 101, 114, 114,
21
+ 97, 97, 109, 109, 101,
22
+ 101, 101, 101, 114, 114,
23
+ 109, 109, 105, 105, 110, 110,
24
+ 97, 97, 108, 108, 115,
25
+ 115, 0, 127, 0, 127,
26
+ 10, 10, 10, 125, 0
27
+ ]
28
+
29
+ class << self
30
+ attr_accessor :_ll_lexer_key_spans
31
+ private :_ll_lexer_key_spans, :_ll_lexer_key_spans=
32
+ end
33
+ self._ll_lexer_key_spans = [
34
+ 0, 13, 1, 1, 1, 1, 1, 1,
35
+ 1, 1, 1, 1, 1, 1, 1, 1,
36
+ 1, 1, 1, 1, 1, 1, 128, 128,
37
+ 1, 116
38
+ ]
39
+
40
+ class << self
41
+ attr_accessor :_ll_lexer_index_offsets
42
+ private :_ll_lexer_index_offsets, :_ll_lexer_index_offsets=
43
+ end
44
+ self._ll_lexer_index_offsets = [
45
+ 0, 0, 14, 16, 18, 20, 22, 24,
46
+ 26, 28, 30, 32, 34, 36, 38, 40,
47
+ 42, 44, 46, 48, 50, 52, 54, 183,
48
+ 312, 314
49
+ ]
50
+
51
+ class << self
52
+ attr_accessor :_ll_lexer_indicies
53
+ private :_ll_lexer_indicies, :_ll_lexer_indicies=
54
+ end
55
+ self._ll_lexer_indicies = [
56
+ 0, 2, 1, 1, 1, 1, 3, 1,
57
+ 1, 1, 1, 1, 4, 1, 5, 1,
58
+ 6, 1, 7, 1, 8, 1, 9, 1,
59
+ 10, 1, 11, 1, 12, 1, 13, 1,
60
+ 14, 1, 15, 1, 16, 1, 17, 1,
61
+ 18, 1, 19, 1, 20, 1, 21, 1,
62
+ 22, 1, 23, 1, 24, 1, 1, 1,
63
+ 1, 1, 1, 1, 1, 1, 1, 26,
64
+ 27, 1, 1, 1, 1, 1, 1, 1,
65
+ 1, 1, 1, 1, 1, 1, 1, 1,
66
+ 1, 1, 1, 1, 1, 1, 26, 1,
67
+ 1, 28, 1, 29, 1, 1, 1, 1,
68
+ 1, 1, 1, 1, 1, 1, 25, 25,
69
+ 25, 25, 25, 25, 25, 25, 25, 25,
70
+ 30, 31, 1, 32, 1, 1, 1, 25,
71
+ 25, 25, 25, 25, 25, 25, 25, 25,
72
+ 25, 25, 25, 25, 25, 25, 25, 25,
73
+ 25, 25, 25, 25, 25, 25, 25, 25,
74
+ 25, 1, 1, 1, 1, 33, 1, 25,
75
+ 25, 25, 25, 25, 25, 25, 25, 25,
76
+ 25, 25, 25, 25, 25, 25, 25, 25,
77
+ 25, 25, 25, 25, 25, 25, 25, 25,
78
+ 25, 34, 35, 1, 1, 1, 25, 36,
79
+ 36, 36, 36, 36, 36, 36, 36, 36,
80
+ 36, 36, 36, 36, 36, 36, 36, 36,
81
+ 36, 36, 36, 36, 36, 36, 36, 36,
82
+ 36, 36, 36, 36, 36, 36, 36, 36,
83
+ 36, 36, 36, 36, 36, 36, 36, 36,
84
+ 36, 36, 36, 36, 36, 36, 36, 25,
85
+ 25, 25, 25, 25, 25, 25, 25, 25,
86
+ 25, 36, 36, 36, 36, 36, 36, 36,
87
+ 25, 25, 25, 25, 25, 25, 25, 25,
88
+ 25, 25, 25, 25, 25, 25, 25, 25,
89
+ 25, 25, 25, 25, 25, 25, 25, 25,
90
+ 25, 25, 36, 36, 36, 36, 25, 36,
91
+ 25, 25, 25, 25, 25, 25, 25, 25,
92
+ 25, 25, 25, 25, 25, 25, 25, 25,
93
+ 25, 25, 25, 25, 25, 25, 25, 25,
94
+ 25, 25, 36, 36, 36, 36, 36, 25,
95
+ 37, 28, 39, 38, 38, 38, 38, 38,
96
+ 38, 38, 38, 38, 38, 38, 38, 38,
97
+ 38, 38, 38, 38, 38, 38, 38, 38,
98
+ 38, 38, 38, 38, 38, 38, 38, 38,
99
+ 38, 38, 38, 38, 38, 38, 38, 38,
100
+ 38, 38, 38, 38, 38, 38, 38, 38,
101
+ 38, 38, 38, 38, 38, 38, 38, 38,
102
+ 38, 38, 38, 38, 38, 38, 38, 38,
103
+ 38, 38, 38, 38, 38, 38, 38, 38,
104
+ 38, 38, 38, 38, 38, 38, 38, 38,
105
+ 38, 38, 38, 38, 38, 38, 38, 38,
106
+ 38, 38, 38, 38, 38, 38, 38, 38,
107
+ 38, 38, 38, 38, 38, 38, 38, 38,
108
+ 38, 38, 38, 38, 38, 38, 38, 38,
109
+ 38, 38, 38, 40, 38, 41, 38, 0
110
+ ]
111
+
112
+ class << self
113
+ attr_accessor :_ll_lexer_trans_targs
114
+ private :_ll_lexer_trans_targs, :_ll_lexer_trans_targs=
115
+ end
116
+ self._ll_lexer_trans_targs = [
117
+ 2, 0, 7, 11, 14, 3, 4, 5,
118
+ 6, 22, 8, 9, 10, 22, 12, 13,
119
+ 22, 15, 16, 17, 18, 19, 20, 21,
120
+ 22, 23, 22, 22, 24, 1, 22, 22,
121
+ 22, 23, 22, 22, 22, 22, 25, 25,
122
+ 25, 25
123
+ ]
124
+
125
+ class << self
126
+ attr_accessor :_ll_lexer_trans_actions
127
+ private :_ll_lexer_trans_actions, :_ll_lexer_trans_actions=
128
+ end
129
+ self._ll_lexer_trans_actions = [
130
+ 0, 0, 0, 0, 0, 0, 0, 0,
131
+ 0, 1, 0, 0, 0, 2, 0, 0,
132
+ 3, 0, 0, 0, 0, 0, 0, 0,
133
+ 4, 7, 8, 9, 0, 0, 10, 11,
134
+ 12, 13, 14, 15, 16, 17, 18, 19,
135
+ 20, 21
136
+ ]
137
+
138
+ class << self
139
+ attr_accessor :_ll_lexer_to_state_actions
140
+ private :_ll_lexer_to_state_actions, :_ll_lexer_to_state_actions=
141
+ end
142
+ self._ll_lexer_to_state_actions = [
143
+ 0, 0, 0, 0, 0, 0, 0, 0,
144
+ 0, 0, 0, 0, 0, 0, 0, 0,
145
+ 0, 0, 0, 0, 0, 0, 5, 0,
146
+ 0, 5
147
+ ]
148
+
149
+ class << self
150
+ attr_accessor :_ll_lexer_from_state_actions
151
+ private :_ll_lexer_from_state_actions, :_ll_lexer_from_state_actions=
152
+ end
153
+ self._ll_lexer_from_state_actions = [
154
+ 0, 0, 0, 0, 0, 0, 0, 0,
155
+ 0, 0, 0, 0, 0, 0, 0, 0,
156
+ 0, 0, 0, 0, 0, 0, 6, 0,
157
+ 0, 6
158
+ ]
159
+
160
+ class << self
161
+ attr_accessor :_ll_lexer_eof_trans
162
+ private :_ll_lexer_eof_trans, :_ll_lexer_eof_trans=
163
+ end
164
+ self._ll_lexer_eof_trans = [
165
+ 0, 0, 0, 0, 0, 0, 0, 0,
166
+ 0, 0, 0, 0, 0, 0, 0, 0,
167
+ 0, 0, 0, 0, 0, 0, 0, 37,
168
+ 38, 0
169
+ ]
170
+
171
+ class << self
172
+ attr_accessor :ll_lexer_start
173
+ end
174
+ self.ll_lexer_start = 22;
175
+ class << self
176
+ attr_accessor :ll_lexer_first_final
177
+ end
178
+ self.ll_lexer_first_final = 22;
179
+ class << self
180
+ attr_accessor :ll_lexer_error
181
+ end
182
+ self.ll_lexer_error = 0;
183
+
184
+ class << self
185
+ attr_accessor :ll_lexer_en_ruby_body
186
+ end
187
+ self.ll_lexer_en_ruby_body = 25;
188
+ class << self
189
+ attr_accessor :ll_lexer_en_main
190
+ end
191
+ self.ll_lexer_en_main = 22;
192
+
193
+
194
+ # line 9 "lib/ll/lexer.rl"
195
+
196
+ # % fix highlight
197
+
198
+ ##
199
+ # @param [String] data The data to lex.
200
+ # @param [String] file The name of the input file.
201
+ #
202
+ def initialize(data, file = SourceLine::DEFAULT_FILE)
203
+ @data = data
204
+ @file = file
205
+
206
+ reset
207
+ end
208
+
209
+ ##
210
+ # Gathers all the tokens for the input and returns them as an Array.
211
+ #
212
+ # @see [#advance]
213
+ # @return [Array]
214
+ #
215
+ def lex
216
+ tokens = []
217
+
218
+ advance do |token|
219
+ tokens << token
220
+ end
221
+
222
+ return tokens
223
+ end
224
+
225
+ ##
226
+ # Resets the internal state of the lexer.
227
+ #
228
+ def reset
229
+ @block = nil
230
+ @line = 1
231
+ @column = 1
232
+ end
233
+
234
+ ##
235
+ # Advances through the input and generates the corresponding tokens. Each
236
+ # token is yielded to the supplied block.
237
+ #
238
+ # @see [#add_token]
239
+ #
240
+ def advance(&block)
241
+ @block = block
242
+
243
+ data = @data # saves ivar lookups while lexing.
244
+ ts = nil
245
+ te = nil
246
+ cs = self.class.ll_lexer_start
247
+ act = 0
248
+ eof = @data.bytesize
249
+ p = 0
250
+ pe = eof
251
+
252
+ mark = 0
253
+ brace_count = 0
254
+ start_line = 0
255
+
256
+ _ll_lexer_eof_trans = self.class.send(:_ll_lexer_eof_trans)
257
+ _ll_lexer_from_state_actions = self.class.send(:_ll_lexer_from_state_actions)
258
+ _ll_lexer_index_offsets = self.class.send(:_ll_lexer_index_offsets)
259
+ _ll_lexer_indicies = self.class.send(:_ll_lexer_indicies)
260
+ _ll_lexer_key_spans = self.class.send(:_ll_lexer_key_spans)
261
+ _ll_lexer_to_state_actions = self.class.send(:_ll_lexer_to_state_actions)
262
+ _ll_lexer_trans_actions = self.class.send(:_ll_lexer_trans_actions)
263
+ _ll_lexer_trans_keys = self.class.send(:_ll_lexer_trans_keys)
264
+ _ll_lexer_trans_targs = self.class.send(:_ll_lexer_trans_targs)
265
+
266
+
267
+ # line 268 "lib/ll/lexer.rb"
268
+ begin
269
+ testEof = false
270
+ _slen, _trans, _keys, _inds, _acts, _nacts = nil
271
+ _goto_level = 0
272
+ _resume = 10
273
+ _eof_trans = 15
274
+ _again = 20
275
+ _test_eof = 30
276
+ _out = 40
277
+ while true
278
+ if _goto_level <= 0
279
+ if p == pe
280
+ _goto_level = _test_eof
281
+ next
282
+ end
283
+ if cs == 0
284
+ _goto_level = _out
285
+ next
286
+ end
287
+ end
288
+ if _goto_level <= _resume
289
+ case _ll_lexer_from_state_actions[cs]
290
+ when 6 then
291
+ # line 1 "NONE"
292
+ begin
293
+ ts = p
294
+ end
295
+ # line 296 "lib/ll/lexer.rb"
296
+ end
297
+ _keys = cs << 1
298
+ _inds = _ll_lexer_index_offsets[cs]
299
+ _slen = _ll_lexer_key_spans[cs]
300
+ _wide = ( _wide)
301
+ _wide = (data.getbyte(p) || 0)
302
+ _trans = if ( _slen > 0 &&
303
+ _ll_lexer_trans_keys[_keys] <= _wide &&
304
+ _wide <= _ll_lexer_trans_keys[_keys + 1]
305
+ ) then
306
+ _ll_lexer_indicies[ _inds + _wide - _ll_lexer_trans_keys[_keys] ]
307
+ else
308
+ _ll_lexer_indicies[ _inds + _slen ]
309
+ end
310
+ end
311
+ if _goto_level <= _eof_trans
312
+ cs = _ll_lexer_trans_targs[_trans]
313
+ if _ll_lexer_trans_actions[_trans] != 0
314
+ case _ll_lexer_trans_actions[_trans]
315
+ when 20 then
316
+ # line 188 "lib/ll/lexer.rl"
317
+ begin
318
+ te = p+1
319
+ begin brace_count += 1 end
320
+ end
321
+ when 21 then
322
+ # line 190 "lib/ll/lexer.rl"
323
+ begin
324
+ te = p+1
325
+ begin
326
+ if brace_count == 1
327
+ emit(:T_RUBY, mark, ts, start_line)
328
+
329
+ mark = 0
330
+ brace_count = 0
331
+ start_line = 0
332
+
333
+ advance_column
334
+
335
+ cs = 22;
336
+ else
337
+ brace_count -= 1
338
+ end
339
+ end
340
+ end
341
+ when 18 then
342
+ # line 206 "lib/ll/lexer.rl"
343
+ begin
344
+ te = p+1
345
+ end
346
+ when 3 then
347
+ # line 215 "lib/ll/lexer.rl"
348
+ begin
349
+ te = p+1
350
+ begin emit(:T_NAME, ts, te) end
351
+ end
352
+ when 4 then
353
+ # line 216 "lib/ll/lexer.rl"
354
+ begin
355
+ te = p+1
356
+ begin emit(:T_TERMINALS, ts, te) end
357
+ end
358
+ when 2 then
359
+ # line 217 "lib/ll/lexer.rl"
360
+ begin
361
+ te = p+1
362
+ begin emit(:T_INNER, ts, te) end
363
+ end
364
+ when 1 then
365
+ # line 218 "lib/ll/lexer.rl"
366
+ begin
367
+ te = p+1
368
+ begin emit(:T_HEADER, ts, te) end
369
+ end
370
+ when 12 then
371
+ # line 220 "lib/ll/lexer.rl"
372
+ begin
373
+ te = p+1
374
+ begin emit(:T_EQUALS, ts, te) end
375
+ end
376
+ when 10 then
377
+ # line 221 "lib/ll/lexer.rl"
378
+ begin
379
+ te = p+1
380
+ begin emit(:T_COLON, ts, te) end
381
+ end
382
+ when 11 then
383
+ # line 222 "lib/ll/lexer.rl"
384
+ begin
385
+ te = p+1
386
+ begin emit(:T_SEMICOLON, ts, te) end
387
+ end
388
+ when 15 then
389
+ # line 223 "lib/ll/lexer.rl"
390
+ begin
391
+ te = p+1
392
+ begin emit(:T_PIPE, ts, te) end
393
+ end
394
+ when 14 then
395
+ # line 226 "lib/ll/lexer.rl"
396
+ begin
397
+ te = p+1
398
+ begin
399
+ mark = ts + 1
400
+ brace_count = 1
401
+ start_line = @line
402
+
403
+ advance_column
404
+
405
+ cs = 25;
406
+ end
407
+ end
408
+ when 17 then
409
+ # line 213 "lib/ll/lexer.rl"
410
+ begin
411
+ te = p
412
+ p = p - 1; end
413
+ when 16 then
414
+ # line 1 "NONE"
415
+ begin
416
+ case act
417
+ when 16 then
418
+ begin begin p = ((te))-1; end
419
+ emit(:T_EPSILON, ts, te) end
420
+ when 18 then
421
+ begin begin p = ((te))-1; end
422
+
423
+ emit(:T_IDENT, ts, te)
424
+ end
425
+ end
426
+ end
427
+ when 19 then
428
+ # line 148 "lib/ll/lexer.rl"
429
+ begin
430
+
431
+ advance_line
432
+
433
+ @column = 1
434
+ end
435
+ # line 186 "lib/ll/lexer.rl"
436
+ begin
437
+ te = p+1
438
+ end
439
+ when 9 then
440
+ # line 148 "lib/ll/lexer.rl"
441
+ begin
442
+
443
+ advance_line
444
+
445
+ @column = 1
446
+ end
447
+ # line 210 "lib/ll/lexer.rl"
448
+ begin
449
+ te = p+1
450
+ end
451
+ when 8 then
452
+ # line 154 "lib/ll/lexer.rl"
453
+ begin
454
+
455
+ advance_column
456
+ end
457
+ # line 211 "lib/ll/lexer.rl"
458
+ begin
459
+ te = p+1
460
+ end
461
+ when 13 then
462
+ # line 1 "NONE"
463
+ begin
464
+ te = p+1
465
+ end
466
+ # line 224 "lib/ll/lexer.rl"
467
+ begin
468
+ act = 16; end
469
+ when 7 then
470
+ # line 1 "NONE"
471
+ begin
472
+ te = p+1
473
+ end
474
+ # line 168 "lib/ll/lexer.rl"
475
+ begin
476
+ act = 18; end
477
+ # line 477 "lib/ll/lexer.rb"
478
+ end
479
+ end
480
+ end
481
+ if _goto_level <= _again
482
+ case _ll_lexer_to_state_actions[cs]
483
+ when 5 then
484
+ # line 1 "NONE"
485
+ begin
486
+ ts = nil; end
487
+ # line 487 "lib/ll/lexer.rb"
488
+ end
489
+
490
+ if cs == 0
491
+ _goto_level = _out
492
+ next
493
+ end
494
+ p += 1
495
+ if p != pe
496
+ _goto_level = _resume
497
+ next
498
+ end
499
+ end
500
+ if _goto_level <= _test_eof
501
+ if p == eof
502
+ if _ll_lexer_eof_trans[cs] > 0
503
+ _trans = _ll_lexer_eof_trans[cs] - 1;
504
+ _goto_level = _eof_trans
505
+ next;
506
+ end
507
+ end
508
+
509
+ end
510
+ if _goto_level <= _out
511
+ break
512
+ end
513
+ end
514
+ end
515
+
516
+ # line 81 "lib/ll/lexer.rl"
517
+
518
+ # % fix highlight
519
+ ensure
520
+ reset
521
+ end
522
+
523
+ private
524
+
525
+ ##
526
+ # Emits a token of which the value is based on the supplied start/stop
527
+ # position.
528
+ #
529
+ # @param [Symbol] type The token type.
530
+ # @param [Fixnum] start
531
+ # @param [Fixnum] stop
532
+ # @param [Fixnum] line
533
+ #
534
+ # @see [#text]
535
+ # @see [#add_token]
536
+ #
537
+ def emit(type, start, stop, line = @line)
538
+ value = slice_input(start, stop)
539
+
540
+ add_token(type, value, line)
541
+ end
542
+
543
+ ##
544
+ # Returns the text between the specified start and stop position.
545
+ #
546
+ # @param [Fixnum] start
547
+ # @param [Fixnum] stop
548
+ # @return [String]
549
+ #
550
+ def slice_input(start, stop)
551
+ return @data.byteslice(start, stop - start)
552
+ end
553
+
554
+ ##
555
+ # Yields a new token to the supplied block.
556
+ #
557
+ # @param [Symbol] type The token type.
558
+ # @param [String] value The token value.
559
+ # @param [Fixnum] line
560
+ #
561
+ # @yieldparam [LL::Token] token
562
+ #
563
+ def add_token(type, value, line = @line)
564
+ source_line = SourceLine.new(@data, line, @column, @file)
565
+ @column += value.length
566
+
567
+ @block.call(Token.new(type, value, source_line))
568
+ end
569
+
570
+ def advance_line
571
+ @line += 1
572
+ end
573
+
574
+ def advance_column
575
+ @column += 1
576
+ end
577
+
578
+
579
+ # line 238 "lib/ll/lexer.rl"
580
+
581
+ end # Lexer
582
+ end # Oga