Skip to content

Commit

Permalink
Merge pull request #20 from jimhigson/errorcases
Browse files Browse the repository at this point in the history
Track depth to throw error if parsing is closed before all JSON objects are complete. Fixes issue #18.
  • Loading branch information
dscape committed Jan 16, 2014
2 parents a74d839 + d3ce217 commit 1385517
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
14 changes: 12 additions & 2 deletions clarinet.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ else env = window;
parser.slashed = false;
parser.unicodeI = 0;
parser.unicodeS = null;
parser.depth = 0;
emit(parser, "onready");
}

Expand Down Expand Up @@ -303,7 +304,9 @@ else env = window;
}

function end(parser) {
if (parser.state !== S.VALUE) error(parser, "Unexpected end");
if (parser.state !== S.VALUE || parser.depth !== 0)
error(parser, "Unexpected end");

closeValue(parser);
parser.c = "";
parser.closed = true;
Expand Down Expand Up @@ -354,7 +357,9 @@ else env = window;
else {
if(c === '}') {
emit(parser, 'onopenobject');
this.depth++;
emit(parser, 'oncloseobject');
this.depth--;
parser.state = parser.stack.pop() || S.VALUE;
continue;
} else parser.stack.push(S.CLOSE_OBJECT);
Expand All @@ -371,10 +376,12 @@ else env = window;
if(parser.state === S.CLOSE_OBJECT) {
parser.stack.push(S.CLOSE_OBJECT);
closeValue(parser, 'onopenobject');
this.depth++;
} else closeValue(parser, 'onkey');
parser.state = S.VALUE;
} else if (c==='}') {
emitNode(parser, 'oncloseobject');
emitNode(parser, 'oncloseobject');
this.depth--;
parser.state = parser.stack.pop() || S.VALUE;
} else if(c===',') {
if(parser.state === S.CLOSE_OBJECT)
Expand All @@ -389,9 +396,11 @@ else env = window;
if (c === '\r' || c === '\n' || c === ' ' || c === '\t') continue;
if(parser.state===S.OPEN_ARRAY) {
emit(parser, 'onopenarray');
this.depth++;
parser.state = S.VALUE;
if(c === ']') {
emit(parser, 'onclosearray');
this.depth--;
parser.state = parser.stack.pop() || S.VALUE;
continue;
} else {
Expand Down Expand Up @@ -422,6 +431,7 @@ else env = window;
parser.state = S.VALUE;
} else if (c===']') {
emitNode(parser, 'onclosearray');
this.depth--;
parser.state = parser.stack.pop() || S.VALUE;
} else if (c === '\r' || c === '\n' || c === ' ' || c === '\t')
continue;
Expand Down
26 changes: 26 additions & 0 deletions test/clarinet.js
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,32 @@ var seps = [undefined, /\t|\n|\r/, '']
, ['ready' , undefined]
]
}
, incomplete_json_terminates_ending_in_number :
{ text : '[[1,2,3],[4,5'
, events :
[ ["openarray" , undefined]
, ["openarray" , undefined]
, ["value" , 1]
, ["value" , 2]
, ["value" , 3]
, ["closearray" , undefined]
, ["openarray" , undefined]
, ["value" , 4]
, ["error" , undefined]
]
}
, incomplete_json_terminates_ending_in_comma :
{ text : '[[1,2,3],'
, events :
[ ["openarray" , undefined]
, ["openarray" , undefined]
, ["value" , 1]
, ["value" , 2]
, ["value" , 3]
, ["closearray" , undefined]
, ["error" , undefined]
]
}
, json_org :
{ text :
('{\r\n' +
Expand Down

0 comments on commit 1385517

Please sign in to comment.