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

fix: support // in value #47

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 5 additions & 5 deletions parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ var onfieldoptions = function (tokens) {
case ',':
tokens.shift()
var name = tokens.shift()
if (name === '(') { // handling [(A) = B]
if (name === '(') { // handling [(A) = B]
name = tokens.shift()
tokens.shift() // remove the end of bracket
tokens.shift() // remove the end of bracket
}
if (tokens[0] !== '=') throw new Error('Unexpected token in field options: ' + tokens[0])
tokens.shift()
Expand Down Expand Up @@ -64,7 +64,7 @@ var onfield = function (tokens) {

case 'map':
field.type = 'map'
field.map = {from: null, to: null}
field.map = { from: null, to: null }
tokens.shift()
if (tokens[0] !== '<') throw new Error('Unexpected token in map type: ' + tokens[0])
tokens.shift()
Expand Down Expand Up @@ -196,7 +196,7 @@ var onextensions = function (tokens) {
to = Number(to)
if (isNaN(to)) throw new Error('Invalid to in extensions definition')
if (tokens.shift() !== ';') throw new Error('Missing ; in extensions definition')
return {from: from, to: to}
return { from: from, to: to }
}
var onmessage = function (tokens) {
tokens.shift()
Expand Down Expand Up @@ -336,7 +336,7 @@ var onoption = function (tokens) {
while (tokens.length) {
if (tokens[0] === ';') {
tokens.shift()
return {name: name, value: value}
return { name: name, value: value }
}
switch (tokens[0]) {
case 'option':
Expand Down
4 changes: 3 additions & 1 deletion test/fixtures/comments.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@
"oneof": null,
"required": false,
"repeated": false,
"options": {}
"options": {
"default": "\"http://127.0.0.1\""
}
}
]
},
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/comments.proto
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ message Point {
message Line {/*
*/required Point start = 1;
required Point end = 2;
optional string label = 3;
optional string label = 3 [default = "http://127.0.0.1"];
}

/**
Expand Down
20 changes: 19 additions & 1 deletion tokenize.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
module.exports = function (sch) {
var noComments = function (line) {
var i = line.indexOf('//')
return i > -1 ? line.slice(0, i) : line
if (i === -1) return line
// check // in value, like [default= "http://127.0.0.1"]
var start = 0
var end = 0
var len = line.length
for (var j = 0; j < len; j++) {
if (line[j] === '"') {
if (end !== 0) {
start = j
end = 0
} else if (start !== 0) {
end = j
} else {
start = j
}
}
}
var inValue = start !== end && start < i && end > i
return inValue ? line : line.slice(0, i)
}

var noMultilineComments = function () {
Expand Down