kpeg 0.9.0 → 0.10.0
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +15 -1
- data/Manifest.txt +2 -4
- data/README.rdoc +32 -0
- data/Rakefile +10 -3
- data/lib/hoe/kpeg.rb +6 -5
- data/lib/kpeg.rb +1 -1
- data/lib/kpeg/code_generator.rb +72 -39
- data/lib/kpeg/compiled_parser.rb +15 -11
- data/lib/kpeg/format_parser.kpeg +21 -9
- data/lib/kpeg/format_parser.rb +42 -39
- data/lib/kpeg/grammar.rb +1 -1
- data/lib/kpeg/grammar_renderer.rb +14 -0
- data/lib/kpeg/position.rb +1 -0
- data/lib/kpeg/string_escape.rb +355 -2
- data/test/test_kpeg_code_generator.rb +166 -0
- data/test/test_kpeg_format.rb +2 -2
- data/test/{test_file_parser_roundtrip.rb → test_kpeg_format_parser_round_trip.rb} +1 -1
- data/test/{test_gen_calc.rb → test_kpeg_grammar.rb} +48 -5
- data/test/test_kpeg_grammar_renderer.rb +46 -5
- metadata +17 -20
- data/Gemfile +0 -12
- data/test/test_left_recursion.rb +0 -50
@@ -14,6 +14,7 @@ class TestKPegCodeGenerator < MiniTest::Unit::TestCase
|
|
14
14
|
require 'kpeg/compiled_parser'
|
15
15
|
|
16
16
|
class Test < KPeg::CompiledParser
|
17
|
+
# :stopdoc:
|
17
18
|
|
18
19
|
# root = .
|
19
20
|
def _root
|
@@ -24,6 +25,7 @@ class Test < KPeg::CompiledParser
|
|
24
25
|
|
25
26
|
Rules = {}
|
26
27
|
Rules[:_root] = rule_info("root", ".")
|
28
|
+
# :startdoc:
|
27
29
|
end
|
28
30
|
STR
|
29
31
|
|
@@ -43,6 +45,7 @@ end
|
|
43
45
|
require 'kpeg/compiled_parser'
|
44
46
|
|
45
47
|
class Test < KPeg::CompiledParser
|
48
|
+
# :stopdoc:
|
46
49
|
|
47
50
|
# root = "hello"
|
48
51
|
def _root
|
@@ -53,6 +56,7 @@ class Test < KPeg::CompiledParser
|
|
53
56
|
|
54
57
|
Rules = {}
|
55
58
|
Rules[:_root] = rule_info("root", "\\\"hello\\\"")
|
59
|
+
# :startdoc:
|
56
60
|
end
|
57
61
|
STR
|
58
62
|
|
@@ -72,6 +76,7 @@ end
|
|
72
76
|
require 'kpeg/compiled_parser'
|
73
77
|
|
74
78
|
class Test < KPeg::CompiledParser
|
79
|
+
# :stopdoc:
|
75
80
|
|
76
81
|
# root = /[0-9]/
|
77
82
|
def _root
|
@@ -82,6 +87,7 @@ class Test < KPeg::CompiledParser
|
|
82
87
|
|
83
88
|
Rules = {}
|
84
89
|
Rules[:_root] = rule_info("root", "/[0-9]/")
|
90
|
+
# :startdoc:
|
85
91
|
end
|
86
92
|
STR
|
87
93
|
|
@@ -104,6 +110,7 @@ end
|
|
104
110
|
require 'kpeg/compiled_parser'
|
105
111
|
|
106
112
|
class Test < KPeg::CompiledParser
|
113
|
+
# :stopdoc:
|
107
114
|
|
108
115
|
# root = /./
|
109
116
|
def _root
|
@@ -114,6 +121,7 @@ class Test < KPeg::CompiledParser
|
|
114
121
|
|
115
122
|
Rules = {}
|
116
123
|
Rules[:_root] = rule_info("root", "/./")
|
124
|
+
# :startdoc:
|
117
125
|
end
|
118
126
|
STR
|
119
127
|
else
|
@@ -121,6 +129,7 @@ end
|
|
121
129
|
require 'kpeg/compiled_parser'
|
122
130
|
|
123
131
|
class Test < KPeg::CompiledParser
|
132
|
+
# :stopdoc:
|
124
133
|
|
125
134
|
# root = /./u
|
126
135
|
def _root
|
@@ -131,6 +140,7 @@ class Test < KPeg::CompiledParser
|
|
131
140
|
|
132
141
|
Rules = {}
|
133
142
|
Rules[:_root] = rule_info("root", "/./u")
|
143
|
+
# :startdoc:
|
134
144
|
end
|
135
145
|
STR
|
136
146
|
end
|
@@ -151,6 +161,7 @@ end
|
|
151
161
|
require 'kpeg/compiled_parser'
|
152
162
|
|
153
163
|
class Test < KPeg::CompiledParser
|
164
|
+
# :stopdoc:
|
154
165
|
|
155
166
|
# root = [a-z]
|
156
167
|
def _root
|
@@ -168,6 +179,7 @@ class Test < KPeg::CompiledParser
|
|
168
179
|
|
169
180
|
Rules = {}
|
170
181
|
Rules[:_root] = rule_info("root", "[a-z]")
|
182
|
+
# :startdoc:
|
171
183
|
end
|
172
184
|
STR
|
173
185
|
|
@@ -189,6 +201,7 @@ end
|
|
189
201
|
require 'kpeg/compiled_parser'
|
190
202
|
|
191
203
|
class Test < KPeg::CompiledParser
|
204
|
+
# :stopdoc:
|
192
205
|
|
193
206
|
# root = [a-z] "hello"
|
194
207
|
def _root
|
@@ -220,6 +233,7 @@ class Test < KPeg::CompiledParser
|
|
220
233
|
|
221
234
|
Rules = {}
|
222
235
|
Rules[:_root] = rule_info("root", "[a-z] \\\"hello\\\"")
|
236
|
+
# :startdoc:
|
223
237
|
end
|
224
238
|
STR
|
225
239
|
|
@@ -242,6 +256,7 @@ end
|
|
242
256
|
require 'kpeg/compiled_parser'
|
243
257
|
|
244
258
|
class Test < KPeg::CompiledParser
|
259
|
+
# :stopdoc:
|
245
260
|
|
246
261
|
# root = ("hello" | "world")
|
247
262
|
def _root
|
@@ -263,6 +278,7 @@ class Test < KPeg::CompiledParser
|
|
263
278
|
|
264
279
|
Rules = {}
|
265
280
|
Rules[:_root] = rule_info("root", "(\\\"hello\\\" | \\\"world\\\")")
|
281
|
+
# :startdoc:
|
266
282
|
end
|
267
283
|
STR
|
268
284
|
|
@@ -298,6 +314,7 @@ end
|
|
298
314
|
require 'kpeg/compiled_parser'
|
299
315
|
|
300
316
|
class Test < KPeg::CompiledParser
|
317
|
+
# :stopdoc:
|
301
318
|
|
302
319
|
# root = "hello"?
|
303
320
|
def _root
|
@@ -313,6 +330,7 @@ class Test < KPeg::CompiledParser
|
|
313
330
|
|
314
331
|
Rules = {}
|
315
332
|
Rules[:_root] = rule_info("root", "\\\"hello\\\"?")
|
333
|
+
# :startdoc:
|
316
334
|
end
|
317
335
|
STR
|
318
336
|
|
@@ -347,6 +365,7 @@ end
|
|
347
365
|
require 'kpeg/compiled_parser'
|
348
366
|
|
349
367
|
class Test < KPeg::CompiledParser
|
368
|
+
# :stopdoc:
|
350
369
|
|
351
370
|
# root = "hello"*
|
352
371
|
def _root
|
@@ -361,6 +380,7 @@ class Test < KPeg::CompiledParser
|
|
361
380
|
|
362
381
|
Rules = {}
|
363
382
|
Rules[:_root] = rule_info("root", "\\\"hello\\\"*")
|
383
|
+
# :startdoc:
|
364
384
|
end
|
365
385
|
STR
|
366
386
|
|
@@ -398,6 +418,7 @@ end
|
|
398
418
|
require 'kpeg/compiled_parser'
|
399
419
|
|
400
420
|
class Test < KPeg::CompiledParser
|
421
|
+
# :stopdoc:
|
401
422
|
|
402
423
|
# root = "hello"+
|
403
424
|
def _root
|
@@ -418,6 +439,7 @@ class Test < KPeg::CompiledParser
|
|
418
439
|
|
419
440
|
Rules = {}
|
420
441
|
Rules[:_root] = rule_info("root", "\\\"hello\\\"+")
|
442
|
+
# :startdoc:
|
421
443
|
end
|
422
444
|
STR
|
423
445
|
|
@@ -462,6 +484,7 @@ end
|
|
462
484
|
require 'kpeg/compiled_parser'
|
463
485
|
|
464
486
|
class Test < KPeg::CompiledParser
|
487
|
+
# :stopdoc:
|
465
488
|
|
466
489
|
# root = "hello"[5, 9]
|
467
490
|
def _root
|
@@ -488,6 +511,7 @@ class Test < KPeg::CompiledParser
|
|
488
511
|
|
489
512
|
Rules = {}
|
490
513
|
Rules[:_root] = rule_info("root", "\\\"hello\\\"[5, 9]")
|
514
|
+
# :startdoc:
|
491
515
|
end
|
492
516
|
STR
|
493
517
|
|
@@ -505,6 +529,7 @@ end
|
|
505
529
|
require 'kpeg/compiled_parser'
|
506
530
|
|
507
531
|
class Test < KPeg::CompiledParser
|
532
|
+
# :stopdoc:
|
508
533
|
|
509
534
|
# root = "hello" "world"
|
510
535
|
def _root
|
@@ -529,6 +554,7 @@ class Test < KPeg::CompiledParser
|
|
529
554
|
|
530
555
|
Rules = {}
|
531
556
|
Rules[:_root] = rule_info("root", "\\\"hello\\\" \\\"world\\\"")
|
557
|
+
# :startdoc:
|
532
558
|
end
|
533
559
|
STR
|
534
560
|
|
@@ -561,6 +587,7 @@ end
|
|
561
587
|
require 'kpeg/compiled_parser'
|
562
588
|
|
563
589
|
class Test < KPeg::CompiledParser
|
590
|
+
# :stopdoc:
|
564
591
|
|
565
592
|
# root = &"hello"
|
566
593
|
def _root
|
@@ -573,6 +600,7 @@ class Test < KPeg::CompiledParser
|
|
573
600
|
|
574
601
|
Rules = {}
|
575
602
|
Rules[:_root] = rule_info("root", "&\\\"hello\\\"")
|
603
|
+
# :startdoc:
|
576
604
|
end
|
577
605
|
STR
|
578
606
|
|
@@ -598,6 +626,7 @@ end
|
|
598
626
|
require 'kpeg/compiled_parser'
|
599
627
|
|
600
628
|
class Test < KPeg::CompiledParser
|
629
|
+
# :stopdoc:
|
601
630
|
|
602
631
|
# root = &{ !defined? @fail }
|
603
632
|
def _root
|
@@ -610,6 +639,7 @@ class Test < KPeg::CompiledParser
|
|
610
639
|
|
611
640
|
Rules = {}
|
612
641
|
Rules[:_root] = rule_info("root", "&{ !defined? @fail }")
|
642
|
+
# :startdoc:
|
613
643
|
end
|
614
644
|
STR
|
615
645
|
|
@@ -636,6 +666,7 @@ end
|
|
636
666
|
require 'kpeg/compiled_parser'
|
637
667
|
|
638
668
|
class Test < KPeg::CompiledParser
|
669
|
+
# :stopdoc:
|
639
670
|
|
640
671
|
# root = !"hello"
|
641
672
|
def _root
|
@@ -649,6 +680,7 @@ class Test < KPeg::CompiledParser
|
|
649
680
|
|
650
681
|
Rules = {}
|
651
682
|
Rules[:_root] = rule_info("root", "!\\\"hello\\\"")
|
683
|
+
# :startdoc:
|
652
684
|
end
|
653
685
|
STR
|
654
686
|
|
@@ -674,6 +706,7 @@ end
|
|
674
706
|
require 'kpeg/compiled_parser'
|
675
707
|
|
676
708
|
class Test < KPeg::CompiledParser
|
709
|
+
# :stopdoc:
|
677
710
|
|
678
711
|
# root = !{ defined? @fail }
|
679
712
|
def _root
|
@@ -687,6 +720,7 @@ class Test < KPeg::CompiledParser
|
|
687
720
|
|
688
721
|
Rules = {}
|
689
722
|
Rules[:_root] = rule_info("root", "!{ defined? @fail }")
|
723
|
+
# :startdoc:
|
690
724
|
end
|
691
725
|
STR
|
692
726
|
|
@@ -715,6 +749,7 @@ end
|
|
715
749
|
require 'kpeg/compiled_parser'
|
716
750
|
|
717
751
|
class Test < KPeg::CompiledParser
|
752
|
+
# :stopdoc:
|
718
753
|
|
719
754
|
# greeting = "hello"
|
720
755
|
def _greeting
|
@@ -733,6 +768,7 @@ class Test < KPeg::CompiledParser
|
|
733
768
|
Rules = {}
|
734
769
|
Rules[:_greeting] = rule_info("greeting", "\\\"hello\\\"")
|
735
770
|
Rules[:_root] = rule_info("root", "greeting")
|
771
|
+
# :startdoc:
|
736
772
|
end
|
737
773
|
STR
|
738
774
|
|
@@ -753,6 +789,7 @@ end
|
|
753
789
|
require 'kpeg/compiled_parser'
|
754
790
|
|
755
791
|
class Test < KPeg::CompiledParser
|
792
|
+
# :stopdoc:
|
756
793
|
|
757
794
|
# greeting = "hello"
|
758
795
|
def _greeting
|
@@ -771,6 +808,7 @@ class Test < KPeg::CompiledParser
|
|
771
808
|
Rules = {}
|
772
809
|
Rules[:_greeting] = rule_info("greeting", "\\\"hello\\\"")
|
773
810
|
Rules[:_root] = rule_info("root", "@greeting")
|
811
|
+
# :startdoc:
|
774
812
|
end
|
775
813
|
STR
|
776
814
|
|
@@ -791,6 +829,7 @@ end
|
|
791
829
|
require 'kpeg/compiled_parser'
|
792
830
|
|
793
831
|
class Test < KPeg::CompiledParser
|
832
|
+
# :stopdoc:
|
794
833
|
|
795
834
|
# greeting = "hello"
|
796
835
|
def _greeting(a,b)
|
@@ -809,6 +848,7 @@ class Test < KPeg::CompiledParser
|
|
809
848
|
Rules = {}
|
810
849
|
Rules[:_greeting] = rule_info("greeting", "\\\"hello\\\"")
|
811
850
|
Rules[:_root] = rule_info("root", "@greeting(1,2)")
|
851
|
+
# :startdoc:
|
812
852
|
end
|
813
853
|
STR
|
814
854
|
|
@@ -836,6 +876,7 @@ end
|
|
836
876
|
require 'kpeg/compiled_parser'
|
837
877
|
|
838
878
|
class Test < KPeg::CompiledParser
|
879
|
+
# :stopdoc:
|
839
880
|
def setup_foreign_grammar
|
840
881
|
@_grammar_blah = TestKPegCodeGenerator::TestParser.new(nil)
|
841
882
|
end
|
@@ -849,6 +890,7 @@ class Test < KPeg::CompiledParser
|
|
849
890
|
|
850
891
|
Rules = {}
|
851
892
|
Rules[:_root] = rule_info("root", "%blah.greeting")
|
893
|
+
# :startdoc:
|
852
894
|
end
|
853
895
|
STR
|
854
896
|
|
@@ -869,6 +911,7 @@ end
|
|
869
911
|
require 'kpeg/compiled_parser'
|
870
912
|
|
871
913
|
class Test < KPeg::CompiledParser
|
914
|
+
# :stopdoc:
|
872
915
|
def setup_foreign_grammar
|
873
916
|
@_grammar_blah = TestKPegCodeGenerator::TestParser.new(nil)
|
874
917
|
end
|
@@ -882,6 +925,7 @@ class Test < KPeg::CompiledParser
|
|
882
925
|
|
883
926
|
Rules = {}
|
884
927
|
Rules[:_root] = rule_info("root", "%blah.greeting2(1,2)")
|
928
|
+
# :startdoc:
|
885
929
|
end
|
886
930
|
STR
|
887
931
|
|
@@ -901,6 +945,7 @@ end
|
|
901
945
|
require 'kpeg/compiled_parser'
|
902
946
|
|
903
947
|
class Test < KPeg::CompiledParser
|
948
|
+
# :stopdoc:
|
904
949
|
|
905
950
|
# root = "hello":t
|
906
951
|
def _root
|
@@ -912,6 +957,7 @@ class Test < KPeg::CompiledParser
|
|
912
957
|
|
913
958
|
Rules = {}
|
914
959
|
Rules[:_root] = rule_info("root", "\\\"hello\\\":t")
|
960
|
+
# :startdoc:
|
915
961
|
end
|
916
962
|
STR
|
917
963
|
|
@@ -929,6 +975,7 @@ end
|
|
929
975
|
require 'kpeg/compiled_parser'
|
930
976
|
|
931
977
|
class Test < KPeg::CompiledParser
|
978
|
+
# :stopdoc:
|
932
979
|
|
933
980
|
# root = "hello"
|
934
981
|
def _root
|
@@ -939,6 +986,7 @@ class Test < KPeg::CompiledParser
|
|
939
986
|
|
940
987
|
Rules = {}
|
941
988
|
Rules[:_root] = rule_info("root", "\\\"hello\\\"")
|
989
|
+
# :startdoc:
|
942
990
|
end
|
943
991
|
STR
|
944
992
|
|
@@ -957,6 +1005,7 @@ end
|
|
957
1005
|
require 'kpeg/compiled_parser'
|
958
1006
|
|
959
1007
|
class Test < KPeg::CompiledParser
|
1008
|
+
# :stopdoc:
|
960
1009
|
|
961
1010
|
# hello = < "hello" > {text}
|
962
1011
|
def _hello
|
@@ -1016,6 +1065,7 @@ class Test < KPeg::CompiledParser
|
|
1016
1065
|
Rules = {}
|
1017
1066
|
Rules[:_hello] = rule_info("hello", "< \\\"hello\\\" > {text}")
|
1018
1067
|
Rules[:_root] = rule_info("root", "hello?:lots {lots}")
|
1068
|
+
# :startdoc:
|
1019
1069
|
end
|
1020
1070
|
STR
|
1021
1071
|
|
@@ -1043,6 +1093,7 @@ end
|
|
1043
1093
|
require 'kpeg/compiled_parser'
|
1044
1094
|
|
1045
1095
|
class Test < KPeg::CompiledParser
|
1096
|
+
# :stopdoc:
|
1046
1097
|
|
1047
1098
|
# hello = < "hello" > {text}
|
1048
1099
|
def _hello
|
@@ -1103,6 +1154,7 @@ class Test < KPeg::CompiledParser
|
|
1103
1154
|
Rules = {}
|
1104
1155
|
Rules[:_hello] = rule_info("hello", "< \\\"hello\\\" > {text}")
|
1105
1156
|
Rules[:_root] = rule_info("root", "hello*:lots {lots}")
|
1157
|
+
# :startdoc:
|
1106
1158
|
end
|
1107
1159
|
STR
|
1108
1160
|
|
@@ -1133,6 +1185,7 @@ end
|
|
1133
1185
|
require 'kpeg/compiled_parser'
|
1134
1186
|
|
1135
1187
|
class Test < KPeg::CompiledParser
|
1188
|
+
# :stopdoc:
|
1136
1189
|
|
1137
1190
|
# hello = < "hello" > {text}
|
1138
1191
|
def _hello
|
@@ -1200,6 +1253,7 @@ class Test < KPeg::CompiledParser
|
|
1200
1253
|
Rules = {}
|
1201
1254
|
Rules[:_hello] = rule_info("hello", "< \\\"hello\\\" > {text}")
|
1202
1255
|
Rules[:_root] = rule_info("root", "hello+:lots {lots}")
|
1256
|
+
# :startdoc:
|
1203
1257
|
end
|
1204
1258
|
STR
|
1205
1259
|
|
@@ -1228,6 +1282,7 @@ end
|
|
1228
1282
|
require 'kpeg/compiled_parser'
|
1229
1283
|
|
1230
1284
|
class Test < KPeg::CompiledParser
|
1285
|
+
# :stopdoc:
|
1231
1286
|
|
1232
1287
|
# root = {3 + 4}
|
1233
1288
|
def _root
|
@@ -1239,6 +1294,7 @@ class Test < KPeg::CompiledParser
|
|
1239
1294
|
|
1240
1295
|
Rules = {}
|
1241
1296
|
Rules[:_root] = rule_info("root", "{3 + 4}")
|
1297
|
+
# :startdoc:
|
1242
1298
|
end
|
1243
1299
|
STR
|
1244
1300
|
|
@@ -1260,6 +1316,7 @@ end
|
|
1260
1316
|
require 'kpeg/compiled_parser'
|
1261
1317
|
|
1262
1318
|
class Test < KPeg::CompiledParser
|
1319
|
+
# :stopdoc:
|
1263
1320
|
|
1264
1321
|
# root = < "hello" > { text }
|
1265
1322
|
def _root
|
@@ -1289,6 +1346,7 @@ class Test < KPeg::CompiledParser
|
|
1289
1346
|
|
1290
1347
|
Rules = {}
|
1291
1348
|
Rules[:_root] = rule_info("root", "< \\\"hello\\\" > { text }")
|
1349
|
+
# :startdoc:
|
1292
1350
|
end
|
1293
1351
|
STR
|
1294
1352
|
|
@@ -1310,6 +1368,7 @@ end
|
|
1310
1368
|
require 'kpeg/compiled_parser'
|
1311
1369
|
|
1312
1370
|
class Test < KPeg::CompiledParser
|
1371
|
+
# :stopdoc:
|
1313
1372
|
|
1314
1373
|
# root = @< "hello" > { bounds }
|
1315
1374
|
def _root
|
@@ -1339,6 +1398,7 @@ class Test < KPeg::CompiledParser
|
|
1339
1398
|
|
1340
1399
|
Rules = {}
|
1341
1400
|
Rules[:_root] = rule_info("root", "@< \\\"hello\\\" > { bounds }")
|
1401
|
+
# :startdoc:
|
1342
1402
|
end
|
1343
1403
|
STR
|
1344
1404
|
|
@@ -1351,6 +1411,28 @@ end
|
|
1351
1411
|
assert_equal [0,5], code.result
|
1352
1412
|
end
|
1353
1413
|
|
1414
|
+
def test_standalone_region
|
1415
|
+
gram = KPeg.grammar do |g|
|
1416
|
+
g.root = g.dot
|
1417
|
+
end
|
1418
|
+
|
1419
|
+
cg = KPeg::CodeGenerator.new "Test", gram
|
1420
|
+
|
1421
|
+
expected = <<-EXPECTED
|
1422
|
+
|
1423
|
+
# This is distinct from setup_parser so that a standalone parser
|
1424
|
+
# can redefine #initialize and still have access to the proper
|
1425
|
+
# parser setup code.
|
1426
|
+
def initialize(str, debug=false)
|
1427
|
+
setup_parser(str, debug)
|
1428
|
+
end
|
1429
|
+
|
1430
|
+
EXPECTED
|
1431
|
+
|
1432
|
+
assert_equal expected,
|
1433
|
+
cg.standalone_region('compiled_parser.rb', 'INITIALIZE')
|
1434
|
+
end
|
1435
|
+
|
1354
1436
|
def test_parse_error
|
1355
1437
|
gram = KPeg.grammar do |g|
|
1356
1438
|
g.world = "world"
|
@@ -1380,6 +1462,7 @@ end
|
|
1380
1462
|
require 'kpeg/compiled_parser'
|
1381
1463
|
|
1382
1464
|
class Test < KPeg::CompiledParser
|
1465
|
+
# :stopdoc:
|
1383
1466
|
|
1384
1467
|
# root = .
|
1385
1468
|
def _root
|
@@ -1390,6 +1473,7 @@ class Test < KPeg::CompiledParser
|
|
1390
1473
|
|
1391
1474
|
Rules = {}
|
1392
1475
|
Rules[:_root] = rule_info("root", ".")
|
1476
|
+
# :startdoc:
|
1393
1477
|
end
|
1394
1478
|
|
1395
1479
|
# require 'some/subclass'
|
@@ -1413,6 +1497,7 @@ end
|
|
1413
1497
|
require 'kpeg/compiled_parser'
|
1414
1498
|
|
1415
1499
|
class Test < KPeg::CompiledParser
|
1500
|
+
# :stopdoc:
|
1416
1501
|
|
1417
1502
|
# root = .
|
1418
1503
|
def _root
|
@@ -1423,6 +1508,7 @@ class Test < KPeg::CompiledParser
|
|
1423
1508
|
|
1424
1509
|
Rules = {}
|
1425
1510
|
Rules[:_root] = rule_info("root", ".")
|
1511
|
+
# :startdoc:
|
1426
1512
|
end
|
1427
1513
|
STR
|
1428
1514
|
|
@@ -1433,6 +1519,51 @@ end
|
|
1433
1519
|
assert cg.parse("hello")
|
1434
1520
|
end
|
1435
1521
|
|
1522
|
+
def test_directive_pre_class
|
1523
|
+
gram = KPeg.grammar do |g|
|
1524
|
+
g.root = g.dot
|
1525
|
+
g.directives['pre-class'] = g.action("\n# some comment\n")
|
1526
|
+
end
|
1527
|
+
|
1528
|
+
str = <<-STR
|
1529
|
+
require 'kpeg/compiled_parser'
|
1530
|
+
|
1531
|
+
# some comment
|
1532
|
+
class Test < KPeg::CompiledParser
|
1533
|
+
# :stopdoc:
|
1534
|
+
|
1535
|
+
# root = .
|
1536
|
+
def _root
|
1537
|
+
_tmp = get_byte
|
1538
|
+
set_failed_rule :_root unless _tmp
|
1539
|
+
return _tmp
|
1540
|
+
end
|
1541
|
+
|
1542
|
+
Rules = {}
|
1543
|
+
Rules[:_root] = rule_info("root", ".")
|
1544
|
+
# :startdoc:
|
1545
|
+
end
|
1546
|
+
STR
|
1547
|
+
|
1548
|
+
cg = KPeg::CodeGenerator.new "Test", gram
|
1549
|
+
|
1550
|
+
assert_equal str, cg.output
|
1551
|
+
|
1552
|
+
assert cg.parse("hello")
|
1553
|
+
end
|
1554
|
+
|
1555
|
+
def test_directive_pre_class_standalone
|
1556
|
+
gram = KPeg.grammar do |g|
|
1557
|
+
g.root = g.dot
|
1558
|
+
g.directives['pre-class'] = g.action("\n# some comment\n")
|
1559
|
+
end
|
1560
|
+
|
1561
|
+
cg = KPeg::CodeGenerator.new "Test", gram
|
1562
|
+
cg.standalone = true
|
1563
|
+
|
1564
|
+
assert_match %r%^# some comment%, cg.output
|
1565
|
+
end
|
1566
|
+
|
1436
1567
|
def test_setup_actions
|
1437
1568
|
gram = KPeg.grammar do |g|
|
1438
1569
|
g.root = g.dot
|
@@ -1446,6 +1577,7 @@ class Test < KPeg::CompiledParser
|
|
1446
1577
|
|
1447
1578
|
attr_reader :foo
|
1448
1579
|
|
1580
|
+
# :stopdoc:
|
1449
1581
|
|
1450
1582
|
# root = .
|
1451
1583
|
def _root
|
@@ -1456,6 +1588,7 @@ class Test < KPeg::CompiledParser
|
|
1456
1588
|
|
1457
1589
|
Rules = {}
|
1458
1590
|
Rules[:_root] = rule_info("root", ".")
|
1591
|
+
# :startdoc:
|
1459
1592
|
end
|
1460
1593
|
STR
|
1461
1594
|
|
@@ -1466,6 +1599,35 @@ end
|
|
1466
1599
|
assert cg.parse("hello")
|
1467
1600
|
end
|
1468
1601
|
|
1602
|
+
def test_output_standalone
|
1603
|
+
gram = KPeg.grammar do |g|
|
1604
|
+
g.root = g.dot
|
1605
|
+
end
|
1606
|
+
|
1607
|
+
cg = KPeg::CodeGenerator.new "Test", gram
|
1608
|
+
cg.standalone = true
|
1609
|
+
|
1610
|
+
# if this fails, also change test_variable_custom_initialize
|
1611
|
+
assert_match 'def initialize(str, debug=false)', cg.output
|
1612
|
+
|
1613
|
+
assert_match '# :stopdoc:', cg.output
|
1614
|
+
assert_match '# :startdoc:', cg.output
|
1615
|
+
|
1616
|
+
assert cg.parse("hello")
|
1617
|
+
end
|
1618
|
+
|
1619
|
+
def test_variable_custom_initialize
|
1620
|
+
gram = KPeg.grammar do |g|
|
1621
|
+
g.root = g.dot
|
1622
|
+
g.variables['custom_initialize'] = 'whatever'
|
1623
|
+
end
|
1624
|
+
|
1625
|
+
cg = KPeg::CodeGenerator.new "Test", gram
|
1626
|
+
cg.standalone = true
|
1627
|
+
|
1628
|
+
refute_match 'def initialize(str, debug=false)', cg.output
|
1629
|
+
end
|
1630
|
+
|
1469
1631
|
def test_ast_generation
|
1470
1632
|
gram = KPeg.grammar do |g|
|
1471
1633
|
g.root = g.dot
|
@@ -1478,6 +1640,7 @@ end
|
|
1478
1640
|
require 'kpeg/compiled_parser'
|
1479
1641
|
|
1480
1642
|
class Test < KPeg::CompiledParser
|
1643
|
+
# :stopdoc:
|
1481
1644
|
|
1482
1645
|
module AST
|
1483
1646
|
class Node; end
|
@@ -1517,6 +1680,7 @@ class Test < KPeg::CompiledParser
|
|
1517
1680
|
|
1518
1681
|
Rules = {}
|
1519
1682
|
Rules[:_root] = rule_info("root", ".")
|
1683
|
+
# :startdoc:
|
1520
1684
|
end
|
1521
1685
|
STR
|
1522
1686
|
|
@@ -1538,6 +1702,7 @@ end
|
|
1538
1702
|
require 'kpeg/compiled_parser'
|
1539
1703
|
|
1540
1704
|
class Test < KPeg::CompiledParser
|
1705
|
+
# :stopdoc:
|
1541
1706
|
|
1542
1707
|
module MegaAST
|
1543
1708
|
class Node; end
|
@@ -1563,6 +1728,7 @@ class Test < KPeg::CompiledParser
|
|
1563
1728
|
|
1564
1729
|
Rules = {}
|
1565
1730
|
Rules[:_root] = rule_info("root", ".")
|
1731
|
+
# :startdoc:
|
1566
1732
|
end
|
1567
1733
|
STR
|
1568
1734
|
|