Search in sources :

Example 1 with Type

use of de.be4.classicalb.core.parser.IDefinitions.Type in project probparsers by bendisposto.

the class StructType method createASTNode.

public PExpression createASTNode(Typechecker typechecker) {
    ArrayList<PRecEntry> list = new ArrayList<PRecEntry>();
    Set<Entry<String, BType>> entrySet = this.types.entrySet();
    for (Entry<String, BType> entry : entrySet) {
        String name = entry.getKey();
        BType type = entry.getValue();
        TIdentifierLiteral literal = new TIdentifierLiteral(name);
        ArrayList<TIdentifierLiteral> idList = new ArrayList<TIdentifierLiteral>();
        idList.add(literal);
        AIdentifierExpression id = new AIdentifierExpression(idList);
        ARecEntry recEntry = new ARecEntry(id, type.createASTNode(typechecker));
        list.add(recEntry);
    }
    AStructExpression node = new AStructExpression(list);
    typechecker.setType(node, new SetType(this));
    return node;
}
Also used : AIdentifierExpression(de.be4.classicalb.core.parser.node.AIdentifierExpression) ARecEntry(de.be4.classicalb.core.parser.node.ARecEntry) ArrayList(java.util.ArrayList) TIdentifierLiteral(de.be4.classicalb.core.parser.node.TIdentifierLiteral) AStructExpression(de.be4.classicalb.core.parser.node.AStructExpression) ARecEntry(de.be4.classicalb.core.parser.node.ARecEntry) PRecEntry(de.be4.classicalb.core.parser.node.PRecEntry) Entry(java.util.Map.Entry) PRecEntry(de.be4.classicalb.core.parser.node.PRecEntry)

Example 2 with Type

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

use of de.be4.classicalb.core.parser.IDefinitions.Type in project probparsers by bendisposto.

the class DefinitionsTest method testExprOrSubstWParams3.

@Test
public void testExprOrSubstWParams3() {
    final String testMachine = "MACHINE Test\nDEFINITIONS\ndefExpr(x)==g(x)\nOPERATIONS\nop=PRE defExpr(x)=42 THEN defExpr(x) END\nEND";
    try {
        getTreeAsString(testMachine);
        fail("Expected exception was not thrown");
    } catch (final BCompoundException e) {
        final CheckException cause = (CheckException) e.getCause();
        assertEquals("Expecting substitution here but found definition with type 'Expression'", cause.getLocalizedMessage());
    // IGNORE, is expected
    }
}
Also used : CheckException(de.be4.classicalb.core.parser.exceptions.CheckException) Helpers.getTreeAsString(util.Helpers.getTreeAsString) BCompoundException(de.be4.classicalb.core.parser.exceptions.BCompoundException) Test(org.junit.Test)

Example 4 with Type

use of de.be4.classicalb.core.parser.IDefinitions.Type in project probparsers by bendisposto.

the class DefinitionCollector method addDefinition.

private void addDefinition(PDefinition def, Type type, String name) {
    if (definitions.containsDefinition(name)) {
        PDefinition existingDefinition = definitions.getDefinition(name);
        File file = definitions.getFile(name);
        StringBuilder sb = new StringBuilder();
        sb.append("Duplicate definition: " + name + ".\n");
        sb.append("(First appearance: ").append(this.getPosition(existingDefinition.getStartPos()));
        if (file != null) {
            sb.append(" in ").append(file.getAbsolutePath());
        }
        sb.append(")\n");
        CheckException e = new CheckException(sb.toString(), def);
        this.exceptions.add(e);
    } else {
        definitions.addDefinition(def, type, name);
    }
}
Also used : PDefinition(de.be4.classicalb.core.parser.node.PDefinition) CheckException(de.be4.classicalb.core.parser.exceptions.CheckException) File(java.io.File)

Example 5 with Type

use of de.be4.classicalb.core.parser.IDefinitions.Type in project probparsers by bendisposto.

the class DefinitionCollector method inASubstitutionDefinitionDefinition.

@Override
public void inASubstitutionDefinitionDefinition(final ASubstitutionDefinitionDefinition node) {
    final String defName = node.getName().getText();
    final Type type = defTypes.getType(defName);
    addDefinition(node, type, defName);
}
Also used : Type(de.be4.classicalb.core.parser.IDefinitions.Type)

Aggregations

CheckException (de.be4.classicalb.core.parser.exceptions.CheckException)8 Type (de.be4.classicalb.core.parser.IDefinitions.Type)6 BCompoundException (de.be4.classicalb.core.parser.exceptions.BCompoundException)5 Test (org.junit.Test)5 Helpers.getTreeAsString (util.Helpers.getTreeAsString)5 TIdentifierLiteral (de.be4.classicalb.core.parser.node.TIdentifierLiteral)3 LinkedList (java.util.LinkedList)3 BParser (de.be4.classicalb.core.parser.BParser)2 VisitorException (de.be4.classicalb.core.parser.exceptions.VisitorException)2 AExpressionDefinitionDefinition (de.be4.classicalb.core.parser.node.AExpressionDefinitionDefinition)2 AFunctionExpression (de.be4.classicalb.core.parser.node.AFunctionExpression)2 AIdentifierExpression (de.be4.classicalb.core.parser.node.AIdentifierExpression)2 PDefinition (de.be4.classicalb.core.parser.node.PDefinition)2 TDefLiteralSubstitution (de.be4.classicalb.core.parser.node.TDefLiteralSubstitution)2 Token (de.be4.classicalb.core.preparser.node.Token)2 File (java.io.File)2 BLexer (de.be4.classicalb.core.parser.BLexer)1 MockedDefinitions (de.be4.classicalb.core.parser.MockedDefinitions)1 BException (de.be4.classicalb.core.parser.exceptions.BException)1 BParseException (de.be4.classicalb.core.parser.exceptions.BParseException)1