protobuf.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /*!-----------------------------------------------------------------------------
  2. * Copyright (c) Microsoft Corporation. All rights reserved.
  3. * Version: 0.31.1(337587859b1c171314b40503171188b6cea6a32a)
  4. * Released under the MIT license
  5. * https://github.com/microsoft/monaco-editor/blob/main/LICENSE.txt
  6. *-----------------------------------------------------------------------------*/
  7. // src/basic-languages/protobuf/protobuf.ts
  8. var namedLiterals = ["true", "false"];
  9. var conf = {
  10. comments: {
  11. lineComment: "//",
  12. blockComment: ["/*", "*/"]
  13. },
  14. brackets: [
  15. ["{", "}"],
  16. ["[", "]"],
  17. ["(", ")"],
  18. ["<", ">"]
  19. ],
  20. surroundingPairs: [
  21. { open: "{", close: "}" },
  22. { open: "[", close: "]" },
  23. { open: "(", close: ")" },
  24. { open: "<", close: ">" },
  25. { open: '"', close: '"' },
  26. { open: "'", close: "'" }
  27. ],
  28. autoClosingPairs: [
  29. { open: "{", close: "}" },
  30. { open: "[", close: "]" },
  31. { open: "(", close: ")" },
  32. { open: "<", close: ">" },
  33. { open: '"', close: '"', notIn: ["string"] },
  34. { open: "'", close: "'", notIn: ["string"] }
  35. ],
  36. autoCloseBefore: ".,=}])>' \n ",
  37. indentationRules: {
  38. increaseIndentPattern: new RegExp("^((?!\\/\\/).)*(\\{[^}\"'`]*|\\([^)\"'`]*|\\[[^\\]\"'`]*)$"),
  39. decreaseIndentPattern: new RegExp("^((?!.*?\\/\\*).*\\*/)?\\s*[\\}\\]].*$")
  40. }
  41. };
  42. var language = {
  43. defaultToken: "",
  44. tokenPostfix: ".proto",
  45. brackets: [
  46. { open: "{", close: "}", token: "delimiter.curly" },
  47. { open: "[", close: "]", token: "delimiter.square" },
  48. { open: "(", close: ")", token: "delimiter.parenthesis" },
  49. { open: "<", close: ">", token: "delimiter.angle" }
  50. ],
  51. symbols: /[=><!~?:&|+\-*/^%]+/,
  52. keywords: [
  53. "syntax",
  54. "import",
  55. "weak",
  56. "public",
  57. "package",
  58. "option",
  59. "repeated",
  60. "oneof",
  61. "map",
  62. "reserved",
  63. "to",
  64. "max",
  65. "enum",
  66. "message",
  67. "service",
  68. "rpc",
  69. "stream",
  70. "returns",
  71. "package",
  72. "optional",
  73. "true",
  74. "false"
  75. ],
  76. builtinTypes: [
  77. "double",
  78. "float",
  79. "int32",
  80. "int64",
  81. "uint32",
  82. "uint64",
  83. "sint32",
  84. "sint64",
  85. "fixed32",
  86. "fixed64",
  87. "sfixed32",
  88. "sfixed64",
  89. "bool",
  90. "string",
  91. "bytes"
  92. ],
  93. operators: ["=", "+", "-"],
  94. namedLiterals,
  95. escapes: `\\\\(u{[0-9A-Fa-f]+}|n|r|t|\\\\|'|\\\${)`,
  96. identifier: /[a-zA-Z]\w*/,
  97. fullIdentifier: /@identifier(?:\s*\.\s*@identifier)*/,
  98. optionName: /(?:@identifier|\(\s*@fullIdentifier\s*\))(?:\s*\.\s*@identifier)*/,
  99. messageName: /@identifier/,
  100. enumName: /@identifier/,
  101. messageType: /\.?\s*(?:@identifier\s*\.\s*)*@messageName/,
  102. enumType: /\.?\s*(?:@identifier\s*\.\s*)*@enumName/,
  103. floatLit: /[0-9]+\s*\.\s*[0-9]*(?:@exponent)?|[0-9]+@exponent|\.[0-9]+(?:@exponent)?/,
  104. exponent: /[eE]\s*[+-]?\s*[0-9]+/,
  105. boolLit: /true\b|false\b/,
  106. decimalLit: /[1-9][0-9]*/,
  107. octalLit: /0[0-7]*/,
  108. hexLit: /0[xX][0-9a-fA-F]+/,
  109. type: /double|float|int32|int64|uint32|uint64|sint32|sint64|fixed32|fixed64|sfixed32|sfixed64|bool|string|bytes|@messageType|@enumType/,
  110. keyType: /int32|int64|uint32|uint64|sint32|sint64|fixed32|fixed64|sfixed32|sfixed64|bool|string/,
  111. tokenizer: {
  112. root: [
  113. { include: "@whitespace" },
  114. [/syntax/, "keyword"],
  115. [/=/, "operators"],
  116. [/;/, "delimiter"],
  117. [
  118. /(")(proto3)(")/,
  119. ["string.quote", "string", { token: "string.quote", switchTo: "@topLevel.proto3" }]
  120. ],
  121. [
  122. /(")(proto2)(")/,
  123. ["string.quote", "string", { token: "string.quote", switchTo: "@topLevel.proto2" }]
  124. ],
  125. [
  126. /.*?/,
  127. { token: "", switchTo: "@topLevel.proto2" }
  128. ]
  129. ],
  130. topLevel: [
  131. { include: "@whitespace" },
  132. { include: "@constant" },
  133. [/=/, "operators"],
  134. [/[;.]/, "delimiter"],
  135. [
  136. /@fullIdentifier/,
  137. {
  138. cases: {
  139. option: { token: "keyword", next: "@option.$S2" },
  140. enum: { token: "keyword", next: "@enumDecl.$S2" },
  141. message: { token: "keyword", next: "@messageDecl.$S2" },
  142. service: { token: "keyword", next: "@serviceDecl.$S2" },
  143. extend: {
  144. cases: {
  145. "$S2==proto2": { token: "keyword", next: "@extendDecl.$S2" }
  146. }
  147. },
  148. "@keywords": "keyword",
  149. "@default": "identifier"
  150. }
  151. }
  152. ]
  153. ],
  154. enumDecl: [
  155. { include: "@whitespace" },
  156. [/@identifier/, "type.identifier"],
  157. [/{/, { token: "@brackets", bracket: "@open", switchTo: "@enumBody.$S2" }]
  158. ],
  159. enumBody: [
  160. { include: "@whitespace" },
  161. { include: "@constant" },
  162. [/=/, "operators"],
  163. [/;/, "delimiter"],
  164. [/option\b/, "keyword", "@option.$S2"],
  165. [/@identifier/, "identifier"],
  166. [/\[/, { token: "@brackets", bracket: "@open", next: "@options.$S2" }],
  167. [/}/, { token: "@brackets", bracket: "@close", next: "@pop" }]
  168. ],
  169. messageDecl: [
  170. { include: "@whitespace" },
  171. [/@identifier/, "type.identifier"],
  172. [/{/, { token: "@brackets", bracket: "@open", switchTo: "@messageBody.$S2" }]
  173. ],
  174. messageBody: [
  175. { include: "@whitespace" },
  176. { include: "@constant" },
  177. [/=/, "operators"],
  178. [/;/, "delimiter"],
  179. [
  180. "(map)(s*)(<)",
  181. ["keyword", "white", { token: "@brackets", bracket: "@open", next: "@map.$S2" }]
  182. ],
  183. [
  184. /@identifier/,
  185. {
  186. cases: {
  187. option: { token: "keyword", next: "@option.$S2" },
  188. enum: { token: "keyword", next: "@enumDecl.$S2" },
  189. message: { token: "keyword", next: "@messageDecl.$S2" },
  190. oneof: { token: "keyword", next: "@oneofDecl.$S2" },
  191. extensions: {
  192. cases: {
  193. "$S2==proto2": { token: "keyword", next: "@reserved.$S2" }
  194. }
  195. },
  196. reserved: { token: "keyword", next: "@reserved.$S2" },
  197. "(?:repeated|optional)": { token: "keyword", next: "@field.$S2" },
  198. required: {
  199. cases: {
  200. "$S2==proto2": { token: "keyword", next: "@field.$S2" }
  201. }
  202. },
  203. "$S2==proto3": { token: "@rematch", next: "@field.$S2" }
  204. }
  205. }
  206. ],
  207. [/\[/, { token: "@brackets", bracket: "@open", next: "@options.$S2" }],
  208. [/}/, { token: "@brackets", bracket: "@close", next: "@pop" }]
  209. ],
  210. extendDecl: [
  211. { include: "@whitespace" },
  212. [/@identifier/, "type.identifier"],
  213. [/{/, { token: "@brackets", bracket: "@open", switchTo: "@extendBody.$S2" }]
  214. ],
  215. extendBody: [
  216. { include: "@whitespace" },
  217. { include: "@constant" },
  218. [/;/, "delimiter"],
  219. [/(?:repeated|optional|required)/, "keyword", "@field.$S2"],
  220. [/\[/, { token: "@brackets", bracket: "@open", next: "@options.$S2" }],
  221. [/}/, { token: "@brackets", bracket: "@close", next: "@pop" }]
  222. ],
  223. options: [
  224. { include: "@whitespace" },
  225. { include: "@constant" },
  226. [/;/, "delimiter"],
  227. [/@optionName/, "annotation"],
  228. [/[()]/, "annotation.brackets"],
  229. [/=/, "operator"],
  230. [/\]/, { token: "@brackets", bracket: "@close", next: "@pop" }]
  231. ],
  232. option: [
  233. { include: "@whitespace" },
  234. [/@optionName/, "annotation"],
  235. [/[()]/, "annotation.brackets"],
  236. [/=/, "operator", "@pop"]
  237. ],
  238. oneofDecl: [
  239. { include: "@whitespace" },
  240. [/@identifier/, "identifier"],
  241. [/{/, { token: "@brackets", bracket: "@open", switchTo: "@oneofBody.$S2" }]
  242. ],
  243. oneofBody: [
  244. { include: "@whitespace" },
  245. { include: "@constant" },
  246. [/;/, "delimiter"],
  247. [/(@identifier)(\s*)(=)/, ["identifier", "white", "delimiter"]],
  248. [
  249. /@fullIdentifier|\./,
  250. {
  251. cases: {
  252. "@builtinTypes": "keyword",
  253. "@default": "type.identifier"
  254. }
  255. }
  256. ],
  257. [/\[/, { token: "@brackets", bracket: "@open", next: "@options.$S2" }],
  258. [/}/, { token: "@brackets", bracket: "@close", next: "@pop" }]
  259. ],
  260. reserved: [
  261. { include: "@whitespace" },
  262. [/,/, "delimiter"],
  263. [/;/, "delimiter", "@pop"],
  264. { include: "@constant" },
  265. [/to\b|max\b/, "keyword"]
  266. ],
  267. map: [
  268. { include: "@whitespace" },
  269. [
  270. /@fullIdentifier|\./,
  271. {
  272. cases: {
  273. "@builtinTypes": "keyword",
  274. "@default": "type.identifier"
  275. }
  276. }
  277. ],
  278. [/,/, "delimiter"],
  279. [/>/, { token: "@brackets", bracket: "@close", switchTo: "identifier" }]
  280. ],
  281. field: [
  282. { include: "@whitespace" },
  283. [
  284. "group",
  285. {
  286. cases: {
  287. "$S2==proto2": { token: "keyword", switchTo: "@groupDecl.$S2" }
  288. }
  289. }
  290. ],
  291. [/(@identifier)(\s*)(=)/, ["identifier", "white", { token: "delimiter", next: "@pop" }]],
  292. [
  293. /@fullIdentifier|\./,
  294. {
  295. cases: {
  296. "@builtinTypes": "keyword",
  297. "@default": "type.identifier"
  298. }
  299. }
  300. ]
  301. ],
  302. groupDecl: [
  303. { include: "@whitespace" },
  304. [/@identifier/, "identifier"],
  305. ["=", "operator"],
  306. [/{/, { token: "@brackets", bracket: "@open", switchTo: "@messageBody.$S2" }],
  307. { include: "@constant" }
  308. ],
  309. type: [
  310. { include: "@whitespace" },
  311. [/@identifier/, "type.identifier", "@pop"],
  312. [/./, "delimiter"]
  313. ],
  314. identifier: [{ include: "@whitespace" }, [/@identifier/, "identifier", "@pop"]],
  315. serviceDecl: [
  316. { include: "@whitespace" },
  317. [/@identifier/, "identifier"],
  318. [/{/, { token: "@brackets", bracket: "@open", switchTo: "@serviceBody.$S2" }]
  319. ],
  320. serviceBody: [
  321. { include: "@whitespace" },
  322. { include: "@constant" },
  323. [/;/, "delimiter"],
  324. [/option\b/, "keyword", "@option.$S2"],
  325. [/rpc\b/, "keyword", "@rpc.$S2"],
  326. [/\[/, { token: "@brackets", bracket: "@open", next: "@options.$S2" }],
  327. [/}/, { token: "@brackets", bracket: "@close", next: "@pop" }]
  328. ],
  329. rpc: [
  330. { include: "@whitespace" },
  331. [/@identifier/, "identifier"],
  332. [/\(/, { token: "@brackets", bracket: "@open", switchTo: "@request.$S2" }],
  333. [/{/, { token: "@brackets", bracket: "@open", next: "@methodOptions.$S2" }],
  334. [/;/, "delimiter", "@pop"]
  335. ],
  336. request: [
  337. { include: "@whitespace" },
  338. [
  339. /@messageType/,
  340. {
  341. cases: {
  342. stream: { token: "keyword", next: "@type.$S2" },
  343. "@default": "type.identifier"
  344. }
  345. }
  346. ],
  347. [/\)/, { token: "@brackets", bracket: "@close", switchTo: "@returns.$S2" }]
  348. ],
  349. returns: [
  350. { include: "@whitespace" },
  351. [/returns\b/, "keyword"],
  352. [/\(/, { token: "@brackets", bracket: "@open", switchTo: "@response.$S2" }]
  353. ],
  354. response: [
  355. { include: "@whitespace" },
  356. [
  357. /@messageType/,
  358. {
  359. cases: {
  360. stream: { token: "keyword", next: "@type.$S2" },
  361. "@default": "type.identifier"
  362. }
  363. }
  364. ],
  365. [/\)/, { token: "@brackets", bracket: "@close", switchTo: "@rpc.$S2" }]
  366. ],
  367. methodOptions: [
  368. { include: "@whitespace" },
  369. { include: "@constant" },
  370. [/;/, "delimiter"],
  371. ["option", "keyword"],
  372. [/@optionName/, "annotation"],
  373. [/[()]/, "annotation.brackets"],
  374. [/=/, "operator"],
  375. [/}/, { token: "@brackets", bracket: "@close", next: "@pop" }]
  376. ],
  377. comment: [
  378. [/[^\/*]+/, "comment"],
  379. [/\/\*/, "comment", "@push"],
  380. ["\\*/", "comment", "@pop"],
  381. [/[\/*]/, "comment"]
  382. ],
  383. string: [
  384. [/[^\\"]+/, "string"],
  385. [/@escapes/, "string.escape"],
  386. [/\\./, "string.escape.invalid"],
  387. [/"/, { token: "string.quote", bracket: "@close", next: "@pop" }]
  388. ],
  389. stringSingle: [
  390. [/[^\\']+/, "string"],
  391. [/@escapes/, "string.escape"],
  392. [/\\./, "string.escape.invalid"],
  393. [/'/, { token: "string.quote", bracket: "@close", next: "@pop" }]
  394. ],
  395. constant: [
  396. ["@boolLit", "keyword.constant"],
  397. ["@hexLit", "number.hex"],
  398. ["@octalLit", "number.octal"],
  399. ["@decimalLit", "number"],
  400. ["@floatLit", "number.float"],
  401. [/("([^"\\]|\\.)*|'([^'\\]|\\.)*)$/, "string.invalid"],
  402. [/"/, { token: "string.quote", bracket: "@open", next: "@string" }],
  403. [/'/, { token: "string.quote", bracket: "@open", next: "@stringSingle" }],
  404. [/{/, { token: "@brackets", bracket: "@open", next: "@prototext" }],
  405. [/identifier/, "identifier"]
  406. ],
  407. whitespace: [
  408. [/[ \t\r\n]+/, "white"],
  409. [/\/\*/, "comment", "@comment"],
  410. [/\/\/.*$/, "comment"]
  411. ],
  412. prototext: [
  413. { include: "@whitespace" },
  414. { include: "@constant" },
  415. [/@identifier/, "identifier"],
  416. [/[:;]/, "delimiter"],
  417. [/}/, { token: "@brackets", bracket: "@close", next: "@pop" }]
  418. ]
  419. }
  420. };
  421. export {
  422. conf,
  423. language
  424. };