Skip to content

Commit 72d4770

Browse files
tristantarrantmarijnh
authored andcommitted
[protobuf mode] Add
Closes codemirror#3861
1 parent d867743 commit 72d4770

File tree

2 files changed

+136
-0
lines changed

2 files changed

+136
-0
lines changed

mode/protobuf/index.html

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<!doctype html>
2+
3+
<title>CodeMirror: ProtoBuf mode</title>
4+
<meta charset="utf-8"/>
5+
<link rel=stylesheet href="../../doc/docs.css">
6+
7+
<link rel="stylesheet" href="../../lib/codemirror.css">
8+
<script src="../../lib/codemirror.js"></script>
9+
<script src="protobuf.js"></script>
10+
<style>.CodeMirror { border-top: 1px solid #ddd; border-bottom: 1px solid #ddd; }</style>
11+
<div id=nav>
12+
<a href="http://codemirror.net"><h1>CodeMirror</h1><img id=logo src="../../doc/logo.png"></a>
13+
14+
<ul>
15+
<li><a href="../../index.html">Home</a>
16+
<li><a href="../../doc/manual.html">Manual</a>
17+
<li><a href="https://github.com/codemirror/codemirror">Code</a>
18+
</ul>
19+
<ul>
20+
<li><a href="../index.html">Language modes</a>
21+
<li><a class=active href="#">ProtoBuf</a>
22+
</ul>
23+
</div>
24+
25+
<article>
26+
<h2>ProtoBuf mode</h2>
27+
<form><textarea id="code" name="code">
28+
package addressbook;
29+
30+
message Address {
31+
required string street = 1;
32+
required string postCode = 2;
33+
}
34+
35+
message PhoneNumber {
36+
required string number = 1;
37+
}
38+
39+
message Person {
40+
optional int32 id = 1;
41+
required string name = 2;
42+
required string surname = 3;
43+
optional Address address = 4;
44+
repeated PhoneNumber phoneNumbers = 5;
45+
optional uint32 age = 6;
46+
repeated uint32 favouriteNumbers = 7;
47+
optional string license = 8;
48+
enum Gender {
49+
MALE = 0;
50+
FEMALE = 1;
51+
}
52+
optional Gender gender = 9;
53+
optional fixed64 lastUpdate = 10;
54+
required bool deleted = 11 [default = false];
55+
}
56+
57+
</textarea></form>
58+
<script>
59+
var editor = CodeMirror.fromTextArea(document.getElementById("code"), {});
60+
</script>
61+
62+
<p><strong>MIME types defined:</strong> <code>text/x-protobuf</code>.</p>
63+
64+
</article>

mode/protobuf/protobuf.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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

Comments
 (0)