racc 1.4.14 → 1.4.15

Sign up to get free protection for your applications and to get access to all the features.
Files changed (59) hide show
  1. checksums.yaml +5 -5
  2. data/Manifest.txt +50 -0
  3. data/ext/racc/com/headius/racc/Cparse.java +66 -23
  4. data/ext/racc/cparse.c +1 -1
  5. data/ext/racc/depend +1 -1
  6. data/lib/racc/info.rb +2 -2
  7. data/test/assets/bibtex.y +141 -0
  8. data/test/assets/cadenza.y +170 -0
  9. data/test/assets/cast.y +926 -0
  10. data/test/assets/csspool.y +729 -0
  11. data/test/assets/edtf.y +583 -0
  12. data/test/assets/huia.y +318 -0
  13. data/test/assets/journey.y +47 -0
  14. data/test/assets/liquor.y +313 -0
  15. data/test/assets/machete.y +423 -0
  16. data/test/assets/macruby.y +2197 -0
  17. data/test/assets/mediacloth.y +599 -0
  18. data/test/assets/mof.y +649 -0
  19. data/test/assets/namae.y +302 -0
  20. data/test/assets/nasl.y +626 -0
  21. data/test/assets/nokogiri-css.y +255 -0
  22. data/test/assets/opal.y +1807 -0
  23. data/test/assets/php_serialization.y +98 -0
  24. data/test/assets/rdblockparser.y +576 -0
  25. data/test/assets/rdinlineparser.y +561 -0
  26. data/test/assets/riml.y +665 -0
  27. data/test/assets/ruby18.y +1943 -0
  28. data/test/assets/ruby19.y +2174 -0
  29. data/test/assets/ruby20.y +2350 -0
  30. data/test/assets/ruby21.y +2359 -0
  31. data/test/assets/ruby22.y +2381 -0
  32. data/test/assets/tp_plus.y +622 -0
  33. data/test/assets/twowaysql.y +278 -0
  34. data/test/helper.rb +31 -15
  35. data/test/regress/bibtex +474 -0
  36. data/test/regress/cadenza +796 -0
  37. data/test/regress/cast +3425 -0
  38. data/test/regress/csspool +2318 -0
  39. data/test/regress/edtf +1794 -0
  40. data/test/regress/huia +1392 -0
  41. data/test/regress/journey +222 -0
  42. data/test/regress/liquor +885 -0
  43. data/test/regress/machete +833 -0
  44. data/test/regress/mediacloth +1463 -0
  45. data/test/regress/mof +1368 -0
  46. data/test/regress/namae +634 -0
  47. data/test/regress/nasl +2058 -0
  48. data/test/regress/nokogiri-css +836 -0
  49. data/test/regress/opal +6429 -0
  50. data/test/regress/php_serialization +336 -0
  51. data/test/regress/rdblockparser +1061 -0
  52. data/test/regress/rdinlineparser +1243 -0
  53. data/test/regress/riml +3297 -0
  54. data/test/regress/ruby18 +6351 -0
  55. data/test/regress/ruby22 +7456 -0
  56. data/test/regress/tp_plus +1933 -0
  57. data/test/regress/twowaysql +556 -0
  58. data/test/test_racc_command.rb +177 -0
  59. metadata +75 -20
@@ -0,0 +1,599 @@
1
+ # Copyright (c) 2006 Pluron Inc.
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining
4
+ # a copy of this software and associated documentation files (the
5
+ # "Software"), to deal in the Software without restriction, including
6
+ # without limitation the rights to use, copy, modify, merge, publish,
7
+ # distribute, sublicense, and/or sell copies of the Software, and to
8
+ # permit persons to whom the Software is furnished to do so, subject to
9
+ # the following conditions:
10
+ #
11
+ # The above copyright notice and this permission notice shall be
12
+ # included in all copies or substantial portions of the Software.
13
+ #
14
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+
22
+ # The parser for the MediaWiki language.
23
+ #
24
+ # Usage together with a lexer:
25
+ # inputFile = File.new("data/input1", "r")
26
+ # input = inputFile.read
27
+ # parser = MediaWikiParser.new
28
+ # parser.lexer = MediaWikiLexer.new
29
+ # parser.parse(input)
30
+
31
+ class MediaWikiParser
32
+
33
+ token TEXT BOLD_START BOLD_END ITALIC_START ITALIC_END LINK_START LINK_END LINKSEP
34
+ INTLINK_START INTLINK_END INTLINKSEP RESOURCESEP CHAR_ENT
35
+ PRE_START PRE_END PREINDENT_START PREINDENT_END
36
+ SECTION_START SECTION_END HLINE SIGNATURE_NAME SIGNATURE_DATE SIGNATURE_FULL
37
+ PARA_START PARA_END UL_START UL_END OL_START OL_END LI_START LI_END
38
+ DL_START DL_END DT_START DT_END DD_START DD_END TAG_START TAG_END ATTR_NAME ATTR_VALUE
39
+ TABLE_START TABLE_END ROW_START ROW_END HEAD_START HEAD_END CELL_START CELL_END
40
+ KEYWORD TEMPLATE_START TEMPLATE_END CATEGORY PASTE_START PASTE_END
41
+
42
+
43
+ rule
44
+
45
+ wiki:
46
+ repeated_contents
47
+ {
48
+ @nodes.push WikiAST.new(0, @wiki_ast_length)
49
+ #@nodes.last.children.insert(0, val[0])
50
+ #puts val[0]
51
+ @nodes.last.children += val[0]
52
+ }
53
+ ;
54
+
55
+ contents:
56
+ text
57
+ {
58
+ result = val[0]
59
+ }
60
+ | bulleted_list
61
+ {
62
+ result = val[0]
63
+ }
64
+ | numbered_list
65
+ {
66
+ result = val[0]
67
+ }
68
+ | dictionary_list
69
+ {
70
+ list = ListAST.new(@ast_index, @ast_length)
71
+ list.list_type = :Dictionary
72
+ list.children = val[0]
73
+ result = list
74
+ }
75
+ | preformatted
76
+ {
77
+ result = val[0]
78
+ }
79
+ | section
80
+ {
81
+ result = val[0]
82
+ }
83
+ | tag
84
+ {
85
+ result = val[0]
86
+ }
87
+ | template
88
+ {
89
+ result = val[0]
90
+ }
91
+ | KEYWORD
92
+ {
93
+ k = KeywordAST.new(@ast_index, @ast_length)
94
+ k.text = val[0]
95
+ result = k
96
+ }
97
+ | PARA_START para_contents PARA_END
98
+ {
99
+ p = ParagraphAST.new(@ast_index, @ast_length)
100
+ p.children = val[1]
101
+ result = p
102
+ }
103
+ | LINK_START link_contents LINK_END
104
+ {
105
+ l = LinkAST.new(@ast_index, @ast_length)
106
+ l.link_type = val[0]
107
+ l.url = val[1][0]
108
+ l.children += val[1][1..-1] if val[1].length > 1
109
+ result = l
110
+ }
111
+ | PASTE_START para_contents PASTE_END
112
+ {
113
+ p = PasteAST.new(@ast_index, @ast_length)
114
+ p.children = val[1]
115
+ result = p
116
+ }
117
+ | INTLINK_START TEXT RESOURCESEP TEXT reslink_repeated_contents INTLINK_END
118
+ {
119
+ l = ResourceLinkAST.new(@ast_index, @ast_length)
120
+ l.prefix = val[1]
121
+ l.locator = val[3]
122
+ l.children = val[4] unless val[4].nil? or val[4].empty?
123
+ result = l
124
+ }
125
+ | INTLINK_START TEXT intlink_repeated_contents INTLINK_END
126
+ {
127
+ l = InternalLinkAST.new(@ast_index, @ast_length)
128
+ l.locator = val[1]
129
+ l.children = val[2] unless val[2].nil? or val[2].empty?
130
+ result = l
131
+ }
132
+ | INTLINK_START CATEGORY TEXT cat_sort_contents INTLINK_END
133
+ {
134
+ l = CategoryAST.new(@ast_index, @ast_length)
135
+ l.locator = val[2]
136
+ l.sort_as = val[3]
137
+ result = l
138
+ }
139
+ | INTLINK_START RESOURCESEP CATEGORY TEXT intlink_repeated_contents INTLINK_END
140
+ {
141
+ l = CategoryLinkAST.new(@ast_index, @ast_length)
142
+ l.locator = val[3]
143
+ l.children = val[4] unless val[4].nil? or val[4].empty?
144
+ result = l
145
+ }
146
+ | table
147
+ ;
148
+
149
+ para_contents:
150
+ {
151
+ result = nil
152
+ }
153
+ | repeated_contents
154
+ {
155
+ result = val[0]
156
+ }
157
+ ;
158
+
159
+ tag:
160
+ TAG_START tag_attributes TAG_END
161
+ {
162
+ if val[0] != val[2]
163
+ raise Racc::ParseError.new("XHTML end tag #{val[2]} does not match start tag #{val[0]}")
164
+ end
165
+ elem = ElementAST.new(@ast_index, @ast_length)
166
+ elem.name = val[0]
167
+ elem.attributes = val[1]
168
+ result = elem
169
+ }
170
+ | TAG_START tag_attributes repeated_contents TAG_END
171
+ {
172
+ if val[0] != val[3]
173
+ raise Racc::ParseError.new("XHTML end tag #{val[3]} does not match start tag #{val[0]}")
174
+ end
175
+ elem = ElementAST.new(@ast_index, @ast_length)
176
+ elem.name = val[0]
177
+ elem.attributes = val[1]
178
+ elem.children += val[2]
179
+ result = elem
180
+ }
181
+ ;
182
+
183
+ tag_attributes:
184
+ {
185
+ result = nil
186
+ }
187
+ | ATTR_NAME tag_attributes
188
+ {
189
+ attr_map = val[2] ? val[2] : {}
190
+ attr_map[val[0]] = true
191
+ result = attr_map
192
+ }
193
+ | ATTR_NAME ATTR_VALUE tag_attributes
194
+ {
195
+ attr_map = val[2] ? val[2] : {}
196
+ attr_map[val[0]] = val[1]
197
+ result = attr_map
198
+ }
199
+ ;
200
+
201
+
202
+ link_contents:
203
+ TEXT
204
+ {
205
+ result = val
206
+ }
207
+ | TEXT LINKSEP link_repeated_contents
208
+ {
209
+ result = [val[0]]
210
+ result += val[2]
211
+ }
212
+ ;
213
+
214
+
215
+ link_repeated_contents:
216
+ repeated_contents
217
+ {
218
+ result = val[0]
219
+ }
220
+ | repeated_contents LINKSEP link_repeated_contents
221
+ {
222
+ result = val[0]
223
+ result += val[2] if val[2]
224
+ }
225
+ ;
226
+
227
+
228
+ intlink_repeated_contents:
229
+ {
230
+ result = nil
231
+ }
232
+ | INTLINKSEP repeated_contents
233
+ {
234
+ result = val[1]
235
+ }
236
+ ;
237
+
238
+ cat_sort_contents:
239
+ {
240
+ result = nil
241
+ }
242
+ | INTLINKSEP TEXT
243
+ {
244
+ result = val[1]
245
+ }
246
+ ;
247
+
248
+ reslink_repeated_contents:
249
+ {
250
+ result = nil
251
+ }
252
+ | INTLINKSEP reslink_repeated_contents
253
+ {
254
+ result = val[1]
255
+ }
256
+ | INTLINKSEP repeated_contents reslink_repeated_contents
257
+ {
258
+ i = InternalLinkItemAST.new(@ast_index, @ast_length)
259
+ i.children = val[1]
260
+ result = [i]
261
+ result += val[2] if val[2]
262
+ }
263
+ ;
264
+
265
+ repeated_contents: contents
266
+ {
267
+ result = []
268
+ result << val[0]
269
+ }
270
+ | repeated_contents contents
271
+ {
272
+ result = []
273
+ result += val[0]
274
+ result << val[1]
275
+ }
276
+ ;
277
+
278
+ text: element
279
+ {
280
+ p = TextAST.new(@ast_index, @ast_length)
281
+ p.formatting = val[0][0]
282
+ p.contents = val[0][1]
283
+ result = p
284
+ }
285
+ | formatted_element
286
+ {
287
+ result = val[0]
288
+ }
289
+ ;
290
+
291
+ table:
292
+ TABLE_START table_contents TABLE_END
293
+ {
294
+ table = TableAST.new(@ast_index, @ast_length)
295
+ table.children = val[1] unless val[1].nil? or val[1].empty?
296
+ result = table
297
+ }
298
+ | TABLE_START TEXT table_contents TABLE_END
299
+ {
300
+ table = TableAST.new(@ast_index, @ast_length)
301
+ table.options = val[1]
302
+ table.children = val[2] unless val[2].nil? or val[2].empty?
303
+ result = table
304
+ }
305
+
306
+ table_contents:
307
+ {
308
+ result = nil
309
+ }
310
+ | ROW_START row_contents ROW_END table_contents
311
+ {
312
+ row = TableRowAST.new(@ast_index, @ast_length)
313
+ row.children = val[1] unless val[1].nil? or val[1].empty?
314
+ result = [row]
315
+ result += val[3] unless val[3].nil? or val[3].empty?
316
+ }
317
+ | ROW_START TEXT row_contents ROW_END table_contents
318
+ {
319
+ row = TableRowAST.new(@ast_index, @ast_length)
320
+ row.children = val[2] unless val[2].nil? or val[2].empty?
321
+ row.options = val[1]
322
+ result = [row]
323
+ result += val[4] unless val[4].nil? or val[4].empty?
324
+ }
325
+
326
+ row_contents:
327
+ {
328
+ result = nil
329
+ }
330
+ | HEAD_START HEAD_END row_contents
331
+ {
332
+ cell = TableCellAST.new(@ast_index, @ast_length)
333
+ cell.type = :head
334
+ result = [cell]
335
+ result += val[2] unless val[2].nil? or val[2].empty?
336
+ }
337
+ | HEAD_START repeated_contents HEAD_END row_contents
338
+ {
339
+ cell = TableCellAST.new(@ast_index, @ast_length)
340
+ cell.children = val[1] unless val[1].nil? or val[1].empty?
341
+ cell.type = :head
342
+ result = [cell]
343
+ result += val[3] unless val[3].nil? or val[3].empty?
344
+ }
345
+ | CELL_START CELL_END row_contents
346
+ {
347
+ cell = TableCellAST.new(@ast_index, @ast_length)
348
+ cell.type = :body
349
+ result = [cell]
350
+ result += val[2] unless val[2].nil? or val[2].empty?
351
+ }
352
+ | CELL_START repeated_contents CELL_END row_contents
353
+ {
354
+ if val[2] == 'attributes'
355
+ result = []
356
+ else
357
+ cell = TableCellAST.new(@ast_index, @ast_length)
358
+ cell.children = val[1] unless val[1].nil? or val[1].empty?
359
+ cell.type = :body
360
+ result = [cell]
361
+ end
362
+ result += val[3] unless val[3].nil? or val[3].empty?
363
+ if val[2] == 'attributes' and val[3] and val[3].first.class == TableCellAST
364
+ val[3].first.attributes = val[1]
365
+ end
366
+ result
367
+ }
368
+
369
+
370
+ element:
371
+ TEXT
372
+ { return [:None, val[0]] }
373
+ | HLINE
374
+ { return [:HLine, val[0]] }
375
+ | CHAR_ENT
376
+ { return [:CharacterEntity, val[0]] }
377
+ | SIGNATURE_DATE
378
+ { return [:SignatureDate, val[0]] }
379
+ | SIGNATURE_NAME
380
+ { return [:SignatureName, val[0]] }
381
+ | SIGNATURE_FULL
382
+ { return [:SignatureFull, val[0]] }
383
+ ;
384
+
385
+ formatted_element:
386
+ BOLD_START BOLD_END
387
+ {
388
+ result = FormattedAST.new(@ast_index, @ast_length)
389
+ result.formatting = :Bold
390
+ result
391
+ }
392
+ | ITALIC_START ITALIC_END
393
+ {
394
+ result = FormattedAST.new(@ast_index, @ast_length)
395
+ result.formatting = :Italic
396
+ result
397
+ }
398
+ | BOLD_START repeated_contents BOLD_END
399
+ {
400
+ p = FormattedAST.new(@ast_index, @ast_length)
401
+ p.formatting = :Bold
402
+ p.children += val[1]
403
+ result = p
404
+ }
405
+ | ITALIC_START repeated_contents ITALIC_END
406
+ {
407
+ p = FormattedAST.new(@ast_index, @ast_length)
408
+ p.formatting = :Italic
409
+ p.children += val[1]
410
+ result = p
411
+ }
412
+ ;
413
+
414
+ bulleted_list: UL_START list_item list_contents UL_END
415
+ {
416
+ list = ListAST.new(@ast_index, @ast_length)
417
+ list.list_type = :Bulleted
418
+ list.children << val[1]
419
+ list.children += val[2]
420
+ result = list
421
+ }
422
+ ;
423
+
424
+ numbered_list: OL_START list_item list_contents OL_END
425
+ {
426
+ list = ListAST.new(@ast_index, @ast_length)
427
+ list.list_type = :Numbered
428
+ list.children << val[1]
429
+ list.children += val[2]
430
+ result = list
431
+ }
432
+ ;
433
+
434
+ list_contents:
435
+ { result = [] }
436
+ list_item list_contents
437
+ {
438
+ result << val[1]
439
+ result += val[2]
440
+ }
441
+ |
442
+ { result = [] }
443
+ ;
444
+
445
+ list_item:
446
+ LI_START LI_END
447
+ {
448
+ result = ListItemAST.new(@ast_index, @ast_length)
449
+ }
450
+ | LI_START repeated_contents LI_END
451
+ {
452
+ li = ListItemAST.new(@ast_index, @ast_length)
453
+ li.children += val[1]
454
+ result = li
455
+ }
456
+ ;
457
+
458
+ dictionary_list:
459
+ DL_START dictionary_term dictionary_contents DL_END
460
+ {
461
+ result = [val[1]]
462
+ result += val[2]
463
+ }
464
+ | DL_START dictionary_contents DL_END
465
+ {
466
+ result = val[1]
467
+ }
468
+ ;
469
+
470
+ dictionary_term:
471
+ DT_START DT_END
472
+ {
473
+ result = ListTermAST.new(@ast_index, @ast_length)
474
+ }
475
+ | DT_START repeated_contents DT_END
476
+ {
477
+ term = ListTermAST.new(@ast_index, @ast_length)
478
+ term.children += val[1]
479
+ result = term
480
+ }
481
+
482
+ dictionary_contents:
483
+ dictionary_definition dictionary_contents
484
+ {
485
+ result = [val[0]]
486
+ result += val[1] if val[1]
487
+ }
488
+ |
489
+ {
490
+ result = []
491
+ }
492
+
493
+ dictionary_definition:
494
+ DD_START DD_END
495
+ {
496
+ result = ListDefinitionAST.new(@ast_index, @ast_length)
497
+ }
498
+ | DD_START repeated_contents DD_END
499
+ {
500
+ term = ListDefinitionAST.new(@ast_index, @ast_length)
501
+ term.children += val[1]
502
+ result = term
503
+ }
504
+
505
+ preformatted: PRE_START repeated_contents PRE_END
506
+ {
507
+ p = PreformattedAST.new(@ast_index, @ast_length)
508
+ p.children += val[1]
509
+ result = p
510
+ }
511
+ | PREINDENT_START repeated_contents PREINDENT_END
512
+ {
513
+ p = PreformattedAST.new(@ast_index, @ast_length)
514
+ p.indented = true
515
+ p.children += val[1]
516
+ result = p
517
+ }
518
+ ;
519
+
520
+ section: SECTION_START repeated_contents SECTION_END
521
+ { result = [val[1], val[0].length]
522
+ s = SectionAST.new(@ast_index, @ast_length)
523
+ s.children = val[1]
524
+ s.level = val[0].length
525
+ result = s
526
+ }
527
+ ;
528
+
529
+ template: TEMPLATE_START TEXT template_parameters TEMPLATE_END
530
+ {
531
+ t = TemplateAST.new(@ast_index, @ast_length)
532
+ t.template_name = val[1]
533
+ t.children = val[2] unless val[2].nil? or val[2].empty?
534
+ result = t
535
+ }
536
+ ;
537
+
538
+ template_parameters:
539
+ {
540
+ result = nil
541
+ }
542
+ | INTLINKSEP TEXT template_parameters
543
+ {
544
+ p = TemplateParameterAST.new(@ast_index, @ast_length)
545
+ p.parameter_value = val[1]
546
+ result = [p]
547
+ result += val[2] if val[2]
548
+ }
549
+ | INTLINKSEP template template_parameters
550
+ {
551
+ p = TemplateParameterAST.new(@ast_index, @ast_length)
552
+ p.children << val[1]
553
+ result = [p]
554
+ result += val[2] if val[2]
555
+ }
556
+ ;
557
+
558
+ end
559
+
560
+ ---- header ----
561
+ require 'mediacloth/mediawikiast'
562
+
563
+ ---- inner ----
564
+
565
+ attr_accessor :lexer
566
+
567
+ def initialize
568
+ @nodes = []
569
+ @context = []
570
+ @wiki_ast_length = 0
571
+ super
572
+ end
573
+
574
+ #Tokenizes input string and parses it.
575
+ def parse(input)
576
+ @yydebug=true
577
+ lexer.tokenize(input)
578
+ do_parse
579
+ return @nodes.last
580
+ end
581
+
582
+ #Asks the lexer to return the next token.
583
+ def next_token
584
+ token = @lexer.lex
585
+ if token[0].to_s.upcase.include? "_START"
586
+ @context << token[2..3]
587
+ elsif token[0].to_s.upcase.include? "_END"
588
+ @ast_index = @context.last[0]
589
+ @ast_length = token[2] + token[3] - @context.last[0]
590
+ @context.pop
591
+ else
592
+ @ast_index = token[2]
593
+ @ast_length = token[3]
594
+ end
595
+
596
+ @wiki_ast_length += token[3]
597
+
598
+ return token[0..1]
599
+ end