Search in sources :

Example 46 with PExpression

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

the class OpSubstitutions method replaceWithDefExpression.

private ADefinitionExpression replaceWithDefExpression(final Node node, TIdentifierLiteral identifier, final List<PExpression> paramList) {
    final ADefinitionExpression newNode = new ADefinitionExpression();
    newNode.setDefLiteral(identifier);
    if (paramList != null) {
        newNode.setParameters(paramList);
    }
    final PositionedNode posNode = node;
    final PositionedNode newPosNode = newNode;
    newPosNode.setStartPos(posNode.getStartPos());
    newPosNode.setEndPos(posNode.getEndPos());
    node.replaceBy(newNode);
    return newNode;
}
Also used : ADefinitionExpression(de.be4.classicalb.core.parser.node.ADefinitionExpression) PositionedNode(de.hhu.stups.sablecc.patch.PositionedNode)

Example 47 with PExpression

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

the class OpSubstitutions method caseAFunctionExpression.

@Override
public void caseAFunctionExpression(final AFunctionExpression node) {
    if (node.getIdentifier() != null) {
        node.getIdentifier().apply(this);
    }
    if (node.getIdentifier() instanceof ADefinitionExpression && ((ADefinitionExpression) node.getIdentifier()).getParameters().isEmpty()) {
        final LinkedList<PExpression> paramList = new LinkedList<>(node.getParameters());
        final TIdentifierLiteral identifier = ((ADefinitionExpression) node.getIdentifier()).getDefLiteral();
        if (paramList.size() <= definitions.getParameterCount(identifier.getText())) {
            /*
				 * The parameters seem to belong to this definition, so we need
				 * to replace the FunctionExpression by a
				 * DefinitionFunctionExpression. If not enough parameters were
				 * given this will be found by a later check, i.e.
				 * DefinitionUsageCheck.
				 */
            final ADefinitionExpression newNode = replaceWithDefExpression(node, identifier, paramList);
            final List<PExpression> copy = newNode.getParameters();
            for (final PExpression e : copy) {
                e.apply(this);
            }
            return;
        }
    }
    /*
		 * Reached in case that: Identifier of this FunctionExpression is not a
		 * definition or there were more parameters than the definition needs
		 * (by declaration), so we asume the parameters belong to some other
		 * construct (for example a function a level higher in the AST).
		 */
    final List<PExpression> copy = node.getParameters();
    for (final PExpression e : copy) {
        e.apply(this);
    }
}
Also used : ADefinitionExpression(de.be4.classicalb.core.parser.node.ADefinitionExpression) PExpression(de.be4.classicalb.core.parser.node.PExpression) TIdentifierLiteral(de.be4.classicalb.core.parser.node.TIdentifierLiteral) LinkedList(java.util.LinkedList)

Example 48 with PExpression

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

the class ASTPrologTest method testPartition.

@Test
public void testPartition() {
    final PExpression set = createId("set");
    final PExpression one = new AIntegerExpression(new TIntegerLiteral("1"));
    final PExpression two = new AIntegerExpression(new TIntegerLiteral("2"));
    final PExpression three = new AIntegerExpression(new TIntegerLiteral("3"));
    final APartitionPredicate pred = new APartitionPredicate(set, Arrays.asList(one, two, three));
    final String expected = "partition($,identifier($,set),[integer($,1),integer($,2),integer($,3)])";
    checkAST(0, expected, pred);
}
Also used : AIntegerExpression(de.be4.classicalb.core.parser.node.AIntegerExpression) APartitionPredicate(de.be4.classicalb.core.parser.node.APartitionPredicate) TIntegerLiteral(de.be4.classicalb.core.parser.node.TIntegerLiteral) PExpression(de.be4.classicalb.core.parser.node.PExpression) Test(org.junit.Test)

Example 49 with PExpression

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

the class CreateFreetypeTest method createAdd.

private AOperation createAdd(String name, String param, PExpression type, String cons) {
    final AMemberPredicate pre = new AMemberPredicate(createIdentifier(param), type);
    final ASetExtensionExpression newVal = new ASetExtensionExpression(Arrays.<PExpression>asList(new AFunctionExpression(createIdentifier(cons), createIdentifiers(param))));
    final PSubstitution subst = new APreconditionSubstitution(pre, createAssignment(VAR_NAME, new AUnionExpression(createIdentifier(VAR_NAME), newVal)));
    return new AOperation(EMPTY_EXPRS, createIdLits(name), createIdentifiers(param), subst);
}
Also used : AUnionExpression(de.be4.classicalb.core.parser.node.AUnionExpression) ASetExtensionExpression(de.be4.classicalb.core.parser.node.ASetExtensionExpression) APreconditionSubstitution(de.be4.classicalb.core.parser.node.APreconditionSubstitution) AOperation(de.be4.classicalb.core.parser.node.AOperation) PSubstitution(de.be4.classicalb.core.parser.node.PSubstitution) AMemberPredicate(de.be4.classicalb.core.parser.node.AMemberPredicate) AFunctionExpression(de.be4.classicalb.core.parser.node.AFunctionExpression)

Example 50 with PExpression

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

the class RulesMachineChecker method countPlaceHoldersInExpression.

private Integer countPlaceHoldersInExpression(PExpression param) {
    if (param instanceof AConcatExpression) {
        AConcatExpression con = (AConcatExpression) param;
        Integer left = countPlaceHoldersInExpression(con.getLeft());
        Integer right = countPlaceHoldersInExpression(con.getRight());
        if (left == null || right == null) {
            return null;
        } else {
            return left + right;
        }
    } else if (param instanceof AStringExpression) {
        AStringExpression string = (AStringExpression) param;
        String content = string.getContent().getText();
        String subString = "~w";
        return countOccurrences(content, subString);
    } else {
        return null;
    }
}
Also used : AConcatExpression(de.be4.classicalb.core.parser.node.AConcatExpression) AStringExpression(de.be4.classicalb.core.parser.node.AStringExpression)

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