Search in sources :

Example 6 with BException

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

the class RulesProject method checkForCycles.

private boolean checkForCycles(AbstractOperation operation, List<TIdentifierLiteral> directDependencies, List<AbstractOperation> ancestors) {
    List<String> ancestorsNames = new ArrayList<>();
    for (AbstractOperation op : ancestors) {
        String opName = op.getName();
        ancestorsNames.add(opName);
    }
    for (TIdentifierLiteral id : directDependencies) {
        final String opName = id.getText();
        if (ancestorsNames.contains(opName)) {
            StringBuilder sb = new StringBuilder();
            for (int index = ancestorsNames.indexOf(opName); index < ancestors.size(); index++) {
                final String name = ancestors.get(index).getName();
                sb.append(name);
                sb.append(" -> ");
            }
            sb.append(opName);
            this.bExceptionList.add(new BException(operation.getFileName(), new CheckException("Cyclic dependencies between operations: " + sb.toString(), id)));
            return true;
        }
    }
    return false;
}
Also used : CheckException(de.be4.classicalb.core.parser.exceptions.CheckException) ArrayList(java.util.ArrayList) BException(de.be4.classicalb.core.parser.exceptions.BException) TIdentifierLiteral(de.be4.classicalb.core.parser.node.TIdentifierLiteral)

Example 7 with BException

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

the class RulesProject method checkReferencedRuleOperations.

public void checkReferencedRuleOperations() {
    if (this.hasErrors()) {
        return;
    }
    final HashMap<String, RulesParseUnit> map = new HashMap<>();
    for (IModel model : bModels) {
        RulesParseUnit parseUnit = (RulesParseUnit) model;
        map.put(parseUnit.getMachineName(), parseUnit);
    }
    for (IModel model : bModels) {
        if (model instanceof RulesParseUnit) {
            RulesParseUnit rulesParseUnit = (RulesParseUnit) model;
            Set<AIdentifierExpression> referencedRuleOperations = rulesParseUnit.getRulesMachineChecker().getReferencedRuleOperations();
            final HashSet<String> knownRules = new HashSet<>();
            for (RuleOperation ruleOperation : rulesParseUnit.getRulesMachineChecker().getRuleOperations()) {
                knownRules.add(ruleOperation.getName());
            }
            for (RulesMachineReference rulesMachineReference : rulesParseUnit.getMachineReferences()) {
                String referenceName = rulesMachineReference.getName();
                RulesParseUnit otherParseUnit = map.get(referenceName);
                for (RuleOperation ruleOperation : otherParseUnit.getRulesMachineChecker().getRuleOperations()) {
                    knownRules.add(ruleOperation.getName());
                }
            }
            for (AIdentifierExpression aIdentifierExpression : referencedRuleOperations) {
                String ruleName = Utils.getAIdentifierAsString(aIdentifierExpression);
                if (!knownRules.contains(ruleName)) {
                    this.bExceptionList.add(new BException(rulesParseUnit.getPath(), new CheckException("Unknown rule '" + ruleName + "'.", aIdentifierExpression)));
                }
            }
        }
    }
}
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) BException(de.be4.classicalb.core.parser.exceptions.BException) HashSet(java.util.HashSet)

Example 8 with BException

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

the class RulesProject method checkDependsOnComputations.

private void checkDependsOnComputations(AbstractOperation operation) {
    boolean errorOccured = false;
    for (AIdentifierExpression aIdentifierExpression : operation.getDependsOnComputationList()) {
        final String name = aIdentifierExpression.getIdentifier().get(0).getText();
        if (allOperations.containsKey(name)) {
            AbstractOperation abstractOperation = allOperations.get(name);
            if (!(abstractOperation instanceof ComputationOperation)) {
                this.bExceptionList.add(new BException(operation.getFileName(), new CheckException("Identifier '" + name + "' is not a COMPUTATION.", aIdentifierExpression)));
                errorOccured = true;
            }
        } else {
            errorOccured = true;
            this.bExceptionList.add(new BException(operation.getFileName(), new CheckException("Unknown operation: '" + name + "'.", aIdentifierExpression)));
        }
    }
    if (!errorOccured) {
        checkVisibilityOfAIdentifierList(operation, operation.getDependsOnComputationList());
    }
}
Also used : CheckException(de.be4.classicalb.core.parser.exceptions.CheckException) AIdentifierExpression(de.be4.classicalb.core.parser.node.AIdentifierExpression) BException(de.be4.classicalb.core.parser.exceptions.BException)

Example 9 with BException

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

the class RulesProject method checkIdentifiers.

private void checkIdentifiers() {
    if (this.hasErrors()) {
        /*
			 * if there is already an error such as an parse error in one
			 * machine, it makes no sense to check for invalid identifiers
			 * because all declarations of this machine are missing.
			 */
        return;
    }
    final HashMap<String, RulesParseUnit> map = new HashMap<>();
    for (IModel model : bModels) {
        RulesParseUnit parseUnit = (RulesParseUnit) model;
        map.put(parseUnit.getMachineName(), parseUnit);
    }
    for (IModel model : bModels) {
        RulesParseUnit parseUnit = (RulesParseUnit) model;
        HashSet<String> knownIdentifiers = new HashSet<>();
        List<RulesMachineReference> machineReferences = parseUnit.getMachineReferences();
        for (RulesMachineReference rulesMachineReference : machineReferences) {
            String referenceName = rulesMachineReference.getName();
            RulesParseUnit rulesParseUnit = map.get(referenceName);
            RulesMachineChecker checker = rulesParseUnit.getRulesMachineChecker();
            knownIdentifiers.addAll(checker.getGlobalIdentifierNames());
        }
        RulesMachineChecker checker = parseUnit.getRulesMachineChecker();
        Map<String, HashSet<Node>> unknownIdentifierMap = checker.getUnknownIdentifier();
        HashSet<String> unknownIdentifiers = new HashSet<>(unknownIdentifierMap.keySet());
        unknownIdentifiers.removeAll(knownIdentifiers);
        for (String name : unknownIdentifiers) {
            HashSet<Node> hashSet = unknownIdentifierMap.get(name);
            Node node = hashSet.iterator().next();
            this.bExceptionList.add(new BException(parseUnit.getPath(), new CheckException("Unknown identifier '" + name + "'.", node)));
        }
    }
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) CheckException(de.be4.classicalb.core.parser.exceptions.CheckException) Node(de.be4.classicalb.core.parser.node.Node) BException(de.be4.classicalb.core.parser.exceptions.BException) HashSet(java.util.HashSet)

Example 10 with BException

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

the class RulesProject method checkDependsOnRules.

private void checkDependsOnRules(AbstractOperation operation) {
    boolean errorOccured = false;
    for (AIdentifierExpression aIdentifierExpression : operation.getDependsOnRulesList()) {
        final String name = aIdentifierExpression.getIdentifier().get(0).getText();
        if (allOperations.containsKey(name)) {
            AbstractOperation abstractOperation = allOperations.get(name);
            if (!(abstractOperation instanceof RuleOperation)) {
                this.bExceptionList.add(new BException(operation.getFileName(), new CheckException("Operation '" + name + "' is not a RULE operation.", aIdentifierExpression)));
                errorOccured = true;
            }
        } else {
            errorOccured = true;
            this.bExceptionList.add(new BException(operation.getFileName(), new CheckException("Unknown operation: '" + name + "'.", aIdentifierExpression)));
        }
    }
    if (!errorOccured) {
        checkVisibilityOfAIdentifierList(operation, operation.getDependsOnRulesList());
    }
}
Also used : CheckException(de.be4.classicalb.core.parser.exceptions.CheckException) AIdentifierExpression(de.be4.classicalb.core.parser.node.AIdentifierExpression) 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