Search in sources :

Example 31 with PExpression

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

the class RulesTransformation method outAOperatorPredicate.

@Override
public void outAOperatorPredicate(AOperatorPredicate node) {
    // currently all operator handle rule names
    final List<PExpression> arguments = new ArrayList<>(node.getIdentifiers());
    final String operatorName = node.getName().getText();
    final AIdentifierExpression ruleIdentifier = (AIdentifierExpression) arguments.get(0);
    final String ruleName = ruleIdentifier.getIdentifier().get(0).getText();
    AbstractOperation operation = allOperations.get(ruleName);
    if (operation == null || !(operation instanceof RuleOperation)) {
        errorList.add(new CheckException(String.format("'%s' does not match any rule visible to this machine.", ruleName), node));
        return;
    }
    final RuleOperation rule = (RuleOperation) operation;
    switch(operatorName) {
        case RulesGrammar.SUCCEEDED_RULE:
            replacePredicateOperator(node, arguments, RULE_SUCCESS);
            return;
        case RulesGrammar.SUCCEEDED_RULE_ERROR_TYPE:
            replaceSucceededRuleErrorTypeOperator(node, ruleName, rule);
            return;
        case RulesGrammar.FAILED_RULE:
            replacePredicateOperator(node, arguments, RULE_FAIL);
            return;
        case RulesGrammar.FAILED_RULE_ALL_ERROR_TYPES:
            replaceFailedRuleAllErrorTypesOperator(node, rule);
            return;
        case RulesGrammar.FAILED_RULE_ERROR_TYPE:
            replaceFailedRuleErrorTypeOperator(node, rule);
            return;
        case RulesGrammar.NOT_CHECKED_RULE:
            replacePredicateOperator(node, arguments, RULE_NOT_CHECKED);
            return;
        case RulesGrammar.DISABLED_RULE:
            replacePredicateOperator(node, arguments, RULE_DISABLED);
            return;
        default:
            throw new AssertionError("should not happen: " + operatorName);
    }
}
Also used : CheckException(de.be4.classicalb.core.parser.exceptions.CheckException) ArrayList(java.util.ArrayList)

Example 32 with PExpression

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

the class RulesTransformation method translateGetRuleCounterExamplesOperator.

private void translateGetRuleCounterExamplesOperator(AOperatorExpression node) {
    final PExpression pExpression = node.getIdentifiers().get(0);
    final AIdentifierExpression id = (AIdentifierExpression) pExpression;
    final String ruleName = id.getIdentifier().get(0).getText();
    final AbstractOperation operation = allOperations.get(ruleName);
    if (operation == null || !(operation instanceof RuleOperation)) {
        errorList.add(new CheckException(String.format("'%s' does not match any rule visible to this machine.", ruleName), node));
        return;
    }
    final RuleOperation rule = (RuleOperation) operation;
    final String name = id.getIdentifier().get(0).getText() + RULE_COUNTER_EXAMPLE_VARIABLE_SUFFIX;
    if (node.getIdentifiers().size() == 1) {
        final AIdentifierExpression ctVariable = createIdentifier(name, pExpression);
        final ARangeExpression range = createPositinedNode(new ARangeExpression(ctVariable), node);
        node.replaceBy(range);
    } else {
        PExpression funcCall = getSetOfErrorMessagesByErrorType(name, node.getIdentifiers().get(1), rule.getNumberOfErrorTypes());
        node.replaceBy(funcCall);
    }
    return;
}
Also used : CheckException(de.be4.classicalb.core.parser.exceptions.CheckException)

Example 33 with PExpression

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

the class SourcePositionsTest method testVariablesSourcePositions.

@Test
public void testVariablesSourcePositions() throws Exception {
    final String testMachine = "MACHINE test\n" + "VARIABLES\n" + "  xx,\n" + "    yy\n" + "INVARIANT xx:INT & yy:INT\n" + "INITIALISATION xx,yy:=0,0\n" + "END\n";
    final Start result = getAst(testMachine);
    final AAbstractMachineParseUnit machine = (AAbstractMachineParseUnit) result.getPParseUnit();
    AVariablesMachineClause variables = null;
    for (final PMachineClause clause : machine.getMachineClauses()) {
        if (clause instanceof AVariablesMachineClause) {
            variables = (AVariablesMachineClause) clause;
            break;
        }
    }
    if (variables == null) {
        fail("variables clause not found");
    }
    final LinkedList<PExpression> ids = variables.getIdentifiers();
    assertEquals(2, ids.size());
    final AIdentifierExpression x = (AIdentifierExpression) ids.get(0);
    final AIdentifierExpression y = (AIdentifierExpression) ids.get(1);
    // VARIABLES block
    assertEquals(2, variables.getStartPos().getLine());
    assertEquals(1, variables.getStartPos().getPos());
    assertEquals(4, variables.getEndPos().getLine());
    assertEquals(7, variables.getEndPos().getPos());
    // variable x declaration
    assertEquals(3, x.getStartPos().getLine());
    assertEquals(3, x.getStartPos().getPos());
    assertEquals(3, x.getEndPos().getLine());
    assertEquals(5, x.getEndPos().getPos());
    // variable y declaration
    assertEquals(4, y.getStartPos().getLine());
    assertEquals(5, y.getStartPos().getPos());
    assertEquals(4, y.getEndPos().getLine());
    assertEquals(7, y.getEndPos().getPos());
}
Also used : AVariablesMachineClause(de.be4.classicalb.core.parser.node.AVariablesMachineClause) Start(de.be4.classicalb.core.parser.node.Start) AIdentifierExpression(de.be4.classicalb.core.parser.node.AIdentifierExpression) AAbstractMachineParseUnit(de.be4.classicalb.core.parser.node.AAbstractMachineParseUnit) PMachineClause(de.be4.classicalb.core.parser.node.PMachineClause) PExpression(de.be4.classicalb.core.parser.node.PExpression) Test(org.junit.Test)

Example 34 with PExpression

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

the class PreParser method determineType.

private DefinitionType determineType(final Token definition, final Token rhsToken, final Set<String> untypedDefinitions) throws PreParseException {
    final String definitionRhs = rhsToken.getText();
    de.be4.classicalb.core.parser.node.Start start;
    de.be4.classicalb.core.parser.node.Token errorToken = null;
    try {
        start = tryParsing(BParser.FORMULA_PREFIX, definitionRhs);
        // Predicate?
        PParseUnit parseunit = start.getPParseUnit();
        if (parseunit instanceof APredicateParseUnit) {
            return new DefinitionType(IDefinitions.Type.Predicate);
        }
        // Expression or Expression/Substituion (e.g. f(x))?
        AExpressionParseUnit expressionParseUnit = (AExpressionParseUnit) parseunit;
        PreParserIdentifierTypeVisitor visitor = new PreParserIdentifierTypeVisitor(untypedDefinitions);
        expressionParseUnit.apply(visitor);
        if (visitor.isKaboom()) {
            return new DefinitionType();
        }
        PExpression expression = expressionParseUnit.getExpression();
        if ((expression instanceof AIdentifierExpression) || (expression instanceof AFunctionExpression) || (expression instanceof ADefinitionExpression)) {
            return new DefinitionType(IDefinitions.Type.ExprOrSubst);
        }
        return new DefinitionType(IDefinitions.Type.Expression);
    } catch (de.be4.classicalb.core.parser.parser.ParserException e) {
        errorToken = e.getToken();
        try {
            tryParsing(BParser.SUBSTITUTION_PREFIX, definitionRhs);
            return new DefinitionType(IDefinitions.Type.Substitution, errorToken);
        } catch (de.be4.classicalb.core.parser.parser.ParserException ex) {
            final de.be4.classicalb.core.parser.node.Token errorToken2 = ex.getToken();
            if (errorToken.getLine() > errorToken2.getLine() || (errorToken.getLine() == errorToken2.getLine() && errorToken.getPos() >= errorToken2.getPos())) {
                final String newMessage = determineNewErrorMessageWithCorrectedPositionInformations(definition, rhsToken, errorToken, e.getMessage());
                return new DefinitionType(newMessage, errorToken);
            } else {
                final String newMessage = determineNewErrorMessageWithCorrectedPositionInformations(definition, rhsToken, errorToken2, ex.getMessage());
                return new DefinitionType(newMessage, errorToken2);
            }
        } catch (BLexerException e1) {
            errorToken = e1.getLastToken();
            final String newMessage = determineNewErrorMessageWithCorrectedPositionInformations(definition, rhsToken, errorToken, e.getMessage());
            throw new PreParseException(newMessage);
        } catch (de.be4.classicalb.core.parser.lexer.LexerException e3) {
            throw new PreParseException(determineNewErrorMessageWithCorrectedPositionInformationsWithoutToken(definition, rhsToken, e3.getMessage()));
        } catch (IOException e1) {
            throw new PreParseException(e.getMessage());
        }
    } catch (BLexerException e) {
        errorToken = e.getLastToken();
        final String newMessage = determineNewErrorMessageWithCorrectedPositionInformations(definition, rhsToken, errorToken, e.getMessage());
        throw new PreParseException(newMessage);
    } catch (de.be4.classicalb.core.parser.lexer.LexerException e) {
        throw new PreParseException(determineNewErrorMessageWithCorrectedPositionInformationsWithoutToken(definition, rhsToken, e.getMessage()));
    } catch (IOException e) {
        throw new PreParseException(e.getMessage());
    }
}
Also used : PParseUnit(de.be4.classicalb.core.parser.node.PParseUnit) AIdentifierExpression(de.be4.classicalb.core.parser.node.AIdentifierExpression) PExpression(de.be4.classicalb.core.parser.node.PExpression) BLexerException(de.be4.classicalb.core.parser.exceptions.BLexerException) AFunctionExpression(de.be4.classicalb.core.parser.node.AFunctionExpression) PreParseException(de.be4.classicalb.core.parser.exceptions.PreParseException) AExpressionParseUnit(de.be4.classicalb.core.parser.node.AExpressionParseUnit) ParserException(de.be4.classicalb.core.preparser.parser.ParserException) IOException(java.io.IOException) ADefinitionExpression(de.be4.classicalb.core.parser.node.ADefinitionExpression) APredicateParseUnit(de.be4.classicalb.core.parser.node.APredicateParseUnit) BLexerException(de.be4.classicalb.core.parser.exceptions.BLexerException) LexerException(de.be4.classicalb.core.preparser.lexer.LexerException)

Example 35 with PExpression

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

the class PositionAspectTest method testSimpleNode.

@Test
public void testSimpleNode() throws Exception {
    final String testMachine = "#EXPRESSION x";
    final BParser parser = new BParser("testcase");
    final Start startNode = parser.parse(testMachine, true);
    final PExpression expression = ((AExpressionParseUnit) startNode.getPParseUnit()).getExpression();
    final SourcePosition startPos = expression.getStartPos();
    final SourcePosition endPos = expression.getEndPos();
    assertNotNull(startNode);
    assertNotNull(endPos);
    assertEquals(1, startPos.getLine());
    assertEquals(13, startPos.getPos());
    assertEquals(1, endPos.getLine());
    assertEquals(14, endPos.getPos());
}
Also used : AExpressionParseUnit(de.be4.classicalb.core.parser.node.AExpressionParseUnit) Start(de.be4.classicalb.core.parser.node.Start) SourcePosition(de.hhu.stups.sablecc.patch.SourcePosition) BParser(de.be4.classicalb.core.parser.BParser) PExpression(de.be4.classicalb.core.parser.node.PExpression) Test(org.junit.Test)

Aggregations

PExpression (de.be4.classicalb.core.parser.node.PExpression)50 AIdentifierExpression (de.be4.classicalb.core.parser.node.AIdentifierExpression)28 ArrayList (java.util.ArrayList)27 CheckException (de.be4.classicalb.core.parser.exceptions.CheckException)21 TIdentifierLiteral (de.be4.classicalb.core.parser.node.TIdentifierLiteral)12 Test (org.junit.Test)6 Node (de.be4.classicalb.core.parser.node.Node)5 LinkedList (java.util.LinkedList)5 AExpressionParseUnit (de.be4.classicalb.core.parser.node.AExpressionParseUnit)4 AFunctionExpression (de.be4.classicalb.core.parser.node.AFunctionExpression)4 AIntegerExpression (de.be4.classicalb.core.parser.node.AIntegerExpression)4 PSubstitution (de.be4.classicalb.core.parser.node.PSubstitution)4 VisitorException (de.be4.classicalb.core.parser.exceptions.VisitorException)3 ADefinitionExpression (de.be4.classicalb.core.parser.node.ADefinitionExpression)3 AStringExpression (de.be4.classicalb.core.parser.node.AStringExpression)3 Start (de.be4.classicalb.core.parser.node.Start)3 BParser (de.be4.classicalb.core.parser.BParser)2 AExpressionDefinitionDefinition (de.be4.classicalb.core.parser.node.AExpressionDefinitionDefinition)2 AMachineReference (de.be4.classicalb.core.parser.node.AMachineReference)2 AOpSubstitution (de.be4.classicalb.core.parser.node.AOpSubstitution)2