Search in sources :

Example 31 with BException

use of de.be4.classicalb.core.parser.exceptions.BException 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 32 with BException

use of de.be4.classicalb.core.parser.exceptions.BException 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)

Example 33 with BException

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

the class EventBParser method main.

public static void main(final String[] args) {
    if (args.length < 1) {
        System.err.println("usage: BParser [options] <BMachine file>");
        System.err.println();
        System.err.println("Available options are:");
        System.err.println(CLI_SWITCH_VERBOSE + "\t\tVerbose output during lexing and parsing");
        System.err.println(CLI_SWITCH_TIME + "\t\tOutput time used for complete parsing process.");
        System.err.println(CLI_SWITCH_AST + "\t\tPrint AST on standard output.");
        System.err.println(CLI_SWITCH_UI + "\t\tShow AST as Swing UI.");
        System.exit(-1);
    }
    try {
        final long start = System.currentTimeMillis();
        final EventBParser parser = new EventBParser();
        final Start tree = parser.parseFile(new File(args[args.length - 1]), isCliSwitchSet(args, CLI_SWITCH_VERBOSE));
        final long end = System.currentTimeMillis();
        System.out.println();
        if (isCliSwitchSet(args, CLI_SWITCH_TIME)) {
            System.out.println("Time for parsing: " + (end - start) + "ms");
        }
        if (isCliSwitchSet(args, CLI_SWITCH_AST)) {
            System.out.println("AST:");
            tree.apply(new ASTPrinter());
        }
        if (isCliSwitchSet(args, CLI_SWITCH_UI)) {
            tree.apply(new ASTDisplay());
        }
    } catch (final IOException e) {
        System.err.println();
        System.err.println("Error reading input file: " + e.getLocalizedMessage());
        System.exit(-2);
    } catch (final BException e) {
        System.err.println();
        System.err.println("Error parsing input file: " + e.getLocalizedMessage());
        System.exit(-3);
    }
}
Also used : Start(de.be4.eventbalg.core.parser.node.Start) ASTPrinter(de.be4.eventbalg.core.parser.analysis.ASTPrinter) ASTDisplay(de.be4.eventbalg.core.parser.analysis.ASTDisplay) IOException(java.io.IOException) File(java.io.File)

Example 34 with BException

use of de.be4.classicalb.core.parser.exceptions.BException 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);
    }
}
Also used : ParserException(de.be4.eventbalg.core.parser.parser.ParserException) Start(de.be4.eventbalg.core.parser.node.Start) SourcePositions(de.hhu.stups.sablecc.patch.SourcePositions) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) StringReader(java.io.StringReader) PushbackReader(java.io.PushbackReader) PositionedNode(de.hhu.stups.sablecc.patch.PositionedNode) IOException(java.io.IOException) PushbackReader(java.io.PushbackReader) IParser(de.hhu.stups.sablecc.patch.IParser) Parser(de.be4.eventbalg.core.parser.parser.Parser) IToken(de.hhu.stups.sablecc.patch.IToken) StringReader(java.io.StringReader) ITokenListContainer(de.hhu.stups.sablecc.patch.ITokenListContainer) SourcecodeRange(de.hhu.stups.sablecc.patch.SourcecodeRange) LexerException(de.be4.eventbalg.core.parser.lexer.LexerException) IParser(de.hhu.stups.sablecc.patch.IParser)

Example 35 with BException

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

the class AbstractTest method parseInput.

protected Start parseInput(final String input, final boolean debugOutput) throws BException {
    if (debugOutput) {
        System.out.println();
        System.out.println();
    }
    final EventBParser parser = new EventBParser();
    final Start rootNode = parser.parse(input, debugOutput);
    if (debugOutput) {
        rootNode.apply(new ASTPrinter());
    }
    return rootNode;
}
Also used : Start(de.be4.eventbalg.core.parser.node.Start) EventBParser(de.be4.eventbalg.core.parser.EventBParser) ASTPrinter(de.be4.eventbalg.core.parser.analysis.ASTPrinter)

Aggregations

BException (de.be4.classicalb.core.parser.exceptions.BException)22 CheckException (de.be4.classicalb.core.parser.exceptions.CheckException)15 IOException (java.io.IOException)9 ArrayList (java.util.ArrayList)9 Start (de.be4.eventbalg.core.parser.node.Start)7 Test (org.junit.Test)7 BCompoundException (de.be4.classicalb.core.parser.exceptions.BCompoundException)6 Start (de.be4.eventb.core.parser.node.Start)6 PreParseException (de.be4.classicalb.core.parser.exceptions.PreParseException)4 AIdentifierExpression (de.be4.classicalb.core.parser.node.AIdentifierExpression)4 BLexerException (de.be4.classicalb.core.parser.exceptions.BLexerException)3 LexerException (de.be4.classicalb.core.parser.lexer.LexerException)3 TIdentifierLiteral (de.be4.classicalb.core.parser.node.TIdentifierLiteral)3 PrettyPrinter (de.be4.classicalb.core.parser.util.PrettyPrinter)3 AMachineParseUnit (de.be4.eventb.core.parser.node.AMachineParseUnit)3 File (java.io.File)3 ParsingBehaviour (de.be4.classicalb.core.parser.ParsingBehaviour)2 BParseException (de.be4.classicalb.core.parser.exceptions.BParseException)2 Token (de.be4.classicalb.core.preparser.node.Token)2 AInvariant (de.be4.eventb.core.parser.node.AInvariant)2