Search in sources :

Example 16 with Type

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

the class PreParser method evaluateTypes.

private void evaluateTypes(final List<Token> sortedDefinitionList, final Map<Token, Token> definitions) throws PreParseException {
    // use linked list as we rely on pop() and push()
    final LinkedList<Token> remainingDefinitions = new LinkedList<>(sortedDefinitionList);
    final LinkedList<Token> currentlyUnparseableDefinitions = new LinkedList<Token>();
    Set<String> todoDefs = new HashSet<String>();
    for (Token token : remainingDefinitions) {
        todoDefs.add(token.getText());
    }
    // use main parser for the rhs of each definition to determine type
    // if a definition can not be typed this way, it may be due to another
    // definition that is not yet parser (because it appears later in the
    // source code)
    // in this case, the definition is appended to the list again
    // the algorithm terminates if the queue is empty or if no definition
    // has been parsed
    boolean oneParsed = true;
    while (oneParsed) {
        oneParsed = false;
        while (!remainingDefinitions.isEmpty()) {
            final Token definition = remainingDefinitions.pop();
            final Token defRhs = definitions.get(definition);
            Definitions.Type type = null;
            DefinitionType definitionType = determineType(definition, defRhs, todoDefs);
            type = definitionType.type;
            if (type != null) {
                todoDefs.remove(definition.getText());
                oneParsed = true;
                definitionTypes.addTyping(definition.getText(), type);
            // types.addTyping(definition.getText(), type);
            } else {
                currentlyUnparseableDefinitions.push(definition);
            }
        }
        remainingDefinitions.addAll(currentlyUnparseableDefinitions);
        currentlyUnparseableDefinitions.clear();
    }
    if (!remainingDefinitions.isEmpty()) {
        final Token definition = remainingDefinitions.pop();
        final Token defRhs = definitions.get(definition);
        DefinitionType definitionType = determineType(definition, defRhs, todoDefs);
        if (definitionType.errorMessage != null) {
            throw new PreParseException(definitionType.errorMessage + " in file: " + modelFileName);
        // throw new BParseException(definitionType.errorToken,
        // definitionType.errorMessage + " in file: "
        // + modelFileName);
        } else {
            // fall back message
            throw new PreParseException(definition, "[" + definition.getLine() + "," + definition.getPos() + "] expecting wellformed expression, predicate or substitution as DEFINITION body (DEFINITION arguments assumed to be expressions)");
        }
    }
}
Also used : Token(de.be4.classicalb.core.preparser.node.Token) PreParseException(de.be4.classicalb.core.parser.exceptions.PreParseException) LinkedList(java.util.LinkedList) HashSet(java.util.HashSet)

Example 17 with Type

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

the class PreParser method sortDefinitionsByPosition.

@SuppressWarnings("unused")
private LinkedList<Token> sortDefinitionsByPosition(final Map<Token, Token> definitions) {
    // LinkedList will be used as a queue later on!
    // however, the list is needed for collections.sort
    // we can not use a priority queue to sort, as the sorting is done once
    // afterwards, it has to remain unsorted
    final LinkedList<Token> list = new LinkedList<Token>();
    for (final Iterator<Token> iterator = definitions.keySet().iterator(); iterator.hasNext(); ) {
        final Token definition = iterator.next();
        list.add(definition);
    }
    /*
		 * Sort the definitions in order of their appearance in the sourcecode.
		 * Dependencies in between definitions are handled later when computing
		 * there type
		 */
    Collections.sort(list, new Comparator<Token>() {

        public int compare(final Token o1, final Token o2) {
            if (o1.getLine() == o2.getLine()) {
                if (o1.getPos() == o2.getPos())
                    return 0;
                else
                    return o1.getPos() - o2.getPos();
            } else
                return o1.getLine() - o2.getLine();
        }
    });
    return list;
}
Also used : Token(de.be4.classicalb.core.preparser.node.Token) LinkedList(java.util.LinkedList)

Example 18 with Type

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

the class DefinitionCollector method inAPredicateDefinitionDefinition.

@Override
public void inAPredicateDefinitionDefinition(final APredicateDefinitionDefinition 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)

Example 19 with Type

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

the class DefinitionCollector method caseAConversionDefinition.

@Override
public void caseAConversionDefinition(AConversionDefinition node) {
    PDefinition def = node.getDefinition();
    if (def instanceof AExpressionDefinitionDefinition) {
        AExpressionDefinitionDefinition exprDef = (AExpressionDefinitionDefinition) def;
        final String defName = exprDef.getName().getText();
        final Type type = defTypes.getType(defName);
        addDefinition(node, type, defName);
    } else {
        this.exceptions.add(new CheckException("Only an expression is allowed on the right hand side of a conversion definition.", node));
    }
}
Also used : PDefinition(de.be4.classicalb.core.parser.node.PDefinition) Type(de.be4.classicalb.core.parser.IDefinitions.Type) CheckException(de.be4.classicalb.core.parser.exceptions.CheckException) AExpressionDefinitionDefinition(de.be4.classicalb.core.parser.node.AExpressionDefinitionDefinition)

Example 20 with Type

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

the class DefinitionCollector method inAExpressionDefinitionDefinition.

@Override
public void inAExpressionDefinitionDefinition(final AExpressionDefinitionDefinition 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