Parse Error
rascal-0.41.2
Synopsis
Parse of a syntactically incorrect string.
Types
data RuntimeException = ParseError(loc parseloc) | ParseError(loc parseloc, str nt, str s);
Usage
import Exception; (only needed when ParseError is used in catch)
Description
This error is generated when during the execution of a Rascal program the parse function is applied to a syntactically incorrect input sentence.
Remedies:
- Correct the input sentence.
- Adapt the grammar so that it accepts the input sentence.
- Catch the ParseError yourself, see try catch.
Examples
Define the non-terminal As that accepts one or more letters a:
rascal>syntax As = "a"+;
ok
Then import ParseTree so that we can use the parse function:
rascal>import ParseTree;
ok
Now we can parse sentences consisting of letters a:
rascal>parse(#As, "aaaaaaaa");
As: (As) `aaaaaaaa`
But we get an error when parsing syntactically incorrect input (i.e., that does not
consists of letters a only):
rascal>parse(#As, "aaaabaaa");
|TODO:///|: ParseError(|unknown:///|(4,1,<1,4>,<1,5>))
We can also catch the ParseError but first import the Rascal modules Exception and IO:
rascal>import Exception;
ok
rascal>import IO;
ok
rascal>try
|1 >>>> parse(#As, "aaaabaaa");
|2 >>>>catch ParseError(e):
|3 >>>> println("Your input cannot be parsed: <e>");
Your input cannot be parsed: |unknown:///|(4,1,<1,4>,<1,5>)
ok