Search in sources :

Example 31 with Definitions

use of de.be4.classicalb.core.parser.Definitions 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 32 with Definitions

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

the class PreParser method determineDependencies.

private Map<String, Set<String>> determineDependencies(Set<String> definitionNames, Map<Token, Token> definitions) throws PreParseException {
    HashMap<String, Set<String>> dependencies = new HashMap<>();
    for (Entry<Token, Token> entry : definitions.entrySet()) {
        Token nameToken = entry.getKey();
        Token rhsToken = entry.getValue();
        // The FORMULA_PREFIX is needed to switch the lexer state from
        // section to normal. Note, that we do not parse the right hand side
        // of the definition here. Hence FORMULA_PREFIX has no further
        // meaning and substitutions can also be handled by the lexer.
        final Reader reader = new StringReader(BParser.FORMULA_PREFIX + "\n" + rhsToken.getText());
        final BLexer lexer = new BLexer(new PushbackReader(reader, BLexer.PUSHBACK_BUFFER_SIZE), new DefinitionTypes());
        lexer.setParseOptions(parseOptions);
        Set<String> set = new HashSet<>();
        de.be4.classicalb.core.parser.node.Token next = null;
        try {
            next = lexer.next();
            while (!(next instanceof EOF)) {
                if (next instanceof TIdentifierLiteral) {
                    TIdentifierLiteral id = (TIdentifierLiteral) next;
                    String name = id.getText();
                    if (definitionNames.contains(name)) {
                        set.add(name);
                    }
                }
                next = lexer.next();
            }
        } catch (IOException e) {
        } catch (BLexerException e) {
            de.be4.classicalb.core.parser.node.Token errorToken = e.getLastToken();
            final String newMessage = determineNewErrorMessageWithCorrectedPositionInformations(nameToken, rhsToken, errorToken, e.getMessage());
            throw new PreParseException(newMessage);
        } catch (de.be4.classicalb.core.parser.lexer.LexerException e) {
            final String newMessage = determineNewErrorMessageWithCorrectedPositionInformationsWithoutToken(nameToken, rhsToken, e.getMessage());
            throw new PreParseException(newMessage);
        }
        dependencies.put(nameToken.getText(), set);
    }
    return dependencies;
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) Reader(java.io.Reader) StringReader(java.io.StringReader) PushbackReader(java.io.PushbackReader) Token(de.be4.classicalb.core.preparser.node.Token) BLexerException(de.be4.classicalb.core.parser.exceptions.BLexerException) StringReader(java.io.StringReader) PreParseException(de.be4.classicalb.core.parser.exceptions.PreParseException) EOF(de.be4.classicalb.core.parser.node.EOF) HashSet(java.util.HashSet) IOException(java.io.IOException) TIdentifierLiteral(de.be4.classicalb.core.parser.node.TIdentifierLiteral) PushbackReader(java.io.PushbackReader)

Example 33 with Definitions

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

Aggregations

Test (org.junit.Test)19 Ast2String (util.Ast2String)17 BCompoundException (de.be4.classicalb.core.parser.exceptions.BCompoundException)16 BParser (de.be4.classicalb.core.parser.BParser)6 Token (de.be4.classicalb.core.preparser.node.Token)6 PreParseException (de.be4.classicalb.core.parser.exceptions.PreParseException)5 AExpressionDefinitionDefinition (de.be4.classicalb.core.parser.node.AExpressionDefinitionDefinition)5 IDefinitions (de.be4.classicalb.core.parser.IDefinitions)4 BLexerException (de.be4.classicalb.core.parser.exceptions.BLexerException)4 HashSet (java.util.HashSet)4 Definitions (de.be4.classicalb.core.parser.Definitions)3 Start (de.be4.classicalb.core.parser.node.Start)3 TIdentifierLiteral (de.be4.classicalb.core.parser.node.TIdentifierLiteral)3 File (java.io.File)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 CheckException (de.be4.classicalb.core.parser.exceptions.CheckException)2 AIdentifierExpression (de.be4.classicalb.core.parser.node.AIdentifierExpression)2 APredicateDefinitionDefinition (de.be4.classicalb.core.parser.node.APredicateDefinitionDefinition)2 ASubstitutionDefinitionDefinition (de.be4.classicalb.core.parser.node.ASubstitutionDefinitionDefinition)2