protobuf.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: http://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. ];
  21. var keywords = wordRegexp(keywordArray);
  22. CodeMirror.registerHelper("hintWords", "protobuf", keywordArray);
  23. var identifiers = new RegExp("^[_A-Za-z\xa1-\uffff][_A-Za-z0-9\xa1-\uffff]*");
  24. function tokenBase(stream) {
  25. // whitespaces
  26. if (stream.eatSpace()) return null;
  27. // Handle one line Comments
  28. if (stream.match("//")) {
  29. stream.skipToEnd();
  30. return "comment";
  31. }
  32. // Handle Number Literals
  33. if (stream.match(/^[0-9\.+-]/, false)) {
  34. if (stream.match(/^[+-]?0x[0-9a-fA-F]+/))
  35. return "number";
  36. if (stream.match(/^[+-]?\d*\.\d+([EeDd][+-]?\d+)?/))
  37. return "number";
  38. if (stream.match(/^[+-]?\d+([EeDd][+-]?\d+)?/))
  39. return "number";
  40. }
  41. // Handle Strings
  42. if (stream.match(/^"([^"]|(""))*"/)) { return "string"; }
  43. if (stream.match(/^'([^']|(''))*'/)) { return "string"; }
  44. // Handle words
  45. if (stream.match(keywords)) { return "keyword"; }
  46. if (stream.match(identifiers)) { return "variable"; } ;
  47. // Handle non-detected items
  48. stream.next();
  49. return null;
  50. };
  51. CodeMirror.defineMode("protobuf", function() {
  52. return {token: tokenBase};
  53. });
  54. CodeMirror.defineMIME("text/x-protobuf", "protobuf");
  55. });