Search in sources :

Example 6 with BLexer

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

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

the class BParser method eparse.

public Start eparse(String input, IDefinitions context) throws BCompoundException, LexerException, IOException {
    final Reader reader = new StringReader(input);
    Start ast = null;
    List<String> ids = new ArrayList<>();
    final DefinitionTypes defTypes = new DefinitionTypes();
    defTypes.addAll(context.getTypes());
    BLexer bLexer = new BLexer(new PushbackReader(reader, BLexer.PUSHBACK_BUFFER_SIZE), defTypes);
    bLexer.setParseOptions(parseOptions);
    Token t;
    do {
        t = bLexer.next();
        if (t instanceof TIdentifierLiteral) {
            if (!ids.contains(t.getText())) {
                ids.add(t.getText());
            }
        }
    } while (!(t instanceof EOF));
    Parser p = new Parser(new EBLexer(input, BigInteger.ZERO, ids, defTypes));
    boolean ok;
    try {
        ast = p.parse();
        ok = true;
    } catch (Exception e) {
        handleException(e);
        ok = false;
    }
    BigInteger b = new BigInteger("2");
    b = b.pow(ids.size());
    b = b.subtract(BigInteger.ONE);
    while (!ok && b.compareTo(BigInteger.ZERO) > 0) {
        p = new Parser(new EBLexer(input, b, ids, defTypes));
        try {
            ast = p.parse();
            ok = true;
        } catch (ParserException e) {
            b = b.subtract(BigInteger.ONE);
            handleException(e);
        }
    }
    return ast;
}
Also used : ParserException(de.be4.classicalb.core.parser.parser.ParserException) Start(de.be4.classicalb.core.parser.node.Start) ArrayList(java.util.ArrayList) Reader(java.io.Reader) StringReader(java.io.StringReader) PushbackReader(java.io.PushbackReader) Token(de.be4.classicalb.core.parser.node.Token) TIdentifierLiteral(de.be4.classicalb.core.parser.node.TIdentifierLiteral) LexerException(de.be4.classicalb.core.parser.lexer.LexerException) IOException(java.io.IOException) ParserException(de.be4.classicalb.core.parser.parser.ParserException) PushbackReader(java.io.PushbackReader) Parser(de.be4.classicalb.core.parser.parser.Parser) StringReader(java.io.StringReader) BigInteger(java.math.BigInteger) EOF(de.be4.classicalb.core.parser.node.EOF)

Aggregations

PushbackReader (java.io.PushbackReader)7 StringReader (java.io.StringReader)7 Start (de.be4.classicalb.core.parser.node.Start)6 EOF (de.be4.classicalb.core.parser.node.EOF)5 Token (de.be4.classicalb.core.parser.node.Token)5 Test (org.junit.Test)4 BLexer (de.be4.classicalb.core.parser.BLexer)3 BParser (de.be4.classicalb.core.parser.BParser)3 ASTPrinter (de.be4.classicalb.core.parser.visualisation.ASTPrinter)3 IOException (java.io.IOException)3 Reader (java.io.Reader)3 LexerException (de.be4.classicalb.core.parser.lexer.LexerException)2 TIdentifierLiteral (de.be4.classicalb.core.parser.node.TIdentifierLiteral)2 Parser (de.be4.classicalb.core.parser.parser.Parser)2 ParserException (de.be4.classicalb.core.parser.parser.ParserException)2 ArrayList (java.util.ArrayList)2 Helpers.getTreeAsString (util.Helpers.getTreeAsString)2 DefinitionCollector (de.be4.classicalb.core.parser.analysis.checking.DefinitionCollector)1 BLexerException (de.be4.classicalb.core.parser.exceptions.BLexerException)1 PreParseException (de.be4.classicalb.core.parser.exceptions.PreParseException)1