Search in sources :

Example 6 with VisitorException

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

the class ReferencedMachines method registerMachineNames.

private void registerMachineNames(List<PExpression> referencedMachineList) {
    for (PExpression machineExpression : referencedMachineList) {
        if (machineExpression instanceof AIdentifierExpression) {
            AIdentifierExpression identifier = (AIdentifierExpression) machineExpression;
            String name = getIdentifier(identifier.getIdentifier());
            final MachineReference machineReference = new MachineReference(name, identifier);
            if (this.filePathTable.containsKey(name)) {
                machineReference.setDirectoryPath(filePathTable.get(name));
            }
            referncesTable.put(name, machineReference);
        } else if (machineExpression instanceof AFileExpression) {
            final AFileExpression fileNode = (AFileExpression) machineExpression;
            final AIdentifierExpression identifier = (AIdentifierExpression) fileNode.getIdentifier();
            String file = fileNode.getContent().getText().replaceAll("\"", "");
            String name = getIdentifier(identifier.getIdentifier());
            MachineReference machineReference;
            try {
                machineReference = new MachineReference(name, identifier, file);
                referncesTable.put(name, machineReference);
            } catch (CheckException e) {
                throw new VisitorException(e);
            }
        } else {
            throw new AssertionError("Not supported class: " + machineExpression.getClass());
        }
    }
}
Also used : CheckException(de.be4.classicalb.core.parser.exceptions.CheckException) AIdentifierExpression(de.be4.classicalb.core.parser.node.AIdentifierExpression) AFileExpression(de.be4.classicalb.core.parser.node.AFileExpression) TPragmaIdOrString(de.be4.classicalb.core.parser.node.TPragmaIdOrString) VisitorException(de.be4.classicalb.core.parser.exceptions.VisitorException) PExpression(de.be4.classicalb.core.parser.node.PExpression) AFileMachineReference(de.be4.classicalb.core.parser.node.AFileMachineReference) AMachineReference(de.be4.classicalb.core.parser.node.AMachineReference)

Example 7 with VisitorException

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

the class ReferencedMachines method determineRootDirectory.

private void determineRootDirectory(final TPragmaIdOrString packageTerminal, final Node node) {
    final String text = packageTerminal.getText();
    if ((text.startsWith("\"") && text.endsWith("\""))) {
        this.packageName = text.replaceAll("\"", "");
    } else {
        this.packageName = text;
    }
    final String[] packageNameArray = determinePackage(packageTerminal, node);
    File dir;
    try {
        dir = mainFile.getCanonicalFile();
    } catch (IOException e) {
        throw new VisitorIOException(e);
    }
    for (int i = packageNameArray.length - 1; i >= 0; i--) {
        final String name1 = packageNameArray[i];
        dir = dir.getParentFile();
        final String name2 = dir.getName();
        if (!name1.equals(name2)) {
            throw new VisitorException(new CheckException(String.format("Package declaration '%s' does not match the folder structure: %s vs %s", this.packageName, name1, name2), node));
        }
    }
    rootDirectory = dir.getParentFile();
}
Also used : CheckException(de.be4.classicalb.core.parser.exceptions.CheckException) TPragmaIdOrString(de.be4.classicalb.core.parser.node.TPragmaIdOrString) IOException(java.io.IOException) VisitorIOException(de.be4.classicalb.core.parser.exceptions.VisitorIOException) VisitorIOException(de.be4.classicalb.core.parser.exceptions.VisitorIOException) VisitorException(de.be4.classicalb.core.parser.exceptions.VisitorException) File(java.io.File)

Example 8 with VisitorException

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

the class OpSubstitutions method setTypeSubstDef.

private void setTypeSubstDef(final AFuncOpSubstitution node, final String idString) {
    final AExpressionDefinitionDefinition oldDefinition = (AExpressionDefinitionDefinition) definitions.getDefinition(idString);
    final Node defRhs = oldDefinition.getRhs();
    final PSubstitution rhsSubst;
    if (defRhs instanceof AFunctionExpression) {
        final AFunctionExpression rhsFunction = (AFunctionExpression) defRhs;
        rhsSubst = new AOpSubstitution(rhsFunction.getIdentifier(), new LinkedList<PExpression>(rhsFunction.getParameters()));
        rhsSubst.setStartPos(rhsFunction.getStartPos());
        rhsSubst.setEndPos(rhsFunction.getEndPos());
    } else if (defRhs instanceof AIdentifierExpression) {
        final AIdentifierExpression rhsIdent = (AIdentifierExpression) defRhs;
        rhsSubst = new AOpSubstitution(rhsIdent, new LinkedList<PExpression>());
        rhsSubst.setStartPos(rhsIdent.getStartPos());
        rhsSubst.setEndPos(rhsIdent.getEndPos());
    } else {
        // some other expression was parsed (NOT allowed)
        throw new VisitorException(new CheckException("Expecting operation", node));
    }
    final TIdentifierLiteral oldDefId = oldDefinition.getName();
    final TDefLiteralSubstitution defId = new TDefLiteralSubstitution(oldDefId.getText(), oldDefId.getLine(), oldDefId.getPos());
    final ASubstitutionDefinitionDefinition substDef = new ASubstitutionDefinitionDefinition(defId, new LinkedList<PExpression>(oldDefinition.getParameters()), rhsSubst);
    substDef.setStartPos(oldDefinition.getStartPos());
    substDef.setEndPos(oldDefinition.getEndPos());
    definitions.replaceDefinition(idString, Type.Substitution, substDef);
    oldDefinition.replaceBy(substDef);
}
Also used : CheckException(de.be4.classicalb.core.parser.exceptions.CheckException) TDefLiteralSubstitution(de.be4.classicalb.core.parser.node.TDefLiteralSubstitution) AIdentifierExpression(de.be4.classicalb.core.parser.node.AIdentifierExpression) Node(de.be4.classicalb.core.parser.node.Node) PositionedNode(de.hhu.stups.sablecc.patch.PositionedNode) ASubstitutionDefinitionDefinition(de.be4.classicalb.core.parser.node.ASubstitutionDefinitionDefinition) PExpression(de.be4.classicalb.core.parser.node.PExpression) TIdentifierLiteral(de.be4.classicalb.core.parser.node.TIdentifierLiteral) LinkedList(java.util.LinkedList) PSubstitution(de.be4.classicalb.core.parser.node.PSubstitution) AFunctionExpression(de.be4.classicalb.core.parser.node.AFunctionExpression) AExpressionDefinitionDefinition(de.be4.classicalb.core.parser.node.AExpressionDefinitionDefinition) AOpSubstitution(de.be4.classicalb.core.parser.node.AOpSubstitution) VisitorException(de.be4.classicalb.core.parser.exceptions.VisitorException)

Example 9 with VisitorException

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

the class OpSubstitutions method caseAIdentifierExpression.

@Override
public void caseAIdentifierExpression(final AIdentifierExpression node) {
    final String identifierString = Utils.getTIdentifierListAsString(node.getIdentifier());
    final Integer number = scopedVariables.get(identifierString);
    final Type type = definitions.getType(identifierString);
    if (number == null && type != Type.NoDefinition) {
        if (type == Type.Expression || type == Type.ExprOrSubst) {
            /*
				 * getFirst() is enough cause definitions cannot have composed
				 * identifiers
				 */
            replaceWithDefExpression(node, node.getIdentifier().getFirst(), null);
            if (type == Type.ExprOrSubst) {
                // type is determined now => set to Expression
                definitions.setDefinitionType(identifierString, Type.Expression);
            }
        } else {
            // finding some other type here is an error!
            throw new VisitorException(new CheckException("Expecting expression here but found definition with type '" + type + "'", node));
        }
    }
}
Also used : Type(de.be4.classicalb.core.parser.IDefinitions.Type) CheckException(de.be4.classicalb.core.parser.exceptions.CheckException) VisitorException(de.be4.classicalb.core.parser.exceptions.VisitorException)

Aggregations

CheckException (de.be4.classicalb.core.parser.exceptions.CheckException)9 VisitorException (de.be4.classicalb.core.parser.exceptions.VisitorException)9 TPragmaIdOrString (de.be4.classicalb.core.parser.node.TPragmaIdOrString)6 AIdentifierExpression (de.be4.classicalb.core.parser.node.AIdentifierExpression)3 PExpression (de.be4.classicalb.core.parser.node.PExpression)3 Type (de.be4.classicalb.core.parser.IDefinitions.Type)2 AFileMachineReference (de.be4.classicalb.core.parser.node.AFileMachineReference)2 AFunctionExpression (de.be4.classicalb.core.parser.node.AFunctionExpression)2 AMachineReference (de.be4.classicalb.core.parser.node.AMachineReference)2 AOpSubstitution (de.be4.classicalb.core.parser.node.AOpSubstitution)2 TDefLiteralSubstitution (de.be4.classicalb.core.parser.node.TDefLiteralSubstitution)2 TIdentifierLiteral (de.be4.classicalb.core.parser.node.TIdentifierLiteral)2 PositionedNode (de.hhu.stups.sablecc.patch.PositionedNode)2 File (java.io.File)2 LinkedList (java.util.LinkedList)2 BParseException (de.be4.classicalb.core.parser.exceptions.BParseException)1 VisitorIOException (de.be4.classicalb.core.parser.exceptions.VisitorIOException)1 ADefinitionSubstitution (de.be4.classicalb.core.parser.node.ADefinitionSubstitution)1 AExpressionDefinitionDefinition (de.be4.classicalb.core.parser.node.AExpressionDefinitionDefinition)1 AFileExpression (de.be4.classicalb.core.parser.node.AFileExpression)1