Search in sources :

Example 11 with BException

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

the class RulesTransformation method runTransformation.

public void runTransformation() throws BCompoundException {
    start.apply(this);
    DefinitionInjector.injectDefinitions(start, iDefinitions);
    MissingPositionsAdder.injectPositions(start);
    if (!this.errorList.isEmpty()) {
        List<BException> list = new ArrayList<>();
        for (CheckException checkException : this.errorList) {
            list.add(new BException(this.rulesMachineChecker.getFileName(), checkException));
        }
        throw new BCompoundException(list);
    }
}
Also used : CheckException(de.be4.classicalb.core.parser.exceptions.CheckException) ArrayList(java.util.ArrayList) BException(de.be4.classicalb.core.parser.exceptions.BException) BCompoundException(de.be4.classicalb.core.parser.exceptions.BCompoundException)

Example 12 with BException

use of de.be4.classicalb.core.parser.exceptions.BException 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());
}
Also used : ParserException(de.be4.classicalb.core.preparser.parser.ParserException) Start(de.be4.classicalb.core.preparser.node.Start) Token(de.be4.classicalb.core.preparser.node.Token) DefinitionPreCollector(de.be4.classicalb.core.parser.analysis.checking.DefinitionPreCollector) Parser(de.be4.classicalb.core.preparser.parser.Parser) PreParseException(de.be4.classicalb.core.parser.exceptions.PreParseException) BLexerException(de.be4.classicalb.core.parser.exceptions.BLexerException) LexerException(de.be4.classicalb.core.preparser.lexer.LexerException)

Example 13 with BException

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

the class PrologExceptionPrinter method printException.

public static void printException(final OutputStream out, final BCompoundException e, boolean useIndentation, boolean lineOneOff) {
    IPrologTermOutput pto = new PrologTermOutput(out, useIndentation);
    if (e.getBExceptions().size() > 1) {
        pto.openTerm("compound_exception", true);
        pto.openList();
        BCompoundException comp = e;
        for (Exception ex : comp.getBExceptions()) {
            try {
                printBException(pto, (BException) ex, useIndentation, lineOneOff);
            } catch (ClassCastException e2) {
                throw new IllegalStateException("Unexpected exception in compound exceptions:" + ex.getClass().getSimpleName());
            }
        }
        pto.closeList();
        pto.closeTerm();
        pto.fullstop();
        pto.flush();
        return;
    } else if (e.getBExceptions().size() == 1) {
        // single BException
        printBException(pto, e.getBExceptions().get(0), useIndentation, lineOneOff);
        pto.fullstop();
        pto.flush();
    } else {
        throw new IllegalStateException("Empty compoundException.");
    }
}
Also used : PrologTermOutput(de.prob.prolog.output.PrologTermOutput) IPrologTermOutput(de.prob.prolog.output.IPrologTermOutput) BCompoundException(de.be4.classicalb.core.parser.exceptions.BCompoundException) BException(de.be4.classicalb.core.parser.exceptions.BException) BLexerException(de.be4.classicalb.core.parser.exceptions.BLexerException) PreParseException(de.be4.classicalb.core.parser.exceptions.PreParseException) CheckException(de.be4.classicalb.core.parser.exceptions.CheckException) LexerException(de.be4.classicalb.core.parser.lexer.LexerException) IOException(java.io.IOException) BCompoundException(de.be4.classicalb.core.parser.exceptions.BCompoundException) BParseException(de.be4.classicalb.core.parser.exceptions.BParseException) IPrologTermOutput(de.prob.prolog.output.IPrologTermOutput)

Example 14 with BException

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

the class RecursiveMachineLoader method recursivlyLoadMachine.

private void recursivlyLoadMachine(final File machineFile, final Start currentAst, final List<String> ancestors, final boolean isMain, File directory, final IDefinitions definitions) throws BCompoundException {
    // make a copy of the referencing machines
    List<String> newAncestors = new ArrayList<>(ancestors);
    ReferencedMachines refMachines = new ReferencedMachines(machineFile, currentAst, parsingBehaviour.isMachineNameMustMatchFileName());
    try {
        refMachines.findReferencedMachines();
    } catch (BException e) {
        throw new BCompoundException(e);
    }
    final String name = refMachines.getName();
    if (name == null) {
        /*
			 * the parsed file is a definition file, hence the name of the
			 * machine is null
			 */
        throw new BCompoundException(new BException(machineFile.getName(), "Expecting a B machine but was a definition file in file: '" + machineFile.getName() + "\'", null));
    }
    machineFilesLoaded.add(machineFile);
    final int fileNumber = machineFilesLoaded.indexOf(machineFile) + 1;
    getNodeIdMapping().assignIdentifiers(fileNumber, currentAst);
    definitions.assignIdsToNodes(getNodeIdMapping(), machineFilesLoaded);
    injectDefinitions(currentAst, definitions);
    getParsedMachines().put(name, currentAst);
    parsedFiles.put(name, machineFile);
    if (name != null) {
        newAncestors.add(name);
    }
    if (isMain) {
        main = name;
    }
    final Set<String> referencesSet = refMachines.getSetOfReferencedMachines();
    try {
        checkForCycles(newAncestors, referencesSet);
    } catch (BException e) {
        throw new BCompoundException(e);
    }
    final List<MachineReference> references = refMachines.getReferences();
    for (final MachineReference refMachine : references) {
        try {
            final String filePragma = refMachine.getPath();
            File file = null;
            if (filePragma == null) {
                file = lookupFile(directory, refMachine, newAncestors, refMachines.getPathList());
            } else {
                File p = new File(filePragma);
                if (p.isAbsolute()) {
                    file = p;
                } else {
                    file = new File(directory, filePragma);
                }
            }
            if (file.exists() && parsedFiles.containsKey(refMachine.getName()) && !parsedFiles.get(refMachine.getName()).getCanonicalPath().equals(file.getCanonicalPath())) {
                final String message = "Two files with the same name are referenced:\n" + parsedFiles.get(refMachine.getName()).getCanonicalPath() + "\n" + file.getCanonicalPath();
                throw new BException(machineFile.getCanonicalPath(), new CheckException(message, refMachine.getNode()));
            }
            if (!getParsedMachines().containsKey(refMachine.getName())) {
                try {
                    loadMachine(newAncestors, file);
                } catch (IOException e) {
                    throw new BException(machineFile.getCanonicalPath(), new CheckException(e.getMessage(), refMachine.getNode(), e));
                }
            }
        } catch (final BException e) {
            // we do not longer wrap a B Exception in a B Exception
            throw new BCompoundException(e);
        } catch (final IOException e) {
            throw new BCompoundException(new BException(machineFile.getAbsolutePath(), e));
        } catch (final CheckException e) {
            throw new BCompoundException(new BException(machineFile.getAbsolutePath(), e));
        }
    }
}
Also used : CheckException(de.be4.classicalb.core.parser.exceptions.CheckException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) BException(de.be4.classicalb.core.parser.exceptions.BException) File(java.io.File) BCompoundException(de.be4.classicalb.core.parser.exceptions.BCompoundException)

Example 15 with BException

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

the class RulesUtil method getRulesMachineAsBMachine.

public static String getRulesMachineAsBMachine(final String content) {
    RulesProject rulesProject = new RulesProject();
    rulesProject.parseRulesMachines(content, new String[] {});
    rulesProject.checkAndTranslateProject();
    List<IModel> bModels = rulesProject.getBModels();
    List<BException> bExceptionList = rulesProject.getBExceptionList();
    if (!bExceptionList.isEmpty()) {
        throw new RuntimeException(bExceptionList.get(0));
    }
    IModel model = bModels.get(bModels.size() - 2);
    PrettyPrinter pp = new PrettyPrinter();
    model.getStart().apply(pp);
    return pp.getPrettyPrint();
}
Also used : PrettyPrinter(de.be4.classicalb.core.parser.util.PrettyPrinter) BException(de.be4.classicalb.core.parser.exceptions.BException)

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