-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xpath.lua
1814 lines (1665 loc) · 48.5 KB
/
xpath.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
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
dofile("debug.lua")
function enterStep() end
function leaveStep() end
local xstring
-- texlua has slnunicode, otherwise you should use lua-utf8 from luarocks
if unicode and unicode.utf8 then
xstring = unicode.utf8
else
local utf8 = require 'lua-utf8'
if utf8 then
xstring = utf8
xstring.format = string.format
end
end
local match = xstring.match
local stringreader = require("stringreader")
local xpathfunctions = {}
local function register(ns, name, fun)
xpathfunctions[ns] = xpathfunctions[ns] or {}
xpathfunctions[ns][name] = fun
end
local function flattensequence(arg)
local ret = {}
if type(arg) ~= "table" then return arg end
for i = 1, #arg do
if type(arg[i]) == "table" then
local argi = arg[i]
if argi[".__type"] == "element" then
ret[#ret + 1] = argi
elseif argi[".__type"] == "attribute" then
ret[#ret + 1] = tostring(argi)
else
local tmp = flattensequence(arg[i])
if type(tmp) == "table" then
for j = 1, #tmp do
ret[#ret+1] = tmp[j]
end
else
ret[#ret+1] = tmp
end
end
else
ret[#ret+1] = arg[i]
end
end
if #ret == 1 then return ret[1] end
return ret
end
local function isEmptySequence(arg)
if arg == nil then return true end
while true do
if type(arg) == "table" and #arg == 1 and not arg[".__type"] then
arg = arg[1]
else
break
end
end
return type(arg) == "table" and next(arg) == nil
end
local function doCompare(cmpfunc, a, b)
if type(a) == "table" and a[".__type"] == "attribute" then
a = tostring(a)
else
a = flattensequence(a)
end
if type(b) == "table" and b[".__type"] == "attribute" then
b = tostring(b)
else
b = flattensequence(b)
end
if type(a) == "number" or type(a) == "string" then
a = {a}
end
if type(b) == "number" or type(b) == "string" then
b = {b}
end
local taba = {}
local tabb = {}
if not a then return false end
for i = 1, #a do
taba[i] = tostring(a[i])
end
for i = 1, #b do
tabb[i] = tostring(b[i])
end
a = taba
b = tabb
for ca = 1, #a do
for cb = 1, #b do
if cmpfunc(a[ca], b[cb]) then
return true
end
end
end
return false
end
local function isEqual(a, b)
return a == b
end
local function isNotEqual(a, b)
return a ~= b
end
local function isLess(a, b)
return a < b
end
local function isLessEqual(a, b)
return a <= b
end
local function isGreater(a, b)
return a > b
end
local function isGreaterEqual(a, b)
return a >= b
end
-- Read all space until a non-space is found
local function space(sr)
if sr:eof() then
return
end
while match(sr:getc(), "%s") do
if sr:eof() then
return
end
end
sr:back()
end
local function get_num(sr)
local ret = {}
while true do
if sr:eof() then
break
end
local c = sr:getc()
if match(c, "[%d.]") then
table.insert(ret, c)
else
sr:back()
break
end
end
if not sr:eof() and xstring.lower(sr:peek()) == "e" then
table.insert(ret, "e")
sr:getc()
if not sr:eof() and xstring.lower(sr:peek()) == "-" then table.insert(ret, "-") sr:getc() end
while true do
if sr:eof() then
break
end
local c = sr:getc()
if match(c, "[%d.]") then
table.insert(ret, c)
else
sr:back()
break
end
end
end
return table.concat(ret, "")
end
local function get_word(sr)
local ret = {}
while true do
if sr:eof() then
break
end
local c = sr:getc()
if match(c, "[-%a%d]") then
table.insert(ret, c)
elseif match(c, ":") and match(sr:peek(), "%a") then
table.insert(ret, c)
else
sr:back()
break
end
end
return table.concat(ret, "")
end
local function get_comment(sr)
local ret = {}
local level = 1
sr:getc()
while true do
if sr:eof() then
break
end
local c = sr:getc()
if c == ":" and sr:peek() == ")" then
level = level - 1
if level == 0 then
sr:getc()
break
else
table.insert(ret, ":")
end
elseif c == "(" and sr:peek() == ":" then
level = level + 1
sr:getc()
table.insert(ret, "(:")
else
table.insert(ret, c)
end
end
return table.concat(ret)
end
local function get_delimited_string(sr)
local ret = {}
local delim = sr:getc()
while true do
if sr:eof() then
break
end
local c = sr:getc()
if c ~= delim then
table.insert(ret, c)
else
break
end
end
return table.concat(ret, "")
end
local function xpath_test_eltname(eltname)
return function(xmlelt)
if type(xmlelt) == "table" and ( xmlelt[".__name"] == eltname or eltname == "*") then
return true
end
return false
end
end
local TOK_WORD = 1
local TOK_VAR = 2
local TOK_OPENPAREN = 3
local TOK_CLOSEPAREN = 4
local TOK_STRING = 7
local TOK_COMMENT = 8
local TOK_NUMBER = 9
local TOK_OPERATOR = 10
local TOK_OCCURRENCEINDICATOR = 11
local TOK_OPENBRACKET = 12
local TOK_CLOSEBRACKET = 13
local TOK_QNAME = 14
local TOK_NCNAME = 15
local opBooleanEqual
-- [2] Expr ::= ExprSingle ("," ExprSingle)*
function parseExpr(infotbl)
enterStep(infotbl, "2 parseExpr")
local ret = {}
ret[#ret + 1] = parseExprSingle(infotbl)
while true do
local nt = infotbl.peek()
if nt and nt[2] == "," then
infotbl.skip(",")
ret[#ret + 1] = parseExprSingle(infotbl)
else
break
end
end
leaveStep(infotbl, "2 parseExpr")
return function(ctx)
assert(ctx)
if #ret == 1 then
return ret[1](ctx)
else
local new = {}
for i = 1, #ret do
table.insert(new, ret[i](ctx))
end
return new
end
end
end
-- [3] ExprSingle ::= ForExpr | QuantifiedExpr | IfExpr | OrExpr
function parseExprSingle(infotbl)
enterStep(infotbl, "3 parseExprSingle")
local nexttok = infotbl.peek()
local ret
if nexttok then
local nexttoktype = nexttok[1]
local nexttokvalue = nexttok[2]
if nexttokvalue == "for" then
ret = parseForExpr(infotbl)
elseif nexttokvalue == "some" or nexttokvalue == "every" then
parseQuantifiedExpr(infotbl)
elseif nexttokvalue == "if" then
ret = parseIfExpr(infotbl)
else
ret = parseOrExpr(infotbl)
end
end
leaveStep(infotbl, "3 parseExprSingle")
return ret
end
-- [4] ForExpr ::= SimpleForClause "return" ExprSingle
-- Parse `for $foo in ... return` expression
---@return function contextevaluator
function parseForExpr(infotbl)
enterStep(infotbl, "4 parseForExpr")
local sfc = parseSimpleForClause(infotbl)
infotbl.skip("return")
local ret = parseExprSingle(infotbl)
leaveStep(infotbl, "4 parseForExpr")
return function(ctx)
assert(ctx)
local varname, tbl = sfc(ctx)
local newret = {}
for i = 1, #tbl do
ctx.var[varname] = tbl[i]
table.insert(newret, ret(ctx))
end
return newret
end
end
-- [5] SimpleForClause ::= "for" "$" VarName "in" ExprSingle ("," "$" VarName "in" ExprSingle)*
function parseSimpleForClause(infotbl)
enterStep(infotbl, "5 parseSimpleForClause")
infotbl.skip("for")
local nexttok = infotbl.peek()
local nexttoktype = nexttok[1]
local varname = nexttok[2]
if nexttoktype ~= TOK_VAR then
w("parse error simpleForClause")
end
_ = infotbl.nexttok
infotbl.skip("in")
local ret = parseExprSingle(infotbl)
leaveStep(infotbl, "5 parseSimpleForClause")
return function(ctx)
assert(ctx)
return varname, ret(ctx)
end
end
-- [6] QuantifiedExpr ::= ("some" | "every") "$" VarName "in" ExprSingle ("," "$" VarName "in" ExprSingle)* "satisfies" ExprSingle
-- [7] IfExpr ::= "if" "(" Expr ")" "then" ExprSingle "else" ExprSingle
function parseIfExpr(infotbl)
enterStep(infotbl, "7 parseIfExpr")
local nexttok = infotbl.nexttok
if nexttok[2] ~= "if" then
w("parse error, 'if' expected")
end
infotbl.skiptoken(TOK_OPENPAREN)
local test = parseExpr(infotbl)
infotbl.skiptoken(TOK_CLOSEPAREN)
infotbl.skip("then")
local thenpart = parseExprSingle(infotbl)
infotbl.skip("else")
local elsepart = parseExprSingle(infotbl)
leaveStep(infotbl, "7 parseIfExpr")
return function(ctx)
assert(ctx)
if opBooleanEqual(test(ctx),true) then
return thenpart(ctx)
else
return elsepart(ctx)
end
end
end
local function booleanValue(arg)
if arg == nil then return false end
if tonumber(arg) then
return tonumber(arg) ~= 0
elseif type(arg) == "boolean" then
return arg
elseif type(arg) == "string" then
return #arg > 0
elseif type(arg) == "table" and #arg == 0 then
return false
elseif type(arg) == "table" and #arg == 1 then
return booleanValue(arg[1])
end
return true
end
function opBooleanEqual(a,b)
local a = booleanValue(a)
local b = booleanValue(b)
return a == b
end
-- [8] OrExpr ::= AndExpr ( "or" AndExpr )*
function parseOrExpr(infotbl)
enterStep(infotbl, "8 parseOrExpr")
local ret = parseAndExpr(infotbl)
if ret == nil then
return nil
end
local tmp = {ret}
while true do
local nexttok = infotbl.peek()
if nexttok and nexttok[2] == "or" then
_ = infotbl.nexttok
tmp[#tmp + 1] = parseAndExpr(infotbl)
else
break
end
end
if #tmp == 1 then
-- ok, just use the value of AndExpr
else
ret = function(ctx)
for i = 1, #tmp do
if tmp[i](ctx) then
return true
end
end
return false
end
end
leaveStep(infotbl, "8 parseOrExpr")
return ret
end
-- [9] AndExpr ::= ComparisonExpr ( "and" ComparisonExpr )*
function parseAndExpr(infotbl)
enterStep(infotbl, "9 parseAndExpr")
local ret = parseComparisonExpr(infotbl)
if ret == nil then
return nil
end
local tmp = {ret}
while true do
local nexttok = infotbl.peek()
if nexttok and nexttok[2] == "and" then
_ = infotbl.nexttok
tmp[#tmp + 1] = parseAndExpr(infotbl)
else
break
end
end
if #tmp == 1 then
-- ok, just use the value of ComparisonExpr
else
ret = function(ctx)
for i = 1, #tmp do
if not tmp[i](ctx) then
return false
end
end
return true
end
end
leaveStep(infotbl, "9 parseAndExpr")
return ret
end
-- [10] ComparisonExpr ::= RangeExpr ( (ValueComp | GeneralComp| NodeComp) RangeExpr )?
function parseComparisonExpr(infotbl)
enterStep(infotbl, "10 parseComparisonExpr")
local lhs = parseRangeExpr(infotbl)
if lhs == nil then
return nil
end
local nexttok = infotbl.peek()
-- [23] ValueComp ::= "eq" | "ne" | "lt" | "le" | "gt" | "ge"
-- [22] GeneralComp ::= "=" | "!=" | "<" | "<=" | ">" | ">="
-- [24] NodeComp ::= "is" | "<<" | ">>"
local ret = lhs
if
nexttok and
(nexttok[2] == "eq" or nexttok[2] == "ne" or nexttok[2] == "lt" or nexttok[2] == "le" or nexttok[2] == "gt" or
nexttok[2] == "ge" or
nexttok[2] == "=" or
nexttok[2] == "!=" or
nexttok[2] == "<" or
nexttok[2] == "<=" or
nexttok[2] == ">" or
nexttok[2] == ">=" or
nexttok[2] == "is" or
nexttok[2] == "<<" or
nexttok[2] == ">>")
then
local op = (infotbl.nexttok)[2]
local rhs = parseRangeExpr(infotbl)
if op == "=" then
ret = function(ctx)
return doCompare(isEqual, lhs(ctx), rhs(ctx))
end
elseif op == "!=" then
ret = function(ctx)
return doCompare(isNotEqual, lhs(ctx), rhs(ctx))
end
elseif op == "<" then
ret = function(ctx)
return doCompare(isLess, lhs(ctx), rhs(ctx))
end
elseif op == "<=" then
ret = function(ctx)
return doCompare(isLessEqual, lhs(ctx), rhs(ctx))
end
elseif op == ">" then
ret = function(ctx)
return doCompare(isGreater, lhs(ctx), rhs(ctx))
end
elseif op == ">=" then
ret = function(ctx)
return doCompare(isGreaterEqual, lhs(ctx), rhs(ctx))
end
elseif op == "eq" then
ret = function(ctx)
return doCompare(isEqual, lhs(ctx), rhs(ctx))
end
elseif op == "ne" then
ret = function(ctx)
return doCompare(isNotEqual, lhs(ctx), rhs(ctx))
end
elseif op == "lt" then
ret = function(ctx)
return doCompare(isLess, lhs(ctx), rhs(ctx))
end
elseif op == "le" then
ret = function(ctx)
return doCompare(isLessEqual, lhs(ctx), rhs(ctx))
end
elseif op == "gt" then
ret = function(ctx)
return doCompare(isGreater, lhs(ctx), rhs(ctx))
end
elseif op == "ge" then
ret = function(ctx)
return doCompare(isGreaterEqual, lhs(ctx), rhs(ctx))
end
end
end
leaveStep(infotbl, "10 parseComparisonExpr")
return ret
end
-- [11] RangeExpr ::= AdditiveExpr ( "to" AdditiveExpr )?
function parseRangeExpr(infotbl)
enterStep(infotbl, "11 parseRangeExpr")
local ae = parseAdditiveExpr(infotbl)
local nt = infotbl.peek()
local ret
if nt and nt[2] == "to" then
_ = infotbl.nexttok
local to = parseAdditiveExpr(infotbl)
ret = function(ctx)
assert(ctx)
local newret = {}
for i = ae(ctx), to(ctx) do
table.insert(newret, i)
end
return newret
end
else
ret = ae
end
leaveStep(infotbl, "11 parseRangeExpr")
return ret
end
-- [12] AdditiveExpr ::= MultiplicativeExpr ( ("+" | "-") MultiplicativeExpr )*
function parseAdditiveExpr(infotbl)
enterStep(infotbl, "12 parseAdditiveExpr")
ret = parseMultiplicativeExpr(infotbl)
local tbl = {}
if ret == nil then
return nil
end
tbl[#tbl + 1] = ret
while true do
local operator = infotbl.peek()
if not operator then
break
end
if operator[2] == "+" or operator[2] == "-" then
tbl[#tbl + 1] = operator[2]
local op = infotbl.nexttok
tbl[#tbl + 1] = parseMultiplicativeExpr(infotbl)
else
break
end
end
leaveStep(infotbl, "12 parseAdditiveExpr")
return function(ctx)
assert(ctx)
local cur
cur = tbl[1](ctx)
local i = 1
while i < #tbl do
if tbl[i + 1] == "+" then
cur = cur + tbl[i + 2](ctx)
else
cur = cur - tbl[i + 2](ctx)
end
i = i + 2
end
return cur
end
end
-- [13] MultiplicativeExpr ::= UnionExpr ( ("*" | "div" | "idiv" | "mod") UnionExpr )*
function parseMultiplicativeExpr(infotbl)
enterStep(infotbl, "13 parseMultiplicativeExpr")
local ret = parseUnionExpr(infotbl)
if ret == nil then
return nil
end
local tbl = {}
tbl[#tbl + 1] = ret
while true do
local operator = infotbl.peek()
if operator == nil then
break
end
if operator[2] == "*" or operator[2] == "div" or operator[2] == "idiv" or operator[2] == "mod" then
tbl[#tbl + 1] = operator[2]
local op = infotbl.nexttok
tbl[#tbl + 1] = parseUnionExpr(infotbl)
else
break
end
end
leaveStep(infotbl, "13 parseMultiplicativeExpr")
return function(ctx)
if #tbl == 0 then
return tbl
elseif #tbl == 1 then
return tbl[1](ctx)
end
local cur
cur = tbl[1](ctx)
cur = flattensequence(cur)
local i = 1
while i < #tbl do
if tbl[i + 1] == "*" then
cur = cur * tbl[i + 2](ctx)
elseif tbl[i + 1] == "div" then
cur = cur / tbl[i + 2](ctx)
elseif tbl[i + 1] == "idiv" then
local first = cur
local second = tbl[i + 2](ctx)
local a = first / second
if a > 0 then
cur = math.floor(a)
else
cur = math.ceil(a)
end
elseif tbl[i + 1] == "mod" then
cur = cur % tbl[i + 2](ctx)
end
i = i + 2
end
return cur
end
end
-- [14] UnionExpr ::= IntersectExceptExpr ( ("union" | "|") IntersectExceptExpr )*
function parseUnionExpr(infotbl)
enterStep(infotbl, "14 parseUnionExpr")
local ret
ret = parseIntersectExceptExpr(infotbl)
-- while...
-- check for "union" or "|" then parse another IntersectExceptExpr
leaveStep(infotbl, "14 parseUnionExpr")
return ret
end
-- [15] IntersectExceptExpr ::= InstanceofExpr ( ("intersect" | "except") InstanceofExpr )*
function parseIntersectExceptExpr(infotbl)
enterStep(infotbl, "15 parseIntersectExceptExpr")
local ret
ret = parseInstanceofExpr(infotbl)
-- while...
-- check for "intersect" or "except" then parse another InstanceofExpr
leaveStep(infotbl, "15 parseIntersectExceptExpr")
return ret
end
-- [16] InstanceofExpr ::= TreatExpr ( "instance" "of" SequenceType )?
function parseInstanceofExpr(infotbl)
enterStep(infotbl, "16 parseInstanceofExpr")
local ret = parseTreatExpr(infotbl)
leaveStep(infotbl, "16 parseInstanceofExpr")
return ret
end
-- [17] TreatExpr ::= CastableExpr ( "treat" "as" SequenceType )?
function parseTreatExpr(infotbl)
enterStep(infotbl, "17 parseTreatExpr")
local ret = parseCastableExpr(infotbl)
leaveStep(infotbl, "17 parseTreatExpr")
return ret
end
-- [18] CastableExpr ::= CastExpr ( "castable" "as" SingleType )?
function parseCastableExpr(infotbl)
enterStep(infotbl, "18 parseCastableExpr")
local ret = parseCastExpr(infotbl)
leaveStep(infotbl, "18 parseCastableExpr")
return ret
end
-- [19] CastExpr ::= UnaryExpr ( "cast" "as" SingleType )?
function parseCastExpr(infotbl)
enterStep(infotbl, "19 parseCastExpr")
local ret = parseUnaryExpr(infotbl)
leaveStep(infotbl, "19 parseCastExpr")
return ret
end
-- [20] UnaryExpr ::= ("-" | "+")* ValueExpr
function parseUnaryExpr(infotbl)
enterStep(infotbl, "20 parseUnaryExpr")
local mult = 1
while true do
local nexttok = infotbl.peek()
if nexttok and nexttok[2] == "-" or nexttok[2] == "+" then
local op = infotbl.nexttok
if op[2] == "-" then
mult = mult * -1
end
else
break
end
end
local ret = parseValueExpr(infotbl)
leaveStep(infotbl, "20 parseUnaryExpr")
if mult == -1 then
return function(ctx)
return -1 * ret(ctx)
end
else
return ret
end
end
-- [21] ValueExpr ::= PathExpr
function parseValueExpr(infotbl)
enterStep(infotbl, "21 parseValueExpr")
local ret = parsePathExpr(infotbl)
leaveStep(infotbl, "21 parseValueExpr")
return ret
end
-- [25] PathExpr ::= ("/" RelativePathExpr?) | ("//" RelativePathExpr) | RelativePathExpr
function parsePathExpr(infotbl)
enterStep(infotbl, "25 parsePathExpr")
local nexttok = infotbl.peek()
if not nexttok then
leaveStep(infotbl, "25 parsePathExpr")
return
end
local rpe, ret
if nexttok[2] == "/" then
infotbl.skip("/")
rpe = parseRelativePathExpr(infotbl)
if rpe then
ret = function(ctx)
local nn = ctx.nn
nn:root()
return rpe(ctx)
end
else
ret = function(ctx)
local nn = ctx.nn
return nn:root()
end
end
else
ret = parseRelativePathExpr(infotbl)
end
leaveStep(infotbl, "25 parsePathExpr")
return ret
end
-- [26] RelativePathExpr ::= StepExpr (("/" | "//") StepExpr)*
function parseRelativePathExpr(infotbl)
enterStep(infotbl, "26 parseRelativePathExpr")
local ret = {}
ret[#ret+1] = parseStepExpr(infotbl)
while true do
local nt = infotbl.peek()
if not nt then
break
end
if nt[2] == "/" or nt[2] == "//" then
infotbl.skip(nt[2])
nt = infotbl.peek()
local f
local tmp = parseStepExpr(infotbl)
f = function(ctx)
local save_current = ctx.nn.current
local seret = {}
for i = 1, #save_current do
ctx.nn.current = save_current[i]
local x = tmp(ctx)
table.insert(seret,x)
end
return seret
end
ret[#ret+1] = f
else
break
end
end
leaveStep(infotbl, "26 parseRelativePathExpr")
if #ret == 0 then return nil end
if #ret == 1 then return ret[1] end
return function (ctx)
local newret
for i = 1, #ret do
newret = ret[i](ctx)
end
return newret
end
end
-- 27 StepExpr := FilterExpr | AxisStep
function parseStepExpr(infotbl)
enterStep(infotbl, "27 parseStepExpr")
local ret = parseFilterExpr(infotbl)
if not ret then
ret = parseAxisStep(infotbl)
end
leaveStep(infotbl, "27 parseStepExpr")
return ret
end
-- [28] AxisStep ::= (ReverseStep | ForwardStep) PredicateList
function parseAxisStep(infotbl)
enterStep(infotbl, "28 parseAxisStep")
local ret = parseReverseStep(infotbl)
if not ret then
ret = parseForwardStep(infotbl)
end
local pl = parsePredicateList(infotbl)
local newret = ret
if #pl > 0 then
newret = function(ctx)
ret(ctx)
for i = 1, #pl do
local predicate = pl[i]
ctx.nn:filter(ctx,predicate)
end
return ctx.nn.current
end
end
leaveStep(infotbl, "28 parseAxisStep")
return newret
end
-- [29] ForwardStep ::= (ForwardAxis NodeTest) | AbbrevForwardStep
function parseForwardStep(infotbl)
enterStep(infotbl, "29 parseForwardStep")
-- ForwardAxis is something like child:: descendant::
local pfa = parseForwardAxis(infotbl)
local pnt,ret
if pfa then
pnt = parseNodeTest(infotbl)
ret = function(ctx) return ctx.nn:child(xpath_test_eltname(pfa)) end
else
local attributes = false
local nt = infotbl.peek()
-- [31] AbbrevForwardStep == "@"? NodeTest
if nt and nt[2] == "@" then
_ = infotbl.nexttok
attributes = true
end
pnt = parseNodeTest(infotbl)
if pnt then
if attributes then
ret = function(ctx) return ctx.nn:attributes(pnt) end
else
ret = function(ctx) return ctx.nn:child(xpath_test_eltname(pnt)) end
end
end
end
leaveStep(infotbl, "29 parseForwardStep")
return ret
end
-- [30] ForwardAxis ::= ("child" "::") | ("descendant" "::")| ("attribute" "::")| ("self" "::")| ("descendant-or-self" "::")| ("following-sibling" "::")| ("following" "::")| ("namespace" "::")
function parseForwardAxis(infotbl)
enterStep(infotbl, "30 parseForwardAxis")
local nt = infotbl.peek()
local nt2 = infotbl.peek(2)
local ret
if nt and nt2 and nt2 == "::" then
local opname = nt[2]
if
opname == "child" or opname == "descendant" or opname == "attribute" or opname == "self" or
opname == "descendant-or-self" or
opname == "following-sibling" or
opname == "following" or
opname == "namespace"
then
_ = infotbl.nexttok
_ = infotbl.nexttok
ret = {}
end
else
-- w("else")
end
leaveStep(infotbl, "30 parseForwardAxis")
end
-- [32] ReverseStep ::= (ReverseAxis NodeTest) | AbbrevReverseStep
-- [34] AbbrevReverseStep ::= ".."
function parseReverseStep(infotbl)
enterStep(infotbl, "32 parseReverseStep")
local ret = parseReverseAxis(infotbl)
if ret then
ret = parseNodeTest(infotbl)
else
local nt = infotbl.peek()
if nt and nt[2] == ".." then
ret = {}
end
end
leaveStep(infotbl, "32 parseReverseStep")
return ret
end
-- [33] ReverseAxis ::= ("parent" "::") | ("ancestor" "::") | ("preceding-sibling" "::") | ("preceding" "::") | ("ancestor-or-self" "::")
function parseReverseAxis(infotbl)
enterStep(infotbl, "33 parseReverseAxis")
local nt = infotbl.peek()
local nt2 = infotbl.peek(2)
local ret
if nt and nt2 then
local opname = nt[2]
local doublecolon = nt2[2]
if
doublecolon == "::" and
(opname == "parent" or opname == "ancestor" or opname == "preceding-sibling" or opname == "preceding" or
opname == "ancestor-or-self")
then
_ = infotbl.nexttok
_ = infotbl.nexttok
ret = {}
end
end
leaveStep(infotbl, "33 parseReverseAxis")
return ret
end
-- [35] NodeTest ::= KindTest | NameTest
function parseNodeTest(infotbl)
enterStep(infotbl, "35 parseNodeTest")
local ret
ret = parseKindTest(infotbl)
if not ret then
ret = parseNameTest(infotbl)
end
leaveStep(infotbl, "35 parseNodeTest")
return ret
end
-- [36] NameTest ::= QName | Wildcard
function parseNameTest(infotbl)
enterStep(infotbl, "36 parseNameTest")
local nt = infotbl.peek()
local nt2 = infotbl.peek(2)
local nt3 = infotbl.peek(3)
local ret
if nt then
if nt[1] == TOK_QNAME or nt[1] == TOK_NCNAME and not ( nt2 and nt2[2] == ":" ) then
_ = infotbl.nexttok
ret = nt[2]