Search in sources :

Example 1 with AFunctionExpression

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

the class OpSubstitutions method caseAFuncOpSubstitution.

@Override
public void caseAFuncOpSubstitution(final AFuncOpSubstitution node) {
    final PExpression expression = node.getFunction();
    PExpression idExpr = null;
    LinkedList<PExpression> parameters = null;
    Type type = null;
    TIdentifierLiteral idToken = null;
    String idString = null;
    if (expression instanceof AFunctionExpression) {
        // the operation was parsed as a function expression
        final AFunctionExpression function = (AFunctionExpression) expression;
        final PExpression funcId = function.getIdentifier();
        if (funcId instanceof AIdentifierExpression) {
            final AIdentifierExpression identifier = (AIdentifierExpression) funcId;
            idString = Utils.getTIdentifierListAsString(identifier.getIdentifier());
            idToken = identifier.getIdentifier().get(0);
            type = definitions.getType(idString);
        } else {
            type = Type.NoDefinition;
        }
        idExpr = function.getIdentifier();
        parameters = new LinkedList<>(function.getParameters());
    } else if (expression instanceof AIdentifierExpression) {
        // the operation was parsed as an identifier expression
        final AIdentifierExpression identifier = (AIdentifierExpression) expression;
        idString = Utils.getTIdentifierListAsString(identifier.getIdentifier());
        idToken = identifier.getIdentifier().get(0);
        type = definitions.getType(idString);
        idExpr = expression;
        parameters = new LinkedList<>();
    } else {
        // some other expression was parsed (NOT allowed)
        throw new BParseException(null, "Expecting operation");
    }
    if (type != Type.NoDefinition && idToken != null) {
        if (type == Type.Substitution || type == Type.ExprOrSubst) {
            // create DefinitionSubstitution
            final ADefinitionSubstitution defSubst = new ADefinitionSubstitution(new TDefLiteralSubstitution(idToken.getText(), idToken.getLine(), idToken.getPos()), parameters);
            if (type == Type.ExprOrSubst) {
                // type is determined now => set to Substitution
                setTypeSubstDef(node, idString);
            }
            // transfer position information
            final PositionedNode posNode = node;
            final PositionedNode newPosNode = defSubst;
            newPosNode.setStartPos(posNode.getStartPos());
            newPosNode.setEndPos(posNode.getEndPos());
            node.replaceBy(defSubst);
            defSubst.apply(this);
        } else {
            // finding some other type here is an error!
            throw new VisitorException(new CheckException("Expecting substitution here but found definition with type '" + type + "'", node));
        }
    } else {
        // no def, no problem ;-)
        final AOpSubstitution opSubst = new AOpSubstitution(idExpr, parameters);
        opSubst.setStartPos(idExpr.getStartPos());
        opSubst.setEndPos(idExpr.getEndPos());
        node.replaceBy(opSubst);
        opSubst.apply(this);
    }
}
Also used : TDefLiteralSubstitution(de.be4.classicalb.core.parser.node.TDefLiteralSubstitution) CheckException(de.be4.classicalb.core.parser.exceptions.CheckException) AIdentifierExpression(de.be4.classicalb.core.parser.node.AIdentifierExpression) BParseException(de.be4.classicalb.core.parser.exceptions.BParseException) PositionedNode(de.hhu.stups.sablecc.patch.PositionedNode) PExpression(de.be4.classicalb.core.parser.node.PExpression) TIdentifierLiteral(de.be4.classicalb.core.parser.node.TIdentifierLiteral) LinkedList(java.util.LinkedList) Type(de.be4.classicalb.core.parser.IDefinitions.Type) AFunctionExpression(de.be4.classicalb.core.parser.node.AFunctionExpression) ADefinitionSubstitution(de.be4.classicalb.core.parser.node.ADefinitionSubstitution) VisitorException(de.be4.classicalb.core.parser.exceptions.VisitorException) AOpSubstitution(de.be4.classicalb.core.parser.node.AOpSubstitution)

Example 2 with AFunctionExpression

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

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

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

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

Aggregations

AFunctionExpression (de.be4.classicalb.core.parser.node.AFunctionExpression)4 PExpression (de.be4.classicalb.core.parser.node.PExpression)4 AIdentifierExpression (de.be4.classicalb.core.parser.node.AIdentifierExpression)3 TIdentifierLiteral (de.be4.classicalb.core.parser.node.TIdentifierLiteral)3 LinkedList (java.util.LinkedList)3 CheckException (de.be4.classicalb.core.parser.exceptions.CheckException)2 VisitorException (de.be4.classicalb.core.parser.exceptions.VisitorException)2 ADefinitionExpression (de.be4.classicalb.core.parser.node.ADefinitionExpression)2 AOpSubstitution (de.be4.classicalb.core.parser.node.AOpSubstitution)2 PSubstitution (de.be4.classicalb.core.parser.node.PSubstitution)2 TDefLiteralSubstitution (de.be4.classicalb.core.parser.node.TDefLiteralSubstitution)2 PositionedNode (de.hhu.stups.sablecc.patch.PositionedNode)2 Type (de.be4.classicalb.core.parser.IDefinitions.Type)1 BLexerException (de.be4.classicalb.core.parser.exceptions.BLexerException)1 BParseException (de.be4.classicalb.core.parser.exceptions.BParseException)1 PreParseException (de.be4.classicalb.core.parser.exceptions.PreParseException)1 ADefinitionSubstitution (de.be4.classicalb.core.parser.node.ADefinitionSubstitution)1 AExpressionDefinitionDefinition (de.be4.classicalb.core.parser.node.AExpressionDefinitionDefinition)1 AExpressionParseUnit (de.be4.classicalb.core.parser.node.AExpressionParseUnit)1 AMemberPredicate (de.be4.classicalb.core.parser.node.AMemberPredicate)1