forked from snabbco/snabb
/
data.lua
1859 lines (1776 loc) · 62.3 KB
/
data.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
-- Use of this source code is governed by the Apache 2.0 license; see
-- COPYING.
module(..., package.seeall)
local mem = require("lib.stream.mem")
local parser_mod = require("lib.yang.parser")
local schema = require("lib.yang.schema")
local util = require("lib.yang.util")
local value = require("lib.yang.value")
local ffi = require("ffi")
local ctable = require('lib.ctable')
local cltable = require('lib.cltable')
local lib = require('core.lib')
function normalize_id(id)
return id:gsub('[^%w_]', '_')
end
-- Helper for parsing C type declarations.
local function parse_type(str, start, is_member)
local function err(msg, pos)
io.stderr:write('ERROR: While parsing type:\n')
io.stderr:write('ERROR: '..str..'\n')
io.stderr:write('ERROR: '..string.rep(' ', pos - 1)..'^\n')
io.stderr:write('ERROR: '..msg..'\n')
error(msg, 2)
end
local function assert_match(str, pat, pos, what)
local ret = { str:match(pat, pos) }
if not ret[1] then err('bad '..what, pos) end
return unpack(ret)
end
local t, array, member, pos
-- See if it's a struct.
t, pos = str:match('^%s*(struct%s*%b{})%s*()', start)
-- Otherwise it might be a scalar.
if not t then t, pos = str:match('^%s*([%a_][%w_]*)%s*()', start) end
-- We don't do unions currently.
if not t then err('invalid type', start) end
-- If we're parsing a struct or union member, get the name.
if is_member then
member, pos = assert_match(str, '^([%a_][%w_]*)%s*()', pos, 'member name')
end
-- Parse off the array suffix, if any.
if str:match('^%[', pos) then
array, pos = assert_match(str, '^(%b[])%s*()', pos, 'array component')
end
if is_member then
-- Members should have a trailing semicolon.
pos = assert_match(str, '^;%s*()', pos, 'semicolon')
else
-- Nonmembers should parse to the end of the string.
assert_match(str, '^()$', pos, 'suffix')
end
return t, array, member, pos
end
-- We want structural typing, not nominal typing, for Yang data. The
-- "foo" member in "struct { struct { uint16 a; } foo; }" should not
-- have a unique type; we want to be able to instantiate a "struct {
-- uint16 a; }" and get a compatible value. To do this, we parse out
-- nested "struct" types and only ever make one FFI type for each
-- compatible struct kind. The user-facing interface is the "typeof"
-- function below; the "compile_type" helper handles nesting.
--
-- It would be possible to avoid this complexity by having the grammar
-- generate something other than a string "ctype" representation, but
-- then we don't have a type name to serialize into binary data. We
-- might as well embrace the type strings.
local function compile_type(name)
local function maybe_array_type(t, array)
-- If ARRAY is something like "[10]", make a corresponding type.
-- Otherwise just return T.
if array then return ffi.typeof('$'..array, t) end
return t
end
local parsed, array = parse_type(name, 1, false)
local ret
if parsed:match('^struct[%s{]') then
-- It's a struct type; parse out the members and rebuild.
local struct_type = 'struct { '
local struct_type_args = {}
local function add_member(member_type, member_name)
struct_type = struct_type..'$ '..member_name..'; '
table.insert(struct_type_args, member_type)
end
-- Loop from initial "struct {" to final "}".
local pos = assert(parsed:match('^struct%s*{%s*()'))
while not parsed:match('^}$', pos) do
local mtype, mname, marray
mtype, marray, mname, pos = parse_type(parsed, pos, true)
-- Recurse on mtype by calling the caching "typeof" defined
-- below.
add_member(maybe_array_type(typeof(mtype), marray), mname)
end
struct_type = struct_type..'}'
ret = ffi.typeof(struct_type, unpack(struct_type_args))
else
-- Otherwise the type is already structural and we can just use
-- ffi.typeof.
ret = ffi.typeof(parsed)
end
return maybe_array_type(ret, array)
end
local type_cache = {}
function typeof(name)
assert(type(name) == 'string')
if not type_cache[name] then type_cache[name] = compile_type(name) end
return type_cache[name]
end
-- If a "list" node has one key that is string-valued, we will represent
-- instances of that node as normal Lua tables where the key is the
-- table key and the value does not contain the key.
local function table_string_key(keys)
local string_key = nil
for k,v in pairs(keys) do
if v.type ~= 'scalar' then return nil end
if v.argument_type.primitive_type ~= 'string' then return nil end
if string_key ~= nil then return nil end
string_key = k
end
return string_key
end
-- We need to properly support unions. It's a big FIXME! As an
-- intermediate step, we pick the first type in the union. Terrible.
local function elide_unions(t)
while t.primitive_type == 'union' do t = t.union[1] end
return t
end
function data_grammar_from_schema(schema, is_config)
local function is_empty(tab)
for k,v in pairs(tab) do return false end
return true
end
local function struct_ctype(members)
local member_names = {}
for k,v in pairs(members) do
if not v.ctype then return nil end
table.insert(member_names, k)
end
table.sort(member_names)
local ctype = 'struct { '
for _,k in ipairs(member_names) do
-- Separate the array suffix off of things like "uint8_t[4]".
local head, tail = members[k].ctype:match('^([^%[]*)(.*)$')
ctype = ctype..head..' '..normalize_id(k)..tail..'; '
end
ctype = ctype..'}'
return ctype
end
local function value_ctype(type)
-- Note that not all primitive types have ctypes.
return assert(value.types[assert(type.primitive_type)]).ctype
end
local handlers = {}
local function visit(node)
local handler = handlers[node.kind]
if handler then return handler(node) end
end
local function visit_body(node)
local ret = {}
local norm = {}
for k, child in pairs(node.body) do
local out = visit(child)
if out then
ret[k] = out
local id = normalize_id(k)
assert(not norm[id], 'duplicate data identifier: '..id)
norm[id] = k
end
end
return ret
end
function handlers.container(node)
local members = visit_body(node)
if is_empty(members) then return end
return {type='struct', members=members, ctype=struct_ctype(members)}
end
function handlers.choice(node)
local choices = {}
for choice, n in pairs(node.body) do
local members = visit_body(n)
if not is_empty(members) then choices[choice] = members end
end
if is_empty(choices) then return end
return {type="choice", default=node.default, mandatory=node.mandatory,
choices=choices}
end
handlers['leaf-list'] = function(node)
if node.config ~= is_config then return end
local t = elide_unions(node.type)
return {type='array', element_type=t, ctype=value_ctype(t)}
end
function handlers.list(node)
local norm = {}
local keys, values = {}, {}
if node.key then
for k in node.key:split(' +') do
local leaf = node.body[k]
assert(leaf, 'missing key leaf: '..k)
assert(leaf.kind == 'leaf', 'key not a leaf: '..k)
assert(not keys[k], 'duplicate key: '..k)
keys[k] = assert(handlers.leaf(leaf, true))
local id = normalize_id(k)
assert(not norm[id], 'duplicate data identifier: '..id)
norm[id] = k
end
end
for k,node in pairs(node.body) do
if not keys[k] then
values[k] = visit(node)
local id = normalize_id(k)
assert(not norm[id], 'duplicate data identifier: '..id)
norm[id] = k
end
end
if is_empty(values) and node.config ~= is_config then return end
return {type='table', keys=keys, values=values,
string_key=table_string_key(keys),
key_ctype=struct_ctype(keys),
value_ctype=struct_ctype(values)}
end
function handlers.leaf(node, for_key)
if node.config ~= is_config and not for_key then return end
local ctype
local t = elide_unions(node.type)
if node.default or node.mandatory then ctype=value_ctype(t) end
return {type='scalar', argument_type=t,
default=node.default, mandatory=node.mandatory,
is_unique = node.is_unique, ctype=ctype}
end
local members = visit_body(schema)
return {type="struct", members=members, ctype=struct_ctype(members)}
end
data_grammar_from_schema = util.memoize(data_grammar_from_schema)
function config_grammar_from_schema(schema)
return data_grammar_from_schema(schema, true)
end
config_grammar_from_schema = util.memoize(config_grammar_from_schema)
function state_grammar_from_schema(schema)
return data_grammar_from_schema(schema, false)
end
state_grammar_from_schema = util.memoize(state_grammar_from_schema)
function rpc_grammar_from_schema(schema)
local grammar = {}
for _,prop in ipairs({'input', 'output'}) do
grammar[prop] = { type="sequence", members={} }
for k,rpc in pairs(schema.rpcs) do
local node = rpc[prop]
if node then
-- Hack to mark RPC is-config as being true
grammar[prop].members[k] = data_grammar_from_schema(node)
else
grammar[prop].members[k] = {type="struct", members={}}
end
end
end
return grammar
end
function rpc_input_grammar_from_schema(schema)
return rpc_grammar_from_schema(schema).input
end
function rpc_output_grammar_from_schema(schema)
return rpc_grammar_from_schema(schema).output
end
local function range_predicate(range, val)
return function(val)
for _,part in ipairs(range) do
local l, r = unpack(part)
if (l == 'min' or l <= val) and (r == 'max' or val <= r) then
return true
end
end
return false
end
end
local function range_validator(range, f)
if not range then return f end
local is_in_range = range_predicate(range.value)
return function(val)
if is_in_range(val) then return f(val) end
error('value '..val..' is out of the valid range')
end
end
local function length_validator(length, f)
if not length then return f end
local is_in_range = range_predicate(length.value)
return function(val)
if is_in_range(string.length(val)) then return f(val) end
error('length of string '..val..' is out of the valid range')
end
end
local function pattern_validator(pattern, f)
-- FIXME: Implement me!
return f
end
local function bit_validator(range, f)
-- FIXME: Implement me!
return f
end
local function enum_validator(enums, f)
if not enums then return f end
return function (val)
if not enums[val] then
error('enumeration '..val..' is not a valid value')
end
return f(val)
end
end
local function identityref_validator(bases, default_prefix, f)
if not default_prefix then return f end
return function(val)
if not val:match(':') then val = default_prefix..":"..val end
local identity = schema.lookup_identity(val)
for _, base in ipairs(bases) do
if not schema.identity_is_instance_of(identity, base) then
error('identity '..val..' not an instance of '..base)
end
end
return val
end
end
function value_parser(typ)
local prim = typ.primitive_type
local parse = assert(value.types[prim], prim).parse
local validate = function(val) return val end
local function enums (node)
return node.primitive_type == 'enumeration' and node.enums or nil
end
validate = range_validator(typ.range, validate)
validate = length_validator(typ.length, validate)
validate = pattern_validator(typ.pattern, validate)
validate = bit_validator(typ.bit, validate)
validate = enum_validator(enums(typ), validate)
validate = identityref_validator(typ.bases, typ.default_prefix, validate)
-- TODO: union, require-instance.
return function(str, k)
return validate(parse(str, k))
end
end
local function struct_parser(keyword, members, ctype)
local keys = {}
for k,v in pairs(members) do table.insert(keys, k) end
local function init() return nil end
local function parse1(P)
local ret = {}
local expanded_members = {}
for _,k in ipairs(keys) do
if members[k].represents then
-- Choice fields don't include the name of the choice block in the data. They
-- need to be able to provide the parser for the leaves it represents.
local member_parser = members[k].stateful_parser()
for _, node in pairs(members[k].represents()) do
-- Choice fields need to keep state around as they're called multiple times
-- and need to do some validation to comply with spec.
expanded_members[node] = member_parser
end
else
ret[normalize_id(k)] = members[k].init()
expanded_members[k] = members[k]
end
end
P:skip_whitespace()
P:consume("{")
P:skip_whitespace()
while not P:check("}") do
local k = P:parse_identifier()
if k == '' then P:error("Expected a keyword") end
-- Scalar/array parser responsible for requiring whitespace
-- after keyword. Struct/table don't need it as they have
-- braces.
local sub = assert(expanded_members[k], 'unrecognized parameter: '..k)
local id = normalize_id(k)
ret[id] = sub.parse(P, ret[id], k)
P:skip_whitespace()
end
for k,_ in pairs(expanded_members) do
local id = normalize_id(k)
ret[id] = expanded_members[k].finish(ret[id], k)
end
return ret
end
local function parse(P, out)
if out ~= nil then P:error('duplicate parameter: '..keyword) end
return parse1(P)
end
local struct_t = ctype and typeof(ctype)
local function finish(out, leaf)
-- FIXME check mandatory values.
if struct_t then
local ret
if out == nil then ret = struct_t()
else ret = struct_t(out) end
return ret
else
return out
end
end
return {init=init, parse=parse, finish=finish}
end
local function array_parser(keyword, element_type, ctype)
local function init() return {} end
local parsev = value_parser(element_type)
local function parse1(P)
P:consume_whitespace()
local str = P:parse_string()
P:skip_whitespace()
P:consume(";")
return parsev(str, keyword)
end
local function parse(P, out)
table.insert(out, parse1(P))
return out
end
local elt_t = ctype and typeof(ctype)
local array_t = ctype and ffi.typeof('$[?]', elt_t)
local function finish(out)
-- FIXME check min-elements
if array_t then
out = util.ffi_array(array_t(#out, out), elt_t)
end
return out
end
return {init=init, parse=parse, finish=finish}
end
local function scalar_parser(keyword, argument_type, default, mandatory)
local function init() return nil end
local parsev = value_parser(argument_type)
local function parse1(P)
local maybe_str
if argument_type.primitive_type ~= 'empty' then
P:consume_whitespace()
maybe_str = P:parse_string()
end
P:skip_whitespace()
P:consume(";")
return parsev(maybe_str, keyword)
end
local function parse(P, out)
if out ~= nil then P:error('duplicate parameter: '..keyword) end
return parse1(P)
end
local function finish(out)
if out ~= nil then return out end
if default then return parsev(default, keyword) end
if mandatory then error('missing scalar value: '..keyword) end
end
return {init=init, parse=parse, finish=finish}
end
function choice_parser(keyword, choices, members, default, mandatory)
-- Create a table matching the leaf names to the case statement
local choice_map = {}
for case, choice in pairs(choices) do
for leaf in pairs(choice) do
choice_map[leaf] = case
end
end
local function stateful_parser()
-- This holds the value of the chosen case block so we're able to prevent mixing of
-- using different leaves from different case statements.
local chosen
local function init() return {} end
local function parse(P, out, k)
if chosen and choice_map[k] ~= chosen then
error("Only one choice set can exist at one time: "..keyword)
else
chosen = choice_map[k]
end
return members[chosen][k].parse(P, members[chosen][k].init(), k)
end
-- This holds a copy of all the nodes so we know when we've hit the last one.
local function finish(out, k)
if out ~= nil then return out end
if mandatory and chosen == nil then error("missing choice value: "..keyword) end
if default and default == choice_map[k] then
return members[default][k].finish()
end
end
return {init=init, parse=parse, finish=finish}
end
local function represents()
local nodes = {}
for name, _ in pairs(choice_map) do table.insert(nodes, name) end
return nodes
end
return {represents=represents, stateful_parser=stateful_parser}
end
local function ctable_builder(key_t, value_t)
local res = ctable.new({ key_type=key_t, value_type=value_t,
max_occupancy_rate = 0.4 })
local builder = {}
-- Uncomment for progress counters.
-- local counter = 0
function builder:add(key, value)
-- counter = counter + 1
-- if counter % 1000 == 0 then print('ctable add', counter) end
res:add(key, value)
end
function builder:finish() return res end
return builder
end
local function string_keyed_table_builder(string_key)
local res = {}
local builder = {}
function builder:add(key, value)
local str = assert(key[string_key])
assert(res[str] == nil, 'duplicate key: '..str)
res[str] = value
end
function builder:finish() return res end
return builder
end
local function cltable_builder(key_t)
local res = cltable.new({ key_type=key_t })
local builder = {}
function builder:add(key, value)
assert(res[key] == nil, 'duplicate key')
res[key] = value
end
function builder:finish() return res end
return builder
end
local function ltable_builder()
local res = {}
local builder = {}
function builder:add(key, value) res[key] = value end
function builder:finish() return res end
return builder
end
local function table_parser(keyword, keys, values, string_key, key_ctype,
value_ctype)
local members = {}
for k,v in pairs(keys) do members[k] = v end
for k,v in pairs(values) do members[k] = v end
local parser = struct_parser(keyword, members)
local key_t = key_ctype and typeof(key_ctype)
local value_t = value_ctype and typeof(value_ctype)
local init
if key_t and value_t then
function init() return ctable_builder(key_t, value_t) end
elseif string_key then
function init() return string_keyed_table_builder(string_key) end
elseif key_t then
function init() return cltable_builder(key_t) end
else
function init() return ltable_builder() end
end
local function parse1(P)
return parser.finish(parser.parse(P, parser.init()))
end
local function parse(P, assoc)
local struct = parse1(P)
local key, value = {}, {}
if key_t then key = key_t() end
if value_t then value = value_t() end
for k,_ in pairs(keys) do
local id = normalize_id(k)
key[id] = struct[id]
end
for k, v in pairs(struct) do
local id = normalize_id(k)
if keys[k] == nil then
value[id] = struct[id]
end
end
assoc:add(key, value)
return assoc
end
local function finish(assoc)
return assoc:finish()
end
return {init=init, parse=parse, finish=finish}
end
function data_parser_from_grammar(production)
local handlers = {}
local function visit1(keyword, production)
return assert(handlers[production.type])(keyword, production)
end
local function visitn(productions)
local ret = {}
for keyword,production in pairs(productions) do
ret[keyword] = visit1(keyword, production)
end
return ret
end
function handlers.struct(keyword, production)
local members = visitn(production.members)
return struct_parser(keyword, members, production.ctype)
end
function handlers.array(keyword, production)
return array_parser(keyword, production.element_type, production.ctype)
end
function handlers.table(keyword, production)
local keys, values = visitn(production.keys), visitn(production.values)
return table_parser(keyword, keys, values, production.string_key,
production.key_ctype, production.value_ctype)
end
function handlers.scalar(keyword, production)
return scalar_parser(keyword, production.argument_type,
production.default, production.mandatory)
end
function handlers.choice(keyword, production)
local members = {}
for case, choice in pairs(production.choices) do members[case] = visitn(choice) end
return choice_parser(keyword, production.choices, members,
production.default, production.mandatory)
end
local top_parsers = {}
function top_parsers.struct(production)
local struct_t = production.ctype and typeof(production.ctype)
local members = visitn(production.members)
return function(stream)
local P = parser_mod.Parser.new(stream)
local ret = {}
for k,sub in pairs(members) do ret[normalize_id(k)] = sub.init() end
while true do
P:skip_whitespace()
if P:is_eof() then break end
local k = P:parse_identifier()
if k == '' then P:error("Expected a keyword") end
local sub = assert(members[k], 'unrecognized parameter: '..k)
local id = normalize_id(k)
ret[id] = sub.parse(P, ret[id], k)
end
for k,sub in pairs(members) do
local id = normalize_id(k)
ret[id] = sub.finish(ret[id])
end
if struct_t then return struct_t(ret) else return ret end
end
end
function top_parsers.sequence(production)
local members = visitn(production.members)
return function(stream)
local P = parser_mod.Parser.new(stream)
local ret = {}
while true do
P:skip_whitespace()
if P:is_eof() then break end
local k = P:parse_identifier()
P:consume_whitespace()
local sub = assert(members[k], 'unrecognized rpc: '..k)
local data = sub.finish(sub.parse(P, sub.init(), k))
table.insert(ret, {id=k, data=data})
end
return ret
end
end
function top_parsers.array(production)
local parser = visit1('[bare array]', production)
return function(stream)
local P = parser_mod.Parser.new(stream)
local out = parser.init()
while true do
P:skip_whitespace()
if P:is_eof() then break end
out = parser.parse(P, out)
end
return parser.finish(out)
end
end
function top_parsers.table(production)
local parser = visit1('[bare table]', production)
return function(stream)
local P = parser_mod.Parser.new(stream)
local out = parser.init()
while true do
P:skip_whitespace()
if P:is_eof() then break end
out = parser.parse(P, out)
end
return parser.finish(out)
end
end
function top_parsers.scalar(production)
local parse = value_parser(production.argument_type)
return function(stream)
local P = parser_mod.Parser.new(stream)
P:skip_whitespace()
local str = P:parse_string()
P:skip_whitespace()
if not P:is_eof() then P:error("Not end of file") end
return parse(str, '[bare scalar]')
end
end
return assert(top_parsers[production.type])(production)
end
data_parser_from_grammar = util.memoize(data_parser_from_grammar)
function data_parser_from_schema(schema, is_config)
local grammar = data_grammar_from_schema(schema, is_config)
return data_parser_from_grammar(grammar)
end
function config_parser_from_schema(schema)
return data_parser_from_schema(schema, true)
end
function state_parser_from_schema(schema)
return data_parser_from_schema(schema, false)
end
function load_data_for_schema(schema, stream, is_config)
return data_parser_from_schema(schema, is_config)(stream)
end
function load_config_for_schema(schema, stream)
return load_data_for_schema(schema, stream, true)
end
function load_state_for_schema(schema, stream)
return load_data_for_schema(schema, stream, false)
end
function load_data_for_schema_by_name(schema_name, stream, is_config)
local schema = schema.load_schema_by_name(schema_name)
return load_data_for_schema(schema, stream, is_config)
end
function load_config_for_schema_by_name(schema_name, stream)
return load_data_for_schema_by_name(schema_name, stream, true)
end
function load_state_for_schema_by_name(schema_name, stream)
return load_data_for_schema_by_name(schema_name, stream, false)
end
function rpc_input_parser_from_schema(schema)
return data_parser_from_grammar(rpc_input_grammar_from_schema(schema))
end
function rpc_output_parser_from_schema(schema)
return data_parser_from_grammar(rpc_output_grammar_from_schema(schema))
end
local value_serializers = {}
local function value_serializer(typ)
local prim = typ.primitive_type
if value_serializers[prim] then return value_serializers[prim] end
local tostring = assert(value.types[prim], prim).tostring
value_serializers[prim] = tostring
return tostring
end
local function print_yang_string(str, file)
if #str == 0 then
file:write("''")
elseif str:match("^[^%s;{}\"'/]*$") then
file:write(str)
else
file:write('"')
for i=1,#str do
local chr = str:sub(i,i)
if chr == '\n' then
file:write('\\n')
elseif chr == '\t' then
file:write('\\t')
elseif chr == '"' or chr == '\\' then
file:write('\\')
file:write(chr)
else
file:write(chr)
end
end
file:write('"')
end
end
function xpath_printer_from_grammar(production, print_default, root)
if not root then root = '' end
if #root == 1 and root:sub(1, 1) == '/' then
root = ''
end
local handlers = {}
local translators = {}
local function printer(keyword, production, printers)
return assert(handlers[production.type])(keyword, production, printers)
end
local function print_keyword(k, file, path)
path = path:sub(1, 1) ~= '[' and root..'/'..path or root..path
file:write(path)
print_yang_string(k, file)
file:write(' ')
end
local function body_printer(productions, order)
-- Iterate over productions trying to translate to other statements. This
-- is used for example in choice statements raising the lower statements
-- in case blocks up to the level of the choice, in place of the choice.
local translated = {}
for keyword,production in pairs(productions) do
local translator = translators[production.type]
if translator ~= nil then
local statements = translator(keyword, production)
for k,v in pairs(statements) do translated[k] = v end
else
translated[keyword] = production
end
end
productions = translated
if not order then
order = {}
for k,_ in pairs(productions) do table.insert(order, k) end
table.sort(order)
end
local printers = {}
for keyword,production in pairs(productions) do
local printer = printer(keyword, production, printers)
if printer ~= nil then
printers[keyword] = printer
end
end
return function(data, file, indent)
for _,k in ipairs(order) do
local v = data[normalize_id(k)]
if v ~= nil then printers[k](v, file, indent) end
end
end
end
local function key_composer (productions, order)
local printer = body_printer(productions, order)
local file = {t={}}
function file:write (str)
str = str:match("([^%s]+)")
if str and #str > 0 and str ~= ";" and str ~= root..'/' then
table.insert(self.t, str)
end
end
function file:flush ()
local ret = {}
for i=1,#self.t,2 do
local key, value = self.t[i], self.t[i+1]
table.insert(ret, '['..key.."="..value..']')
end
self.t = {}
return table.concat(ret, '')
end
return function (data, path)
path = path or ''
printer(data, file, path)
return file:flush()
end
end
function translators.choice(keyword, production)
local rtn = {}
for case, body in pairs(production.choices) do
for name, statement in pairs(body) do
rtn[name] = statement
end
end
return rtn
end
function handlers.struct(keyword, production)
local print_body = body_printer(production.members)
return function(data, file, path)
print_body(data, file, path..keyword..'/')
end
end
function handlers.array(keyword, production)
local serialize = value_serializer(production.element_type)
return function(data, file, indent)
local count = 1
for _,v in ipairs(data) do
print_keyword(keyword.."[position()="..count.."]", file, '')
print_yang_string(serialize(v), file)
file:write('\n')
count = count + 1
end
end
end
-- As a special case, the table handler allows the keyword to be nil,
-- for printing tables at the top level without keywords.
function handlers.table(keyword, production)
local key_order, value_order = {}, {}
for k,_ in pairs(production.keys) do table.insert(key_order, k) end
for k,_ in pairs(production.values) do table.insert(value_order, k) end
table.sort(key_order)
table.sort(value_order)
local compose_key = key_composer(production.keys, key_order)
local print_value = body_printer(production.values, value_order)
if production.key_ctype and production.value_ctype then
return function(data, file, path)
path = path or ''
for entry in data:iterate() do
local key = compose_key(entry.key)
local path = path..(keyword or '')..key..'/'
print_value(entry.value, file, path)
end
end
elseif production.string_key then
local id = normalize_id(production.string_key)
return function(data, file, path)
path = path or ''
for key, value in pairs(data) do
local key = compose_key({[id]=key})
local path = path..(keyword or '')..key..'/'
print_value(value, file, path)
end
end
elseif production.key_ctype then
return function(data, file, path)
path = path or ''
for key, value in cltable.pairs(data) do
local key = compose_key(key)
local path = path..(keyword or '')..key..'/'
print_value(value, file, path)
end
end
else
return function(data, file, path)
path = path or ''
for key, value in pairs(data) do
local key = compose_key(key)
local path = path..(keyword or '')..key..'/'
print_value(value, file, path)
end
end
end
end
function handlers.scalar(keyword, production)
local serialize = value_serializer(production.argument_type)
return function(data, file, path)
local str = serialize(data)
if print_default or str ~= production.default then
print_keyword(keyword, file, path)
print_yang_string(str, file)
file:write('\n')
end
end
end
local top_printers = {}
function top_printers.struct(production)
local printer = body_printer(production.members)
return function(data, file)
printer(data, file, '')
return file:flush()
end
end
function top_printers.sequence(production)
local printers = {}
for k,v in pairs(production.members) do
printers[k] = printer(k, v)
end
return function(data, file)
for _,elt in ipairs(data) do
local id = assert(elt.id)
assert(printers[id])(elt.data, file, '')
end
return file:flush()
end
end
function top_printers.table(production)
local printer = handlers.table(nil, production)
return function(data, file)
printer(data, file, '')
return file:flush()
end
end
function top_printers.array(production)
local serialize = value_serializer(production.element_type)
return function(data, file, indent)
local count = 1
for _,v in ipairs(data) do
file:write(root.."[position()="..count.."]")
file:write(' ')
print_yang_string(serialize(v), file)
file:write('\n')
count = count + 1
end
return file:flush()
end
end
function top_printers.scalar(production)
local serialize = value_serializer(production.argument_type)