forked from vsbenas/parser-gen
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser-gen.lua
967 lines (826 loc) · 26.8 KB
/
parser-gen.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
local function req(src, mod)
mod = mod or ""
local m = pcall(require, src) and require(src)
or pcall(require, mod.."."..src) and require( mod.."."..src) -- luapower sub module path
or nil
assert(m, 'parser-gen depend on "'..src..'" from "'..mod..'". Please check your path')
return m
end
local m = req "lpeglabel"
local peg = require "peg-parser"
local eg = req("errorgen", "peg-parser")
local s = req("stack", "peg-parser")
local ast = require "parser-gen.ast"
local isfinal,isaction,isrule,isgrammar,istoken,iscapture,finalNode = ast.isfinal,ast.isaction,ast.isrule,ast.isgrammar,ast.istoken,ast.iscapture,ast.finalNode
local pegdebugtrace = require('pegdebug').trace
local pp = require('pp')
-- create stack for tokens inside captures. nil - not inside capture, 0 - inside capture, 1 - token found inside capture
local tokenstack = s.Stack:Create()
local function warn(...)
print('Warning: ', ...)
end
--local subject, errors, errorfunc
local opts = {} -- options
local errors = {}
local function resetOptions()
opts = {}
opts.generrors = false
opts.nocaptures = false
opts.re_useext = false
opts.re_sortrules = true
opts.trace = false
opts.traceoptions = {
['-'] = {SKIP=false},
['+'] = {SKIP=false}
}
opts.skipspaces = true
opts.usenodes = false
opts.recovery = true
opts.labels = {}
opts.definitions = {}
opts.subject = ""
opts.subjectlines = nil
opts.filename = ""
opts.errorfunction = nil
opts.specialrules = {} -- will contain SKIP and SYNC rules
return opts
end
-- check values and fill default to the opts
local function mergeOptions(options)
if options ~= nil then
for k, v in pairs(opts) do
if options[k] ~= nil then opts[k] = options[k] end
end
-- special case for errorfunction
if options.errorfunction ~= nil then opts.errorfunction = options.errorfunction end
-- check illegal options
for k, v in pairs(options) do
if opts[k] == nil then
warn('options "' ..k.. '" ignored') end
end
end
-- ensure errorfunction is a function
if type(opts.errorfunction) ~= 'function' then
opts.errorfunction = nil
end
end
-- Lua 5.1 compatibility:
local unpack = unpack or table.unpack
local Predef = { nl = m.P"\n", cr = m.P"\r", tab = m.P"\t" }
local function updatelocale()
m.locale(Predef)
local any = m.P(1)
Predef.a = Predef.alpha
Predef.c = Predef.cntrl
Predef.d = Predef.digit
Predef.g = Predef.graph
Predef.l = Predef.lower
Predef.p = Predef.punct
Predef.s = Predef.space
Predef.u = Predef.upper
Predef.w = Predef.alnum
Predef.x = Predef.xdigit
Predef.A = any - Predef.a
Predef.C = any - Predef.c
Predef.D = any - Predef.d
Predef.G = any - Predef.g
Predef.L = any - Predef.l
Predef.P = any - Predef.p
Predef.S = any - Predef.s
Predef.U = any - Predef.u
Predef.W = any - Predef.w
Predef.X = any - Predef.x
end
updatelocale()
local function defaultsync(patt)
return (m.P(1)^-1) * (-patt * m.P(1))^0
end
local function sync (patt)
return patt --(-patt * m.P(1))^0 * patt^0 -- skip until we find the pattern and consume it(if we do)
end
local function pattspaces (patt)
if opts.skipspaces then
return patt * opts.specialrules.SKIP ^0
else
return patt
end
end
local function token (patt)
local incapture = tokenstack:pop() -- returns nil if not in capture
if not incapture then
return pattspaces(patt)
end
tokenstack:push(1)
return patt
end
-- functions used by the tool
local function iscompiled (gr)
return m.type(gr) == "pattern"
end
local bg = {} -- local variable to keep global function buildgrammar
local function addspaces (caps)
local hastoken = tokenstack:pop()
if hastoken == 1 then
return pattspaces(caps)
end
return caps
end
local function applyaction(action, op1, op2, tokenrule)
if action == "or" then
return op1 + op2
elseif action == "and" then
return op1 * op2
elseif action == "&" then
return #op1
elseif action == "!" then
return -op1
elseif action == "+" then
return op1^1
elseif action == "*" then
return op1^0
elseif action == "?" then
return op1^-1
elseif action == "^" then
return op1^op2
elseif action == "^LABEL" then
return op1 + m.T(op2)
elseif action == "->" then
return op1 / op2
-- in captures we add SPACES^0
elseif action == "=>" then
return addspaces(m.Cmt(op1,op2))
elseif action == "tcap" then
return m.Ct(op1) -- nospaces
elseif action == "gcap" then
return addspaces(m.Cg(op1, op2))
elseif action == "bref" then
return m.Cb(op1) --m.Cmt(m.Cb(op1), equalcap) -- do we need to add spaces to bcap?
elseif action == "poscap" then
return addspaces(m.Cp())
elseif action == "subcap" then
return addspaces(m.Cs(op1))
elseif action == "scap" then
return addspaces(m.C(op1))
elseif action == "anychar" then
if not opts.nocaptures and not tokenrule then
return m.C(m.P(1))
end
return m.P(1)
elseif action == "label" then
return m.T(op1) -- lpeglabel
elseif action == "%" then
if opts.definitions[op1] then
return opts.definitions[op1]
elseif Predef[op1] then
return Predef[op1]
else
error("Definition for '%"..op1.."' unspecified(use options.definitions parameter)")
end
elseif action == "class" then
local res = op1
if not tokenrule then
if not opts.nocaptures then
res = m.C(res)
end
res = token(res)
end
return res
elseif action == "invert" then
return m.P(1) - op1
elseif action == "range" then
return m.R(op1)
elseif action == "classset" then
return op1 + op2
else
error("Unsupported action '"..action.."'")
end
end
local function applyfinal(action, term, tokenterm, tokenrule)
if action == "t" then
local res = m.P(term)
if not tokenrule then
if not opts.nocaptures then
res = m.C(res)
end
if opts.skipspaces then
res = token(res)
end
end
return res
elseif action == "tchar" then
return m.P(term)
elseif action == "nt" then
if opts.skipspaces and tokenterm and (not tokenrule) then
return token(m.V(term))
else
return m.V(term)
end
elseif action == "func" then
if opts.definitions[term] then
if opts.trace then
local ftrace=function(...)
print('in function: '..term..'('..pp.format(...)..')')
return opts.definitions[term](...)
end
return ftrace
else
return opts.definitions[term]
end
else
error("Definition for function '"..term.."' unspecified (use options.definitions parameter)")
end
elseif action == "name" then -- simple name
return term
elseif action == "s" then -- simple coted string
return term
elseif action == "num" then -- numbered string
return tonumber(term)
end
end
local function applygrammar(gram, opts)
if opts.trace then
return m.P(pegdebugtrace(gram, opts.traceoptions))
else
return m.P(gram)
end
end
local function traverse (ast, tokenrule)
if not ast then
return nil
end
if isfinal(ast) then
local typefn, fn, tok = finalNode(ast)
return applyfinal(typefn, fn, tok, tokenrule)
elseif isaction(ast) then
local act, op1, op2, ret1, ret2
act = ast["action"]
op1 = ast["op1"]
op2 = ast["op2"]
-- post-order traversal
if iscapture(act) then
tokenstack:push(0) -- not found any tokens yet
end
ret1 = traverse(op1, tokenrule)
ret2 = traverse(op2, tokenrule)
return applyaction(act, ret1, ret2, tokenrule)
elseif isgrammar(ast) then
--
local g = bg.buildgrammar (ast)
return applygrammar(g, opts)
else
peg.print_r(ast)
error("Unsupported AST")
end
end
local function specialrules(ast, builder)
-- initialize values
opts.specialrules.SKIP = (Predef.space + Predef.nl)
opts.skipspaces = true
opts.specialrules.SYNC = nil
opts.recovery = true
-- find SPACE and SYNC rules
for i, v in ipairs(ast) do
local name = v["rulename"]
local rule
if name == "SKIP" then
rule = traverse(v["rule"], true)
if v["rule"]["t"] == '' then
opts.skipspaces = false
else
opts.skipspaces = true
opts.specialrules.SKIP = rule
end
builder[name] = rule
elseif name == "SYNC" then
rule = traverse(v["rule"], true)
if v["rule"]["t"] == '' then-- SYNC <- ''
opts.recovery=false
else
opts.recovery= true
opts.specialrules.SYNC = rule
end
builder[name] = rule
end
end
if not opts.specialrules.SYNC and opts.recovery then
opts.specialrules.SYNC = defaultsync(opts.specialrules.SKIP)
end
end
local function recorderror(position, label)
-- call error function here
local line, col = peg.calcline( opts.subject, position)
local desc
if label == 0 then
desc = "Syntax error"
else
desc = opts.labels[label]
end
if not opts["subjectlines"] then opts.subjectlines = peg.splitlines(opts.subject) end
local fline = opts.subjectlines[line]
if opts.errorfunction then
opts.errorfunction(desc, line, col, fline, label, opts.filename)
end
local err = { line = line, col = col, label=label, msg = desc, failLine = fline }
table.insert(errors, err)
end
local function record(label)
return (m.Cp() * m.Cc(label)) / recorderror
end
local function buildrecoverylabel(builder, ast)
local synctoken = pattspaces(sync(opts.specialrules.SYNC))
-- TODO : see if we could iterate over builder instead ?
for lbl, desc in pairs(opts.labels) do
local found = false
for i, v in ipairs(ast) do
local name = v["rulename"]
local isspecial = name == "SKIP" or name == "SYNC"
local rule = v["rule"]
if name == lbl then
found = true
end
end
if not found then
builder[lbl] = record(lbl) * synctoken
end
end
end
function bg.buildgrammar (ast)
local builder = {}
specialrules(ast, builder)
local initialrule
for i, v in ipairs(ast) do
local istokenrule = v["token"] == "1"
local isfragment = v["fragment"] == "1"
local isnode = v["node"] == "1"
if isnode and not opts.usenodes then
warn("Node mode disabled - please use options.usenodes=true before compiling the grammar")
end
local name = v["rulename"]
local isspecial = name == "SKIP" or name == "SYNC"
local rule = v["rule"]
if i == 1 then
initialrule = name
table.insert(builder, name) -- lpeg syntax
end
if not builder[name] then -- dont traverse rules for SKIP and SYNC twice
builder[name] =traverse( rule, istokenrule) -- TODO : protect the traverse call to get the name of the offending rule when something goes wrong
end
if not opts.nocaptures and not isfragment and not isspecial and ((not opts.usenodes) or (opts.usenodes and isnode)) then
if istokenrule then
builder[name] = m.C(builder[name])
end
builder[name] = m.Ct(m.Cg(m.Cc(name),"rule") * m.Cg(m.Cp(),"pos") * builder[name])
end
end
if opts.skipspaces then
builder[initialrule] = opts.specialrules.SKIP^0 * builder[initialrule] -- skip spaces at the beginning of the input
end
if opts.recovery then
-- builder[initialrule] = buildrecovery(builder[initialrule]) -- build recovery on top of initial rule
buildrecoverylabel(builder, ast)
end
-- TODO : add case insensitive keyword support
return builder
end
local function build(ast, opts)
if isgrammar(ast) then
return traverse(ast)
else -- input is not a grammar - skip spaces and sync by default
opts.specialrules.SKIP = (Predef.space + Predef.nl)
opts.skipspaces = true
opts.specialrules.SYNC = defaultsync()
opts.recovery = true
local res = opts.specialrules.SKIP^0 * traverse(ast)
if not opts.nocaptures then
res = m.Ct(res)
end
return res
end
end
-- ast to grammar
-- TODO: rename *AstToPeg en *Re
local function defaultsyncAstToPEG(patt)
return '.? (!'..patt.. ' .)*' --(m.P(1)^-1) * (-patt * m.P(1))^0
end
local function pattspacesAstToPEG (patt)
if opts.skipspaces then
return '('.. patt .." SKIP*)"
else
return patt
end
end
local function tokenAstToPEG (patt)
local incapture = tokenstack:pop() -- returns nil if not in capture
if not incapture then
return pattspacesAstToPEG(patt)
end
tokenstack:push(1)
return patt
end
local function addspacesAstToPEG (caps)
local hastoken = tokenstack:pop()
if hastoken == 1 then
return pattspacesAstToPEG(caps)
end
return caps
end
local function applyActionAstToPEG(action, op1, op2, tokenrule)
if action == "or" then
return '(' .. op1 ..' / '.. op2 ..')'
elseif action == "and" then
return op1 ..' ' .. op2
elseif action == "&" then
return '&'..op1
elseif action == "!" then
return '!'..op1
elseif action == "+" then
return op1..'+'
elseif action == "*" then
return op1..'*'
elseif action == "?" then
return op1..'?'
elseif action == "^" then
return op1..'^'..op2
elseif action == "^LABEL" then
-- local lab = op2
-- if not lab then
-- error("Label '"..op2.."' unspecified using setlabels()")
-- end
return op1 .. '^'..op2
elseif action == "->" then
if op1 == "''" then -- spacial case for empty capture
return op1 ..' -> '.. op2
else
return '('.. op1 ..') -> '.. op2
end
-- in captures we add SPACES^0
elseif action == "=>" then
-- TODO: action '=>' to check for space
--return addspaces(m.Cmt(op1,op2))
return op1 ..'=>'..op2
elseif action == "tcap" then
return '{| '..op1..' |}' -- nospaces
elseif action == "gcap" then
-- return addspaces(m.Cg(op1, op2))
if op2 ~= nil then -- named group capture
return addspacesAstToPEG('{:'..op2..': '.. op1..' :}')
else -- anonymous group capture
return addspacesAstToPEG('{:'..op1..':}')
end
elseif action == "bref" then
-- TODO: action 'bref' to check for space
-- return m.Cb(op1) --m.Cmt(m.Cb(op1), equalcap) -- do we need to add spaces to bcap?
return '='..op1
elseif action == "poscap" then
-- return addspaces(m.Cp())
return addspacesAstToPEG('{}')
elseif action == "subcap" then
-- return addspaces(m.Cs(op1))
return addspacesAstToPEG('{~'..op1..'~}')
elseif action == "scap" then
--return addspaces(m.C(op1))
return addspacesAstToPEG('{'..op1..'}')
elseif action == "anychar" then
if not opts.nocaptures and not tokenrule then
return '{.}' -- return m.C(m.P(1))
end
return '.'--return m.P(1)
elseif action == "label" then
-- local lab = op1
-- if not lab then
-- error("Label '"..op1.."' unspecified using setlabels()")
-- end
-- return m.T(op1) -- lpeglabel
return '%{'..op1..'}'
elseif action == "%" then
if opts.definitions[op1] then
return '%'..op1 --return opts.definitions[op1]
elseif Predef[op1] then
return '%'..op1 -- return Predef[op1]
else
error("Definition for '%"..op1.."' unspecified(use options.definitions parameter)")
end
elseif action == "class" then
local res = '['..op1..']'
if not tokenrule then
if not opts.nocaptures then
res = '{'..res..'}'
end
res = tokenAstToPEG(res)
end
return res
elseif action == "invert" then -- in char range
return '^'..op1
elseif action == "range" then
return string.sub(op1,1,1)..'-'..string.sub(op1,2,2)
elseif action =="classset" then
return op1 .. op2
else
error("Unsupported action '"..action.."'")
end
end
local function applyfinalAstToPEG(action, term, tokenterm, tokenrule)
if action == "t" then
local res = term
if term:find("'") then -- term contains single quote
if term:find('"') then -- both style of quote: error out
error("Unsupported terminal with both quote style ("..term..")")
end
res = '"'..term..'"' -- use double quote
else -- use single quote
res = "'"..term.."'" -- m.P(term)
end
if not tokenrule then
if not opts.nocaptures then
res = '{'..res..'}' -- m.C(res)
end
if opts.skipspaces then
res = tokenAstToPEG(res)
end
end
return res
elseif action == "tchar" then
-- if term == "'" then
-- return [[\']]
-- end
return term
elseif action == "nt" then
if opts.skipspaces and tokenterm and (not tokenrule) then
return tokenAstToPEG(term)-- m.V(term))
else
return term -- m.V(term)
end
elseif action == "func" then
return term --opts.definitions[term]
elseif action == "name" then -- simple name
return term
elseif action == "s" then -- simple cotted string
return "'"..term.."'"
elseif action == "num" then -- numbered string
return tonumber(term)
end
end
local function traverseAstToPEG(ast, tokenrule)
if not ast then
return nil
end
if isfinal(ast) then
local typefn, fn, tok = finalNode(ast)
return applyfinalAstToPEG(typefn, fn, tok, tokenrule)
elseif isaction(ast) then
local act, op1, op2, labs, ret1, ret2
act = ast["action"]
op1 = ast["op1"]
op2 = ast["op2"]
-- post-order traversal
if iscapture(act) then
tokenstack:push(0) -- not found any tokens yet
end
ret1 = traverseAstToPEG(op1, tokenrule)
ret2 = traverseAstToPEG(op2, tokenrule)
return applyActionAstToPEG(act, ret1, ret2, tokenrule)
elseif isgrammar(ast) then
--
local g = bg.grammarAstToPEG (ast)
return g
else
peg.print_r(ast)
error("Unsupported AST")
end
end
local function specialrulesAstToPEG(ast, builder)
-- initialize values
opts.specialrules.SKIP_re = '(%s / %nl)' --(Predef.space + Predef.nl)
-- opts.skipspaces = true
opts.specialrules.SYNC_re = nil
-- opts.recovery = true
-- find SPACE and SYNC rules
for i, v in ipairs(ast) do
local name = v["rulename"]
local rule
if name == "SKIP" then
rule = traverseAstToPEG(v["rule"], true)
if v["rule"]["t"] == '' then
opts.skipspaces = false
else
opts.skipspaces = true
opts.specialrules.SKIP_re = rule
end
builder[name] = rule
elseif name == "SYNC" then
rule = traverseAstToPEG(v["rule"], true)
if v["rule"]["t"] == '' then-- SYNC <- ''
opts.recovery=false
opts.specialrules.SYNC_re = "''"
else
opts.recovery= true
opts.specialrules.SYNC_re = rule
end
builder[name] = rule
end
end
if not opts.specialrules.SYNC_re and opts.recovery then
opts.specialrules.SYNC_re = defaultsyncAstToPEG("SKIP")
end
if opts.skipspaces and not builder["SKIP"] then
builder["SKIP"] = opts.specialrules.SKIP_re
end
if opts.recovery and not builder["SYNC"] then
builder["SYNC"] = opts.specialrules.SYNC_re
end
end
local function recordAstToPEG(label)
--return (m.Cp() * m.Cc(label)) / recorderror
return "({} {'' -> '"..label.."'}) -> recorderror"
end
local function buildrecoverylabelAstToPEG(builder, ast)
local synctoken = pattspacesAstToPEG('SYNC')
-- TODO : see if we could iterate over builder instead ?
for lbl, desc in pairs(opts.labels) do
local found = false
for i, v in ipairs(ast) do
local name = v["rulename"]
local isspecial = name == "SKIP" or name == "SYNC"
local rule = v["rule"]
if name == lbl then
found = true
end
end
if not found then
builder[lbl] = recordAstToPEG(lbl) ..' '.. synctoken
end
end
end
function bg.grammarAstToPEG(ast)
local builder = {}
local fragments = {}
local nodes = {}
specialrulesAstToPEG(ast, builder)
local initialrule
for i, v in ipairs(ast) do
local istokenrule = v["token"] == "1"
local isfragment = v["fragment"] == "1"
local isnode = v["node"] == "1"
if isnode and not opts.usenodes then
warn("Node mode disabled - please use options.usenodes = true before compiling the grammar")
end
local name = v["rulename"]
local isspecial = name == "SKIP" or name == "SYNC"
local rule = v["rule"]
if isfragment then fragments[name] = true end
if isnode then nodes[name] = true end
if i == 1 then
initialrule = name
table.insert(builder, name) -- lpeg syntax
end
if not builder[name] then -- dont traverse rules twice
builder[name] = traverseAstToPEG(rule, istokenrule)
end
if not opts.nocaptures and not isfragment and not isspecial and ((not opts.usenodes) or (opts.usenodes and isnode)) then
if istokenrule then
-- builder[name] = m.C(builder[name])
builder[name] = '{'..builder[name]..'}'
end
--builder[name] = m.Ct(m.Cg(m.Cc(name),"rule") * m.Cg(m.Cp(),"pos") * builder[name])
builder[name] = "{| {:rule: '' -> '"..name.."' :} {:pos: {} :} " ..builder[name] .." |}"
if istokenrule then
builder[name] = builder[name] .. ' -- token'
end
if isnode then
builder[name] = builder[name] .. ' -- node'
end
end
end
if opts.skipspaces then
-- builder[initialrule] = opts.SKIP^0 * builder[initialrule] -- skip spaces at the beginning of the input
builder[initialrule] = 'SKIP* '.. builder[initialrule] -- skip spaces at the beginning of the input
end
if opts.recovery then
opts.definitions.recorderror = recorderror -- add recording function in definitions
-- builder[initialrule] = buildrecoveryAstToPEG(builder[initialrule]) -- build recovery on top of initial rule
-- assert(false)
buildrecoverylabelAstToPEG(builder, ast)
end
-- TODO : add case insensitive keyword support
local regrammar = {}
local done = {}
local rules = {}
for k, v in pairs(builder) do
if type(k) == 'string' then table.insert(rules, k) end
end
if opts.re_sortrules then table.sort(rules) end
table.insert(regrammar, initialrule .. ' <- ' .. builder[initialrule]) -- initial rule
done[initialrule] = true
for i, k in ipairs(rules) do
if type(k) == 'string' then
if not done[k] then
local rule = k
if opts.re_useext and fragments[k] then rule = 'fragment ' ..k end
if opts.re_useext and nodes[k] then rule = 'node ' ..k end
table.insert(regrammar, rule ..' <- '..builder[k])
done[k] = true
end
end
end
return table.concat(regrammar, '\n' )
end
local function astToPEG(ast, options)
resetOptions()
mergeOptions(options)
if isgrammar(ast) then
return traverseAstToPEG(ast), opts
end
end
-- end ast to grammar
-- recovery grammar
-- end
-- Compile the grammar to a LPEG objet.
--
-- @param p : a grammar to compile
-- @param pre-defined sets and functions
-- @param options table may be nil, default are
-- {generrors = false, nocaptures = false}
-- available fields are
-- nocaptures = false -- do not put capture around terminals
-- skipspaces = true -- add space skiping in rules
-- re_useext = false -- output grammar with parser-gen extentions (fragment, node)
-- re_sortrules = true -- output grammar with rules sorted
-- recovery = true -- add automatic recovery when applicable
-- usenodes = false -- node mode to generate AST on node rules only
-- labels = {err1="Error message1"} -- nice error messages for your labels (or generated recovery )
-- definitions = {} -- additional functions for the grammar
-- errorfunction = function(desc, line, col, fline, label, filename) ... end -- function called when a label is thrown
-- subject = "" -- _internal_ the input string that will be parsed and allow for better errors messages. Will be overvitten by parse()
-- @return the compiled grammar as a LPEG userdata
local function compile(p, options)
if iscompiled(p) then return p end
resetOptions()
mergeOptions(options)
--re.setlabels(tlabels)
--re.compile(input,defs)
-- build ast
local ast = peg.pegToAST(p, opts.defs)
if opts.generrors then
local follow = eg.follow(ast)
local errors = eg.adderrors(ast, follow)
-- setlabels (errors, true) -- add errors generated by errorgen
for k,v in pairs(errors) do
opts.labels[k] = v
print ("generate error "..k..': '..v)
end
end
local gram = build(ast, opts)
if not gram then
-- TODO : find error using relabel module
end
return gram
end
local function genErrorRecovery(ast, options)
mergeOptions(options)
if not ast then return nil, opts end
if not isgrammar(ast) then return nil, opts end
if opts.generrors then
local follow = eg.follow(ast)
local errors = eg.adderrors(ast, follow)
for k,v in pairs(errors) do
opts.labels[k] = v
print ("ast generate error "..k..': '..v)
end
end
return ast, opts
end
--- Compile grammar and parse input in one go.
-- Compile the grammar
-- @param input the string to parse
-- @param grammar the relabel style grammar to use
-- @param options see compile()
-- @return (res, errors) a table containing result of match and errors
-- or nil if everything goes as planned
local function parse (input, grammar, options)
if not iscompiled(grammar) then
local cp = compile(grammar, options)
grammar = cp
else
mergeOptions(options)
end
opts.subject = input
opts.subjectlines = nil -- reset lines
-- set up recovery table
errors = {}
-- end
local r, e, sfail = m.match(grammar,input)
if not r then
recorderror(#input - sfail, e)
end
if #errors == 0 then errors=nil end
return r, errors
end
local pg = {
compile=compile,
parse=parse,
-- follow=follow,
calcline = peg.calcline,
astToPEG=astToPEG,
genErrorRecovery=genErrorRecovery
}
return pg