Skip to content

Commit

Permalink
Merge pull request #75 from podak/line-number-fix
Browse files Browse the repository at this point in the history
Adds a fix for calculating line number
  • Loading branch information
evan-king committed Aug 24, 2023
2 parents a9d21ca + 3ecf298 commit 4c6cff8
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 6 deletions.
16 changes: 11 additions & 5 deletions clarinet.js
Expand Up @@ -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;
Expand All @@ -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:
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -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"
},
Expand Down
16 changes: 16 additions & 0 deletions test/numeric.json
@@ -0,0 +1,16 @@
{
"a": {
"b": 5
},
"c": {
"d": {
"e": 1
},
"f": [
7,
8,
9
],
"g": 5
}
}
15 changes: 15 additions & 0 deletions test/position.js → test/positions.js
Expand Up @@ -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();
});
});
});
});

0 comments on commit 4c6cff8

Please sign in to comment.