-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lxpath.lua
3004 lines (2778 loc) · 83.9 KB
/
lxpath.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
local M = {
private = {},
funcs = {},
dodebug = false,
debugindent = " ",
fnNS = "http://www.w3.org/2005/xpath-functions",
xsNS = "http://www.w3.org/2001/XMLSchema",
stringmatch = string.match,
stringfind = string.find,
findfile = function(fn) return fn end,
parse_xml = function(fn) return {} end -- dummy
}
local debuglevel = 0
local nan = 0 / 0
local function unread_rune(tbl)
tbl.pos = tbl.pos - 1
end
---@return string
---@return boolean
local function read_rune(tbl)
local r = tbl[tbl.pos]
tbl.pos = tbl.pos + 1
if tbl.pos > #tbl + 1 then return r, true end
return r, false
end
local function is_letter(str)
return M.stringmatch(str, "%w")
end
local function is_digit(str)
return M.stringmatch(str, "[0-9]")
end
local function is_space(str)
return M.stringmatch(str, "%s")
end
---@param runes table
---@return string
local function get_qname(runes)
local word = {}
local hasColon = false
local r, eof
while true do
r, eof = read_rune(runes)
if eof then break end
if is_letter(r) or is_digit(r) or r == '_' or r == '-' or r == '·' or r == '‿' or r == '⁀' then
word[#word + 1] = r
elseif r == ":" then
if hasColon then
unread_rune(runes)
break
end
word[#word + 1] = r
hasColon = true
else
unread_rune(runes)
break
end
end
return table.concat(word)
end
M.private.get_qname = get_qname
---@return string
local function get_delimited_string(tbl)
local str = {}
local eof = false
local r
local delim = read_rune(tbl)
while true do
r, eof = read_rune(tbl)
if eof then break end
if r == delim then
break
else
str[#str + 1] = r
end
end
return table.concat(str)
end
---@return string comment
local function get_comment(tbl)
local level = 1
local cur, after
local eof
local comment = {}
while true do
cur, eof = read_rune(tbl)
if eof then break end
after, eof = read_rune(tbl)
if eof then break end
if cur == ':' and after == ')' then
level = level - 1
if level == 0 then
break
end
elseif cur == '(' and after == ':' then
level = level + 1
end
comment[#comment + 1] = cur
if after == ':' or after == '(' then
unread_rune(tbl)
else
-- add after to comment
comment[#comment + 1] = after
end
end
return table.concat(comment)
end
---@return number?
local function get_num(runes)
local tbl = {}
local eof = false
local r
while true do
r, eof = read_rune(runes)
if eof then break end
if '0' <= r and r <= '9' then
tbl[#tbl + 1] = r
elseif r == "." or r == "e" or r == "-" then
tbl[#tbl + 1] = r
else
unread_rune(runes)
break
end
end
return tonumber(table.concat(tbl, ""))
end
M.private.get_num = get_num
---@return table
local function split_chars(str)
local runes = {}
for _, c in utf8.codes(str) do
runes[#runes + 1] = utf8.char(c)
end
runes.pos = 1
return runes
end
M.private.split_chars = split_chars
---@class token
---@class tokenlist
local tokenlist = {}
function tokenlist:new(o)
o = o or {} -- create object if user does not provide one
setmetatable(o, self)
self.__index = self
self.pos = 1
self.attributeMode = false
return o
end
---@param pos integer?
---@return token?
---@return boolean
function tokenlist:peek(pos)
pos = pos or 1
if self.pos + pos - 1 > #self then
return nil, true
end
return self[self.pos + pos - 1], false
end
---@return token?
---@return string?
function tokenlist:read()
if self.pos > #self then
return nil, "eof"
end
self.pos = self.pos + 1
return self[self.pos - 1], nil
end
---@return string?
function tokenlist:unread()
if self.pos == 1 then
return "eof"
end
self.pos = self.pos - 1
return nil
end
---@return string?
function tokenlist:skipNCName(name)
local tok, errmsg = self:read()
if errmsg then
return errmsg
end
if tok[2] ~= "tokQName" then
return "QName expected, got " .. tok[2]
end
if tok[1] == name then return nil end
return ""
end
---@param tokvalues table
---@return token?
---@return string?
function tokenlist:readNexttokIfIsOneOfValue(tokvalues, typ)
if self.pos > #self then
return nil, nil
end
for _, tokvalue in ipairs(tokvalues) do
if self[self.pos][1] == tokvalue then
if typ and self[self.pos][2] == typ then
return self:read()
elseif typ and self[self.pos][2] ~= typ then
-- ignore
else
return self:read()
end
end
end
return nil, nil
end
function tokenlist:nextTokIsType(typ)
if self.pos > #self then return false end
local t = self:peek()
return t[2] == typ
end
---@return boolean true if the next token is the provided type.
function tokenlist:skipType(typ)
if self.pos > #self then return false end
local t = self:peek()
if t[2] == typ then
self:read()
return true
end
end
---@param str string
---@return tokenlist?
---@return string?
function M.string_to_tokenlist(str)
if str == nil then return {} end
local tokens = tokenlist:new()
local nextrune
local eof
local runes = split_chars(str)
while true do
local r
r, eof = read_rune(runes)
if eof then break end
if '0' <= r and r <= '9' then
unread_rune(runes)
local num
num = get_num(runes)
if num then
tokens[#tokens + 1] = { num, "tokNumber" }
end
elseif r == '.' then
nextrune, eof = read_rune(runes)
if eof then
tokens[#tokens + 1] = { '.', "tokOperator" }
break
end
if nextrune == "." then
tokens[#tokens + 1] = { '..', "tokOperator" }
elseif '0' <= nextrune and nextrune <= '9' then
unread_rune(runes)
unread_rune(runes)
local num
num = get_num(runes)
tokens[#tokens + 1] = { num, "tokNumber" }
else
unread_rune(runes)
tokens[#tokens + 1] = { '.', "tokOperator" }
end
elseif r == '+' or r == '-' or r == '*' or r == '?' or r == '@' or r == '|' or r == '=' then
tokens[#tokens + 1] = { r, "tokOperator" }
elseif r == "," then
tokens[#tokens + 1] = { r, "tokComma" }
elseif r == '>' or r == '<' then
nextrune, eof = read_rune(runes)
if eof then break end
if nextrune == '=' or nextrune == r then
tokens[#tokens + 1] = { r .. nextrune, "tokOperator" }
else
tokens[#tokens + 1] = { r, "tokOperator" }
unread_rune(runes)
end
elseif r == '!' then
nextrune, eof = read_rune(runes)
if eof then break end
if nextrune == '=' then
tokens[#tokens + 1] = { "!=", "tokOperator" }
else
return nil, string.format("= expected after !, got %s", nextrune)
end
elseif r == '/' or r == ':' then
nextrune, eof = read_rune(runes)
if eof then
tokens[#tokens + 1] = { r, "tokOperator" }
break
end
if nextrune == r then
tokens[#tokens + 1] = { r .. r, "tokOperator" }
else
tokens[#tokens + 1] = { r, "tokOperator" }
unread_rune(runes)
end
elseif r == '[' then
tokens[#tokens + 1] = { r, "tokOpenBracket" }
elseif r == ']' then
tokens[#tokens + 1] = { r, "tokCloseBracket" }
elseif r == '$' then
local name
name = get_qname(runes)
tokens[#tokens + 1] = { name, "tokVarname" }
elseif is_space(r) then
-- ignore whitespace
elseif is_letter(r) then
unread_rune(runes)
local name
name = get_qname(runes)
nextrune, eof = read_rune(runes)
if eof then
tokens[#tokens + 1] = { name, "tokQName" }
break
end
if nextrune == ':' then
tokens[#tokens + 1] = { string.sub(name, 1, -2), "tokDoubleColon" }
else
unread_rune(runes)
tokens[#tokens + 1] = { name, "tokQName" }
end
elseif r == '"' or r == "'" then
unread_rune(runes)
str = get_delimited_string(runes)
tokens[#tokens + 1] = { str, "tokString" }
elseif r == '(' then
nextrune, eof = read_rune(runes)
if eof then
return tokens, "parse error, unbalanced ( at end"
end
if nextrune == ':' then
get_comment(runes)
else
unread_rune(runes)
tokens[#tokens + 1] = { "(", "tokOpenParen" }
end
elseif r == ')' then
tokens[#tokens + 1] = { ")", "tokCloseParen" }
else
return nil, string.format("Invalid char for xpath expression %q", r)
end
end
return tokens
end
--------------------------
local function is_element(itm)
return type(itm) == "table" and itm[".__type"] == "element"
end
local function is_document(itm)
return type(itm) == "table" and itm[".__type"] == "document"
end
local function is_attribute(itm)
return type(itm) == "table" and itm[".__type"] == "attribute"
end
M.is_element = is_element
M.is_attribute = is_attribute
local string_value
local function number_value(sequence)
if type(sequence) == "string" then return tonumber(sequence) end
if is_attribute(sequence) then
return tonumber(sequence.value)
end
if type(sequence) == "number" then
return sequence
end
if not sequence then
return nil, "empty sequence"
end
if #sequence == 0 then
return nil, "empty sequence"
end
if #sequence > 1 then
return nil, "number value, # must be 1"
end
if is_attribute(sequence[1]) then
return tonumber(sequence[1].value)
end
return tonumber(string_value(sequence)), nil
end
local function boolean_value(seq)
if type(seq) == "boolean" then
return seq
end
if #seq == 0 then return false, nil end
if #seq > 1 then return false, "invalid argument for boolean value" end
local val = seq[1]
local ok = false
if type(val) == "string" then
ok = (val ~= "")
elseif type(val) == "number" then
ok = (val ~= 0 and val == val)
elseif type(val) == "boolean" then
ok = val
elseif is_element(val) then
return true
end
return ok, nil
end
function string_value(seq)
local ret = {}
if type(seq) == "string" then return seq end
if is_attribute(seq) then return seq.value end
for _, itm in ipairs(seq) do
if tonumber(itm) and itm ~= itm then
ret[#ret + 1] = 'NaN'
elseif is_element(itm) then
for _, cld in ipairs(itm) do
ret[#ret + 1] = string_value(cld)
end
elseif is_attribute(itm) then
ret[#ret + 1] = itm.value
elseif type(itm) == "table" then
ret[#ret + 1] = string_value(itm)
else
ret[#ret + 1] = tostring(itm)
end
end
return table.concat(ret)
end
M.string_value = string_value
M.boolean_value = boolean_value
M.number_value = number_value
local function docomparestring(op, left, right)
if op == "=" then
return left == right, nil
elseif op == "!=" then
return left ~= right, nil
elseif op == "<" then
return left < right, nil
elseif op == ">" then
return left > right, nil
elseif op == "<=" then
return left <= right, nil
elseif op == ">=" then
return left >= right, nil
else
return nil, "not implemented: op " .. op
end
end
local function docomparenumber(op, left, right)
if op == "=" then
return left == right, nil
elseif op == "!=" then
return left ~= right, nil
elseif op == "<" then
return left < right, nil
elseif op == ">" then
return left > right, nil
elseif op == "<=" then
return left <= right, nil
elseif op == ">=" then
return left >= right, nil
else
return nil, "not implemented: number comparison op " .. op
end
end
local function docomparefunc(op, leftitem, rightitem)
if is_attribute(leftitem) then leftitem = leftitem.value end
if is_attribute(rightitem) then rightitem = rightitem.value end
if type(leftitem) == "boolean" or type(rightitem) == "boolean" then
local x, errmsg = docomparestring(op, string_value({ leftitem }), string_value({ rightitem }))
return x, errmsg
elseif type(number_value(leftitem)) == "number" and type(number_value(rightitem)) == "number" then
local x, errmsg = docomparenumber(op, number_value(leftitem), number_value(rightitem))
return x, errmsg
elseif type(leftitem) == "string" or type(rightitem) == "string" then
local x, errmsg = docomparestring(op, string_value({ leftitem }), string_value({ rightitem }))
return x, errmsg
else
assert(false, "nyi")
end
end
local function docompare(op, lhs, rhs)
local evaler = function(ctx)
local left, right, errmsg, ok
left, errmsg = lhs(ctx)
if errmsg ~= nil then return nil, errmsg end
right, errmsg = rhs(ctx)
if errmsg ~= nil then return nil, errmsg end
for _, leftitem in ipairs(left) do
for _, rightitem in ipairs(right) do
ok, errmsg = docomparefunc(op, leftitem, rightitem)
if errmsg ~= nil then return nil, errmsg end
if ok then return { true }, nil end
end
end
return { false }, nil
end
return evaler, nil
end
local function patternescape(s)
return (s:gsub('%%', '%%%%')
:gsub('^%^', '%%^')
:gsub('%$$', '%%$')
:gsub('%(', '%%(')
:gsub('%)', '%%)')
:gsub('%.', '%%.')
:gsub('%[', '%%[')
:gsub('%]', '%%]')
:gsub('%*', '%%*')
:gsub('%+', '%%+')
:gsub('%-', '%%-')
:gsub('%?', '%%?'))
end
local function fnAbs(cts, seq)
local firstarg = seq[1]
local n, errmsg = number_value(firstarg)
if not n or errmsg then return nil, errmsg end
return { math.abs(n) }, nil
end
local function fnBoolean(cts, seq)
local firstarg = seq[1]
local tf, errmsg = boolean_value(firstarg)
if tf == nil or errmsg then return nil, errmsg end
return { tf }, nil
end
local function fnCeiling(cts, seq)
local n, errmsg = number_value(seq[1])
if errmsg then return errmsg end
if n == nil then return { nan }, nil end
return { math.ceil(n) }, nil
end
local function fnConcat(ctx, seq)
local ret = {}
for _, itm in ipairs(seq) do
ret[#ret + 1] = string_value(itm)
end
return { table.concat(ret) }
end
local function fnCodepointsToString(ctx, seq)
local firstarg = seq[1]
local ret = {}
for _, itm in ipairs(firstarg) do
local n, errmsg = number_value(itm)
if errmsg then
return nil, errmsg
end
ret[#ret + 1] = utf8.char(n)
end
return { table.concat(ret) }, nil
end
local function fnContains(ctx, seq)
local firstarg = string_value(seq[1])
local secondarg = string_value(seq[2])
local x = string.find(firstarg, secondarg, 1, true)
return { x ~= nil }, nil
end
local function fnCount(ctx, seq)
local firstarg = seq[1]
if not firstarg then return { 0 }, nil end
return { #firstarg }, nil
end
local function fnDoc(ctx, seq)
local firstarg = string_value(seq[1])
local fn = M.findfile(firstarg)
local xmltab = M.parse_xml(fn)
ctx.sequence = xmltab[1]
return {ctx.sequence}, nil
end
local function fnEmpty(ctx, seq)
return { #seq[1] == 0 }, nil
end
local function fnEndsWith(ctx, seq)
local firstarg = string_value(seq[1])
local secondarg = string_value(seq[2])
secondarg = patternescape(secondarg)
local m = M.stringmatch(firstarg, secondarg .. "$")
return { m ~= nil }, nil
end
local function fnFalse(ctx, seq)
return { false }, nil
end
local function fnFloor(ctx, seq)
local n, errmsg = number_value(seq[1])
if errmsg then return errmsg end
if n == nil then return { nan }, nil end
return { math.floor(n) }, nil
end
local function fnLast(ctx, seq)
return { ctx.size }, nil
end
local function fnLocalName(ctx, seq)
local input_seq = ctx.sequence
if #seq == 1 then
input_seq = seq[1]
end
-- first item
seq = input_seq
if #seq == 0 then
return { "" }, nil
end
if #seq > 1 then
return {}, "sequence too long"
end
-- first element
seq = seq[1]
if is_element(seq) then
return { seq[".__local_name"] }, nil
end
return { "" }, nil
end
-- Not unicode aware!
local function fnLowerCase(ctx, seq)
local firstarg = seq[1]
local x = string_value(firstarg)
return { string.lower(x) }, nil
end
local function fnMax(ctx, seq)
local firstarg = seq[1]
local x
for _, itm in ipairs(firstarg) do
if not x then
x = number_value({ itm })
else
local y = number_value({ itm })
if y > x then x = y end
end
end
return { x }, nil
end
local function fnMatches(ctx, seq)
local text = string_value(seq[1])
local re = string_value(seq[2])
if string.match(text, re) then
return { true }, nil
end
return { false }, nil
end
local function fnMin(ctx, seq)
local firstarg = seq[1]
local x
for _, itm in ipairs(firstarg) do
if not x then
x = number_value({ itm })
else
local y = number_value({ itm })
if y < x then x = y end
end
end
return { x }, nil
end
local function fnNormalizeSpace(ctx, seq)
local firstarg = seq[1]
local x = string_value(firstarg)
x = x:gsub("^%s+", "")
x = x:gsub("%s+$", "")
x = x:gsub("%s+", " ")
return { x }, nil
end
local function fnNot(ctx, seq)
local firstarg = seq[1]
local x, errmsg = boolean_value(firstarg)
if errmsg then
return {}, errmsg
end
return { not x }, nil
end
local function fnNumber(ctx, seq)
local x = number_value(seq[1])
if not x then return { nan }, nil end
return { x }, nil
end
local function fnPosition(ctx, seq)
return { ctx.pos }, nil
end
local function fnReverse(ctx, seq)
local firstarg = seq[1]
local ret = {}
for i = #firstarg, 1, -1 do
ret[#ret + 1] = firstarg[i]
end
return ret, nil
end
local function fnRoot(ctx, seq)
if #seq ~= 0 then
return nil, "not yet implmented: root(arg)"
end
if not ctx.xmldoc then
return nil, "no root found"
end
if not ctx.xmldoc[1] then
return nil, "no root found"
end
for i = 1, #ctx.xmldoc[1] do
local tab = ctx.xmldoc[1][i]
if is_element(tab) then
ctx.sequence = { tab }
return { tab }, nil
end
end
return nil, "no root found"
end
local function fnRound(ctx, seq)
local firstarg = seq[1]
if #firstarg == 0 then
return {}, nil
end
local n, errmsg = number_value(firstarg)
if errmsg then
return nil, errmsg
end
return { math.floor(n + 0.5) }, nil
end
local function fnString(ctx, seq)
local input_seq = ctx.sequence
if #seq == 1 then
input_seq = seq[1]
end
-- first item
seq = input_seq
local x = string_value(seq)
return { x }, nil
end
local function fnStartsWith(ctx, seq)
local firstarg = string_value(seq[1])
local secondarg = string_value(seq[2])
secondarg = patternescape(secondarg)
local m = M.stringmatch(firstarg, "^" .. secondarg)
return { m ~= nil }, nil
end
local function fnStringJoin(ctx, seq)
local firstarg = seq[1]
local secondarg = seq[2]
if #secondarg ~= 1 then
return nil, "string-join: second argument should be a string"
end
local tab = {}
for _, itm in ipairs(firstarg) do
local str = string_value(itm)
tab[#tab + 1] = str
end
return { table.concat(tab, string_value(secondarg[1])) }, nil
end
local function fnStringLength(ctx, seq)
local input_seq = ctx.sequence
if #seq == 1 then
input_seq = seq[1]
end
-- first item
seq = input_seq
local x = string_value(seq)
return { utf8.len(x) }, nil
end
local function fnStringToCodepoints(ctx, seq)
local str = string_value(seq[1])
local ret = {}
for _, c in utf8.codes(str) do
ret[#ret + 1] = c
end
return ret, nil
end
local function fnSubstring(ctx, seq)
local str = string_value(seq[1])
local pos, errmsg = number_value(seq[2])
if errmsg then
return nil, errmsg
end
local len = #str
if #seq > 2 then
len = number_value(seq[3])
end
local ret = {}
local l = 0
for i, c in utf8.codes(str) do
if i >= pos and l < len then
ret[#ret + 1] = utf8.char(c)
l = l + 1
end
end
return { table.concat(ret) }, nil
end
local function fnSubstringAfter(ctx, seq)
local firstarg = string_value(seq[1])
local secondarg = string_value(seq[2])
local a, b = M.stringfind(firstarg, secondarg, 1, true)
if not a then return { "" }, nil end
return { string.sub(firstarg, b + 1, -1) }
end
local function fnSubstringBefore(ctx, seq)
local firstarg = string_value(seq[1])
local secondarg = string_value(seq[2])
local a = M.stringfind(firstarg, secondarg, 1, true)
if not a then return { "" }, nil end
return { string.sub(firstarg, 1, a - 1) }
end
local function fnTrue(ctx, seq)
return { true }, nil
end
local function fnUnparsedText(ctx, seq)
local firstarg = string_value(seq[1])
local fn = M.findfile(firstarg)
local rd,msg = io.open(fn,"r")
if not rd then
return nil, msg
end
local txt = rd:read("a")
rd:close()
return {txt},nil
end
-- Not unicode aware!
local function fnUpperCase(ctx, seq)
local firstarg = seq[1]
local x = string_value(firstarg)
return { string.upper(x) }, nil
end
local funcs = {
-- function name, namespace, function, minarg, maxarg
{ "abs", M.fnNS, fnAbs, 1, 1 },
{ "boolean", M.fnNS, fnBoolean, 1, 1 },
{ "ceiling", M.fnNS, fnCeiling, 1, 1 },
{ "codepoints-to-string", M.fnNS, fnCodepointsToString, 1, 1 },
-- { "compare", M.fnNS, fnCompare, 2, 2 },
{ "concat", M.fnNS, fnConcat, 0, -1 },
{ "contains", M.fnNS, fnContains, 2, 2 },
{ "count", M.fnNS, fnCount, 1, 1 },
{ "doc", M.fnNS, fnDoc, 1, 1 },
{ "empty", M.fnNS, fnEmpty, 1, 1 },
{ "false", M.fnNS, fnFalse, 0, 0 },
{ "floor", M.fnNS, fnFloor, 1, 1 },
{ "last", M.fnNS, fnLast, 0, 0 },
{ "local-name", M.fnNS, fnLocalName, 0, 1 },
{ "lower-case", M.fnNS, fnLowerCase, 1, 1 },
{ "max", M.fnNS, fnMax, 1, 1 },
{ "matches", M.fnNS, fnMatches, 2, 3 },
{ "min", M.fnNS, fnMin, 1, 1 },
{ "normalize-space", M.fnNS, fnNormalizeSpace, 1, 1 },
{ "not", M.fnNS, fnNot, 1, 1 },
{ "number", M.fnNS, fnNumber, 1, 1 },
{ "position", M.fnNS, fnPosition, 0, 0 },
{ "reverse", M.fnNS, fnReverse, 1, 1 },
{ "root", M.fnNS, fnRoot, 0, 1 },
{ "round", M.fnNS, fnRound, 1, 1 },
{ "starts-with", M.fnNS, fnStartsWith, 2, 2 },
{ "ends-with", M.fnNS, fnEndsWith, 2, 2 },
{ "substring-after", M.fnNS, fnSubstringAfter, 2, 2 },
{ "substring-before", M.fnNS, fnSubstringBefore, 2, 2 },
{ "string-join", M.fnNS, fnStringJoin, 2, 2 },
{ "string-length", M.fnNS, fnStringLength, 0, 1 },
{ "string-to-codepoints", M.fnNS, fnStringToCodepoints, 1, 1 },
{ "string", M.fnNS, fnString, 0, 1 },
{ "substring", M.fnNS, fnSubstring, 2, 3 },
{ "true", M.fnNS, fnTrue, 0, 0 },
{ "unparsed-text", M.fnNS, fnUnparsedText, 1, 1 },
{ "upper-case", M.fnNS, fnUpperCase, 1, 1 },
}
local function registerFunction(func)
M.funcs[func[2] .. " " .. func[1]] = func
end
for _, func in ipairs(funcs) do
registerFunction(func)
end
M.registerFunction = registerFunction
local function getFunction(namespace, fname)
return M.funcs[namespace .. " " .. fname]
end
local function callFunction(fname, seq, ctx)
local fn = {}
for str in string.gmatch(fname, "([^:]+)") do
table.insert(fn, str)
end
local namespace = M.fnNS
if #fn == 2 then
namespace = ctx.namespaces[fn[1]]
fname = fn[2]
end
local func = getFunction(namespace, fname)
if not func then return {}, string.format("cannot find function with name %s",fname) end
local minarg, maxarg = func[4], func[5]
if #seq < minarg or (maxarg ~= -1 and #seq > maxarg) then
if minarg == maxarg then
return {}, string.format("function %s requires %d arguments, %d supplied", func[1], minarg, #seq)
else
return {}, string.format("function %s requires %d to %d arguments, %d supplied", func[1], minarg, maxarg,
#seq)
end
end
if func then
return func[3](ctx, seq)
end
return {}, "Could not find function " .. fname .. " with name space " .. namespace
end
local function filter(ctx, f)
local res = {}
local errmsg, predicate
local copysequence = ctx.sequence
local positions
local lengths
if ctx.positions then
positions = ctx.positions
lengths = ctx.lengths
else
positions = {}
lengths = {}
for i = 1, #ctx.sequence do
positions[#positions + 1] = i
lengths[#lengths + 1] = 1
end
end
for i, itm in ipairs(copysequence) do
ctx.sequence = { itm }