Search in sources :

Example 16 with BCompoundException

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

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

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

the class StructuralTest method testUnclosedComment.

@Test
public void testUnclosedComment() {
    final String emptyMachine = "MACHINE ClassicalB\n SETS pp ; qq\n /* CONSTANTS ccc,ddd\n VARIABLES xxx,yyy\n OPERATIONS\n  op1 = BEGIN xxx := 1; v <-- op2(2) END;\n  op2 = ANY q WHERE q : NAT THEN yyy := ccc END\nEND";
    try {
        getTreeAsString(emptyMachine);
        fail("Expected exception was not thrown");
    } catch (final BCompoundException e) {
        final BLexerException ex = (BLexerException) e.getCause();
        // checking the start position of the comment
        assertEquals(3, ex.getLastLine());
        assertEquals(2, ex.getLastPos());
        assertTrue(e.getMessage().contains("Comment not closed."));
    }
}
Also used : BLexerException(de.be4.classicalb.core.parser.exceptions.BLexerException) Ast2String(util.Ast2String) BCompoundException(de.be4.classicalb.core.parser.exceptions.BCompoundException) Test(org.junit.Test)

Example 19 with BCompoundException

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

the class StructuralTest method checkForInvalidSemicolonBeforeEnd.

@Test
public void checkForInvalidSemicolonBeforeEnd() throws Exception {
    String s = "MACHINE MissingSemicolon\nOPERATIONS\n Foo=BEGIN skip\n; END\nEND";
    try {
        getTreeAsString(s);
        fail("Invalid Semicolon was not detected");
    } catch (BCompoundException e) {
        final CheckException cause = (CheckException) e.getCause();
        Node node = cause.getNodes()[0];
        assertEquals(4, node.getStartPos().getLine());
        assertEquals(1, node.getStartPos().getPos());
        assertTrue(e.getMessage().contains("Invalid semicolon after last substitution"));
    }
}
Also used : CheckException(de.be4.classicalb.core.parser.exceptions.CheckException) Node(de.be4.classicalb.core.parser.node.Node) Ast2String(util.Ast2String) BCompoundException(de.be4.classicalb.core.parser.exceptions.BCompoundException) Test(org.junit.Test)

Example 20 with BCompoundException

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

the class SyntaxErrorsDetectedOnTokenStreamTest method checkForClauseAfterConjunction.

@Test
public void checkForClauseAfterConjunction() throws Exception {
    String s = "MACHINE Definitions\nPROPERTIES\n 1=1 & VARIABLES";
    try {
        getTreeAsString(s);
        fail("& VARIABLES was not detected");
    } catch (BCompoundException e) {
        System.out.println(e.getMessage());
        assertTrue(e.getMessage().contains("& VARIABLES"));
    }
}
Also used : Ast2String(util.Ast2String) BCompoundException(de.be4.classicalb.core.parser.exceptions.BCompoundException) Test(org.junit.Test)

Aggregations

BCompoundException (de.be4.classicalb.core.parser.exceptions.BCompoundException)73 Test (org.junit.Test)64 Ast2String (util.Ast2String)62 Start (de.be4.classicalb.core.parser.node.Start)52 BParser (de.be4.classicalb.core.parser.BParser)29 CheckException (de.be4.classicalb.core.parser.exceptions.CheckException)19 File (java.io.File)15 BParseException (de.be4.classicalb.core.parser.exceptions.BParseException)9 IOException (java.io.IOException)8 ArrayList (java.util.ArrayList)8 ParsingBehaviour (de.be4.classicalb.core.parser.ParsingBehaviour)7 RecursiveMachineLoader (de.be4.classicalb.core.parser.analysis.prolog.RecursiveMachineLoader)7 BException (de.be4.classicalb.core.parser.exceptions.BException)7 Helpers.getTreeAsString (util.Helpers.getTreeAsString)7 BLexerException (de.be4.classicalb.core.parser.exceptions.BLexerException)5 PreParseException (de.be4.classicalb.core.parser.exceptions.PreParseException)5 LexerException (de.be4.classicalb.core.parser.lexer.LexerException)5 Node (de.be4.classicalb.core.parser.node.Node)5 ASTProlog (de.be4.classicalb.core.parser.analysis.prolog.ASTProlog)4 IPrologTermOutput (de.prob.prolog.output.IPrologTermOutput)4