protoc.lua 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059
  1. local function meta(name, t)
  2. t = t or {}
  3. t.__name = name
  4. t.__index = t
  5. return t
  6. end
  7. local function default(t, k, def)
  8. local v = t[k]
  9. if not v then
  10. v = def or {}
  11. t[k] = v
  12. end
  13. return v
  14. end
  15. local Lexer = meta "Lexer" do
  16. local escape = {
  17. a = "\a", b = "\b", f = "\f", n = "\n",
  18. r = "\r", t = "\t", v = "\v"
  19. }
  20. local function tohex(x) return string.byte(tonumber(x, 16)) end
  21. local function todec(x) return string.byte(tonumber(x, 10)) end
  22. local function toesc(x) return escape[x] or x end
  23. function Lexer.new(name, src)
  24. local self = {
  25. name = name,
  26. src = src,
  27. pos = 1
  28. }
  29. return setmetatable(self, Lexer)
  30. end
  31. function Lexer:__call(patt, pos)
  32. return self.src:match(patt, pos or self.pos)
  33. end
  34. function Lexer:test(patt)
  35. self:whitespace()
  36. local pos = self('^'..patt..'%s*()')
  37. if not pos then return false end
  38. self.pos = pos
  39. return true
  40. end
  41. function Lexer:expected(patt, name)
  42. if not self:test(patt) then
  43. return self:error((name or "'"..patt.."'").." expected")
  44. end
  45. return self
  46. end
  47. function Lexer:pos2loc(pos)
  48. local linenr = 1
  49. pos = pos or self.pos
  50. for start, stop in self.src:gmatch "()[^\n]*()\n?" do
  51. if start <= pos and pos <= stop then
  52. return linenr, pos - start + 1
  53. end
  54. linenr = linenr + 1
  55. end
  56. end
  57. function Lexer:error(fmt, ...)
  58. local ln, co = self:pos2loc()
  59. return error(("%s:%d:%d: "..fmt):format(self.name, ln, co, ...))
  60. end
  61. function Lexer:opterror(opt, msg)
  62. if not opt then return self:error(msg) end
  63. return nil
  64. end
  65. function Lexer:whitespace()
  66. local pos, c = self "^%s*()(%/?)"
  67. self.pos = pos
  68. if c == '' then return self end
  69. return self:comment()
  70. end
  71. function Lexer:comment()
  72. local pos = self "^%/%/[^\n]*\n?()"
  73. if not pos then
  74. if self "^%/%*" then
  75. pos = self "^%/%*.-%*%/()"
  76. if not pos then
  77. self:error "unfinished comment"
  78. end
  79. end
  80. end
  81. if not pos then return self end
  82. self.pos = pos
  83. return self:whitespace()
  84. end
  85. function Lexer:line_end(opt)
  86. self:whitespace()
  87. local pos = self '^[%s;]*%s*()'
  88. if not pos then
  89. return self:opterror(opt, "';' expected")
  90. end
  91. self.pos = pos
  92. return pos
  93. end
  94. function Lexer:eof()
  95. self:whitespace()
  96. return self.pos > #self.src
  97. end
  98. function Lexer:keyword(kw, opt)
  99. self:whitespace()
  100. local ident, pos = self "^([%a_][%w_]*)%s*()"
  101. if not ident or ident ~= kw then
  102. return self:opterror(opt, "''"..kw..'" expected')
  103. end
  104. self.pos = pos
  105. return kw
  106. end
  107. function Lexer:ident(name, opt)
  108. self:whitespace()
  109. local b, ident, pos = self "^()([%a_][%w_]*)%s*()"
  110. if not ident then
  111. return self:opterror(opt, (name or 'name')..' expected')
  112. end
  113. self.pos = pos
  114. return ident, b
  115. end
  116. function Lexer:full_ident(name, opt)
  117. self:whitespace()
  118. local b, ident, pos = self "^()([%a_][%w_.]*)%s*()"
  119. if not ident or ident:match "%.%.+" then
  120. return self:opterror(opt, (name or 'name')..' expected')
  121. end
  122. self.pos = pos
  123. return ident, b
  124. end
  125. function Lexer:integer(opt)
  126. self:whitespace()
  127. local ns, oct, hex, s, pos =
  128. self "^([+-]?)(0?)([xX]?)([0-9a-fA-F]+)%s*()"
  129. local n
  130. if oct == '0' and hex == '' then
  131. n = tonumber(s, 8)
  132. elseif oct == '' and hex == '' then
  133. n = tonumber(s, 10)
  134. elseif oct == '0' and hex ~= '' then
  135. n = tonumber(s, 16)
  136. end
  137. if not n then
  138. return self:opterror(opt, 'integer expected')
  139. end
  140. self.pos = pos
  141. return ns == '-' and -n or n
  142. end
  143. function Lexer:number(opt)
  144. self:whitespace()
  145. if self:test "nan%f[%A]" then
  146. return 0.0/0.0
  147. elseif self:test "inf%f[%A]" then
  148. return 1.0/0.0
  149. end
  150. local ns, d1, s, d2, s2, pos = self "^([+-]?)(%.?)([0-9]+)(%.?)([0-9]*)()"
  151. if not ns then
  152. return self:opterror(opt, 'floating-point number expected')
  153. end
  154. local es, pos2 = self("(^[eE][+-]?[0-9]+)%s*()", pos)
  155. if d1 == "." and d2 == "." then
  156. return self:error "malformed floating-point number"
  157. end
  158. self.pos = pos2 or pos
  159. local n = tonumber(d1..s..d2..s2..(es or ""))
  160. return ns == '-' and -n or n
  161. end
  162. function Lexer:quote(opt)
  163. self:whitespace()
  164. local q, start = self '^(["\'])()'
  165. if not start then
  166. return self:opterror(opt, 'string expected')
  167. end
  168. self.pos = start
  169. local patt = '()(\\?'..q..')%s*()'
  170. while true do
  171. local stop, s, pos = self(patt)
  172. if not stop then
  173. self.pos = start-1
  174. return self:error "unfinished string"
  175. end
  176. self.pos = pos
  177. if s == q then
  178. return self.src:sub(start, stop-1)
  179. :gsub("\\x(%x+)", tohex)
  180. :gsub("\\(%d+)", todec)
  181. :gsub("\\(.)", toesc)
  182. end
  183. end
  184. end
  185. function Lexer:constant(opt)
  186. local c = self:full_ident('constant', 'opt') or
  187. self:number('opt') or
  188. self:quote('opt')
  189. if not c and not opt then
  190. return self:error "constant expected"
  191. end
  192. return c
  193. end
  194. function Lexer:option_name()
  195. local ident
  196. if self:test "%(" then
  197. ident = self:full_ident "option name"
  198. self:expected "%)"
  199. else
  200. ident = self:ident "option name"
  201. end
  202. while self:test "%." do
  203. ident = ident .. "." .. self:ident()
  204. end
  205. return ident
  206. end
  207. function Lexer:type_name()
  208. if self:test "%." then
  209. local id, pos = self:full_ident "type name"
  210. return "."..id, pos
  211. else
  212. return self:full_ident "type name"
  213. end
  214. end
  215. end
  216. local Parser = meta "Parser" do
  217. Parser.typemap = {}
  218. Parser.loaded = {}
  219. Parser.paths = { "", "." }
  220. function Parser.new()
  221. local self = {}
  222. self.typemap = {}
  223. self.loaded = {}
  224. self.paths = { "", "." }
  225. return setmetatable(self, Parser)
  226. end
  227. function Parser:error(msg)
  228. return self.lex:error(msg)
  229. end
  230. function Parser:addpath(path)
  231. self.paths[#self.paths+1] = path
  232. end
  233. function Parser:parsefile(name)
  234. local info = self.loaded[name]
  235. if info then return info end
  236. local errors = {}
  237. for _, path in ipairs(self.paths) do
  238. local fn = path ~= "" and path.."/"..name or name
  239. local fh, err = io.open(fn)
  240. if fh then
  241. local content = fh:read "*a"
  242. info = self:parse(content, name)
  243. fh:close()
  244. return info
  245. end
  246. errors[#errors + 1] = err or fn..": ".."unknown error"
  247. end
  248. if self.import_fallback then
  249. info = self.import_fallback(name)
  250. end
  251. if not info then
  252. error("module load error: "..name.."\n\t"..table.concat(errors, "\n\t"))
  253. end
  254. return info
  255. end
  256. -- parser
  257. local labels = { optional = 1; required = 2; repeated = 3 }
  258. local key_types = {
  259. int32 = 5; int64 = 3; uint32 = 13;
  260. uint64 = 4; sint32 = 17; sint64 = 18;
  261. fixed32 = 7; fixed64 = 6; sfixed32 = 15;
  262. sfixed64 = 16; bool = 8; string = 9;
  263. }
  264. local com_types = {
  265. group = 10; message = 11; enum = 14;
  266. }
  267. local types = {
  268. double = 1; float = 2; int32 = 5;
  269. int64 = 3; uint32 = 13; uint64 = 4;
  270. sint32 = 17; sint64 = 18; fixed32 = 7;
  271. fixed64 = 6; sfixed32 = 15; sfixed64 = 16;
  272. bool = 8; string = 9; bytes = 12;
  273. group = 10; message = 11; enum = 14;
  274. }
  275. local function register_type(self, lex, tname, type)
  276. if not tname:match "%."then
  277. tname = self.prefix..tname
  278. end
  279. if self.typemap[tname] then
  280. return lex:error("type %s already defined", tname)
  281. end
  282. self.typemap[tname] = type
  283. end
  284. local function type_info(lex, tname)
  285. local tenum = types[tname]
  286. if com_types[tname] then
  287. return lex:error("invalid type name: "..tname)
  288. elseif tenum then
  289. tname = nil
  290. end
  291. return tenum, tname
  292. end
  293. local function map_info(lex)
  294. local keyt = lex:ident "key type"
  295. if not key_types[keyt] then
  296. return lex:error("invalid key type: "..keyt)
  297. end
  298. local valt = lex:expected "," :type_name()
  299. local name = lex:expected ">" :ident()
  300. local ident = name:gsub("^%a", string.upper)
  301. :gsub("_(%a)", string.upper).."Entry"
  302. local kt, ktn = type_info(lex, keyt)
  303. local vt, vtn = type_info(lex, valt)
  304. return name, types.message, ident, {
  305. name = ident,
  306. field = {
  307. {
  308. name = "key",
  309. number = 1;
  310. label = labels.optional,
  311. type = kt,
  312. type_name = ktn
  313. },
  314. {
  315. name = "value",
  316. number = 2;
  317. label = labels.optional,
  318. type = vt,
  319. type_name = vtn
  320. },
  321. },
  322. options = { map_entry = true }
  323. }
  324. end
  325. local function inline_option(lex, info)
  326. if lex:test "%[" then
  327. info = info or {}
  328. while true do
  329. local name = lex:option_name()
  330. local value = lex:expected '=' :constant()
  331. info[name] = value
  332. if lex:test "%]" then
  333. return info
  334. end
  335. lex:expected ','
  336. end
  337. end
  338. end
  339. local function field(self, lex, ident)
  340. local name, type, type_name, map_entry
  341. if ident == "map" and lex:test "%<" then
  342. name, type, type_name, map_entry = map_info(lex)
  343. self.locmap[map_entry.field[1]] = lex.pos
  344. self.locmap[map_entry.field[2]] = lex.pos
  345. register_type(self, lex, type_name, types.message)
  346. else
  347. type, type_name = type_info(lex, ident)
  348. name = lex:ident()
  349. end
  350. local info = {
  351. name = name,
  352. number = lex:expected "=":integer(),
  353. label = ident == "map" and labels.repeated or labels.optional,
  354. type = type,
  355. type_name = type_name
  356. }
  357. local options = inline_option(lex)
  358. if options then
  359. info.default_value, options.default = tostring(options.default), nil
  360. info.json_name, options.json_name = options.json_name, nil
  361. if options.packed and options.packed == "false" then
  362. options.packed = false
  363. end
  364. end
  365. info.options = options
  366. if info.number <= 0 then
  367. lex:error("invalid tag number: "..info.number)
  368. end
  369. return info, map_entry
  370. end
  371. local function label_field(self, lex, ident)
  372. local label = labels[ident]
  373. local info, map_entry
  374. if not label then
  375. if self.syntax == "proto2" and ident ~= "map" then
  376. return lex:error("proto2 disallow missing label")
  377. end
  378. return field(self, lex, ident)
  379. end
  380. if label == labels.optional and self.syntax == "proto3" then
  381. return lex:error("proto3 disallow 'optional' label")
  382. end
  383. info, map_entry = field(self, lex, lex:type_name())
  384. info.label = label
  385. return info, map_entry
  386. end
  387. local toplevel = {} do
  388. function toplevel:package(lex, info)
  389. local package = lex:full_ident 'package name'
  390. lex:line_end()
  391. info.package = package
  392. self.prefix = "."..package.."."
  393. return self
  394. end
  395. function toplevel:import(lex, info)
  396. local mode = lex:ident('"weak" or "public"', 'opt') or "public"
  397. if mode ~= 'weak' and mode ~= 'public' then
  398. return lex:error '"weak or "public" expected'
  399. end
  400. local name = lex:quote()
  401. lex:line_end()
  402. local result = self:parsefile(name)
  403. if self.on_import then
  404. self.on_import(result)
  405. end
  406. local dep = default(info, 'dependency')
  407. local index = #dep
  408. dep[index+1] = name
  409. if mode == "public" then
  410. local it = default(info, 'public_dependency')
  411. it[#it+1] = index
  412. else
  413. local it = default(info, 'weak_dependency')
  414. it[#it+1] = index
  415. end
  416. end
  417. local msg_body = {} do
  418. function msg_body:message(lex, info)
  419. local nested_type = default(info, 'nested_type')
  420. nested_type[#nested_type+1] = toplevel.message(self, lex)
  421. return self
  422. end
  423. function msg_body:enum(lex, info)
  424. local nested_type = default(info, 'enum_type')
  425. nested_type[#nested_type+1] = toplevel.enum(self, lex)
  426. return self
  427. end
  428. function msg_body:extend(lex, info)
  429. local extension = default(info, 'extension')
  430. local nested_type = default(info, 'nested_type')
  431. local ft, mt = toplevel.extend(self, lex, {})
  432. for _, v in ipairs(ft) do
  433. extension[#extension+1] = v
  434. end
  435. for _, v in ipairs(mt) do
  436. nested_type[#nested_type+1] = v
  437. end
  438. return self
  439. end
  440. function msg_body:extensions(lex, info)
  441. local rt = default(info, 'extension_range')
  442. repeat
  443. local start = lex:integer "field number range"
  444. local stop = math.floor(2^29)
  445. lex:keyword 'to'
  446. if not lex:keyword('max', 'opt') then
  447. stop = lex:integer "field number range end or 'max'"
  448. end
  449. rt[#rt+1] = { start = start, ['end'] = stop }
  450. until not lex:test ','
  451. lex:line_end()
  452. return self
  453. end
  454. function msg_body:reserved(lex, info)
  455. if lex:test '%a' then
  456. local rt = default(info, 'reserved_name')
  457. repeat
  458. rt[#rt+1] = lex:ident 'field name'
  459. until not lex:test ','
  460. else
  461. local rt = default(info, 'reserved_range')
  462. local first = true
  463. repeat
  464. local start = lex:integer(first and 'field name or number range'
  465. or 'field number range')
  466. if lex:keyword('to', 'opt') then
  467. local stop = lex:integer 'field number range end'
  468. rt[#rt+1] = { start = start, ['end'] = stop }
  469. else
  470. rt[#rt+1] = { start = start, ['end'] = start }
  471. end
  472. first = false
  473. until not lex:test ','
  474. end
  475. lex:line_end()
  476. return self
  477. end
  478. function msg_body:oneof(lex, info)
  479. local fs = default(info, "field")
  480. local ts = default(info, "nested_type")
  481. local ot = default(info, "oneof_decl")
  482. local index = #ot + 1
  483. local oneof = { name = lex:ident() }
  484. lex:expected "{"
  485. while not lex:test "}" do
  486. local ident = lex:type_name()
  487. if ident == "option" then
  488. toplevel.option(self, lex, oneof)
  489. else
  490. local f, t = field(self, lex, ident, "no_label")
  491. self.locmap[f] = lex.pos
  492. if t then ts[#ts+1] = t end
  493. f.oneof_index = index - 1
  494. fs[#fs+1] = f
  495. end
  496. lex:line_end 'opt'
  497. end
  498. ot[index] = oneof
  499. end
  500. end
  501. function toplevel:message(lex, info)
  502. local name = lex:ident 'message name'
  503. local type = { name = name }
  504. register_type(self, lex, name, types.message)
  505. local prefix = self.prefix
  506. self.prefix = prefix..name.."."
  507. lex:expected "{"
  508. while not lex:test "}" do
  509. local ident, pos = lex:type_name()
  510. local body_parser = msg_body[ident]
  511. if body_parser then
  512. body_parser(self, lex, type)
  513. else
  514. local fs = default(type, 'field')
  515. local f, t = label_field(self, lex, ident)
  516. self.locmap[f] = pos
  517. fs[#fs+1] = f
  518. if t then
  519. local ts = default(type, 'nested_type')
  520. ts[#ts+1] = t
  521. end
  522. end
  523. lex:line_end 'opt'
  524. end
  525. lex:line_end 'opt'
  526. if info then
  527. info = default(info, 'message_type')
  528. info[#info+1] = type
  529. end
  530. self.prefix = prefix
  531. return type
  532. end
  533. function toplevel:enum(lex, info)
  534. local name = lex:ident 'enum name'
  535. local enum = { name = name }
  536. register_type(self, lex, name, types.enum)
  537. lex:expected "{"
  538. while not lex:test "}" do
  539. local ident = lex:ident 'enum constant name'
  540. if ident == 'option' then
  541. toplevel.option(self, lex, default(enum, 'options'))
  542. else
  543. local values = default(enum, 'value')
  544. local number = lex:expected '=' :integer()
  545. lex:line_end()
  546. values[#values+1] = {
  547. name = ident,
  548. number = number,
  549. options = inline_option(lex)
  550. }
  551. end
  552. lex:line_end 'opt'
  553. end
  554. lex:line_end 'opt'
  555. if info then
  556. info = default(info, 'enum_type')
  557. info[#info+1] = enum
  558. end
  559. return enum
  560. end
  561. function toplevel:option(lex, info)
  562. local ident = lex:option_name()
  563. lex:expected "="
  564. local value = lex:constant()
  565. lex:line_end()
  566. local options = info and default(info, 'options') or {}
  567. options[ident] = value
  568. return options, self
  569. end
  570. function toplevel:extend(lex, info)
  571. local name = lex:type_name()
  572. local ft = info and default(info, 'extension') or {}
  573. local mt = info and default(info, 'message_type') or {}
  574. lex:expected "{"
  575. while not lex:test "}" do
  576. local ident, pos = lex:type_name()
  577. local f, t = label_field(self, lex, ident)
  578. self.locmap[f] = pos
  579. f.extendee = name
  580. ft[#ft+1] = f
  581. mt[#mt+1] = t
  582. lex:line_end 'opt'
  583. end
  584. return ft, mt
  585. end
  586. local svr_body = {} do
  587. function svr_body:rpc(lex, info)
  588. local name, pos = lex:ident "rpc name"
  589. local rpc = { name = name }
  590. self.locmap[rpc] = pos
  591. local _, tn
  592. lex:expected "%("
  593. rpc.client_stream = lex:keyword("stream", "opt")
  594. _, tn = type_info(lex, lex:type_name())
  595. if not tn then return lex:error "rpc input type must by message" end
  596. rpc.input_type = tn
  597. lex:expected "%)" :expected "returns" :expected "%("
  598. rpc.server_stream = lex:keyword("stream", "opt")
  599. _, tn = type_info(lex, lex:type_name())
  600. if not tn then return lex:error "rpc output type must by message" end
  601. rpc.output_type = tn
  602. lex:expected "%)"
  603. if lex:test "{" then
  604. while not lex:test "}" do
  605. lex:line_end "opt"
  606. lex:keyword "option"
  607. toplevel.option(self, lex, default(rpc, 'options'))
  608. end
  609. end
  610. lex:line_end "opt"
  611. local t = default(info, "method")
  612. t[#t+1] = rpc
  613. end
  614. function svr_body.stream(_, lex)
  615. lex:error "stream not implement yet"
  616. end
  617. end
  618. function toplevel:service(lex, info)
  619. local name = lex:ident 'service name'
  620. local svr = { name = name }
  621. lex:expected "{"
  622. while not lex:test "}" do
  623. local ident = lex:type_name()
  624. local body_parser = svr_body[ident]
  625. if body_parser then
  626. body_parser(self, lex, svr)
  627. else
  628. return lex:error "expected 'rpc' or 'option' in service body"
  629. end
  630. lex:line_end 'opt'
  631. end
  632. lex:line_end 'opt'
  633. if info then
  634. info = default(info, 'service')
  635. info[#info+1] = svr
  636. end
  637. return svr
  638. end
  639. end
  640. local function make_context(self, lex)
  641. local ctx = {
  642. syntax = "proto2";
  643. locmap = {};
  644. prefix = ".";
  645. lex = lex;
  646. parser = self;
  647. }
  648. ctx.loaded = self.loaded
  649. ctx.typemap = self.typemap
  650. ctx.paths = self.paths
  651. function ctx.import_fallback(import_name)
  652. if self.unknown_import == true then
  653. return true
  654. elseif type(self.unknown_import) == 'string' then
  655. return import_name:match(self.unknown_import) and true or nil
  656. elseif self.unknown_import then
  657. return self:unknown_import(import_name)
  658. end
  659. end
  660. function ctx.type_fallback(type_name)
  661. if self.unknown_type == true then
  662. return true
  663. elseif type(self.unknown_type) == 'string' then
  664. return type_name:match(self.unknown_type) and true
  665. elseif self.unknown_type then
  666. return self:unknown_type(type_name)
  667. end
  668. end
  669. function ctx.on_import(info)
  670. if self.on_import then
  671. return self.on_import(info)
  672. end
  673. end
  674. return setmetatable(ctx, Parser)
  675. end
  676. function Parser:parse(src, name)
  677. local loaded = self.loaded[name]
  678. if loaded then
  679. if loaded == true then
  680. error("loop loaded: "..name)
  681. end
  682. return loaded
  683. end
  684. name = name or "<input>"
  685. self.loaded[name] = true
  686. local lex = Lexer.new(name, src)
  687. local info = { name = lex.name }
  688. local ctx = make_context(self, lex)
  689. local syntax = lex:keyword('syntax', 'opt')
  690. if syntax then
  691. info.syntax = lex:expected '=' :quote()
  692. ctx.syntax = info.syntax
  693. lex:line_end()
  694. end
  695. while not lex:eof() do
  696. local ident = lex:ident()
  697. local top_parser = toplevel[ident]
  698. if top_parser then
  699. top_parser(ctx, lex, info)
  700. else
  701. lex:error("unknown keyword '"..ident.."'")
  702. end
  703. lex:line_end "opt"
  704. end
  705. self.loaded[name] = name ~= "<input>" and info or nil
  706. return ctx:resolve(lex, info)
  707. end
  708. -- resolver
  709. local function empty() end
  710. local function iter(t, k)
  711. local v = t[k]
  712. if v then return ipairs(v) end
  713. return empty
  714. end
  715. local function check_dup(self, lex, type, map, k, v)
  716. local old = map[v[k]]
  717. if old then
  718. local ln, co = lex:pos2loc(self.locmap[old])
  719. lex:error("%s '%s' exists, previous at %d:%d",
  720. type, v[k], ln, co)
  721. end
  722. map[v[k]] = v
  723. end
  724. local function check_type(self, lex, tname)
  725. if tname:match "^%." then
  726. local t = self.typemap[tname]
  727. if not t then
  728. return lex:error("unknown type '%s'", tname)
  729. end
  730. return t, tname
  731. end
  732. local prefix = self.prefix
  733. for i = #prefix+1, 1, -1 do
  734. local op = prefix[i]
  735. prefix[i] = tname
  736. local tn = table.concat(prefix, ".", 1, i)
  737. prefix[i] = op
  738. local t = self.typemap[tn]
  739. if t then return t, tn end
  740. end
  741. local tn, t
  742. if self.type_fallback then
  743. tn, t = self.type_fallback(tname)
  744. end
  745. if tn then
  746. t = types[t or "message"]
  747. if tn == true then tn = "."..tname end
  748. return t, tn
  749. end
  750. return lex:error("unknown type '%s'", tname)
  751. end
  752. local function check_field(self, lex, info)
  753. if info.extendee then
  754. local t, tn = check_type(self, lex, info.extendee)
  755. if t ~= types.message then
  756. lex:error("message type expected in extension")
  757. end
  758. info.extendee = tn
  759. end
  760. if info.type_name then
  761. local t, tn = check_type(self, lex, info.type_name)
  762. info.type = t
  763. info.type_name = tn
  764. end
  765. end
  766. local function check_enum(self, lex, info)
  767. local names, numbers = {}, {}
  768. for _, v in iter(info, 'value') do
  769. lex.pos = self.locmap[v]
  770. check_dup(self, lex, 'enum name', names, 'name', v)
  771. if not (info.options
  772. and info.options.options
  773. and info.options.options.allow_alias) then
  774. check_dup(self, lex, 'enum number', numbers, 'number', v)
  775. end
  776. end
  777. end
  778. local function check_message(self, lex, info)
  779. self.prefix[#self.prefix+1] = info.name
  780. local names, numbers = {}, {}
  781. for _, v in iter(info, 'field') do
  782. lex.pos = assert(self.locmap[v])
  783. check_dup(self, lex, 'field name', names, 'name', v)
  784. check_dup(self, lex, 'field number', numbers, 'number', v)
  785. check_field(self, lex, v)
  786. end
  787. for _, v in iter(info, 'nested_type') do
  788. check_message(self, lex, v)
  789. end
  790. for _, v in iter(info, 'extension') do
  791. lex.pos = assert(self.locmap[v])
  792. check_field(self, lex, v)
  793. end
  794. self.prefix[#self.prefix] = nil
  795. end
  796. local function check_service(self, lex, info)
  797. local names = {}
  798. for _, v in iter(info, 'method') do
  799. lex.pos = self.locmap[v]
  800. check_dup(self, lex, 'rpc name', names, 'name', v)
  801. local t, tn = check_type(self, lex, v.input_type)
  802. v.input_type = tn
  803. if t ~= types.message then
  804. lex:error "message type expected in parameter"
  805. end
  806. t, tn = check_type(self, lex, v.output_type)
  807. v.output_type = tn
  808. if t ~= types.message then
  809. lex:error "message type expected in return"
  810. end
  811. end
  812. end
  813. function Parser:resolve(lex, info)
  814. self.prefix = { "", info.package }
  815. for _, v in iter(info, 'message_type') do
  816. check_message(self, lex, v)
  817. end
  818. for _, v in iter(info, 'enum_type') do
  819. check_enum(self, lex, v)
  820. end
  821. for _, v in iter(info, 'service') do
  822. check_service(self, lex, v)
  823. end
  824. for _, v in iter(info, 'extension') do
  825. lex.pos = assert(self.locmap[v])
  826. check_field(self, lex, v)
  827. end
  828. self.prefix = nil
  829. return info
  830. end
  831. end
  832. local has_pb, pb = pcall(require, "pb") do
  833. if has_pb then
  834. local descriptor_pb =
  835. "\10\249#\10\16descriptor.proto\18\15google.protobuf\"G\10\17FileDescript"..
  836. "orSet\0182\10\4file\24\1 \3(\0112$.google.protobuf.FileDescriptorProto\""..
  837. "\219\3\10\19FileDescriptorProto\18\12\10\4name\24\1 \1(\9\18\15\10\7pack"..
  838. "age\24\2 \1(\9\18\18\10\10dependency\24\3 \3(\9\18\25\10\17public_depend"..
  839. "ency\24\10 \3(\5\18\23\10\15weak_dependency\24\11 \3(\5\0186\10\12messag"..
  840. "e_type\24\4 \3(\0112 .google.protobuf.DescriptorProto\0187\10\9enum_type"..
  841. "\24\5 \3(\0112$.google.protobuf.EnumDescriptorProto\0188\10\7service\24"..
  842. "\6 \3(\0112'.google.protobuf.ServiceDescriptorProto\0188\10\9extension"..
  843. "\24\7 \3(\0112%.google.protobuf.FieldDescriptorProto\18-\10\7options\24"..
  844. "\8 \1(\0112\28.google.protobuf.FileOptions\0189\10\16source_code_info\24"..
  845. "\9 \1(\0112\31.google.protobuf.SourceCodeInfo\18\14\10\6syntax\24\12 \1("..
  846. "\9\"\228\3\10\15DescriptorProto\18\12\10\4name\24\1 \1(\9\0184\10\5field"..
  847. "\24\2 \3(\0112%.google.protobuf.FieldDescriptorProto\0188\10\9extension"..
  848. "\24\6 \3(\0112%.google.protobuf.FieldDescriptorProto\0185\10\11nested_ty"..
  849. "pe\24\3 \3(\0112 .google.protobuf.DescriptorProto\0187\10\9enum_type\24"..
  850. "\4 \3(\0112$.google.protobuf.EnumDescriptorProto\18H\10\15extension_rang"..
  851. "e\24\5 \3(\0112/.google.protobuf.DescriptorProto.ExtensionRange\0189\10"..
  852. "\10oneof_decl\24\8 \3(\0112%.google.protobuf.OneofDescriptorProto\0180"..
  853. "\10\7options\24\7 \1(\0112\31.google.protobuf.MessageOptions\26,\10\14Ex"..
  854. "tensionRange\18\13\10\5start\24\1 \1(\5\18\11\10\3end\24\2 \1(\5\"\169\5"..
  855. "\10\20FieldDescriptorProto\18\12\10\4name\24\1 \1(\9\18\14\10\6number\24"..
  856. "\3 \1(\5\18:\10\5label\24\4 \1(\0142+.google.protobuf.FieldDescriptorPro"..
  857. "to.Label\0188\10\4type\24\5 \1(\0142*.google.protobuf.FieldDescriptorPro"..
  858. "to.Type\18\17\10\9type_name\24\6 \1(\9\18\16\10\8extendee\24\2 \1(\9\18"..
  859. "\21\10\13default_value\24\7 \1(\9\18\19\10\11oneof_index\24\9 \1(\5\18."..
  860. "\10\7options\24\8 \1(\0112\29.google.protobuf.FieldOptions\"\182\2\10\4T"..
  861. "ype\18\15\10\11TYPE_DOUBLE\16\1\18\14\10\10TYPE_FLOAT\16\2\18\14\10\10TY"..
  862. "PE_INT64\16\3\18\15\10\11TYPE_UINT64\16\4\18\14\10\10TYPE_INT32\16\5\18"..
  863. "\16\10\12TYPE_FIXED64\16\6\18\16\10\12TYPE_FIXED32\16\7\18\13\10\9TYPE_B"..
  864. "OOL\16\8\18\15\10\11TYPE_STRING\16\9\18\14\10\10TYPE_GROUP\16\10\18\16"..
  865. "\10\12TYPE_MESSAGE\16\11\18\14\10\10TYPE_BYTES\16\12\18\15\10\11TYPE_UIN"..
  866. "T32\16\13\18\13\10\9TYPE_ENUM\16\14\18\17\10\13TYPE_SFIXED32\16\15\18\17"..
  867. "\10\13TYPE_SFIXED64\16\16\18\15\10\11TYPE_SINT32\16\17\18\15\10\11TYPE_S"..
  868. "INT64\16\18\"C\10\5Label\18\18\10\14LABEL_OPTIONAL\16\1\18\18\10\14LABEL"..
  869. "_REQUIRED\16\2\18\18\10\14LABEL_REPEATED\16\3\"$\10\20OneofDescriptorPro"..
  870. "to\18\12\10\4name\24\1 \1(\9\"\140\1\10\19EnumDescriptorProto\18\12\10\4"..
  871. "name\24\1 \1(\9\0188\10\5value\24\2 \3(\0112).google.protobuf.EnumValueD"..
  872. "escriptorProto\18-\10\7options\24\3 \1(\0112\28.google.protobuf.EnumOpti"..
  873. "ons\"l\10\24EnumValueDescriptorProto\18\12\10\4name\24\1 \1(\9\18\14\10"..
  874. "\6number\24\2 \1(\5\0182\10\7options\24\3 \1(\0112!.google.protobuf.Enum"..
  875. "ValueOptions\"\144\1\10\22ServiceDescriptorProto\18\12\10\4name\24\1 \1("..
  876. "\9\0186\10\6method\24\2 \3(\0112&.google.protobuf.MethodDescriptorProto"..
  877. "\0180\10\7options\24\3 \1(\0112\31.google.protobuf.ServiceOptions\"\193"..
  878. "\1\10\21MethodDescriptorProto\18\12\10\4name\24\1 \1(\9\18\18\10\10input"..
  879. "_type\24\2 \1(\9\18\19\10\11output_type\24\3 \1(\9\18/\10\7options\24\4 "..
  880. "\1(\0112\30.google.protobuf.MethodOptions\18\31\10\16client_streaming\24"..
  881. "\5 \1(\8:\5false\18\31\10\16server_streaming\24\6 \1(\8:\5false\"\231\4"..
  882. "\10\11FileOptions\18\20\10\12java_package\24\1 \1(\9\18\28\10\20java_out"..
  883. "er_classname\24\8 \1(\9\18\"\10\19java_multiple_files\24\10 \1(\8:\5fals"..
  884. "e\18,\10\29java_generate_equals_and_hash\24\20 \1(\8:\5false\18%\10\22ja"..
  885. "va_string_check_utf8\24\27 \1(\8:\5false\18F\10\12optimize_for\24\9 \1("..
  886. "\0142).google.protobuf.FileOptions.OptimizeMode:\5SPEED\18\18\10\10go_pa"..
  887. "ckage\24\11 \1(\9\18\"\10\19cc_generic_services\24\16 \1(\8:\5false\18$"..
  888. "\10\21java_generic_services\24\17 \1(\8:\5false\18\"\10\19py_generic_ser"..
  889. "vices\24\18 \1(\8:\5false\18\25\10\10deprecated\24\23 \1(\8:\5false\18"..
  890. "\31\10\16cc_enable_arenas\24\31 \1(\8:\5false\18\25\10\17objc_class_pref"..
  891. "ix\24$ \1(\9\18C\10\20uninterpreted_option\24\231\7 \3(\0112$.google.pro"..
  892. "tobuf.UninterpretedOption\":\10\12OptimizeMode\18\9\10\5SPEED\16\1\18\13"..
  893. "\10\9CODE_SIZE\16\2\18\16\10\12LITE_RUNTIME\16\3*\9\8\232\7\16\128\128"..
  894. "\128\128\2\"\230\1\10\14MessageOptions\18&\10\23message_set_wire_format"..
  895. "\24\1 \1(\8:\5false\18.\10\31no_standard_descriptor_accessor\24\2 \1(\8:"..
  896. "\5false\18\25\10\10deprecated\24\3 \1(\8:\5false\18\17\10\9map_entry\24"..
  897. "\7 \1(\8\18C\10\20uninterpreted_option\24\231\7 \3(\0112$.google.protobu"..
  898. "f.UninterpretedOption*\9\8\232\7\16\128\128\128\128\2\"\160\2\10\12Field"..
  899. "Options\18:\10\5ctype\24\1 \1(\0142#.google.protobuf.FieldOptions.CType:"..
  900. "\6STRING\18\14\10\6packed\24\2 \1(\8\18\19\10\4lazy\24\5 \1(\8:\5false"..
  901. "\18\25\10\10deprecated\24\3 \1(\8:\5false\18\19\10\4weak\24\10 \1(\8:\5f"..
  902. "alse\18C\10\20uninterpreted_option\24\231\7 \3(\0112$.google.protobuf.Un"..
  903. "interpretedOption\"/\10\5CType\18\10\10\6STRING\16\0\18\8\10\4CORD\16\1"..
  904. "\18\16\10\12STRING_PIECE\16\2*\9\8\232\7\16\128\128\128\128\2\"\141\1\10"..
  905. "\11EnumOptions\18\19\10\11allow_alias\24\2 \1(\8\18\25\10\10deprecated"..
  906. "\24\3 \1(\8:\5false\18C\10\20uninterpreted_option\24\231\7 \3(\0112$.goo"..
  907. "gle.protobuf.UninterpretedOption*\9\8\232\7\16\128\128\128\128\2\"}\10"..
  908. "\16EnumValueOptions\18\25\10\10deprecated\24\1 \1(\8:\5false\18C\10\20un"..
  909. "interpreted_option\24\231\7 \3(\0112$.google.protobuf.UninterpretedOptio"..
  910. "n*\9\8\232\7\16\128\128\128\128\2\"{\10\14ServiceOptions\18\25\10\10depr"..
  911. "ecated\24! \1(\8:\5false\18C\10\20uninterpreted_option\24\231\7 \3(\0112"..
  912. "$.google.protobuf.UninterpretedOption*\9\8\232\7\16\128\128\128\128\2\"z"..
  913. "\10\13MethodOptions\18\25\10\10deprecated\24! \1(\8:\5false\18C\10\20uni"..
  914. "nterpreted_option\24\231\7 \3(\0112$.google.protobuf.UninterpretedOption"..
  915. "*\9\8\232\7\16\128\128\128\128\2\"\158\2\10\19UninterpretedOption\18;\10"..
  916. "\4name\24\2 \3(\0112-.google.protobuf.UninterpretedOption.NamePart\18\24"..
  917. "\10\16identifier_value\24\3 \1(\9\18\26\10\18positive_int_value\24\4 \1("..
  918. "\4\18\26\10\18negative_int_value\24\5 \1(\3\18\20\10\12double_value\24\6"..
  919. "\32\1(\1\18\20\10\12string_value\24\7 \1(\12\18\23\10\15aggregate_value"..
  920. "\24\8 \1(\9\0263\10\8NamePart\18\17\10\9name_part\24\1 \2(\9\18\20\10\12"..
  921. "is_extension\24\2 \2(\8\"\213\1\10\14SourceCodeInfo\18:\10\8location\24"..
  922. "\1 \3(\0112(.google.protobuf.SourceCodeInfo.Location\26\134\1\10\8Locati"..
  923. "on\18\16\10\4path\24\1 \3(\5B\2\16\1\18\16\10\4span\24\2 \3(\5B\2\16\1"..
  924. "\18\24\10\16leading_comments\24\3 \1(\9\18\25\10\17trailing_comments\24"..
  925. "\4 \1(\9\18!\10\25leading_detached_comments\24\6 \3(\9B)\10\19com.google"..
  926. ".protobufB\16DescriptorProtosH\1"
  927. function Parser.reload()
  928. assert(pb.load(descriptor_pb))
  929. end
  930. local function do_compile(self, f, ...)
  931. if self.include_imports then
  932. local old = self.on_import
  933. local infos = {}
  934. function self.on_import(info)
  935. infos[#infos+1] = info
  936. end
  937. local r = f(...)
  938. infos[#infos+1] = r
  939. self.on_import = old
  940. return { file = infos }
  941. end
  942. return { file = { f(...) } }
  943. end
  944. function Parser:compile(s, name)
  945. local set = do_compile(self, self.parse, self, s, name)
  946. return pb.encode('.google.protobuf.FileDescriptorSet', set)
  947. end
  948. function Parser:compilefile(fn)
  949. local set = do_compile(self, self.parsefile, self, fn)
  950. return pb.encode('.google.protobuf.FileDescriptorSet', set)
  951. end
  952. function Parser:load(s, name)
  953. local ret, pos = pb.load(self:compile(s, name))
  954. if ret then return ret, pos end
  955. error("load failed at offset "..pos)
  956. end
  957. function Parser:loadfile(fn)
  958. local ret, pos = pb.load(self:compilefile(fn))
  959. if ret then return ret, pos end
  960. error("load failed at offset "..pos)
  961. end
  962. Parser.reload()
  963. end
  964. end
  965. return Parser