Search in sources :

Example 6 with PreParseException

use of de.be4.classicalb.core.parser.exceptions.PreParseException 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);
    }
}
Also used : DefinitionCollector(de.be4.classicalb.core.parser.analysis.checking.DefinitionCollector) ParserException(de.be4.classicalb.core.parser.parser.ParserException) Start(de.be4.classicalb.core.parser.node.Start) ArrayList(java.util.ArrayList) Reader(java.io.Reader) StringReader(java.io.StringReader) PushbackReader(java.io.PushbackReader) Token(de.be4.classicalb.core.parser.node.Token) IOException(java.io.IOException) PushbackReader(java.io.PushbackReader) StringReader(java.io.StringReader) LexerException(de.be4.classicalb.core.parser.lexer.LexerException)

Example 7 with PreParseException

use of de.be4.classicalb.core.parser.exceptions.PreParseException in project probparsers by bendisposto.

the class PreParser method evaluateDefinitionFiles.

private void evaluateDefinitionFiles(final List<Token> list) throws PreParseException, BException, BCompoundException {
    IDefinitionFileProvider cache = null;
    if (contentProvider instanceof IDefinitionFileProvider) {
        cache = (IDefinitionFileProvider) contentProvider;
    }
    for (final Token fileNameToken : list) {
        final List<String> newDoneList = new ArrayList<String>(doneDefFiles);
        try {
            final String fileName = fileNameToken.getText();
            if (doneDefFiles.contains(fileName)) {
                StringBuilder sb = new StringBuilder();
                for (String string : doneDefFiles) {
                    sb.append(string).append(" -> ");
                }
                sb.append(fileName);
                throw new PreParseException(fileNameToken, "Cyclic references in definition files: " + sb.toString());
            }
            IDefinitions definitions;
            if (cache != null && cache.getDefinitions(fileName) != null) {
                definitions = cache.getDefinitions(fileName);
            } else {
                final String content = contentProvider.getFileContent(directory, fileName);
                newDoneList.add(fileName);
                final File file = contentProvider.getFile(directory, fileName);
                String filePath = fileName;
                if (file != null) {
                    filePath = file.getCanonicalPath();
                }
                final BParser parser = new BParser(filePath, parseOptions);
                parser.setDirectory(directory);
                parser.setDoneDefFiles(newDoneList);
                parser.setDefinitions(new Definitions(file));
                parser.parse(content, debugOutput, contentProvider);
                definitions = parser.getDefinitions();
                if (cache != null) {
                    cache.storeDefinition(fileName, definitions);
                }
            }
            defFileDefinitions.addDefinitions(definitions);
            definitionTypes.addAll(definitions.getTypes());
        } catch (final IOException e) {
            throw new PreParseException(fileNameToken, "Definition file cannot be read: " + e.getLocalizedMessage());
        } finally {
        }
    }
}
Also used : ArrayList(java.util.ArrayList) Token(de.be4.classicalb.core.preparser.node.Token) IOException(java.io.IOException) PreParseException(de.be4.classicalb.core.parser.exceptions.PreParseException) File(java.io.File)

Example 8 with PreParseException

use of de.be4.classicalb.core.parser.exceptions.PreParseException in project probparsers by bendisposto.

the class PreParser method evaluateTypes.

private void evaluateTypes(final List<Token> sortedDefinitionList, final Map<Token, Token> definitions) throws PreParseException {
    // use linked list as we rely on pop() and push()
    final LinkedList<Token> remainingDefinitions = new LinkedList<>(sortedDefinitionList);
    final LinkedList<Token> currentlyUnparseableDefinitions = new LinkedList<Token>();
    Set<String> todoDefs = new HashSet<String>();
    for (Token token : remainingDefinitions) {
        todoDefs.add(token.getText());
    }
    // use main parser for the rhs of each definition to determine type
    // if a definition can not be typed this way, it may be due to another
    // definition that is not yet parser (because it appears later in the
    // source code)
    // in this case, the definition is appended to the list again
    // the algorithm terminates if the queue is empty or if no definition
    // has been parsed
    boolean oneParsed = true;
    while (oneParsed) {
        oneParsed = false;
        while (!remainingDefinitions.isEmpty()) {
            final Token definition = remainingDefinitions.pop();
            final Token defRhs = definitions.get(definition);
            Definitions.Type type = null;
            DefinitionType definitionType = determineType(definition, defRhs, todoDefs);
            type = definitionType.type;
            if (type != null) {
                todoDefs.remove(definition.getText());
                oneParsed = true;
                definitionTypes.addTyping(definition.getText(), type);
            // types.addTyping(definition.getText(), type);
            } else {
                currentlyUnparseableDefinitions.push(definition);
            }
        }
        remainingDefinitions.addAll(currentlyUnparseableDefinitions);
        currentlyUnparseableDefinitions.clear();
    }
    if (!remainingDefinitions.isEmpty()) {
        final Token definition = remainingDefinitions.pop();
        final Token defRhs = definitions.get(definition);
        DefinitionType definitionType = determineType(definition, defRhs, todoDefs);
        if (definitionType.errorMessage != null) {
            throw new PreParseException(definitionType.errorMessage + " in file: " + modelFileName);
        // throw new BParseException(definitionType.errorToken,
        // definitionType.errorMessage + " in file: "
        // + modelFileName);
        } else {
            // fall back message
            throw new PreParseException(definition, "[" + definition.getLine() + "," + definition.getPos() + "] expecting wellformed expression, predicate or substitution as DEFINITION body (DEFINITION arguments assumed to be expressions)");
        }
    }
}
Also used : Token(de.be4.classicalb.core.preparser.node.Token) PreParseException(de.be4.classicalb.core.parser.exceptions.PreParseException) LinkedList(java.util.LinkedList) HashSet(java.util.HashSet)

Example 9 with PreParseException

use of de.be4.classicalb.core.parser.exceptions.PreParseException in project probparsers by bendisposto.

the class PreParser method determineDependencies.

private Map<String, Set<String>> determineDependencies(Set<String> definitionNames, Map<Token, Token> definitions) throws PreParseException {
    HashMap<String, Set<String>> dependencies = new HashMap<>();
    for (Entry<Token, Token> entry : definitions.entrySet()) {
        Token nameToken = entry.getKey();
        Token rhsToken = entry.getValue();
        // The FORMULA_PREFIX is needed to switch the lexer state from
        // section to normal. Note, that we do not parse the right hand side
        // of the definition here. Hence FORMULA_PREFIX has no further
        // meaning and substitutions can also be handled by the lexer.
        final Reader reader = new StringReader(BParser.FORMULA_PREFIX + "\n" + rhsToken.getText());
        final BLexer lexer = new BLexer(new PushbackReader(reader, BLexer.PUSHBACK_BUFFER_SIZE), new DefinitionTypes());
        lexer.setParseOptions(parseOptions);
        Set<String> set = new HashSet<>();
        de.be4.classicalb.core.parser.node.Token next = null;
        try {
            next = lexer.next();
            while (!(next instanceof EOF)) {
                if (next instanceof TIdentifierLiteral) {
                    TIdentifierLiteral id = (TIdentifierLiteral) next;
                    String name = id.getText();
                    if (definitionNames.contains(name)) {
                        set.add(name);
                    }
                }
                next = lexer.next();
            }
        } catch (IOException e) {
        } catch (BLexerException e) {
            de.be4.classicalb.core.parser.node.Token errorToken = e.getLastToken();
            final String newMessage = determineNewErrorMessageWithCorrectedPositionInformations(nameToken, rhsToken, errorToken, e.getMessage());
            throw new PreParseException(newMessage);
        } catch (de.be4.classicalb.core.parser.lexer.LexerException e) {
            final String newMessage = determineNewErrorMessageWithCorrectedPositionInformationsWithoutToken(nameToken, rhsToken, e.getMessage());
            throw new PreParseException(newMessage);
        }
        dependencies.put(nameToken.getText(), set);
    }
    return dependencies;
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) Reader(java.io.Reader) StringReader(java.io.StringReader) PushbackReader(java.io.PushbackReader) Token(de.be4.classicalb.core.preparser.node.Token) BLexerException(de.be4.classicalb.core.parser.exceptions.BLexerException) StringReader(java.io.StringReader) PreParseException(de.be4.classicalb.core.parser.exceptions.PreParseException) EOF(de.be4.classicalb.core.parser.node.EOF) HashSet(java.util.HashSet) IOException(java.io.IOException) TIdentifierLiteral(de.be4.classicalb.core.parser.node.TIdentifierLiteral) PushbackReader(java.io.PushbackReader)

Example 10 with PreParseException

use of de.be4.classicalb.core.parser.exceptions.PreParseException in project probparsers by bendisposto.

the class PrologExceptionPrinter method printBException.

public static void printBException(IPrologTermOutput pto, final BException e, boolean useIndentation, boolean lineOneOff) {
    Throwable cause = e.getCause();
    String filename = e.getFilename();
    if (cause == null) {
        printGeneralException(pto, e, filename, useIndentation, lineOneOff, true);
    } else {
        while (cause.getClass().equals(BException.class) && cause.getCause() != null) {
            BException bex = (BException) cause;
            cause = bex.getCause();
            filename = bex.getFilename();
        }
        if (cause instanceof BLexerException) {
            printBLexerException(pto, (BLexerException) cause, filename, useIndentation, lineOneOff);
        } else if (cause instanceof LexerException) {
            printLexerException(pto, (LexerException) cause, filename, useIndentation, lineOneOff);
        } else if (cause instanceof BParseException) {
            printBParseException(pto, (BParseException) cause, filename, useIndentation, lineOneOff);
        } else if (cause instanceof PreParseException) {
            printPreParseException(pto, (PreParseException) cause, filename, useIndentation, lineOneOff);
        } else if (cause instanceof CheckException) {
            printCheckException(pto, (CheckException) cause, filename, useIndentation, lineOneOff);
        } else {
            printGeneralException(pto, cause, filename, useIndentation, lineOneOff, false);
        }
    }
}
Also used : BLexerException(de.be4.classicalb.core.parser.exceptions.BLexerException) CheckException(de.be4.classicalb.core.parser.exceptions.CheckException) BParseException(de.be4.classicalb.core.parser.exceptions.BParseException) BException(de.be4.classicalb.core.parser.exceptions.BException) BLexerException(de.be4.classicalb.core.parser.exceptions.BLexerException) LexerException(de.be4.classicalb.core.parser.lexer.LexerException) PreParseException(de.be4.classicalb.core.parser.exceptions.PreParseException)

Aggregations

PreParseException (de.be4.classicalb.core.parser.exceptions.PreParseException)9 Token (de.be4.classicalb.core.preparser.node.Token)5 BLexerException (de.be4.classicalb.core.parser.exceptions.BLexerException)4 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 BCompoundException (de.be4.classicalb.core.parser.exceptions.BCompoundException)2 LexerException (de.be4.classicalb.core.parser.lexer.LexerException)2 LexerException (de.be4.classicalb.core.preparser.lexer.LexerException)2 ParserException (de.be4.classicalb.core.preparser.parser.ParserException)2 PushbackReader (java.io.PushbackReader)2 Reader (java.io.Reader)2 StringReader (java.io.StringReader)2 HashMap (java.util.HashMap)2 Set (java.util.Set)2 Test (org.junit.Test)2 BParser (de.be4.classicalb.core.parser.BParser)1 DefinitionCollector (de.be4.classicalb.core.parser.analysis.checking.DefinitionCollector)1 DefinitionPreCollector (de.be4.classicalb.core.parser.analysis.checking.DefinitionPreCollector)1 BException (de.be4.classicalb.core.parser.exceptions.BException)1