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