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