Search in sources :

Example 36 with AIdentifierExpression

use of de.be4.classicalb.core.parser.node.AIdentifierExpression in project probparsers by bendisposto.

the class BMachine method addPromotesClause.

public void addPromotesClause(List<String> operationList) {
    APromotesMachineClause promotes = new APromotesMachineClause();
    List<PExpression> opList = new ArrayList<>();
    for (String name : operationList) {
        List<TIdentifierLiteral> idList = new ArrayList<>();
        idList.add(new TIdentifierLiteral(name));
        AIdentifierExpression idExpr = new AIdentifierExpression(idList);
        opList.add(idExpr);
    }
    promotes.setOperationNames(opList);
    this.parseUnit.getMachineClauses().add(promotes);
}
Also used : AIdentifierExpression(de.be4.classicalb.core.parser.node.AIdentifierExpression) ArrayList(java.util.ArrayList) APromotesMachineClause(de.be4.classicalb.core.parser.node.APromotesMachineClause) PExpression(de.be4.classicalb.core.parser.node.PExpression) TIdentifierLiteral(de.be4.classicalb.core.parser.node.TIdentifierLiteral)

Example 37 with AIdentifierExpression

use of de.be4.classicalb.core.parser.node.AIdentifierExpression 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 38 with AIdentifierExpression

use of de.be4.classicalb.core.parser.node.AIdentifierExpression 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 39 with AIdentifierExpression

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

Example 40 with AIdentifierExpression

use of de.be4.classicalb.core.parser.node.AIdentifierExpression in project probparsers by bendisposto.

the class RulesProject method findImplicitDependenciesToComputations.

private void findImplicitDependenciesToComputations() {
    HashMap<String, ComputationOperation> variableToComputation = new HashMap<>();
    for (AbstractOperation operation : allOperations.values()) {
        if (operation instanceof ComputationOperation && !operation.replacesOperation()) {
            ComputationOperation comp = (ComputationOperation) operation;
            for (String defName : comp.getDefineVariables()) {
                variableToComputation.put(defName, comp);
            }
        }
    }
    for (AbstractOperation operation : allOperations.values()) {
        final Set<String> readVariables = operation.getReadVariables();
        readVariables.retainAll(variableToComputation.keySet());
        final HashSet<String> variablesInScope = new HashSet<>();
        // defined before read.
        if (operation instanceof ComputationOperation) {
            variablesInScope.addAll(((ComputationOperation) operation).getDefineVariables());
        }
        for (AIdentifierExpression aIdentifier : operation.getDependsOnComputationList()) {
            String opName = aIdentifier.getIdentifier().get(0).getText();
            AbstractOperation abstractOperation = allOperations.get(opName);
            if (abstractOperation instanceof ComputationOperation) {
                variablesInScope.addAll(((ComputationOperation) abstractOperation).getDefineVariables());
            }
        }
        readVariables.removeAll(variablesInScope);
        if (!readVariables.isEmpty()) {
            List<ComputationOperation> inferredDependenciesToComputations = new ArrayList<>();
            for (String varName : readVariables) {
                ComputationOperation computationOperation = variableToComputation.get(varName);
                inferredDependenciesToComputations.add(computationOperation);
                List<String> machineReferences = operation.getMachineReferencesAsString();
                machineReferences.add(operation.getMachineName());
                if (!machineReferences.contains(computationOperation.getMachineName())) {
                    this.bExceptionList.add(new BException(operation.getFileName(), new CheckException("Missing reference to RULES_MACHINE '" + computationOperation.getMachineName() + "' in order to use variable '" + varName + "'.", operation.getVariableReadByName(varName))));
                }
                operation.setImplicitComputationDependencies(inferredDependenciesToComputations);
            }
        } else {
            operation.setImplicitComputationDependencies(Collections.<ComputationOperation>emptyList());
        }
    }
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) CheckException(de.be4.classicalb.core.parser.exceptions.CheckException) AIdentifierExpression(de.be4.classicalb.core.parser.node.AIdentifierExpression) ArrayList(java.util.ArrayList) BException(de.be4.classicalb.core.parser.exceptions.BException) HashSet(java.util.HashSet)

Aggregations

AIdentifierExpression (de.be4.classicalb.core.parser.node.AIdentifierExpression)43 PExpression (de.be4.classicalb.core.parser.node.PExpression)27 ArrayList (java.util.ArrayList)25 CheckException (de.be4.classicalb.core.parser.exceptions.CheckException)22 TIdentifierLiteral (de.be4.classicalb.core.parser.node.TIdentifierLiteral)14 Node (de.be4.classicalb.core.parser.node.Node)5 Test (org.junit.Test)5 BException (de.be4.classicalb.core.parser.exceptions.BException)4 VisitorException (de.be4.classicalb.core.parser.exceptions.VisitorException)4 HashSet (java.util.HashSet)4 LinkedList (java.util.LinkedList)4 AExpressionDefinitionDefinition (de.be4.classicalb.core.parser.node.AExpressionDefinitionDefinition)3 AFunctionExpression (de.be4.classicalb.core.parser.node.AFunctionExpression)3 AIntegerExpression (de.be4.classicalb.core.parser.node.AIntegerExpression)3 AStringExpression (de.be4.classicalb.core.parser.node.AStringExpression)3 BParser (de.be4.classicalb.core.parser.BParser)2 IDefinitions (de.be4.classicalb.core.parser.IDefinitions)2 Type (de.be4.classicalb.core.parser.IDefinitions.Type)2 AEqualPredicate (de.be4.classicalb.core.parser.node.AEqualPredicate)2 AExpressionParseUnit (de.be4.classicalb.core.parser.node.AExpressionParseUnit)2