Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Protobuf improvements #1948

Merged
merged 3 commits into from
Jul 1, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 46 additions & 8 deletions components/prism-protobuf.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,46 @@
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 primitives = /\b(?:double|float|int32|int64|uint32|uint64|sint32|sint64|fixed32|fixed64|sfixed32|sfixed64|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|message|oneof|package|service)(?=\s+(?:$|\w))|extensions|import|option|public|syntax)\b/,
RegExp(/\bto(?=\s+(?:max\b|__))/.source.replace('__', Prism.languages.clike.number.source))
],
'builtin': /\b(?:optional|repeated|required|reserved)\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': /[<>.,]/,
'primitives': {
pattern: primitives,
alias: 'symbol'
}
}
},
'primitive': {
pattern: primitives,
alias: 'symbol'
},
'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));
2 changes: 1 addition & 1 deletion components/prism-protobuf.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions examples/prism-protobuf.html
Original file line number Diff line number Diff line change
@@ -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>
21 changes: 21 additions & 0 deletions tests/languages/protobuf/annotation_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
int32 foo = 1 [deprecated=true];


----------------------------------------------------

[
["primitive", "int32"],
" foo ",
["operator", "="],
["number", "1"],
["punctuation", "["],
["annotation", "deprecated"],
["operator", "="],
["boolean", "true"],
["punctuation", "]"],
["punctuation", ";"]
]

----------------------------------------------------

Check for annotations.
18 changes: 18 additions & 0 deletions tests/languages/protobuf/builtin_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
optional
repeated
required
reserved


----------------------------------------------------

[
["builtin", "optional"],
["builtin", "repeated"],
["builtin", "required"],
["builtin", "reserved"]
]

----------------------------------------------------

Check for builtins.
103 changes: 103 additions & 0 deletions tests/languages/protobuf/class-name_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
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
59 changes: 28 additions & 31 deletions tests/languages/protobuf/keyword_feature.test
Original file line number Diff line number Diff line change
@@ -1,40 +1,37 @@
message Point {
required int32 x = 1;
required int32 y = 2;
optional string label = 3;
}
enum
extend
extensions
import
message
oneof
option
package
public
service
syntax

0 to 1


----------------------------------------------------

[
["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", "package"],
["keyword", "public"],
["keyword", "service"],
["keyword", "syntax"],
["number", "0"],
["keyword", "to"],
["number", "1"]
]

----------------------------------------------------

Check for keywords and builtins
Check for keywords
24 changes: 24 additions & 0 deletions tests/languages/protobuf/map_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
map<string, .foo.Foo> bar;


----------------------------------------------------

[
["map", [
"map",
["punctuation", "<"],
["primitives", "string"],
["punctuation", ","],
["punctuation", "."],
"foo",
["punctuation", "."],
"Foo",
["punctuation", ">"]
]],
" bar",
["punctuation", ";"]
]

----------------------------------------------------

Check for maps.
40 changes: 40 additions & 0 deletions tests/languages/protobuf/primitives_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
double
float
int32
int64
uint32
uint64
sint32
sint64
fixed32
fixed64
sfixed32
sfixed64
bool
string
bytes


----------------------------------------------------

[
["primitive", "double"],
["primitive", "float"],
["primitive", "int32"],
["primitive", "int64"],
["primitive", "uint32"],
["primitive", "uint64"],
["primitive", "sint32"],
["primitive", "sint64"],
["primitive", "fixed32"],
["primitive", "fixed64"],
["primitive", "sfixed32"],
["primitive", "sfixed64"],
["primitive", "bool"],
["primitive", "string"],
["primitive", "bytes"]
]

----------------------------------------------------

Check for primitive types.
3 changes: 2 additions & 1 deletion tests/languages/protobuf/string_feature.test
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
'"bar"'
" // comment "


----------------------------------------------------

[
Expand All @@ -20,4 +21,4 @@

----------------------------------------------------

Checks for single-quoted and double-quoted strings.
Checks for single-quoted and double-quoted strings.