Skip to content

Commit

Permalink
add parsing for syntax (used in scalapb)
Browse files Browse the repository at this point in the history
  • Loading branch information
sachee committed Mar 15, 2017
1 parent 86c5276 commit abacb0e
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/descriptors/ProtoDescriptor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ module.exports = function parser(identifier, string) {
case 'package':
parsePackage()
break
case 'syntax':
parseSyntax()
break
case 'option':
parseOption(proto)
break
Expand Down Expand Up @@ -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";
Expand Down
2 changes: 2 additions & 0 deletions tests/parsing_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
2 changes: 2 additions & 0 deletions tests/protos/kitchen-sink.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down

0 comments on commit abacb0e

Please sign in to comment.