Search in sources :

Example 31 with CheckException

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

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

the class PredicatesTest method testBFalse.

@Test
public void testBFalse() throws BCompoundException {
    parser.getOptions().setRestrictProverExpressions(true);
    try {
        getPredicateAsString("bfalse");
        fail("exception expected");
    } catch (BCompoundException e) {
        assertTrue(e.getFirstException().getCause() instanceof CheckException);
    }
    parser.getOptions().setRestrictProverExpressions(false);
    final String actual = getPredicateAsString("bfalse");
    final String expected = "AFalsityPredicate()";
    assertEquals(expected, actual);
}
Also used : CheckException(de.be4.classicalb.core.parser.exceptions.CheckException) Ast2String(util.Ast2String) BCompoundException(de.be4.classicalb.core.parser.exceptions.BCompoundException) Test(org.junit.Test)

Example 33 with CheckException

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

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

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

the class DefinitionsTest method testExprOrSubstWParams3.

@Test
public void testExprOrSubstWParams3() {
    final String testMachine = "MACHINE Test\nDEFINITIONS\ndefExpr(x)==g(x)\nOPERATIONS\nop=PRE defExpr(x)=42 THEN defExpr(x) END\nEND";
    try {
        getTreeAsString(testMachine);
        fail("Expected exception was not thrown");
    } catch (final BCompoundException e) {
        final CheckException cause = (CheckException) e.getCause();
        assertEquals("Expecting substitution here but found definition with type 'Expression'", cause.getLocalizedMessage());
    // IGNORE, is expected
    }
}
Also used : CheckException(de.be4.classicalb.core.parser.exceptions.CheckException) Helpers.getTreeAsString(util.Helpers.getTreeAsString) BCompoundException(de.be4.classicalb.core.parser.exceptions.BCompoundException) Test(org.junit.Test)

Aggregations

CheckException (de.be4.classicalb.core.parser.exceptions.CheckException)78 BCompoundException (de.be4.classicalb.core.parser.exceptions.BCompoundException)19 AIdentifierExpression (de.be4.classicalb.core.parser.node.AIdentifierExpression)17 PExpression (de.be4.classicalb.core.parser.node.PExpression)16 Test (org.junit.Test)15 BException (de.be4.classicalb.core.parser.exceptions.BException)14 ArrayList (java.util.ArrayList)13 TPragmaIdOrString (de.be4.classicalb.core.parser.node.TPragmaIdOrString)12 Ast2String (util.Ast2String)10 VisitorException (de.be4.classicalb.core.parser.exceptions.VisitorException)9 Node (de.be4.classicalb.core.parser.node.Node)8 TIdentifierLiteral (de.be4.classicalb.core.parser.node.TIdentifierLiteral)8 File (java.io.File)8 HashSet (java.util.HashSet)8 IOException (java.io.IOException)7 ARuleOperation (de.be4.classicalb.core.parser.node.ARuleOperation)4 PositionedNode (de.hhu.stups.sablecc.patch.PositionedNode)4 Helpers.getTreeAsString (util.Helpers.getTreeAsString)4 Type (de.be4.classicalb.core.parser.IDefinitions.Type)3 AIntegerExpression (de.be4.classicalb.core.parser.node.AIntegerExpression)3