-
Notifications
You must be signed in to change notification settings - Fork 387
/
protoc.lua
1193 lines (1098 loc) · 37.5 KB
/
protoc.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 string = string
local tonumber = tonumber
local setmetatable = setmetatable
local error = error
local ipairs = ipairs
local io = io
local table = table
local math = math
local assert = assert
local tostring = tostring
local type = type
local insert_tab = table.insert
local str_gmatch = string.gmatch
local function meta(name, t)
t = t or {}
t.__name = name
t.__index = t
return t
end
local function default(t, k, def)
local v = t[k]
if not v then
v = def or {}
t[k] = v
end
return v
end
local Lexer = meta "Lexer" do
local escape = {
a = "\a", b = "\b", f = "\f", n = "\n",
r = "\r", t = "\t", v = "\v"
}
local function tohex(x) return string.byte(tonumber(x, 16)) end
local function todec(x) return string.byte(tonumber(x, 10)) end
local function toesc(x) return escape[x] or x end
function Lexer.new(name, src)
local self = {
name = name,
src = src,
pos = 1
}
return setmetatable(self, Lexer)
end
function Lexer:__call(patt, pos)
return self.src:match(patt, pos or self.pos)
end
function Lexer:test(patt)
self:whitespace()
local pos = self('^'..patt..'%s*()')
if not pos then return false end
self.pos = pos
return true
end
function Lexer:expected(patt, name)
if not self:test(patt) then
return self:error((name or ("'"..patt.."'")).." expected")
end
return self
end
function Lexer:pos2loc(pos)
local linenr = 1
pos = pos or self.pos
for start, stop in self.src:gmatch "()[^\n]*()\n?" do
if start <= pos and pos <= stop then
return linenr, pos - start + 1
end
linenr = linenr + 1
end
end
function Lexer:error(fmt, ...)
local ln, co = self:pos2loc()
return error(("%s:%d:%d: "..fmt):format(self.name, ln, co, ...))
end
function Lexer:opterror(opt, msg)
if not opt then return self:error(msg) end
return nil
end
function Lexer:whitespace()
local pos, c = self "^%s*()(%/?)"
self.pos = pos
if c == '' then return self end
return self:comment()
end
function Lexer:comment()
local pos = self "^%/%/[^\n]*\n?()"
if not pos then
if self "^%/%*" then
pos = self "^%/%*.-%*%/()"
if not pos then
self:error "unfinished comment"
end
end
end
if not pos then return self end
self.pos = pos
return self:whitespace()
end
function Lexer:line_end(opt)
self:whitespace()
local pos = self '^[%s;]*%s*()'
if not pos then
return self:opterror(opt, "';' expected")
end
self.pos = pos
return pos
end
function Lexer:eof()
self:whitespace()
return self.pos > #self.src
end
function Lexer:keyword(kw, opt)
self:whitespace()
local ident, pos = self "^([%a_][%w_]*)%s*()"
if not ident or ident ~= kw then
return self:opterror(opt, "''"..kw..'" expected')
end
self.pos = pos
return kw
end
function Lexer:ident(name, opt)
self:whitespace()
local b, ident, pos = self "^()([%a_][%w_]*)%s*()"
if not ident then
return self:opterror(opt, (name or 'name')..' expected')
end
self.pos = pos
return ident, b
end
function Lexer:full_ident(name, opt)
self:whitespace()
local b, ident, pos = self "^()([%a_.][%w_.]*)%s*()"
if not ident or ident:match "%.%.+" then
return self:opterror(opt, (name or 'name')..' expected')
end
self.pos = pos
return ident, b
end
function Lexer:integer(opt)
self:whitespace()
local ns, oct, hex, s, pos =
self "^([+-]?)(0?)([xX]?)([0-9a-fA-F]+)%s*()"
local n
if oct == '0' and hex == '' then
n = tonumber(s, 8)
elseif oct == '' and hex == '' then
n = tonumber(s, 10)
elseif oct == '0' and hex ~= '' then
n = tonumber(s, 16)
end
if not n then
return self:opterror(opt, 'integer expected')
end
self.pos = pos
return ns == '-' and -n or n
end
function Lexer:number(opt)
self:whitespace()
if self:test "nan%f[%A]" then
return 0.0/0.0
elseif self:test "inf%f[%A]" then
return 1.0/0.0
end
local ns, d1, s, d2, s2, pos = self "^([+-]?)(%.?)([0-9]+)(%.?)([0-9]*)()"
if not ns then
return self:opterror(opt, 'floating-point number expected')
end
local es, pos2 = self("(^[eE][+-]?[0-9]+)%s*()", pos)
if d1 == "." and d2 == "." then
return self:error "malformed floating-point number"
end
self.pos = pos2 or pos
local n = tonumber(d1..s..d2..s2..(es or ""))
return ns == '-' and -n or n
end
function Lexer:quote(opt)
self:whitespace()
local q, start = self '^(["\'])()'
if not start then
return self:opterror(opt, 'string expected')
end
self.pos = start
local patt = '()(\\?'..q..')%s*()'
while true do
local stop, s, pos = self(patt)
if not stop then
self.pos = start-1
return self:error "unfinished string"
end
self.pos = pos
if s == q then
return self.src:sub(start, stop-1)
:gsub("\\x(%x+)", tohex)
:gsub("\\(%d+)", todec)
:gsub("\\(.)", toesc)
end
end
end
function Lexer:structure(opt)
self:whitespace()
if not self:test "{" then
return self:opterror(opt, 'opening curly brace expected')
end
local t = {}
while not self:test "}" do
local pos, name, npos = self "^%s*()(%b[])()"
if not pos then
name = self:full_ident "field name"
else
self.pos = npos
end
self:test ":"
local value = self:constant()
self:test ","
self:line_end "opt"
t[name] = value
end
return t
end
function Lexer:array(opt)
self:whitespace()
if not self:test "%[" then
return self:opterror(opt, 'opening square bracket expected')
end
local t = {}
while not self:test "]" do
local value = self:constant()
self:test ","
t[#t + 1] = value
end
return t
end
function Lexer:constant(opt)
local c = self:full_ident('constant', 'opt')
if c == "true" then return true end
if c == "false" then return false end
if c == "none" then return nil end
if c then return c end
c = self:number('opt') or
self:quote('opt') or
self:structure('opt') or
self:array('opt')
if c == nil and not opt then
return self:error "constant expected"
end
return c
end
function Lexer:option_name()
local ident
if self:test "%(" then
ident = self:full_ident "option name"
self:expected "%)"
else
ident = self:ident "option name"
end
while self:test "%." do
ident = ident .. "." .. self:ident()
end
return ident
end
function Lexer:type_name()
if self:test "%." then
local id, pos = self:full_ident "type name"
return "."..id, pos
else
return self:full_ident "type name"
end
end
end
local Parser = meta "Parser" do
Parser.typemap = {}
Parser.loaded = {}
Parser.paths = { "", "." }
function Parser.new()
local self = {}
self.typemap = {}
self.loaded = {}
self.paths = { "", "." }
return setmetatable(self, Parser)
end
function Parser:reset()
self.typemap = {}
self.loaded = {}
return self
end
function Parser:error(msg)
return self.lex:error(msg)
end
function Parser:addpath(path)
insert_tab(self.paths, path)
end
function Parser:parsefile(name)
local info = self.loaded[name]
if info then return info end
local errors = {}
for _, path in ipairs(self.paths) do
local fn = path ~= "" and path.."/"..name or name
local fh, err = io.open(fn)
if fh then
local content = fh:read "*a"
info = self:parse(content, name)
fh:close()
return info
end
insert_tab(errors, err or fn..": ".."unknown error")
end
local import_fallback = self.unknown_import
if import_fallback == true then
info = import_fallback
elseif import_fallback then
info = import_fallback(self, name)
end
if not info then
error("module load error: "..name.."\n\t"..table.concat(errors, "\n\t"))
end
return info
end
-- parser
local labels = { optional = 1; required = 2; repeated = 3 }
local key_types = {
int32 = 5; int64 = 3; uint32 = 13;
uint64 = 4; sint32 = 17; sint64 = 18;
fixed32 = 7; fixed64 = 6; sfixed32 = 15;
sfixed64 = 16; bool = 8; string = 9;
}
local com_types = {
group = 10; message = 11; enum = 14;
}
local types = {
double = 1; float = 2; int32 = 5;
int64 = 3; uint32 = 13; uint64 = 4;
sint32 = 17; sint64 = 18; fixed32 = 7;
fixed64 = 6; sfixed32 = 15; sfixed64 = 16;
bool = 8; string = 9; bytes = 12;
group = 10; message = 11; enum = 14;
}
local function register_type(self, lex, tname, typ)
if not tname:match "%."then
tname = self.prefix..tname
end
if self.typemap[tname] then
return lex:error("type %s already defined", tname)
end
self.typemap[tname] = typ
end
local function type_info(lex, tname)
local tenum = types[tname]
if com_types[tname] then
return lex:error("invalid type name: "..tname)
elseif tenum then
tname = nil
end
return tenum, tname
end
local function map_info(lex)
local keyt = lex:ident "key type"
if not key_types[keyt] then
return lex:error("invalid key type: "..keyt)
end
local valt = lex:expected "," :type_name()
local name = lex:expected ">" :ident()
local ident = name:gsub("^%a", string.upper)
:gsub("_(%a)", string.upper).."Entry"
local kt, ktn = type_info(lex, keyt)
local vt, vtn = type_info(lex, valt)
return name, types.message, ident, {
name = ident,
field = {
{
name = "key",
number = 1;
label = labels.optional,
type = kt,
type_name = ktn
},
{
name = "value",
number = 2;
label = labels.optional,
type = vt,
type_name = vtn
},
},
options = { map_entry = true }
}
end
local function inline_option(lex, info)
if lex:test "%[" then
info = info or {}
while true do
local name = lex:option_name()
local value = lex:expected '=' :constant()
info[name] = value
if lex:test "%]" then
return info
end
lex:expected ','
end
end
end
local function field(self, lex, ident)
local name, typ, type_name, map_entry
if ident == "map" and lex:test "%<" then
name, typ, type_name, map_entry = map_info(lex)
self.locmap[map_entry.field[1]] = lex.pos
self.locmap[map_entry.field[2]] = lex.pos
register_type(self, lex, type_name, types.message)
else
typ, type_name = type_info(lex, ident)
name = lex:ident()
end
local info = {
name = name,
number = lex:expected "=":integer(),
label = ident == "map" and labels.repeated or labels.optional,
type = typ,
type_name = type_name
}
local options = inline_option(lex)
if options then
info.default_value, options.default = tostring(options.default), nil
info.json_name, options.json_name = options.json_name, nil
info.options = options
end
if info.number <= 0 then
lex:error("invalid tag number: "..info.number)
end
return info, map_entry
end
local function label_field(self, lex, ident, parent)
local label = labels[ident]
local info, map_entry
if not label then
if self.syntax == "proto2" and ident ~= "map" then
return lex:error("proto2 disallow missing label")
end
return field(self, lex, ident)
end
local proto3_optional = label == labels.optional and self.syntax == "proto3"
if proto3_optional and not (self.proto3_optional and parent) then
return lex:error("proto3 disallow 'optional' label")
end
info, map_entry = field(self, lex, lex:type_name())
if proto3_optional then
local ot = default(parent, "oneof_decl")
info.oneof_index = #ot
ot[#ot+1] = { name = "_" .. info.name }
else
info.label = label
end
return info, map_entry
end
local toplevel = {} do
function toplevel:package(lex, info)
local package = lex:full_ident 'package name'
lex:line_end()
info.package = package
self.prefix = "."..package.."."
return self
end
function toplevel:import(lex, info)
local mode = lex:ident('"weak" or "public"', 'opt') or "public"
if mode ~= 'weak' and mode ~= 'public' then
return lex:error '"weak or "public" expected'
end
local name = lex:quote()
lex:line_end()
local result = self:parsefile(name)
if self.on_import then
self.on_import(result)
end
local dep = default(info, 'dependency')
local index = #dep
dep[index+1] = name
if mode == "public" then
local it = default(info, 'public_dependency')
insert_tab(it, index)
else
local it = default(info, 'weak_dependency')
insert_tab(it, index)
end
end
local msgbody = {} do
function msgbody:message(lex, info)
local nested_type = default(info, 'nested_type')
insert_tab(nested_type, toplevel.message(self, lex))
return self
end
function msgbody:enum(lex, info)
local nested_type = default(info, 'enum_type')
insert_tab(nested_type, toplevel.enum(self, lex))
return self
end
function msgbody:extend(lex, info)
local extension = default(info, 'extension')
local nested_type = default(info, 'nested_type')
local ft, mt = toplevel.extend(self, lex, {})
for _, v in ipairs(ft) do
insert_tab(extension, v)
end
for _, v in ipairs(mt) do
insert_tab(nested_type, v)
end
return self
end
function msgbody:extensions(lex, info)
local rt = default(info, 'extension_range')
local idx = #rt
repeat
local start = lex:integer "field number range"
local stop = math.floor(2^29)
if lex:keyword('to', 'opt') then
if not lex:keyword('max', 'opt') then
stop = lex:integer "field number range end or 'max'"
end
insert_tab(rt, { start = start, ['end'] = stop })
else
insert_tab(rt, { start = start, ['end'] = start })
end
until not lex:test ','
rt[idx+1].options = inline_option(lex)
lex:line_end()
return self
end
function msgbody:reserved(lex, info)
lex:whitespace()
if not lex '^%d' then
local rt = default(info, 'reserved_name')
repeat
insert_tab(rt, (lex:quote()))
until not lex:test ','
else
local rt = default(info, 'reserved_range')
local first = true
repeat
local start = lex:integer(first and 'field name or number range'
or 'field number range')
if lex:keyword('to', 'opt') then
if lex:keyword('max', 'opt') then
insert_tab(rt, { start = start, ['end'] = 2^29-1 })
else
local stop = lex:integer 'field number range end'
insert_tab(rt, { start = start, ['end'] = stop })
end
else
insert_tab(rt, { start = start, ['end'] = start })
end
first = false
until not lex:test ','
end
lex:line_end()
return self
end
function msgbody:oneof(lex, info)
local fs = default(info, "field")
local ts = default(info, "nested_type")
local ot = default(info, "oneof_decl")
local index = #ot + 1
local oneof = { name = lex:ident() }
lex:expected "{"
while not lex:test "}" do
local ident = lex:type_name()
if ident == "option" then
toplevel.option(self, lex, oneof)
else
local f, t = field(self, lex, ident)
self.locmap[f] = lex.pos
if t then insert_tab(ts, t) end
f.oneof_index = index - 1
insert_tab(fs, f)
end
lex:line_end 'opt'
end
ot[index] = oneof
end
function msgbody:option(lex, info)
toplevel.option(self, lex, info)
end
end
function toplevel:message(lex, info)
local name = lex:ident 'message name'
local typ = { name = name }
register_type(self, lex, name, types.message)
local prefix = self.prefix
self.prefix = prefix..name.."."
lex:expected "{"
while not lex:test "}" do
local ident, pos = lex:type_name()
local body_parser = msgbody[ident]
if body_parser then
body_parser(self, lex, typ)
else
local fs = default(typ, 'field')
local f, t = label_field(self, lex, ident, typ)
self.locmap[f] = pos
insert_tab(fs, f)
if t then
local ts = default(typ, 'nested_type')
insert_tab(ts, t)
end
end
lex:line_end 'opt'
end
lex:line_end 'opt'
if info then
info = default(info, 'message_type')
insert_tab(info, typ)
end
self.prefix = prefix
return typ
end
function toplevel:enum(lex, info)
local name, pos = lex:ident 'enum name'
local enum = { name = name }
self.locmap[enum] = pos
register_type(self, lex, name, types.enum)
lex:expected "{"
while not lex:test "}" do
local ident, pos = lex:ident 'enum constant name'
if ident == 'option' then
toplevel.option(self, lex, enum)
elseif ident == 'reserved' then
msgbody.reserved(self, lex, enum)
else
local values = default(enum, 'value')
local number = lex:expected '=' :integer()
local value = {
name = ident,
number = number,
options = inline_option(lex)
}
self.locmap[value] = pos
insert_tab(values, value)
end
lex:line_end 'opt'
end
lex:line_end 'opt'
if info then
info = default(info, 'enum_type')
insert_tab(info, enum)
end
return enum
end
function toplevel:option(lex, info)
local ident = lex:option_name()
lex:expected "="
local value = lex:constant()
lex:line_end()
local options = info and default(info, 'options') or {}
options[ident] = value
return options, self
end
function toplevel:extend(lex, info)
local name = lex:type_name()
local ft = info and default(info, 'extension') or {}
local mt = info and default(info, 'message_type') or {}
lex:expected "{"
while not lex:test "}" do
local ident, pos = lex:type_name()
local f, t = label_field(self, lex, ident)
self.locmap[f] = pos
f.extendee = name
insert_tab(ft, f)
insert_tab(mt, t)
lex:line_end 'opt'
end
return ft, mt
end
local svr_body = {} do
function svr_body:rpc(lex, info)
local name, pos = lex:ident "rpc name"
local rpc = { name = name }
self.locmap[rpc] = pos
local _, tn
lex:expected "%("
rpc.client_streaming = lex:keyword("stream", "opt")
_, tn = type_info(lex, lex:type_name())
if not tn then return lex:error "rpc input type must by message" end
rpc.input_type = tn
lex:expected "%)" :expected "returns" :expected "%("
rpc.server_streaming = lex:keyword("stream", "opt")
_, tn = type_info(lex, lex:type_name())
if not tn then return lex:error "rpc output type must by message" end
rpc.output_type = tn
lex:expected "%)"
if lex:test "{" then
while not lex:test "}" do
lex:line_end "opt"
lex:keyword "option"
toplevel.option(self, lex, rpc)
end
end
lex:line_end "opt"
local t = default(info, "method")
insert_tab(t, rpc)
end
function svr_body:option(lex, info)
return toplevel.option(self, lex, info)
end
function svr_body.stream(_, lex)
lex:error "stream not implement yet"
end
end
function toplevel:service(lex, info)
local name, pos = lex:ident 'service name'
local svr = { name = name }
self.locmap[svr] = pos
lex:expected "{"
while not lex:test "}" do
local ident = lex:type_name()
local body_parser = svr_body[ident]
if body_parser then
body_parser(self, lex, svr)
else
return lex:error "expected 'rpc' or 'option' in service body"
end
lex:line_end 'opt'
end
lex:line_end 'opt'
if info then
info = default(info, 'service')
insert_tab(info, svr)
end
return svr
end
end
local function make_context(self, lex)
local ctx = {
syntax = "proto2";
locmap = {};
prefix = ".";
lex = lex;
}
ctx.loaded = self.loaded
ctx.typemap = self.typemap
ctx.paths = self.paths
ctx.proto3_optional =
self.proto3_optional or self.experimental_allow_proto3_optional
ctx.unknown_type = self.unknown_type
ctx.unknown_import = self.unknown_import
ctx.on_import = self.on_import
return setmetatable(ctx, Parser)
end
function Parser:parse(src, name)
local loaded = self.loaded[name]
if loaded then
if loaded == true then
error("loop loaded: "..name)
end
return loaded
end
name = name or "<input>"
self.loaded[name] = true
local lex = Lexer.new(name, src)
local ctx = make_context(self, lex)
local info = { name = lex.name, syntax = ctx.syntax }
local syntax = lex:keyword('syntax', 'opt')
if syntax then
info.syntax = lex:expected '=' :quote()
ctx.syntax = info.syntax
lex:line_end()
end
while not lex:eof() do
local ident = lex:ident()
local top_parser = toplevel[ident]
if top_parser then
top_parser(ctx, lex, info)
else
lex:error("unknown keyword '"..ident.."'")
end
lex:line_end "opt"
end
self.loaded[name] = name ~= "<input>" and info or nil
return ctx:resolve(lex, info)
end
-- resolver
local function empty() end
local function iter(t, k)
local v = t[k]
if v then return ipairs(v) end
return empty
end
local function check_dup(self, lex, typ, map, k, v)
local old = map[v[k]]
if old then
local ln, co = lex:pos2loc(self.locmap[old])
lex:error("%s '%s' exists, previous at %d:%d",
typ, v[k], ln, co)
end
map[v[k]] = v
end
local function check_type(self, lex, tname)
if tname:match "^%." then
local t = self.typemap[tname]
if not t then
return lex:error("unknown type '%s'", tname)
end
return t, tname
end
local prefix = self.prefix
for i = #prefix+1, 1, -1 do
local op = prefix[i]
prefix[i] = tname
local tn = table.concat(prefix, ".", 1, i)
prefix[i] = op
local t = self.typemap[tn]
if t then return t, tn end
end
local tn, t
local type_fallback = self.unknown_type
if type_fallback then
if type_fallback == true then
tn = true
elseif type(type_fallback) == 'string' then
tn = tname:match(type_fallback) and true
else
tn = type_fallback(self, tname)
end
end
if tn then
t = types[t or "message"]
if tn == true then tn = "."..tname end
return t, tn
end
return lex:error("unknown type '%s'", tname)
end
local function check_field(self, lex, info)
if info.extendee then
local t, tn = check_type(self, lex, info.extendee)
if t ~= types.message then
lex:error("message type expected in extension")
end
info.extendee = tn
end
if info.type_name then
local t, tn = check_type(self, lex, info.type_name)
info.type = t
info.type_name = tn
end
end
local function check_enum(self, lex, info)
local names, numbers = {}, {}
for _, v in iter(info, 'value') do
lex.pos = assert(self.locmap[v])
check_dup(self, lex, 'enum name', names, 'name', v)
if not (info.options and info.options.allow_alias) then
check_dup(self, lex, 'enum number', numbers, 'number', v)
end
end
end
local function check_message(self, lex, info)
insert_tab(self.prefix, info.name)
local names, numbers = {}, {}
for _, v in iter(info, 'field') do
lex.pos = assert(self.locmap[v])
check_dup(self, lex, 'field name', names, 'name', v)
check_dup(self, lex, 'field number', numbers, 'number', v)
check_field(self, lex, v)
end
for _, v in iter(info, 'nested_type') do
check_message(self, lex, v)
end
for _, v in iter(info, 'extension') do
lex.pos = assert(self.locmap[v])
check_field(self, lex, v)
end
self.prefix[#self.prefix] = nil
end
local function check_service(self, lex, info)
local names = {}
for _, v in iter(info, 'method') do
lex.pos = self.locmap[v]
check_dup(self, lex, 'rpc name', names, 'name', v)
local t, tn = check_type(self, lex, v.input_type)
v.input_type = tn
if t ~= types.message then
lex:error "message type expected in parameter"
end
t, tn = check_type(self, lex, v.output_type)
v.output_type = tn
if t ~= types.message then
lex:error "message type expected in return"
end
end
end
function Parser:resolve(lex, info)
self.prefix = { "" }
for token in str_gmatch(info.package or "", "[^.]+") do
insert_tab(self.prefix, token)
end
for _, v in iter(info, 'message_type') do
check_message(self, lex, v)
end
for _, v in iter(info, 'enum_type') do
check_enum(self, lex, v)
end
for _, v in iter(info, 'service') do
check_service(self, lex, v)
end
for _, v in iter(info, 'extension') do
lex.pos = assert(self.locmap[v])
check_field(self, lex, v)
end
self.prefix = nil
return info
end
end
local has_pb, pb = pcall(require, "pb") do
if has_pb then
local descriptor_pb =
"\10\179;\10\16descriptor.proto\18\15google.protobuf\"M\10\17FileDescrip"..
"torSet\0188\10\4file\24\1 \3(\0112$.google.protobuf.FileDescriptorProto"..
"R\4file\"\228\4\10\19FileDescriptorProto\18\18\10\4name\24\1 \1(\9R\4na"..
"me\18\24\10\7package\24\2 \1(\9R\7package\18\30\10\10dependency\24\3 \3"..
"(\9R\10dependency\18+\10\17public_dependency\24\10 \3(\5R\16publicDepen"..