Protobuf improvements (#1948) This makes numerous improvements to the Protobuf grammar adding (almost) full support for PB2 and PB3 syntax.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
diff --git a/components/prism-protobuf.js b/components/prism-protobuf.js
index 44e6cd5..4e369f8 100644
--- a/components/prism-protobuf.js
+++ b/components/prism-protobuf.js
@@ -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));
diff --git a/components/prism-protobuf.min.js b/components/prism-protobuf.min.js
index a55dbe2..ffbdaee 100644
--- a/components/prism-protobuf.min.js
+++ b/components/prism-protobuf.min.js
@@ -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"}});
\ No newline at end of file
+!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);
\ No newline at end of file
diff --git a/examples/prism-protobuf.html b/examples/prism-protobuf.html
new file mode 100644
index 0000000..7174552
--- /dev/null
+++ b/examples/prism-protobuf.html
@@ -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>
diff --git a/tests/languages/protobuf/annotation_feature.test b/tests/languages/protobuf/annotation_feature.test
new file mode 100644
index 0000000..ec68a55
--- /dev/null
+++ b/tests/languages/protobuf/annotation_feature.test
@@ -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.
diff --git a/tests/languages/protobuf/builtin_feature.test b/tests/languages/protobuf/builtin_feature.test
new file mode 100644
index 0000000..bf170b9
--- /dev/null
+++ b/tests/languages/protobuf/builtin_feature.test
@@ -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.
diff --git a/tests/languages/protobuf/class-name_feature.test b/tests/languages/protobuf/class-name_feature.test
new file mode 100644
index 0000000..5f0ffb7
--- /dev/null
+++ b/tests/languages/protobuf/class-name_feature.test
@@ -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
diff --git a/tests/languages/protobuf/keyword_feature.test b/tests/languages/protobuf/keyword_feature.test
index eabe71e..1272833 100644
--- a/tests/languages/protobuf/keyword_feature.test
+++ b/tests/languages/protobuf/keyword_feature.test
@@ -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
\ No newline at end of file
+Check for keywords
diff --git a/tests/languages/protobuf/map_feature.test b/tests/languages/protobuf/map_feature.test
new file mode 100644
index 0000000..94a54f6
--- /dev/null
+++ b/tests/languages/protobuf/map_feature.test
@@ -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.
diff --git a/tests/languages/protobuf/string_feature.test b/tests/languages/protobuf/string_feature.test
index 3d2b45c..8929696 100644
--- a/tests/languages/protobuf/string_feature.test
+++ b/tests/languages/protobuf/string_feature.test
@@ -20,4 +20,4 @@
----------------------------------------------------
-Checks for single-quoted and double-quoted strings.
\ No newline at end of file
+Checks for single-quoted and double-quoted strings.