diff --git a/clarinet.js b/clarinet.js index 7aa2e8dc..7e234e4e 100644 --- a/clarinet.js +++ b/clarinet.js @@ -387,6 +387,7 @@ "Cannot write after close. Assign an onready handler."); if (chunk === null) return end(parser); var i = 0, c = chunk.charCodeAt(0), p = parser.p; + var lockIncrements = false; if (clarinet.DEBUG) console.log('write -> [' + chunk + ']'); while (c) { p = c; @@ -401,11 +402,15 @@ if(!c) break; if (clarinet.DEBUG) console.log(i,c,clarinet.STATE[parser.state]); - parser.position ++; - if (c === Char.lineFeed) { - parser.line ++; - parser.column = 0; - } else parser.column ++; + if (!lockIncrements) { + parser.position ++; + if (c === Char.lineFeed) { + parser.line ++; + parser.column = 0; + } else parser.column ++; + } else { + lockIncrements = false; + } switch (parser.state) { case S.BEGIN: @@ -666,6 +671,7 @@ } else { closeNumber(parser); i--; // go back one + lockIncrements = true; // do not apply increments for a single cycle parser.state = parser.stack.pop() || S.VALUE; } continue; diff --git a/package.json b/package.json index 81dd266c..82553d6b 100644 --- a/package.json +++ b/package.json @@ -38,7 +38,7 @@ "underscore": "1.2.3" }, "scripts": { - "test": "mocha -r should -t 10000 -s 2000 test/parser.spec.js test/clarinet.js test/npm.js test/utf8-chunks.js test/position.js", + "test": "mocha -r should -t 10000 -s 2000 test/parser.spec.js test/clarinet.js test/npm.js test/utf8-chunks.js test/positions.js", "bench": "cd benchmark && npm test", "prebench": "cd benchmark && test -e node_modules || npm i --ignore-scripts" }, diff --git a/test/numeric.json b/test/numeric.json new file mode 100644 index 00000000..316b0f6b --- /dev/null +++ b/test/numeric.json @@ -0,0 +1,16 @@ +{ + "a": { + "b": 5 + }, + "c": { + "d": { + "e": 1 + }, + "f": [ + 7, + 8, + 9 + ], + "g": 5 + } +} \ No newline at end of file diff --git a/test/position.js b/test/positions.js similarity index 61% rename from test/position.js rename to test/positions.js index 7bb4f481..6f222aac 100644 --- a/test/position.js +++ b/test/positions.js @@ -22,4 +22,19 @@ describe('clarinet', function(){ }); }); }); + describe('#line number', function() { + it('should be able to correctly track the line number', function (done){ + fs.readFile('test/numeric.json', 'utf8', function (err,data) { + if (err) { + done(err); + } + parser.onend = function() { + assert.equal(16, this.line); + }; + parser.write(data); + parser.close(); + done(); + }); + }); + }); });