protoc.lua 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192
  1. module(...,package.seeall)
  2. local string = string
  3. local tonumber = tonumber
  4. local setmetatable = setmetatable
  5. local error = error
  6. local ipairs = ipairs
  7. local io = io
  8. local table = table
  9. local math = math
  10. local assert = assert
  11. local tostring = tostring
  12. local type = type
  13. local insert_tab = table.insert
  14. local function meta(name, t)
  15. t = t or {}
  16. t.__name = name
  17. t.__index = t
  18. return t
  19. end
  20. local function default(t, k, def)
  21. local v = t[k]
  22. if not v then
  23. v = def or {}
  24. t[k] = v
  25. end
  26. return v
  27. end
  28. local Lexer = meta "Lexer" do
  29. local escape = {
  30. a = "\a", b = "\b", f = "\f", n = "\n",
  31. r = "\r", t = "\t", v = "\v"
  32. }
  33. local function tohex(x) return string.byte(tonumber(x, 16)) end
  34. local function todec(x) return string.byte(tonumber(x, 10)) end
  35. local function toesc(x) return escape[x] or x end
  36. function Lexer.new(name, src)
  37. local self = {
  38. name = name,
  39. src = src,
  40. pos = 1
  41. }
  42. return setmetatable(self, Lexer)
  43. end
  44. function Lexer:__call(patt, pos)
  45. return self.src:match(patt, pos or self.pos)
  46. end
  47. function Lexer:test(patt)
  48. self:whitespace()
  49. local pos = self('^'..patt..'%s*()')
  50. if not pos then return false end
  51. self.pos = pos
  52. return true
  53. end
  54. function Lexer:expected(patt, name)
  55. if not self:test(patt) then
  56. return self:error((name or "'"..patt.."'").." expected")
  57. end
  58. return self
  59. end
  60. function Lexer:pos2loc(pos)
  61. local linenr = 1
  62. pos = pos or self.pos
  63. for start, stop in self.src:gmatch "()[^\n]*()\n?" do
  64. if start <= pos and pos <= stop then
  65. return linenr, pos - start + 1
  66. end
  67. linenr = linenr + 1
  68. end
  69. end
  70. function Lexer:error(fmt, ...)
  71. local ln, co = self:pos2loc()
  72. return error(("%s:%d:%d: "..fmt):format(self.name, ln, co, ...))
  73. end
  74. function Lexer:opterror(opt, msg)
  75. if not opt then return self:error(msg) end
  76. return nil
  77. end
  78. function Lexer:whitespace()
  79. local pos, c = self "^%s*()(%/?)"
  80. self.pos = pos
  81. if c == '' then return self end
  82. return self:comment()
  83. end
  84. function Lexer:comment()
  85. local pos = self "^%/%/[^\n]*\n?()"
  86. if not pos then
  87. if self "^%/%*" then
  88. pos = self "^%/%*.-%*%/()"
  89. if not pos then
  90. self:error "unfinished comment"
  91. end
  92. end
  93. end
  94. if not pos then return self end
  95. self.pos = pos
  96. return self:whitespace()
  97. end
  98. function Lexer:line_end(opt)
  99. self:whitespace()
  100. local pos = self '^[%s;]*%s*()'
  101. if not pos then
  102. return self:opterror(opt, "';' expected")
  103. end
  104. self.pos = pos
  105. return pos
  106. end
  107. function Lexer:eof()
  108. self:whitespace()
  109. return self.pos > #self.src
  110. end
  111. function Lexer:keyword(kw, opt)
  112. self:whitespace()
  113. local ident, pos = self "^([%a_][%w_]*)%s*()"
  114. if not ident or ident ~= kw then
  115. return self:opterror(opt, "''"..kw..'" expected')
  116. end
  117. self.pos = pos
  118. return kw
  119. end
  120. function Lexer:ident(name, opt)
  121. self:whitespace()
  122. local b, ident, pos = self "^()([%a_][%w_]*)%s*()"
  123. if not ident then
  124. return self:opterror(opt, (name or 'name')..' expected')
  125. end
  126. self.pos = pos
  127. return ident, b
  128. end
  129. function Lexer:full_ident(name, opt)
  130. self:whitespace()
  131. local b, ident, pos = self "^()([%a_][%w_.]*)%s*()"
  132. if not ident or ident:match "%.%.+" then
  133. return self:opterror(opt, (name or 'name')..' expected')
  134. end
  135. self.pos = pos
  136. return ident, b
  137. end
  138. function Lexer:integer(opt)
  139. self:whitespace()
  140. local ns, oct, hex, s, pos =
  141. self "^([+-]?)(0?)([xX]?)([0-9a-fA-F]+)%s*()"
  142. local n
  143. if oct == '0' and hex == '' then
  144. n = tonumber(s, 8)
  145. elseif oct == '' and hex == '' then
  146. n = tonumber(s, 10)
  147. elseif oct == '0' and hex ~= '' then
  148. n = tonumber(s, 16)
  149. end
  150. if not n then
  151. return self:opterror(opt, 'integer expected')
  152. end
  153. self.pos = pos
  154. return ns == '-' and -n or n
  155. end
  156. function Lexer:number(opt)
  157. self:whitespace()
  158. if self:test "nan%f[%A]" then
  159. return 0.0/0.0
  160. elseif self:test "inf%f[%A]" then
  161. return 1.0/0.0
  162. end
  163. local ns, d1, s, d2, s2, pos = self "^([+-]?)(%.?)([0-9]+)(%.?)([0-9]*)()"
  164. if not ns then
  165. return self:opterror(opt, 'floating-point number expected')
  166. end
  167. local es, pos2 = self("(^[eE][+-]?[0-9]+)%s*()", pos)
  168. if d1 == "." and d2 == "." then
  169. return self:error "malformed floating-point number"
  170. end
  171. self.pos = pos2 or pos
  172. local n = tonumber(d1..s..d2..s2..(es or ""))
  173. return ns == '-' and -n or n
  174. end
  175. function Lexer:quote(opt)
  176. self:whitespace()
  177. local q, start = self '^(["\'])()'
  178. if not start then
  179. return self:opterror(opt, 'string expected')
  180. end
  181. self.pos = start
  182. local patt = '()(\\?'..q..')%s*()'
  183. while true do
  184. local stop, s, pos = self(patt)
  185. if not stop then
  186. self.pos = start-1
  187. return self:error "unfinished string"
  188. end
  189. self.pos = pos
  190. if s == q then
  191. return self.src:sub(start, stop-1)
  192. :gsub("\\x(%x+)", tohex)
  193. :gsub("\\(%d+)", todec)
  194. :gsub("\\(.)", toesc)
  195. end
  196. end
  197. end
  198. function Lexer:structure(opt)
  199. self:whitespace()
  200. if not self:test "{" then
  201. return self:opterror(opt, 'opening curly brace expected')
  202. end
  203. local t = {}
  204. while not self:test "}" do
  205. local ident = self:full_ident "field name" -- TODO: full_ident?
  206. self:test ":"
  207. local value = self:constant()
  208. self:test ","
  209. self:line_end "opt"
  210. t[ident] = value
  211. end
  212. return t
  213. end
  214. function Lexer:array(opt)
  215. self:whitespace()
  216. if not self:test "%[" then
  217. return self:opterror(opt, 'opening square bracket expected')
  218. end
  219. local t = {}
  220. while not self:test "]" do
  221. local value = self:constant()
  222. self:test ","
  223. t[#t + 1] = value
  224. end
  225. return t
  226. end
  227. function Lexer:constant(opt)
  228. local c = self:full_ident('constant', 'opt') or
  229. self:number('opt') or
  230. self:quote('opt') or
  231. self:structure('opt') or
  232. self:array('opt')
  233. if not c and not opt then
  234. return self:error "constant expected"
  235. end
  236. return c
  237. end
  238. function Lexer:option_name()
  239. local ident
  240. if self:test "%(" then
  241. ident = self:full_ident "option name"
  242. self:expected "%)"
  243. else
  244. ident = self:ident "option name"
  245. end
  246. while self:test "%." do
  247. ident = ident .. "." .. self:ident()
  248. end
  249. return ident
  250. end
  251. function Lexer:type_name()
  252. if self:test "%." then
  253. local id, pos = self:full_ident "type name"
  254. return "."..id, pos
  255. else
  256. return self:full_ident "type name"
  257. end
  258. end
  259. end
  260. local Parser = meta "Parser" do
  261. Parser.typemap = {}
  262. Parser.loaded = {}
  263. Parser.paths = { "", "." }
  264. function Parser.new()
  265. local self = {}
  266. self.typemap = {}
  267. self.loaded = {}
  268. self.paths = { "", "." }
  269. return setmetatable(self, Parser)
  270. end
  271. function Parser:reset()
  272. self.typemap = {}
  273. self.loaded = {}
  274. return self
  275. end
  276. function Parser:error(msg)
  277. return self.lex:error(msg)
  278. end
  279. function Parser:addpath(path)
  280. insert_tab(self.paths, path)
  281. end
  282. function Parser:parsefile(name)
  283. local info = self.loaded[name]
  284. if info then return info end
  285. local errors = {}
  286. for _, path in ipairs(self.paths) do
  287. local fn = path ~= "" and path.."/"..name or name
  288. local fh, err = io.open(fn)
  289. if fh then
  290. local content = fh:read "*a"
  291. info = self:parse(content, name)
  292. fh:close()
  293. return info
  294. end
  295. insert_tab(errors, err or fn..": ".."unknown error")
  296. end
  297. if self.import_fallback then
  298. info = self.import_fallback(name)
  299. end
  300. if not info then
  301. error("module load error: "..name.."\n\t"..table.concat(errors, "\n\t"))
  302. end
  303. return info
  304. end
  305. -- parser
  306. local labels = { optional = 1; required = 2; repeated = 3 }
  307. local key_types = {
  308. int32 = 5; int64 = 3; uint32 = 13;
  309. uint64 = 4; sint32 = 17; sint64 = 18;
  310. fixed32 = 7; fixed64 = 6; sfixed32 = 15;
  311. sfixed64 = 16; bool = 8; string = 9;
  312. }
  313. local com_types = {
  314. group = 10; message = 11; enum = 14;
  315. }
  316. local types = {
  317. double = 1; float = 2; int32 = 5;
  318. int64 = 3; uint32 = 13; uint64 = 4;
  319. sint32 = 17; sint64 = 18; fixed32 = 7;
  320. fixed64 = 6; sfixed32 = 15; sfixed64 = 16;
  321. bool = 8; string = 9; bytes = 12;
  322. group = 10; message = 11; enum = 14;
  323. }
  324. local function register_type(self, lex, tname, typ)
  325. if not tname:match "%."then
  326. tname = self.prefix..tname
  327. end
  328. if self.typemap[tname] then
  329. return lex:error("type %s already defined", tname)
  330. end
  331. self.typemap[tname] = typ
  332. end
  333. local function type_info(lex, tname)
  334. local tenum = types[tname]
  335. if com_types[tname] then
  336. return lex:error("invalid type name: "..tname)
  337. elseif tenum then
  338. tname = nil
  339. end
  340. return tenum, tname
  341. end
  342. local function map_info(lex)
  343. local keyt = lex:ident "key type"
  344. if not key_types[keyt] then
  345. return lex:error("invalid key type: "..keyt)
  346. end
  347. local valt = lex:expected "," :type_name()
  348. local name = lex:expected ">" :ident()
  349. local ident = name:gsub("^%a", string.upper)
  350. :gsub("_(%a)", string.upper).."Entry"
  351. local kt, ktn = type_info(lex, keyt)
  352. local vt, vtn = type_info(lex, valt)
  353. return name, types.message, ident, {
  354. name = ident,
  355. field = {
  356. {
  357. name = "key",
  358. number = 1;
  359. label = labels.optional,
  360. type = kt,
  361. type_name = ktn
  362. },
  363. {
  364. name = "value",
  365. number = 2;
  366. label = labels.optional,
  367. type = vt,
  368. type_name = vtn
  369. },
  370. },
  371. options = { map_entry = true }
  372. }
  373. end
  374. local function inline_option(lex, info)
  375. if lex:test "%[" then
  376. info = info or {}
  377. while true do
  378. local name = lex:option_name()
  379. local value = lex:expected '=' :constant()
  380. info[name] = value
  381. if lex:test "%]" then
  382. return info
  383. end
  384. lex:expected ','
  385. end
  386. end
  387. end
  388. local function field(self, lex, ident)
  389. local name, typ, type_name, map_entry
  390. if ident == "map" and lex:test "%<" then
  391. name, typ, type_name, map_entry = map_info(lex)
  392. self.locmap[map_entry.field[1]] = lex.pos
  393. self.locmap[map_entry.field[2]] = lex.pos
  394. register_type(self, lex, type_name, types.message)
  395. else
  396. typ, type_name = type_info(lex, ident)
  397. name = lex:ident()
  398. end
  399. local info = {
  400. name = name,
  401. number = lex:expected "=":integer(),
  402. label = ident == "map" and labels.repeated or labels.optional,
  403. type = typ,
  404. type_name = type_name
  405. }
  406. local options = inline_option(lex)
  407. if options then
  408. info.default_value, options.default = tostring(options.default), nil
  409. info.json_name, options.json_name = options.json_name, nil
  410. if options.packed and options.packed == "false" then
  411. options.packed = false
  412. end
  413. end
  414. info.options = options
  415. if info.number <= 0 then
  416. lex:error("invalid tag number: "..info.number)
  417. end
  418. return info, map_entry
  419. end
  420. local function label_field(self, lex, ident, parent)
  421. local label = labels[ident]
  422. local info, map_entry
  423. if not label then
  424. if self.syntax == "proto2" and ident ~= "map" then
  425. return lex:error("proto2 disallow missing label")
  426. end
  427. return field(self, lex, ident)
  428. end
  429. local proto3_optional = label == labels.optional and self.syntax == "proto3"
  430. if proto3_optional and not (self.proto3_optional and parent) then
  431. return lex:error("proto3 disallow 'optional' label")
  432. end
  433. info, map_entry = field(self, lex, lex:type_name())
  434. if proto3_optional then
  435. local ot = default(parent, "oneof_decl")
  436. info.oneof_index = #ot
  437. ot[#ot+1] = { name = "optional_" .. info.name }
  438. else
  439. info.label = label
  440. end
  441. return info, map_entry
  442. end
  443. local toplevel = {} do
  444. function toplevel:package(lex, info)
  445. local package = lex:full_ident 'package name'
  446. lex:line_end()
  447. info.package = package
  448. self.prefix = "."..package.."."
  449. return self
  450. end
  451. function toplevel:import(lex, info)
  452. local mode = lex:ident('"weak" or "public"', 'opt') or "public"
  453. if mode ~= 'weak' and mode ~= 'public' then
  454. return lex:error '"weak or "public" expected'
  455. end
  456. local name = lex:quote()
  457. lex:line_end()
  458. local result = self:parsefile(name)
  459. if self.on_import then
  460. self.on_import(result)
  461. end
  462. local dep = default(info, 'dependency')
  463. local index = #dep
  464. dep[index+1] = name
  465. if mode == "public" then
  466. local it = default(info, 'public_dependency')
  467. insert_tab(it, index)
  468. else
  469. local it = default(info, 'weak_dependency')
  470. insert_tab(it, index)
  471. end
  472. end
  473. local msg_body = {} do
  474. function msg_body:message(lex, info)
  475. local nested_type = default(info, 'nested_type')
  476. insert_tab(nested_type, toplevel.message(self, lex))
  477. return self
  478. end
  479. function msg_body:enum(lex, info)
  480. local nested_type = default(info, 'enum_type')
  481. insert_tab(nested_type, toplevel.enum(self, lex))
  482. return self
  483. end
  484. function msg_body:extend(lex, info)
  485. local extension = default(info, 'extension')
  486. local nested_type = default(info, 'nested_type')
  487. local ft, mt = toplevel.extend(self, lex, {})
  488. for _, v in ipairs(ft) do
  489. insert_tab(extension, v)
  490. end
  491. for _, v in ipairs(mt) do
  492. insert_tab(nested_type, v)
  493. end
  494. return self
  495. end
  496. function msg_body:extensions(lex, info)
  497. local rt = default(info, 'extension_range')
  498. repeat
  499. local start = lex:integer "field number range"
  500. local stop = math.floor(2^29)
  501. lex:keyword 'to'
  502. if not lex:keyword('max', 'opt') then
  503. stop = lex:integer "field number range end or 'max'"
  504. end
  505. insert_tab(rt, { start = start, ['end'] = stop })
  506. until not lex:test ','
  507. lex:line_end()
  508. return self
  509. end
  510. function msg_body:reserved(lex, info)
  511. lex:whitespace()
  512. if not lex '^%d' then
  513. local rt = default(info, 'reserved_name')
  514. repeat
  515. insert_tab(rt, (lex:quote()))
  516. until not lex:test ','
  517. else
  518. local rt = default(info, 'reserved_range')
  519. local first = true
  520. repeat
  521. local start = lex:integer(first and 'field name or number range'
  522. or 'field number range')
  523. if lex:keyword('to', 'opt') then
  524. if lex:keyword('max', 'opt') then
  525. insert_tab(rt, { start = start, ['end'] = 2^29-1 })
  526. else
  527. local stop = lex:integer 'field number range end'
  528. insert_tab(rt, { start = start, ['end'] = stop })
  529. end
  530. else
  531. insert_tab(rt, { start = start, ['end'] = start })
  532. end
  533. first = false
  534. until not lex:test ','
  535. end
  536. lex:line_end()
  537. return self
  538. end
  539. function msg_body:oneof(lex, info)
  540. local fs = default(info, "field")
  541. local ts = default(info, "nested_type")
  542. local ot = default(info, "oneof_decl")
  543. local index = #ot + 1
  544. local oneof = { name = lex:ident() }
  545. lex:expected "{"
  546. while not lex:test "}" do
  547. local ident = lex:type_name()
  548. if ident == "option" then
  549. toplevel.option(self, lex, oneof)
  550. else
  551. local f, t = field(self, lex, ident, "no_label")
  552. self.locmap[f] = lex.pos
  553. if t then insert_tab(ts, t) end
  554. f.oneof_index = index - 1
  555. insert_tab(fs, f)
  556. end
  557. lex:line_end 'opt'
  558. end
  559. ot[index] = oneof
  560. end
  561. function msg_body:option(lex, info)
  562. toplevel.option(self, lex, default(info, 'options'))
  563. end
  564. end
  565. function toplevel:message(lex, info)
  566. local name = lex:ident 'message name'
  567. local typ = { name = name }
  568. register_type(self, lex, name, types.message)
  569. local prefix = self.prefix
  570. self.prefix = prefix..name.."."
  571. lex:expected "{"
  572. while not lex:test "}" do
  573. local ident, pos = lex:type_name()
  574. local body_parser = msg_body[ident]
  575. if body_parser then
  576. body_parser(self, lex, typ)
  577. else
  578. local fs = default(typ, 'field')
  579. local f, t = label_field(self, lex, ident, typ)
  580. self.locmap[f] = pos
  581. insert_tab(fs, f)
  582. if t then
  583. local ts = default(typ, 'nested_type')
  584. insert_tab(ts, t)
  585. end
  586. end
  587. lex:line_end 'opt'
  588. end
  589. lex:line_end 'opt'
  590. if info then
  591. info = default(info, 'message_type')
  592. insert_tab(info, typ)
  593. end
  594. self.prefix = prefix
  595. return typ
  596. end
  597. function toplevel:enum(lex, info)
  598. local name = lex:ident 'enum name'
  599. local enum = { name = name }
  600. register_type(self, lex, name, types.enum)
  601. lex:expected "{"
  602. while not lex:test "}" do
  603. local ident = lex:ident 'enum constant name'
  604. if ident == 'option' then
  605. toplevel.option(self, lex, default(enum, 'options'))
  606. elseif ident == 'reserved' then
  607. msg_body.reserved(self, lex, enum)
  608. else
  609. local values = default(enum, 'value')
  610. local number = lex:expected '=' :integer()
  611. lex:line_end()
  612. insert_tab(values, {
  613. name = ident,
  614. number = number,
  615. options = inline_option(lex)
  616. })
  617. end
  618. lex:line_end 'opt'
  619. end
  620. lex:line_end 'opt'
  621. if info then
  622. info = default(info, 'enum_type')
  623. insert_tab(info, enum)
  624. end
  625. return enum
  626. end
  627. function toplevel:option(lex, info)
  628. local ident = lex:option_name()
  629. lex:expected "="
  630. local value = lex:constant()
  631. lex:line_end()
  632. local options = info and default(info, 'options') or {}
  633. options[ident] = value
  634. return options, self
  635. end
  636. function toplevel:extend(lex, info)
  637. local name = lex:type_name()
  638. local ft = info and default(info, 'extension') or {}
  639. local mt = info and default(info, 'message_type') or {}
  640. lex:expected "{"
  641. while not lex:test "}" do
  642. local ident, pos = lex:type_name()
  643. local f, t = label_field(self, lex, ident)
  644. self.locmap[f] = pos
  645. f.extendee = name
  646. insert_tab(ft, f)
  647. insert_tab(mt, t)
  648. lex:line_end 'opt'
  649. end
  650. return ft, mt
  651. end
  652. local svr_body = {} do
  653. function svr_body:rpc(lex, info)
  654. local name, pos = lex:ident "rpc name"
  655. local rpc = { name = name }
  656. self.locmap[rpc] = pos
  657. local _, tn
  658. lex:expected "%("
  659. rpc.client_streaming = lex:keyword("stream", "opt")
  660. _, tn = type_info(lex, lex:type_name())
  661. if not tn then return lex:error "rpc input type must by message" end
  662. rpc.input_type = tn
  663. lex:expected "%)" :expected "returns" :expected "%("
  664. rpc.server_streaming = lex:keyword("stream", "opt")
  665. _, tn = type_info(lex, lex:type_name())
  666. if not tn then return lex:error "rpc output type must by message" end
  667. rpc.output_type = tn
  668. lex:expected "%)"
  669. if lex:test "{" then
  670. while not lex:test "}" do
  671. lex:line_end "opt"
  672. lex:keyword "option"
  673. toplevel.option(self, lex, default(rpc, 'options'))
  674. end
  675. end
  676. lex:line_end "opt"
  677. local t = default(info, "method")
  678. insert_tab(t, rpc)
  679. end
  680. function svr_body:option(lex, info)
  681. toplevel.option(self, lex, default(info, 'options')) -- TODO: should be deeper in the info?
  682. end
  683. function svr_body.stream(_, lex)
  684. lex:error "stream not implement yet"
  685. end
  686. end
  687. function toplevel:service(lex, info)
  688. local name = lex:ident 'service name'
  689. local svr = { name = name }
  690. lex:expected "{"
  691. while not lex:test "}" do
  692. local ident = lex:type_name()
  693. local body_parser = svr_body[ident]
  694. if body_parser then
  695. body_parser(self, lex, svr)
  696. else
  697. return lex:error "expected 'rpc' or 'option' in service body"
  698. end
  699. lex:line_end 'opt'
  700. end
  701. lex:line_end 'opt'
  702. if info then
  703. info = default(info, 'service')
  704. insert_tab(info, svr)
  705. end
  706. return svr
  707. end
  708. end
  709. local function make_context(self, lex)
  710. local ctx = {
  711. syntax = "proto2";
  712. locmap = {};
  713. prefix = ".";
  714. lex = lex;
  715. parser = self;
  716. }
  717. ctx.loaded = self.loaded
  718. ctx.typemap = self.typemap
  719. ctx.paths = self.paths
  720. ctx.proto3_optional =
  721. self.proto3_optional or self.experimental_allow_proto3_optional
  722. function ctx.import_fallback(import_name)
  723. if self.unknown_import == true then
  724. return true
  725. elseif type(self.unknown_import) == 'string' then
  726. return import_name:match(self.unknown_import) and true or nil
  727. elseif self.unknown_import then
  728. return self:unknown_import(import_name)
  729. end
  730. end
  731. function ctx.type_fallback(type_name)
  732. if self.unknown_type == true then
  733. return true
  734. elseif type(self.unknown_type) == 'string' then
  735. return type_name:match(self.unknown_type) and true
  736. elseif self.unknown_type then
  737. return self:unknown_type(type_name)
  738. end
  739. end
  740. function ctx.on_import(info)
  741. if self.on_import then
  742. return self.on_import(info)
  743. end
  744. end
  745. return setmetatable(ctx, Parser)
  746. end
  747. function Parser:parse(src, name)
  748. local loaded = self.loaded[name]
  749. if loaded then
  750. if loaded == true then
  751. error("loop loaded: "..name)
  752. end
  753. return loaded
  754. end
  755. name = name or "<input>"
  756. self.loaded[name] = true
  757. local lex = Lexer.new(name, src)
  758. local ctx = make_context(self, lex)
  759. local info = { name = lex.name, syntax = ctx.syntax }
  760. local syntax = lex:keyword('syntax', 'opt')
  761. if syntax then
  762. info.syntax = lex:expected '=' :quote()
  763. ctx.syntax = info.syntax
  764. lex:line_end()
  765. end
  766. while not lex:eof() do
  767. local ident = lex:ident()
  768. local top_parser = toplevel[ident]
  769. if top_parser then
  770. top_parser(ctx, lex, info)
  771. else
  772. lex:error("unknown keyword '"..ident.."'")
  773. end
  774. lex:line_end "opt"
  775. end
  776. self.loaded[name] = name ~= "<input>" and info or nil
  777. return ctx:resolve(lex, info)
  778. end
  779. -- resolver
  780. local function empty() end
  781. local function iter(t, k)
  782. local v = t[k]
  783. if v then return ipairs(v) end
  784. return empty
  785. end
  786. local function check_dup(self, lex, typ, map, k, v)
  787. local old = map[v[k]]
  788. if old then
  789. local ln, co = lex:pos2loc(self.locmap[old])
  790. lex:error("%s '%s' exists, previous at %d:%d",
  791. typ, v[k], ln, co)
  792. end
  793. map[v[k]] = v
  794. end
  795. local function check_type(self, lex, tname)
  796. if tname:match "^%." then
  797. local t = self.typemap[tname]
  798. if not t then
  799. return lex:error("unknown type '%s'", tname)
  800. end
  801. return t, tname
  802. end
  803. local prefix = self.prefix
  804. for i = #prefix+1, 1, -1 do
  805. local op = prefix[i]
  806. prefix[i] = tname
  807. local tn = table.concat(prefix, ".", 1, i)
  808. prefix[i] = op
  809. local t = self.typemap[tn]
  810. if t then return t, tn end
  811. end
  812. local tn, t
  813. if self.type_fallback then
  814. tn, t = self.type_fallback(tname)
  815. end
  816. if tn then
  817. t = types[t or "message"]
  818. if tn == true then tn = "."..tname end
  819. return t, tn
  820. end
  821. return lex:error("unknown type '%s'", tname)
  822. end
  823. local function check_field(self, lex, info)
  824. if info.extendee then
  825. local t, tn = check_type(self, lex, info.extendee)
  826. if t ~= types.message then
  827. lex:error("message type expected in extension")
  828. end
  829. info.extendee = tn
  830. end
  831. if info.type_name then
  832. local t, tn = check_type(self, lex, info.type_name)
  833. info.type = t
  834. info.type_name = tn
  835. end
  836. end
  837. local function check_enum(self, lex, info)
  838. local names, numbers = {}, {}
  839. for _, v in iter(info, 'value') do
  840. lex.pos = self.locmap[v]
  841. check_dup(self, lex, 'enum name', names, 'name', v)
  842. if not (info.options
  843. and info.options.options
  844. and info.options.options.allow_alias) then
  845. check_dup(self, lex, 'enum number', numbers, 'number', v)
  846. end
  847. end
  848. end
  849. local function check_message(self, lex, info)
  850. insert_tab(self.prefix, info.name)
  851. local names, numbers = {}, {}
  852. for _, v in iter(info, 'field') do
  853. lex.pos = assert(self.locmap[v])
  854. check_dup(self, lex, 'field name', names, 'name', v)
  855. check_dup(self, lex, 'field number', numbers, 'number', v)
  856. check_field(self, lex, v)
  857. end
  858. for _, v in iter(info, 'nested_type') do
  859. check_message(self, lex, v)
  860. end
  861. for _, v in iter(info, 'extension') do
  862. lex.pos = assert(self.locmap[v])
  863. check_field(self, lex, v)
  864. end
  865. self.prefix[#self.prefix] = nil
  866. end
  867. local function check_service(self, lex, info)
  868. local names = {}
  869. for _, v in iter(info, 'method') do
  870. lex.pos = self.locmap[v]
  871. check_dup(self, lex, 'rpc name', names, 'name', v)
  872. local t, tn = check_type(self, lex, v.input_type)
  873. v.input_type = tn
  874. if t ~= types.message then
  875. lex:error "message type expected in parameter"
  876. end
  877. t, tn = check_type(self, lex, v.output_type)
  878. v.output_type = tn
  879. if t ~= types.message then
  880. lex:error "message type expected in return"
  881. end
  882. end
  883. end
  884. function Parser:resolve(lex, info)
  885. self.prefix = { "", info.package }
  886. for _, v in iter(info, 'message_type') do
  887. check_message(self, lex, v)
  888. end
  889. for _, v in iter(info, 'enum_type') do
  890. check_enum(self, lex, v)
  891. end
  892. for _, v in iter(info, 'service') do
  893. check_service(self, lex, v)
  894. end
  895. for _, v in iter(info, 'extension') do
  896. lex.pos = assert(self.locmap[v])
  897. check_field(self, lex, v)
  898. end
  899. self.prefix = nil
  900. return info
  901. end
  902. end
  903. local has_pb, pb = pcall(require, "pb") do
  904. if has_pb then
  905. local descriptor_pb =
  906. "\10\179;\10\16descriptor.proto\18\15google.protobuf\"M\10\17FileDescrip"..
  907. "torSet\0188\10\4file\24\1 \3(\0112$.google.protobuf.FileDescriptorProto"..
  908. "R\4file\"\228\4\10\19FileDescriptorProto\18\18\10\4name\24\1 \1(\9R\4na"..
  909. "me\18\24\10\7package\24\2 \1(\9R\7package\18\30\10\10dependency\24\3 \3"..
  910. "(\9R\10dependency\18+\10\17public_dependency\24\10 \3(\5R\16publicDepen"..
  911. "dency\18'\10\15weak_dependency\24\11 \3(\5R\14weakDependency\18C\10\12m"..
  912. "essage_type\24\4 \3(\0112 .google.protobuf.DescriptorProtoR\11messageTy"..
  913. "pe\18A\10\9enum_type\24\5 \3(\0112$.google.protobuf.EnumDescriptorProto"..
  914. "R\8enumType\18A\10\7service\24\6 \3(\0112'.google.protobuf.ServiceDescr"..
  915. "iptorProtoR\7service\18C\10\9extension\24\7 \3(\0112%.google.protobuf.F"..
  916. "ieldDescriptorProtoR\9extension\0186\10\7options\24\8 \1(\0112\28.googl"..
  917. "e.protobuf.FileOptionsR\7options\18I\10\16source_code_info\24\9 \1(\011"..
  918. "2\31.google.protobuf.SourceCodeInfoR\14sourceCodeInfo\18\22\10\6syntax"..
  919. "\24\12 \1(\9R\6syntax\"\185\6\10\15DescriptorProto\18\18\10\4name\24\1 "..
  920. "\1(\9R\4name\18;\10\5field\24\2 \3(\0112%.google.protobuf.FieldDescript"..
  921. "orProtoR\5field\18C\10\9extension\24\6 \3(\0112%.google.protobuf.FieldD"..
  922. "escriptorProtoR\9extension\18A\10\11nested_type\24\3 \3(\0112 .google.p"..
  923. "rotobuf.DescriptorProtoR\10nestedType\18A\10\9enum_type\24\4 \3(\0112$."..
  924. "google.protobuf.EnumDescriptorProtoR\8enumType\18X\10\15extension_range"..
  925. "\24\5 \3(\0112/.google.protobuf.DescriptorProto.ExtensionRangeR\14exten"..
  926. "sionRange\18D\10\10oneof_decl\24\8 \3(\0112%.google.protobuf.OneofDescr"..
  927. "iptorProtoR\9oneofDecl\0189\10\7options\24\7 \1(\0112\31.google.protobu"..
  928. "f.MessageOptionsR\7options\18U\10\14reserved_range\24\9 \3(\0112..googl"..
  929. "e.protobuf.DescriptorProto.ReservedRangeR\13reservedRange\18#\10\13rese"..
  930. "rved_name\24\10 \3(\9R\12reservedName\26z\10\14ExtensionRange\18\20\10"..
  931. "\5start\24\1 \1(\5R\5start\18\16\10\3end\24\2 \1(\5R\3end\18@\10\7optio"..
  932. "ns\24\3 \1(\0112&.google.protobuf.ExtensionRangeOptionsR\7options\0267"..
  933. "\10\13ReservedRange\18\20\10\5start\24\1 \1(\5R\5start\18\16\10\3end\24"..
  934. "\2 \1(\5R\3end\"|\10\21ExtensionRangeOptions\18X\10\20uninterpreted_opt"..
  935. "ion\24\231\7 \3(\0112$.google.protobuf.UninterpretedOptionR\19uninterpr"..
  936. "etedOption*\9\8\232\7\16\128\128\128\128\2\"\193\6\10\20FieldDescriptor"..
  937. "Proto\18\18\10\4name\24\1 \1(\9R\4name\18\22\10\6number\24\3 \1(\5R\6nu"..
  938. "mber\18A\10\5label\24\4 \1(\0142+.google.protobuf.FieldDescriptorProto."..
  939. "LabelR\5label\18>\10\4type\24\5 \1(\0142*.google.protobuf.FieldDescript"..
  940. "orProto.TypeR\4type\18\27\10\9type_name\24\6 \1(\9R\8typeName\18\26\10"..
  941. "\8extendee\24\2 \1(\9R\8extendee\18#\10\13default_value\24\7 \1(\9R\12d"..
  942. "efaultValue\18\31\10\11oneof_index\24\9 \1(\5R\10oneofIndex\18\27\10\9j"..
  943. "son_name\24\10 \1(\9R\8jsonName\0187\10\7options\24\8 \1(\0112\29.googl"..
  944. "e.protobuf.FieldOptionsR\7options\18'\10\15proto3_optional\24\17 \1(\8R"..
  945. "\14proto3Optional\"\182\2\10\4Type\18\15\10\11TYPE_DOUBLE\16\1\18\14\10"..
  946. "\10TYPE_FLOAT\16\2\18\14\10\10TYPE_INT64\16\3\18\15\10\11TYPE_UINT64\16"..
  947. "\4\18\14\10\10TYPE_INT32\16\5\18\16\10\12TYPE_FIXED64\16\6\18\16\10\12T"..
  948. "YPE_FIXED32\16\7\18\13\10\9TYPE_BOOL\16\8\18\15\10\11TYPE_STRING\16\9"..
  949. "\18\14\10\10TYPE_GROUP\16\10\18\16\10\12TYPE_MESSAGE\16\11\18\14\10\10T"..
  950. "YPE_BYTES\16\12\18\15\10\11TYPE_UINT32\16\13\18\13\10\9TYPE_ENUM\16\14"..
  951. "\18\17\10\13TYPE_SFIXED32\16\15\18\17\10\13TYPE_SFIXED64\16\16\18\15\10"..
  952. "\11TYPE_SINT32\16\17\18\15\10\11TYPE_SINT64\16\18\"C\10\5Label\18\18\10"..
  953. "\14LABEL_OPTIONAL\16\1\18\18\10\14LABEL_REQUIRED\16\2\18\18\10\14LABEL_"..
  954. "REPEATED\16\3\"c\10\20OneofDescriptorProto\18\18\10\4name\24\1 \1(\9R\4"..
  955. "name\0187\10\7options\24\2 \1(\0112\29.google.protobuf.OneofOptionsR\7o"..
  956. "ptions\"\227\2\10\19EnumDescriptorProto\18\18\10\4name\24\1 \1(\9R\4nam"..
  957. "e\18?\10\5value\24\2 \3(\0112).google.protobuf.EnumValueDescriptorProto"..
  958. "R\5value\0186\10\7options\24\3 \1(\0112\28.google.protobuf.EnumOptionsR"..
  959. "\7options\18]\10\14reserved_range\24\4 \3(\01126.google.protobuf.EnumDe"..
  960. "scriptorProto.EnumReservedRangeR\13reservedRange\18#\10\13reserved_name"..
  961. "\24\5 \3(\9R\12reservedName\26;\10\17EnumReservedRange\18\20\10\5start"..
  962. "\24\1 \1(\5R\5start\18\16\10\3end\24\2 \1(\5R\3end\"\131\1\10\24EnumVal"..
  963. "ueDescriptorProto\18\18\10\4name\24\1 \1(\9R\4name\18\22\10\6number\24"..
  964. "\2 \1(\5R\6number\18;\10\7options\24\3 \1(\0112!.google.protobuf.EnumVa"..
  965. "lueOptionsR\7options\"\167\1\10\22ServiceDescriptorProto\18\18\10\4name"..
  966. "\24\1 \1(\9R\4name\18>\10\6method\24\2 \3(\0112&.google.protobuf.Method"..
  967. "DescriptorProtoR\6method\0189\10\7options\24\3 \1(\0112\31.google.proto"..
  968. "buf.ServiceOptionsR\7options\"\137\2\10\21MethodDescriptorProto\18\18"..
  969. "\10\4name\24\1 \1(\9R\4name\18\29\10\10input_type\24\2 \1(\9R\9inputTyp"..
  970. "e\18\31\10\11output_type\24\3 \1(\9R\10outputType\0188\10\7options\24\4"..
  971. " \1(\0112\30.google.protobuf.MethodOptionsR\7options\0180\10\16client_s"..
  972. "treaming\24\5 \1(\8:\5falseR\15clientStreaming\0180\10\16server_streami"..
  973. "ng\24\6 \1(\8:\5falseR\15serverStreaming\"\145\9\10\11FileOptions\18!"..
  974. "\10\12java_package\24\1 \1(\9R\11javaPackage\0180\10\20java_outer_class"..
  975. "name\24\8 \1(\9R\18javaOuterClassname\0185\10\19java_multiple_files\24"..
  976. "\10 \1(\8:\5falseR\17javaMultipleFiles\18D\10\29java_generate_equals_an"..
  977. "d_hash\24\20 \1(\8B\2\24\1R\25javaGenerateEqualsAndHash\18:\10\22java_s"..
  978. "tring_check_utf8\24\27 \1(\8:\5falseR\19javaStringCheckUtf8\18S\10\12op"..
  979. "timize_for\24\9 \1(\0142).google.protobuf.FileOptions.OptimizeMode:\5SP"..
  980. "EEDR\11optimizeFor\18\29\10\10go_package\24\11 \1(\9R\9goPackage\0185"..
  981. "\10\19cc_generic_services\24\16 \1(\8:\5falseR\17ccGenericServices\0189"..
  982. "\10\21java_generic_services\24\17 \1(\8:\5falseR\19javaGenericServices"..
  983. "\0185\10\19py_generic_services\24\18 \1(\8:\5falseR\17pyGenericServices"..
  984. "\0187\10\20php_generic_services\24* \1(\8:\5falseR\18phpGenericServices"..
  985. "\18%\10\10deprecated\24\23 \1(\8:\5falseR\10deprecated\18.\10\16cc_enab"..
  986. "le_arenas\24\31 \1(\8:\4trueR\14ccEnableArenas\18*\10\17objc_class_pref"..
  987. "ix\24$ \1(\9R\15objcClassPrefix\18)\10\16csharp_namespace\24% \1(\9R\15"..
  988. "csharpNamespace\18!\10\12swift_prefix\24' \1(\9R\11swiftPrefix\18(\10"..
  989. "\16php_class_prefix\24( \1(\9R\14phpClassPrefix\18#\10\13php_namespace"..
  990. "\24) \1(\9R\12phpNamespace\0184\10\22php_metadata_namespace\24, \1(\9R"..
  991. "\20phpMetadataNamespace\18!\10\12ruby_package\24- \1(\9R\11rubyPackage"..
  992. "\18X\10\20uninterpreted_option\24\231\7 \3(\0112$.google.protobuf.Unint"..
  993. "erpretedOptionR\19uninterpretedOption\":\10\12OptimizeMode\18\9\10\5SPE"..
  994. "ED\16\1\18\13\10\9CODE_SIZE\16\2\18\16\10\12LITE_RUNTIME\16\3*\9\8\232"..
  995. "\7\16\128\128\128\128\2J\4\8&\16'\"\227\2\10\14MessageOptions\18<\10\23"..
  996. "message_set_wire_format\24\1 \1(\8:\5falseR\20messageSetWireFormat\18L"..
  997. "\10\31no_standard_descriptor_accessor\24\2 \1(\8:\5falseR\28noStandardD"..
  998. "escriptorAccessor\18%\10\10deprecated\24\3 \1(\8:\5falseR\10deprecated"..
  999. "\18\27\10\9map_entry\24\7 \1(\8R\8mapEntry\18X\10\20uninterpreted_optio"..
  1000. "n\24\231\7 \3(\0112$.google.protobuf.UninterpretedOptionR\19uninterpret"..
  1001. "edOption*\9\8\232\7\16\128\128\128\128\2J\4\8\4\16\5J\4\8\5\16\6J\4\8\6"..
  1002. "\16\7J\4\8\8\16\9J\4\8\9\16\10\"\226\3\10\12FieldOptions\18A\10\5ctype"..
  1003. "\24\1 \1(\0142#.google.protobuf.FieldOptions.CType:\6STRINGR\5ctype\18"..
  1004. "\22\10\6packed\24\2 \1(\8R\6packed\18G\10\6jstype\24\6 \1(\0142$.google"..
  1005. ".protobuf.FieldOptions.JSType:\9JS_NORMALR\6jstype\18\25\10\4lazy\24\5 "..
  1006. "\1(\8:\5falseR\4lazy\18%\10\10deprecated\24\3 \1(\8:\5falseR\10deprecat"..
  1007. "ed\18\25\10\4weak\24\10 \1(\8:\5falseR\4weak\18X\10\20uninterpreted_opt"..
  1008. "ion\24\231\7 \3(\0112$.google.protobuf.UninterpretedOptionR\19uninterpr"..
  1009. "etedOption\"/\10\5CType\18\10\10\6STRING\16\0\18\8\10\4CORD\16\1\18\16"..
  1010. "\10\12STRING_PIECE\16\2\"5\10\6JSType\18\13\10\9JS_NORMAL\16\0\18\13\10"..
  1011. "\9JS_STRING\16\1\18\13\10\9JS_NUMBER\16\2*\9\8\232\7\16\128\128\128\128"..
  1012. "\2J\4\8\4\16\5\"s\10\12OneofOptions\18X\10\20uninterpreted_option\24"..
  1013. "\231\7 \3(\0112$.google.protobuf.UninterpretedOptionR\19uninterpretedOp"..
  1014. "tion*\9\8\232\7\16\128\128\128\128\2\"\192\1\10\11EnumOptions\18\31\10"..
  1015. "\11allow_alias\24\2 \1(\8R\10allowAlias\18%\10\10deprecated\24\3 \1(\8:"..
  1016. "\5falseR\10deprecated\18X\10\20uninterpreted_option\24\231\7 \3(\0112$."..
  1017. "google.protobuf.UninterpretedOptionR\19uninterpretedOption*\9\8\232\7"..
  1018. "\16\128\128\128\128\2J\4\8\5\16\6\"\158\1\10\16EnumValueOptions\18%\10"..
  1019. "\10deprecated\24\1 \1(\8:\5falseR\10deprecated\18X\10\20uninterpreted_o"..
  1020. "ption\24\231\7 \3(\0112$.google.protobuf.UninterpretedOptionR\19uninter"..
  1021. "pretedOption*\9\8\232\7\16\128\128\128\128\2\"\156\1\10\14ServiceOption"..
  1022. "s\18%\10\10deprecated\24! \1(\8:\5falseR\10deprecated\18X\10\20uninterp"..
  1023. "reted_option\24\231\7 \3(\0112$.google.protobuf.UninterpretedOptionR\19"..
  1024. "uninterpretedOption*\9\8\232\7\16\128\128\128\128\2\"\224\2\10\13Method"..
  1025. "Options\18%\10\10deprecated\24! \1(\8:\5falseR\10deprecated\18q\10\17id"..
  1026. "empotency_level\24\" \1(\0142/.google.protobuf.MethodOptions.Idempotenc"..
  1027. "yLevel:\19IDEMPOTENCY_UNKNOWNR\16idempotencyLevel\18X\10\20uninterprete"..
  1028. "d_option\24\231\7 \3(\0112$.google.protobuf.UninterpretedOptionR\19unin"..
  1029. "terpretedOption\"P\10\16IdempotencyLevel\18\23\10\19IDEMPOTENCY_UNKNOWN"..
  1030. "\16\0\18\19\10\15NO_SIDE_EFFECTS\16\1\18\14\10\10IDEMPOTENT\16\2*\9\8"..
  1031. "\232\7\16\128\128\128\128\2\"\154\3\10\19UninterpretedOption\18A\10\4na"..
  1032. "me\24\2 \3(\0112-.google.protobuf.UninterpretedOption.NamePartR\4name"..
  1033. "\18)\10\16identifier_value\24\3 \1(\9R\15identifierValue\18,\10\18posit"..
  1034. "ive_int_value\24\4 \1(\4R\16positiveIntValue\18,\10\18negative_int_valu"..
  1035. "e\24\5 \1(\3R\16negativeIntValue\18!\10\12double_value\24\6 \1(\1R\11do"..
  1036. "ubleValue\18!\10\12string_value\24\7 \1(\12R\11stringValue\18'\10\15agg"..
  1037. "regate_value\24\8 \1(\9R\14aggregateValue\26J\10\8NamePart\18\27\10\9na"..
  1038. "me_part\24\1 \2(\9R\8namePart\18!\10\12is_extension\24\2 \2(\8R\11isExt"..
  1039. "ension\"\167\2\10\14SourceCodeInfo\18D\10\8location\24\1 \3(\0112(.goog"..
  1040. "le.protobuf.SourceCodeInfo.LocationR\8location\26\206\1\10\8Location\18"..
  1041. "\22\10\4path\24\1 \3(\5B\2\16\1R\4path\18\22\10\4span\24\2 \3(\5B\2\16"..
  1042. "\1R\4span\18)\10\16leading_comments\24\3 \1(\9R\15leadingComments\18+"..
  1043. "\10\17trailing_comments\24\4 \1(\9R\16trailingComments\18:\10\25leading"..
  1044. "_detached_comments\24\6 \3(\9R\23leadingDetachedComments\"\209\1\10\17G"..
  1045. "eneratedCodeInfo\18M\10\10annotation\24\1 \3(\0112-.google.protobuf.Gen"..
  1046. "eratedCodeInfo.AnnotationR\10annotation\26m\10\10Annotation\18\22\10\4p"..
  1047. "ath\24\1 \3(\5B\2\16\1R\4path\18\31\10\11source_file\24\2 \1(\9R\10sour"..
  1048. "ceFile\18\20\10\5begin\24\3 \1(\5R\5begin\18\16\10\3end\24\4 \1(\5R\3en"..
  1049. "dB~\10\19com.google.protobufB\16DescriptorProtosH\1Z-google.golang.org/"..
  1050. "protobuf/types/descriptorpb\248\1\1\162\2\3GPB\170\2\26Google.Protobuf."..
  1051. "Reflection"
  1052. function Parser.reload()
  1053. assert(pb.load(descriptor_pb), "load descriptor msg failed")
  1054. end
  1055. local function do_compile(self, f, ...)
  1056. if self.include_imports then
  1057. local old = self.on_import
  1058. local infos = {}
  1059. function self.on_import(info)
  1060. insert_tab(infos, info)
  1061. end
  1062. local r = f(...)
  1063. insert_tab(infos, r)
  1064. self.on_import = old
  1065. return { file = infos }
  1066. end
  1067. return { file = { f(...) } }
  1068. end
  1069. function Parser:compile(s, name)
  1070. if self == Parser then self = Parser.new() end
  1071. local set = do_compile(self, self.parse, self, s, name)
  1072. return pb.encode('.google.protobuf.FileDescriptorSet', set)
  1073. end
  1074. function Parser:compilefile(fn)
  1075. if self == Parser then self = Parser.new() end
  1076. local set = do_compile(self, self.parsefile, self, fn)
  1077. return pb.encode('.google.protobuf.FileDescriptorSet', set)
  1078. end
  1079. function Parser:load(s, name)
  1080. if self == Parser then self = Parser.new() end
  1081. local ret, pos = pb.load(self:compile(s, name))
  1082. if ret then return ret, pos end
  1083. error("load failed at offset "..pos)
  1084. end
  1085. function Parser:loadfile(fn)
  1086. if self == Parser then self = Parser.new() end
  1087. local ret, pos = pb.load(self:compilefile(fn))
  1088. if ret then return ret, pos end
  1089. error("load failed at offset "..pos)
  1090. end
  1091. Parser.reload()
  1092. end
  1093. end
  1094. return Parser