Skip to content

Commit

Permalink
added accurate parse error test
Browse files Browse the repository at this point in the history
  • Loading branch information
jurgenvinju committed Dec 11, 2024
1 parent dcc0c8e commit 5adbb8a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/org/rascalmpl/library/lang/json/IO.rsc
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
module lang::json::IO

import util::Maybe;
import Exception;

@synopsis{JSON parse errors have more information than general parse errors}
@description{
* `location` is the place where the parsing got stuck (going from left to right).
* `cause` is a factual diagnosis of what was expected at that position, versus what was found.
* `path` is a path query string into the JSON value from the root down to the leaf where the error was detected.
}
data RuntimeException = ParseError(loc location, str cause="", str path="");
data RuntimeException(str cause="", str path="");

@javaClass{org.rascalmpl.library.lang.json.IO}
@synopsis{Maps any Rascal value to a JSON string}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,26 @@ test bool originTracking() {
return true;
}
test bool accurateParseErrors() {
ex = readFile(|std:///lang/rascal/tests/library/lang/json/glossary.json|);
broken = ex[..size(ex)/2] + ex[size(ex)/2+10..];
try {
ex2 = parseJSON(#node, broken, trackOrigins=true);
}
catch ParseError(loc l):
return l.begin.line == 14;
try {
// accurate locations have to be provided also when trackOrigins=false
ex2 = parseJSON(#node, broken, trackOrigins=false);
}
catch ParseError(loc l): // , cause=/^Unterminated object.*/, path="$.glossary.GlossDiv.GlossList.GlossEntry.GlossTerm") :
return l.begin.line == 14;
return true;
}
data Cons = cons(str bla = "null");
test bool dealWithNull() {
Expand Down

0 comments on commit 5adbb8a

Please sign in to comment.