From abacb0eb7761e8810fbd8a2f49ef8c7de8de6e86 Mon Sep 17 00:00:00 2001 From: sasha solomon Date: Wed, 15 Mar 2017 16:52:07 -0700 Subject: [PATCH] add parsing for syntax (used in scalapb) --- lib/descriptors/ProtoDescriptor.js | 11 +++++++++++ lib/parser.js | 11 +++++++++++ tests/parsing_test.js | 2 ++ tests/protos/kitchen-sink.proto | 2 ++ 4 files changed, 26 insertions(+) diff --git a/lib/descriptors/ProtoDescriptor.js b/lib/descriptors/ProtoDescriptor.js index 2d8fbb8..3555e1f 100644 --- a/lib/descriptors/ProtoDescriptor.js +++ b/lib/descriptors/ProtoDescriptor.js @@ -58,6 +58,7 @@ ProtoDescriptor.prototype.toTemplateObject = function () { var result = { name: this.getName(), package: this._package, + syntax: this._syntax, options: this._options, importNames: this._importNames, get messages() { @@ -106,6 +107,16 @@ ProtoDescriptor.prototype.getPackage = function () { return this._package } +ProtoDescriptor.prototype.setSyntax = function (syntax) { + this._syntax = syntax + return this +} + + +ProtoDescriptor.prototype.getSyntax = function () { + return this._syntax +} + ProtoDescriptor.prototype.addOption = function (name, value) { this._options[name] = value diff --git a/lib/parser.js b/lib/parser.js index 98dad02..49ff041 100644 --- a/lib/parser.js +++ b/lib/parser.js @@ -50,6 +50,9 @@ module.exports = function parser(identifier, string) { case 'package': parsePackage() break + case 'syntax': + parseSyntax() + break case 'option': parseOption(proto) break @@ -81,6 +84,14 @@ module.exports = function parser(identifier, string) { expect(Token.Type.TERMINATOR) } + // Parses the syntax statement: syntax = "proto2"; + function parseSyntax() { + expect(Token.Type.OPERATOR) + var syntax = expect(Token.Type.STRING).content + expect(Token.Type.TERMINATOR) + proto.setSyntax(syntax) + } + // Parses a file level option: // option optionName = "optionValue"; // option (optionName) = "optionValue"; diff --git a/tests/parsing_test.js b/tests/parsing_test.js index 89e26bf..b80da40 100644 --- a/tests/parsing_test.js +++ b/tests/parsing_test.js @@ -19,6 +19,8 @@ exports.testKitchenSinkParsing = function (test) { test.equal(proto.getPackage(), 'some_package') + test.equal(proto.getSyntax(), 'proto2') + // Test imports. test.equal(proto.getImportNames().length, 4) test.equal(proto.getImportNames()[0], 'protos/options.proto') diff --git a/tests/protos/kitchen-sink.proto b/tests/protos/kitchen-sink.proto index 212c1be..ef8c37a 100644 --- a/tests/protos/kitchen-sink.proto +++ b/tests/protos/kitchen-sink.proto @@ -4,6 +4,8 @@ * Both comment types should be supported and ignore other tokens such as message FooBar { }. */ +syntax = "proto2"; + package some_package; import "protos/options.proto";