Protobuf improvements (#1948)

This makes numerous improvements to the Protobuf grammar adding (almost) full support for PB2 and PB3 syntax.
This commit is contained in:
Michael Schmidt 2019-07-01 19:55:12 +02:00 committed by GitHub
parent 36a5fa0eb8
commit de10bd1d85
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 279 additions and 41 deletions

View File

@ -1,8 +1,36 @@
Prism.languages.protobuf = Prism.languages.extend('clike', {
keyword: /\b(?:package|import|message|enum)\b/,
builtin: /\b(?:required|repeated|optional|reserved)\b/,
primitive: {
pattern: /\b(?:double|float|int32|int64|uint32|uint64|sint32|sint64|fixed32|fixed64|sfixed32|sfixed64|bool|string|bytes)\b/,
alias: 'symbol'
}
});
(function (Prism) {
var builtinTypes = /\b(?:double|float|[su]?int(?:32|64)|s?fixed(?:32|64)|bool|string|bytes)\b/;
Prism.languages.protobuf = Prism.languages.extend('clike', {
'class-name': {
pattern: /(\b(?:enum|extend|message|service)\s+)[A-Za-z_]\w*(?=\s*\{)/,
lookbehind: true
},
'keyword': /\b(?:enum|extend|extensions|import|message|oneof|option|optional|package|public|repeated|required|reserved|service|syntax|to)\b/
});
Prism.languages.insertBefore('protobuf', 'operator', {
'map': {
pattern: /\bmap<\s*[\w.]+\s*,\s*[\w.]+\s*>(?=\s+[A-Za-z_]\w*\s*[=;])/,
alias: 'class-name',
inside: {
'punctuation': /[<>.,]/,
'builtin': builtinTypes
}
},
'builtin': builtinTypes,
'positional-class-name': {
pattern: /(?:\b|\B\.)[A-Za-z_]\w*(?:\.[A-Za-z_]\w*)*(?=\s+[A-Za-z_]\w*\s*[=;])/,
alias: 'class-name',
inside: {
'punctuation': /\./
}
},
'annotation': {
pattern: /(\[\s*)[A-Za-z_]\w*(?=\s*=)/,
lookbehind: true
}
});
}(Prism));

View File

@ -1 +1 @@
Prism.languages.protobuf=Prism.languages.extend("clike",{keyword:/\b(?:package|import|message|enum)\b/,builtin:/\b(?:required|repeated|optional|reserved)\b/,primitive:{pattern:/\b(?:double|float|int32|int64|uint32|uint64|sint32|sint64|fixed32|fixed64|sfixed32|sfixed64|bool|string|bytes)\b/,alias:"symbol"}});
!function(e){var a=/\b(?:double|float|[su]?int(?:32|64)|s?fixed(?:32|64)|bool|string|bytes)\b/;e.languages.protobuf=e.languages.extend("clike",{"class-name":{pattern:/(\b(?:enum|extend|message|service)\s+)[A-Za-z_]\w*(?=\s*\{)/,lookbehind:!0},keyword:/\b(?:enum|extend|extensions|import|message|oneof|option|optional|package|public|repeated|required|reserved|service|syntax|to)\b/}),e.languages.insertBefore("protobuf","operator",{map:{pattern:/\bmap<\s*[\w.]+\s*,\s*[\w.]+\s*>(?=\s+[A-Za-z_]\w*\s*[=;])/,alias:"class-name",inside:{punctuation:/[<>.,]/,builtin:a}},builtin:a,"positional-class-name":{pattern:/(?:\b|\B\.)[A-Za-z_]\w*(?:\.[A-Za-z_]\w*)*(?=\s+[A-Za-z_]\w*\s*[=;])/,alias:"class-name",inside:{punctuation:/\./}},annotation:{pattern:/(\[\s*)[A-Za-z_]\w*(?=\s*=)/,lookbehind:!0}})}(Prism);

View File

@ -0,0 +1,25 @@
<h2>Full example</h2>
<pre><code>syntax = "proto3";
package foo.generated;
option java_package = "org.foo.generated";
option optimize_for = SPEED;
// What's up with all the foo?
message Foo {
message Bar {
optional string key = 1;
optional Foo value = 2;
optional string value_raw = 3 [deprecated=true];
}
enum Level {
INFO = 0;
WARN = 1;
ERROR = 2;
}
repeated Property property = 1;
}</code></pre>

View File

@ -0,0 +1,20 @@
int32 foo = 1 [deprecated=true];
----------------------------------------------------
[
["builtin", "int32"],
" foo ",
["operator", "="],
["number", "1"],
["punctuation", "["],
["annotation", "deprecated"],
["operator", "="],
["boolean", "true"],
["punctuation", "]"],
["punctuation", ";"]
]
----------------------------------------------------
Check for annotations.

View File

@ -0,0 +1,39 @@
double
float
int32
int64
uint32
uint64
sint32
sint64
fixed32
fixed64
sfixed32
sfixed64
bool
string
bytes
----------------------------------------------------
[
["builtin", "double"],
["builtin", "float"],
["builtin", "int32"],
["builtin", "int64"],
["builtin", "uint32"],
["builtin", "uint64"],
["builtin", "sint32"],
["builtin", "sint64"],
["builtin", "fixed32"],
["builtin", "fixed64"],
["builtin", "sfixed32"],
["builtin", "sfixed64"],
["builtin", "bool"],
["builtin", "string"],
["builtin", "bytes"]
]
----------------------------------------------------
Check for builtin types.

View File

@ -0,0 +1,102 @@
syntax = "proto2";
syntax = "proto3";
option java_multiple_files = true;
import public "new.proto";
import "other.proto";
enum Foo {}
extend Foo {}
service Foo {}
message Foo {
Bar Bar = 0;
foo.Bar Bar2 = 0;
.baz.Bar Bar3 = 0;
}
----------------------------------------------------
[
["keyword", "syntax"],
["operator", "="],
["string", "\"proto2\""],
["punctuation", ";"],
["keyword", "syntax"],
["operator", "="],
["string", "\"proto3\""],
["punctuation", ";"],
["keyword", "option"],
" java_multiple_files ",
["operator", "="],
["boolean", "true"],
["punctuation", ";"],
["keyword", "import"],
["keyword", "public"],
["string", "\"new.proto\""],
["punctuation", ";"],
["keyword", "import"],
["string", "\"other.proto\""],
["punctuation", ";"],
["keyword", "enum"],
["class-name", "Foo"],
["punctuation", "{"],
["punctuation", "}"],
["keyword", "extend"],
["class-name", "Foo"],
["punctuation", "{"],
["punctuation", "}"],
["keyword", "service"],
["class-name", "Foo"],
["punctuation", "{"],
["punctuation", "}"],
["keyword", "message"],
["class-name", "Foo"],
["punctuation", "{"],
["positional-class-name", [
"Bar"
]],
" Bar ",
["operator", "="],
["number", "0"],
["punctuation", ";"],
["positional-class-name", [
"foo",
["punctuation", "."],
"Bar"
]],
" Bar2 ",
["operator", "="],
["number", "0"],
["punctuation", ";"],
["positional-class-name", [
["punctuation", "."],
"baz",
["punctuation", "."],
"Bar"
]],
" Bar3 ",
["operator", "="],
["number", "0"],
["punctuation", ";"],
["punctuation", "}"]
]
----------------------------------------------------
Check for class names

View File

@ -1,40 +1,41 @@
message Point {
required int32 x = 1;
required int32 y = 2;
optional string label = 3;
}
enum
extend
extensions
import
message
oneof
option
optional
package
public
repeated
required
reserved
service
syntax
to
----------------------------------------------------
[
["keyword", "enum"],
["keyword", "extend"],
["keyword", "extensions"],
["keyword", "import"],
["keyword", "message"],
" Point ",
["punctuation", "{"],
["builtin", "required"],
["primitive", "int32"],
" x ",
["operator", "="],
["number", "1"],
["punctuation", ";"],
["builtin", "required"],
["primitive", "int32"],
" y ",
["operator", "="],
["number", "2"],
["punctuation", ";"],
["builtin", "optional"],
["primitive", "string"],
" label ",
["operator", "="],
["number", "3"],
["punctuation", ";"],
["punctuation", "}"]
["keyword", "oneof"],
["keyword", "option"],
["keyword", "optional"],
["keyword", "package"],
["keyword", "public"],
["keyword", "repeated"],
["keyword", "required"],
["keyword", "reserved"],
["keyword", "service"],
["keyword", "syntax"],
["keyword", "to"]
]
----------------------------------------------------
Check for keywords and builtins
Check for keywords

View File

@ -0,0 +1,23 @@
map<string, .foo.Foo> bar;
----------------------------------------------------
[
["map", [
"map",
["punctuation", "<"],
["builtin", "string"],
["punctuation", ","],
["punctuation", "."],
"foo",
["punctuation", "."],
"Foo",
["punctuation", ">"]
]],
" bar",
["punctuation", ";"]
]
----------------------------------------------------
Check for maps.

View File

@ -20,4 +20,4 @@
----------------------------------------------------
Checks for single-quoted and double-quoted strings.
Checks for single-quoted and double-quoted strings.