Skip to content

Commit

Permalink
Merge pull request #10 from dpup/nick-conformance
Browse files Browse the repository at this point in the history
Make test protos in pbnj protoc-compliant
  • Loading branch information
nicks committed Jan 20, 2015
2 parents e175595 + 9713d3a commit 951e470
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 17 deletions.
5 changes: 3 additions & 2 deletions lib/Project.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,11 @@ Project.prototype.setOutDir = function (outDir) {
* @param {string} protoFile The proto file to compile, all imports will be followed.
* @param {string} templateName The Soy template name to use when compiling.
* @param {string=} opt_suffix Optional suffix for generated files.
* @param {boolean=} opt_skipImports If true, dont' follow imports.
* @return {Project}
*/
Project.prototype.addJob = function (protoFile, templateName, opt_suffix) {
this.addProto(protoFile, true)
Project.prototype.addJob = function (protoFile, templateName, opt_suffix, opt_skipImports) {
this.addProto(protoFile, !opt_skipImports)
this._compileJobs.push({
proto: this._resolve(protoFile),
template: templateName,
Expand Down
6 changes: 5 additions & 1 deletion lib/Token.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ Token.Type = {
END_BLOCK: 9,
START_OPTION: 10,
END_OPTION: 11,
NUMBER: 12
NUMBER: 12,
START_PAREN: 13,
END_PAREN: 14
}


Expand All @@ -59,6 +61,8 @@ Token.toErrorString = function (value) {
case Token.Type.END_BLOCK: return '} (close block)'
case Token.Type.START_OPTION: return '[ (open option)'
case Token.Type.END_OPTION: return '] (close option)'
case Token.Type.START_PAREN: return '( (open parenthesis)'
case Token.Type.END_PAREN: return ') (close parenthesis)'
case Token.Type.NUMBER: return 'number'
default:
return 'Token Value=' + value
Expand Down
2 changes: 1 addition & 1 deletion lib/descriptors/ProtoDescriptor.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ ProtoDescriptor.prototype.getOptionKeys = function () {


ProtoDescriptor.prototype.addImportName = function (fileName) {
this._importNames.push(path.resolve(path.dirname(this._filePath), fileName))
this._importNames.push(path.resolve(fileName))
return this
}

Expand Down
29 changes: 27 additions & 2 deletions lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,21 @@ module.exports = function parser(identifier, string) {
expect(Token.Type.TERMINATOR)
}

// Parses a file level option: option optionName = "optionValue";
// Parses a file level option:
// option optionName = "optionValue";
// option (optionName) = "optionValue";
function parseOption(parent) {
var hasParens = false
if (peek().type == Token.Type.START_PAREN) {
expect(Token.Type.START_PAREN)
hasParens = true
}

var key = expect(Token.Type.WORD)
if (hasParens) {
expect(Token.Type.END_PAREN)
}

var equals = expect(Token.Type.OPERATOR)
var value = expect(Token.Type.STRING)
expect(Token.Type.TERMINATOR)
Expand Down Expand Up @@ -185,10 +197,23 @@ module.exports = function parser(identifier, string) {
})
}

// Parses field options: [ name1 = value1, name2 = value2 ]
// Parses field options: [ name1 = value1, name2 = value2, (name3) = value3 ]
function parseFieldOptions(parent) {
parseBlock(function (token) {
var hasParens = false
var nameToken = token
if (token.type == Token.Type.START_PAREN) {
token = expect(Token.Type.WORD)
hasParens = true
} else if (token.type != Token.Type.WORD) {
throw new ParseError(identifier, token, 'expected ' + Token.toErrorString(Token.Type.WORD) + '.')
}

var name = token.content
if (hasParens) {
expect(Token.Type.END_PAREN)
}

expect(Token.Type.OPERATOR)
var value = expect([Token.Type.WORD, Token.Type.STRING, Token.Type.NUMBER]).content
parent.addOption(name, value)
Expand Down
2 changes: 2 additions & 0 deletions lib/tokenize.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ module.exports = function tokenize(string, fileName) {
else if (lookingAt('}')) consumeChar(Token.Type.END_BLOCK)
else if (lookingAt('[')) consumeChar(Token.Type.START_OPTION)
else if (lookingAt(']')) consumeChar(Token.Type.END_OPTION)
else if (lookingAt('(')) consumeChar(Token.Type.START_PAREN)
else if (lookingAt(')')) consumeChar(Token.Type.END_PAREN)
else if (lookingAtNumberFirstCharacter()) consumeNumber()
else if (lookingAtWordFirstCharacter()) consumeWord()
else if (lookingAtRe(/\s/)) nextChar()
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pbnj",
"version": "0.1.2",
"version": "0.2.0",
"keywords": ["protocol", "buffer", "proto", "protobuf", "parser", "codegen"],
"description": "JavaScript protocol buffer schema parser and template based code generator",
"homepage": "https://github.com/dpup/pbnj",
Expand All @@ -24,7 +24,7 @@
} ],
"main": "lib/pbnj.js",
"scripts": {
"test": "nodeunit ./tests/"
"test": "nodeunit ./tests/ && tests/protoc_test.sh"
},
"dependencies": {
"mkdirp": "0.3.5",
Expand Down
2 changes: 1 addition & 1 deletion tests/parsing_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ exports.testKitchenSinkParsing = function (test) {

// Test imports.
test.equal(proto.getImportNames().length, 1)
test.equal(proto.getImportNames()[0], path.join(process.cwd(), 'some-other-file.proto'))
test.equal(proto.getImportNames()[0], path.join(process.cwd(), 'tests/protos/options.proto'))

// Test proto level options.
test.equal(proto.getOptionKeys().length, 2)
Expand Down
8 changes: 8 additions & 0 deletions tests/protoc_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/sh

cd `dirname $0`/..
mkdir tmp_protoc_test
protoc tests/protos/*.proto --proto_path=.:/usr/local/include --cpp_out=tmp_protoc_test
RESULT=$?
rm -fR tmp_protoc_test
exit $RESULT
10 changes: 5 additions & 5 deletions tests/protos/kitchen-sink.proto
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

package some_package;

import "some-other-file.proto";
import "tests/protos/options.proto";

option file_level_option = "string value";
option another_option = "Just \"testing\" that strings parse.";
option (file_level_option) = "string value";
option (another_option) = "Just \"testing\" that strings parse.";


message ThisIsTheKitchenSink {
Expand All @@ -25,8 +25,8 @@ message ThisIsTheKitchenSink {


message AnotherMessage {
option message_level_option = "XYZ";
required int64 id = 1000 [option=1, something_else="foobar"];
option (message_level_option) = "XYZ";
required int64 id = 1000 [(option)=1, (something_else)="foobar"];
optional string x = 1; optional string y = 2; optional string z = 3;

message MessagesWithinMessages {
Expand Down
17 changes: 17 additions & 0 deletions tests/protos/options.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2015. A Medium Corporation.

import "google/protobuf/descriptor.proto";

extend google.protobuf.FieldOptions {
optional int32 option = 20150103;
optional string something_else = 20150104;
}

extend google.protobuf.MessageOptions {
optional string message_level_option = 20150102;
}

extend google.protobuf.FileOptions {
optional string file_level_option = 20150101;
optional string another_option = 20150105;
}
2 changes: 1 addition & 1 deletion tests/protos/person.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Proto definition for a Person
*/

import "common.proto";
import "tests/protos/common.proto";

package examples;

Expand Down
4 changes: 2 additions & 2 deletions tests/protos/vehicle.proto
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Proto definition for a Car

import "common.proto";
import "person.proto";
import "tests/protos/common.proto";
import "tests/protos/person.proto";

package examples;

Expand Down

0 comments on commit 951e470

Please sign in to comment.