protobuf.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://codemirror.net/LICENSE
  3. (function(mod) {
  4. if (typeof exports == "object" && typeof module == "object") // CommonJS
  5. mod(require("../../lib/codemirror"));
  6. else if (typeof define == "function" && define.amd) // AMD
  7. define(["../../lib/codemirror"], mod);
  8. else // Plain browser env
  9. mod(CodeMirror);
  10. })(function(CodeMirror) {
  11. "use strict";
  12. function wordRegexp(words) {
  13. return new RegExp("^((" + words.join(")|(") + "))\\b", "i");
  14. };
  15. var keywordArray = [
  16. "package", "message", "import", "syntax",
  17. "required", "optional", "repeated", "reserved", "default", "extensions", "packed",
  18. "bool", "bytes", "double", "enum", "float", "string",
  19. "int32", "int64", "uint32", "uint64", "sint32", "sint64", "fixed32", "fixed64", "sfixed32", "sfixed64",
  20. "option", "service", "rpc", "returns"
  21. ];
  22. var keywords = wordRegexp(keywordArray);
  23. CodeMirror.registerHelper("hintWords", "protobuf", keywordArray);
  24. var identifiers = new RegExp("^[_A-Za-z\xa1-\uffff][_A-Za-z0-9\xa1-\uffff]*");
  25. function tokenBase(stream) {
  26. // whitespaces
  27. if (stream.eatSpace()) return null;
  28. // Handle one line Comments
  29. if (stream.match("//")) {
  30. stream.skipToEnd();
  31. return "comment";
  32. }
  33. // Handle Number Literals
  34. if (stream.match(/^[0-9\.+-]/, false)) {
  35. if (stream.match(/^[+-]?0x[0-9a-fA-F]+/))
  36. return "number";
  37. if (stream.match(/^[+-]?\d*\.\d+([EeDd][+-]?\d+)?/))
  38. return "number";
  39. if (stream.match(/^[+-]?\d+([EeDd][+-]?\d+)?/))
  40. return "number";
  41. }
  42. // Handle Strings
  43. if (stream.match(/^"([^"]|(""))*"/)) { return "string"; }
  44. if (stream.match(/^'([^']|(''))*'/)) { return "string"; }
  45. // Handle words
  46. if (stream.match(keywords)) { return "keyword"; }
  47. if (stream.match(identifiers)) { return "variable"; } ;
  48. // Handle non-detected items
  49. stream.next();
  50. return null;
  51. };
  52. CodeMirror.defineMode("protobuf", function() {
  53. return {
  54. token: tokenBase,
  55. fold: "brace"
  56. };
  57. });
  58. CodeMirror.defineMIME("text/x-protobuf", "protobuf");
  59. });