use of de.be4.classicalb.core.preparser.lexer.LexerException in project probparsers by bendisposto.
the class EventBParser method parse.
/**
* Parses the input string.
*
* @param input
* the {@link String} to be parsed
* @param debugOutput
* output debug messages on standard out?
* @return the root node of the AST
* @throws BException
* The {@link BException} class stores the actual exception as
* delegate and forwards all method calls to it. So it is save
* for tools to just use this exception if they want to extract
* an error message. If the tools needs to extract additional
* information, such as a sourcecode position or involved tokens
* respectively nodes, it needs to retrieve the delegate
* exception. The {@link BException} class offers a
* {@link BException#getCause()} method for this, which returns
* the delegate exception.
* <p>
* Internal exceptions:
* <ul>
* <li> {@link EventBLexerException}: If any error occurs in the
* generated or customized lexer a {@link LexerException} is
* thrown. Usually the lexer classes just throw a
* {@link LexerException}. But this class unfortunately does not
* contain any explicit information about the sourcecode
* position where the error occured. Using aspect-oriented
* programming we intercept the throwing of these exceptions to
* replace them by our own exception. In our own exception we
* provide the sourcecode position of the last characters that
* were read from the input.</li>
* <li> {@link EventBParseException}: This exception is thrown in
* two situations. On the one hand if the parser throws a
* {@link ParserException} we convert it into a
* {@link EventBParseException}. On the other hand it can be
* thrown if any error is found during the AST transformations
* after the parser has finished. We try to provide a token if a
* single token is involved in the error. Otherwise a
* {@link SourcecodeRange} is provided, which can be used to
* retrieve detailed position information from the
* {@link SourcePositions} (s. {@link #getSourcePositions()}).</li>
* <li> {@link CheckException}: If any problem occurs while
* performing semantic checks, a {@link CheckException} is
* thrown. We provide one or more nodes that are involved in the
* problem. For example, if we find dublicate machine clauses,
* we will list all occurances in the exception.</li>
* </ul>
*/
public Start parse(final String input, final boolean debugOutput) throws BException {
final Reader reader = new StringReader(input);
try {
/*
* Main parser
*/
final EventBLexer lexer = new EventBLexer(new PushbackReader(reader, 99));
lexer.setDebugOutput(debugOutput);
Parser parser = new Parser(lexer);
final Start rootNode = parser.parse();
final List<IToken> tokenList = ((ITokenListContainer) lexer).getTokenList();
/*
* Retrieving sourcecode positions which were found by ParserAspect
*/
final Map<PositionedNode, SourcecodeRange> positions = ((IParser) parser).getMapping();
sourcePositions = new SourcePositions(tokenList, positions);
parser = null;
return rootNode;
} catch (final LexerException e) {
/*
* Actually it's supposed to be a EventBLexerException because the
* aspect 'LexerAspect' replaces any LexerException to provide
* sourcecode position information in the BLexerException.
*/
throw new BException(e);
} catch (final ParserException e) {
throw new BException(createEventBParseException(e));
} catch (final EventBParseException e) {
throw new BException(e);
} catch (final IOException e) {
// shouldn't happen and if, we cannot handle it
throw new BException(e);
}
}
use of de.be4.classicalb.core.preparser.lexer.LexerException in project probparsers by bendisposto.
the class PreParser method parse.
public void parse() throws PreParseException, IOException, BException, BCompoundException {
final PreLexer preLexer = new PreLexer(pushbackReader);
final Parser preParser = new Parser(preLexer);
Start rootNode = null;
try {
rootNode = preParser.parse();
} catch (final ParserException e) {
if (e.getToken() instanceof de.be4.classicalb.core.preparser.node.TDefinitions) {
final Token errorToken = e.getToken();
final String message = "[" + errorToken.getLine() + "," + errorToken.getPos() + "] " + "Clause 'DEFINITIONS' is used more than once";
throw new PreParseException(e.getToken(), message);
} else {
throw new PreParseException(e.getToken(), e.getLocalizedMessage());
}
} catch (final LexerException e) {
throw new PreParseException(e.getLocalizedMessage());
}
final DefinitionPreCollector collector = new DefinitionPreCollector();
rootNode.apply(collector);
evaluateDefinitionFiles(collector.getFileDefinitions());
List<Token> sortedDefinitionList = sortDefinitionsByTopologicalOrderAndCheckForCycles(collector.getDefinitions());
evaluateTypes(sortedDefinitionList, collector.getDefinitions());
}
use of de.be4.classicalb.core.preparser.lexer.LexerException in project probparsers by bendisposto.
the class CliBParser method parseExtendedFormula.
private static void parseExtendedFormula(String theFormula, IDefinitions context) {
try {
BParser parser = new BParser();
parser.setDefinitions(context);
Start start = parser.eparse(theFormula, context);
PrologTermStringOutput strOutput = new PrologTermStringOutput();
NodeIdAssignment na = new NodeIdAssignment();
start.apply(na);
ClassicalPositionPrinter pprinter = new ClassicalPositionPrinter(na, -1, 0);
ASTProlog printer = new ASTProlog(strOutput, pprinter);
start.apply(printer);
strOutput.fullstop();
// A Friendly Reminder: strOutput includes a newline!
print(strOutput.toString());
} catch (NullPointerException e) {
// Not Parseable - Sadly, calling e.getLocalizedMessage() on the
// NullPointerException returns NULL itself, thus triggering another
// NullPointerException in the catch statement. Therefore we need a
// second catch statement with a special case for the
// NullPointerException instead of catching a general Exception
// print("EXCEPTION NullPointerException" + System.lineSeparator());
PrologTermStringOutput strOutput = new PrologTermStringOutput();
strOutput.openTerm("exception").printAtom("NullPointerException").closeTerm();
strOutput.fullstop();
strOutput.flush();
print(strOutput.toString());
} catch (BCompoundException e) {
PrologExceptionPrinter.printException(socketOutputStream, e, false, true);
} catch (LexerException e) {
PrologTermStringOutput strOutput = new PrologTermStringOutput();
strOutput.openTerm("exception").printAtom(e.getLocalizedMessage()).closeTerm();
strOutput.fullstop();
strOutput.flush();
print(strOutput.toString());
} catch (IOException e) {
PrologExceptionPrinter.printException(socketOutputStream, e, theFormula, false, true);
}
}
use of de.be4.classicalb.core.preparser.lexer.LexerException in project probparsers by bendisposto.
the class CtlParser method parseFormula.
protected Start parseFormula(final String formula) throws LtlParseException, IOException {
StringReader reader = new StringReader(formula);
PushbackReader r = new PushbackReader(reader);
Lexer l = new CtlLexer(r);
Parser p = new Parser(l);
Start ast = null;
try {
ast = p.parse();
} catch (ParserException e) {
final UniversalToken token = UniversalToken.createToken(e.getToken());
throw new LtlParseException(token, e.getLocalizedMessage());
} catch (LexerException e) {
throw new LtlParseException(null, e.getLocalizedMessage());
}
return ast;
}
use of de.be4.classicalb.core.preparser.lexer.LexerException in project probparsers by bendisposto.
the class BParser method parse.
/**
* Parses the input string.
*
* @param input
* The {@link String} to be parsed
* @param debugOutput
* output debug messages on standard out?
* @param contentProvider
* A {@link IFileContentProvider} that is able to load content of
* referenced files during the parsing process. The content
* provider is used for referenced definition files for example.
* @return the root node of the AST
* @throws BCompoundException
* The {@link BCompoundException} class stores all
* {@link BException}s occurred during the parsing process. The
* {@link BException} class stores the actual exception as
* delegate and forwards all method calls to it. So it is save
* for tools to just use this exception if they want to extract
* an error message. If the tools needs to extract additional
* information, such as a source code position or involved
* tokens respectively nodes, it needs to retrieve the delegate
* exception. The {@link BException} class offers a
* {@link BException#getCause()} method for this, which returns
* the delegate exception.
* <p>
* Internal exceptions:
* <ul>
* <li>{@link PreParseException}: This exception contains errors
* that occur during the preparsing. If possible it supplies a
* token, where the error occurred.</li>
* <li>{@link BLexerException}: If any error occurs in the
* generated or customized lexer a {@link LexerException} is
* thrown. Usually the lexer classes just throw a
* {@link LexerException}. But this class unfortunately does not
* contain any explicit information about the source code
* position where the error occurred. Using aspect-oriented
* programming we intercept the throwing of these exceptions to
* replace them by our own exception. In our own exception we
* provide the source code position of the last characters that
* were read from the input.</li>
* <li>{@link BParseException}: This exception is thrown in two
* situations. On the one hand if the parser throws a
* {@link ParserException} we convert it into a
* {@link BParseException}. On the other hand it can be thrown
* if any error is found during the AST transformations after
* the parser has finished.</li>
* <li>{@link CheckException}: If any problem occurs while
* performing semantic checks, a {@link CheckException} is
* thrown. We provide one or more nodes that are involved in the
* problem. For example, if we find duplicate machine clauses,
* we will list all occurrences in the exception.</li>
* </ul>
*/
public Start parse(final String input, final boolean debugOutput, final IFileContentProvider contentProvider) throws BCompoundException {
final Reader reader = new StringReader(input);
try {
// PreParsing
final DefinitionTypes defTypes = preParsing(debugOutput, reader, contentProvider, directory);
/*
* The definition types are used in the lexer in order to replace an
* identifier token by a definition call token. This is required if
* the definition is a predicate because an identifier can not be
* parsed as a predicate. For example "... SELECT def THEN ... "
* would yield to a parse error. The lexer will replace the
* identifier token "def" by a TDefLiteralPredicate which will be
* excepted by the parser
*
*/
defTypes.addAll(definitions.getTypes());
/*
* Main parser
*/
final BLexer lexer = new BLexer(new PushbackReader(reader, BLexer.PUSHBACK_BUFFER_SIZE), defTypes);
lexer.setParseOptions(parseOptions);
SabbleCCBParser parser = new SabbleCCBParser(lexer);
final Start rootNode = parser.parse();
final List<BException> bExceptionList = new ArrayList<>();
/*
* Collect available definition declarations. Needs to be done now
* cause they are needed by the following transformations.
*/
final DefinitionCollector collector = new DefinitionCollector(defTypes, this.definitions);
collector.collectDefinitions(rootNode);
List<CheckException> definitionsCollectorExceptions = collector.getExceptions();
for (CheckException checkException : definitionsCollectorExceptions) {
bExceptionList.add(new BException(getFileName(), checkException));
}
// perfom AST transformations that can't be done by SableCC
try {
applyAstTransformations(rootNode);
} catch (CheckException e) {
throw new BCompoundException(new BException(getFileName(), e));
}
// perform some semantic checks which are not done in the parser
List<CheckException> checkExceptions = performSemanticChecks(rootNode);
for (CheckException checkException : checkExceptions) {
bExceptionList.add(new BException(getFileName(), checkException));
}
if (!bExceptionList.isEmpty()) {
throw new BCompoundException(bExceptionList);
}
return rootNode;
} catch (final LexerException e) {
throw new BCompoundException(new BException(getFileName(), e));
} catch (final BParseException e) {
throw new BCompoundException(new BException(getFileName(), e));
} catch (final IOException e) {
throw new BCompoundException(new BException(getFileName(), e));
} catch (final PreParseException e) {
throw new BCompoundException(new BException(getFileName(), e));
} catch (final ParserException e) {
final Token token = e.getToken();
String msg = getImprovedErrorMessageBasedOnTheErrorToken(token);
if (msg == null) {
msg = e.getLocalizedMessage();
}
final String realMsg = e.getRealMsg();
throw new BCompoundException(new BException(getFileName(), new BParseException(token, msg, realMsg, e)));
} catch (BException e) {
throw new BCompoundException(e);
}
}
Aggregations