Search in sources :

Example 6 with Lexer

use of de.be4.ltl.core.ctlparser.lexer.Lexer 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 Lexer

use of de.be4.ltl.core.ctlparser.lexer.Lexer in project probparsers by bendisposto.

the class EventBParser method parse.

/**
 * Parses the input string.
 *
 * @param input
 *            the {@link String} to be parsed
 * @param debugOutput
 *            output debug messages on standard out?
 * @return the root node of the AST
 * @throws BException
 *             The {@link BException} class stores the actual exception as
 *             delegate and forwards all method calls to it. So it is save
 *             for tools to just use this exception if they want to extract
 *             an error message. If the tools needs to extract additional
 *             information, such as a sourcecode position or involved tokens
 *             respectively nodes, it needs to retrieve the delegate
 *             exception. The {@link BException} class offers a
 *             {@link BException#getCause()} method for this, which returns
 *             the delegate exception.
 *             <p>
 *             Internal exceptions:
 *             <ul>
 *             <li>{@link EventBLexerException}: If any error occurs in the
 *             generated or customized lexer a {@link LexerException} is
 *             thrown. Usually the lexer classes just throw a
 *             {@link LexerException}. But this class unfortunately does not
 *             contain any explicit information about the sourcecode
 *             position where the error occured. Using aspect-oriented
 *             programming we intercept the throwing of these exceptions to
 *             replace them by our own exception. In our own exception we
 *             provide the sourcecode position of the last characters that
 *             were read from the input.</li>
 *             <li>{@link EventBParseException}: This exception is thrown in
 *             two situations. On the one hand if the parser throws a
 *             {@link ParserException} we convert it into a
 *             {@link EventBParseException}. On the other hand it can be
 *             thrown if any error is found during the AST transformations
 *             after the parser has finished. We try to provide a token if a
 *             single token is involved in the error. Otherwise a
 *             {@link SourcecodeRange} is provided, which can be used to
 *             retrieve detailed position information from the
 *             {@link SourcePositions} (s. {@link #getSourcePositions()}).
 *             </li>
 *             <li>{@link CheckException}: If any problem occurs while
 *             performing semantic checks, a {@link CheckException} is
 *             thrown. We provide one or more nodes that are involved in the
 *             problem. For example, if we find dublicate machine clauses,
 *             we will list all occurances in the exception.</li>
 *             </ul>
 */
public Start parse(final String input, final boolean debugOutput) throws BException {
    final Reader reader = new StringReader(input);
    try {
        /*
			 * Main parser
			 */
        final EventBLexer lexer = new EventBLexer(new PushbackReader(reader, 99));
        lexer.setDebugOutput(debugOutput);
        Parser parser = new Parser(lexer);
        final Start rootNode = parser.parse();
        final List<IToken> tokenList = ((ITokenListContainer) lexer).getTokenList();
        /*
			 * Retrieving sourcecode positions which were found by ParserAspect
			 */
        final Map<PositionedNode, SourcecodeRange> positions = ((IParser) parser).getMapping();
        sourcePositions = new SourcePositions(tokenList, positions);
        parser = null;
        return rootNode;
    } catch (final LexerException e) {
        /*
			 * Actually it's supposed to be a EventBLexerException because the
			 * aspect 'LexerAspect' replaces any LexerException to provide
			 * sourcecode position information in the BLexerException.
			 */
        throw new BException(e);
    } catch (final ParserException e) {
        throw new BException(createEventBParseException(e));
    } catch (final EventBParseException e) {
        throw new BException(e);
    } catch (final IOException e) {
        // shouldn't happen and if, we cannot handle it
        throw new BException(e);
    }
}
Also used : ParserException(de.be4.eventbalg.core.parser.parser.ParserException) Start(de.be4.eventbalg.core.parser.node.Start) SourcePositions(de.hhu.stups.sablecc.patch.SourcePositions) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) StringReader(java.io.StringReader) PushbackReader(java.io.PushbackReader) PositionedNode(de.hhu.stups.sablecc.patch.PositionedNode) IOException(java.io.IOException) PushbackReader(java.io.PushbackReader) IParser(de.hhu.stups.sablecc.patch.IParser) Parser(de.be4.eventbalg.core.parser.parser.Parser) IToken(de.hhu.stups.sablecc.patch.IToken) StringReader(java.io.StringReader) ITokenListContainer(de.hhu.stups.sablecc.patch.ITokenListContainer) SourcecodeRange(de.hhu.stups.sablecc.patch.SourcecodeRange) LexerException(de.be4.eventbalg.core.parser.lexer.LexerException) IParser(de.hhu.stups.sablecc.patch.IParser)

Example 8 with Lexer

use of de.be4.ltl.core.ctlparser.lexer.Lexer in project probparsers by bendisposto.

the class LTLFormulaVisitor method parseLTLFormula.

public static Start parseLTLFormula(String ltlFormula) throws ParserException, LexerException, IOException {
    StringReader reader = new StringReader(ltlFormula);
    PushbackReader r = new PushbackReader(reader);
    Lexer l = new LtlLexer(r);
    Parser p = new Parser(l);
    Start ast = null;
    ast = p.parse();
    return ast;
}
Also used : Lexer(de.be4.ltl.core.parser.lexer.Lexer) LtlLexer(de.be4.ltl.core.parser.internal.LtlLexer) Start(de.be4.ltl.core.parser.node.Start) StringReader(java.io.StringReader) LtlLexer(de.be4.ltl.core.parser.internal.LtlLexer) PushbackReader(java.io.PushbackReader) Parser(de.be4.ltl.core.parser.parser.Parser) BParser(de.be4.classicalb.core.parser.BParser)

Example 9 with Lexer

use of de.be4.ltl.core.ctlparser.lexer.Lexer in project probparsers by bendisposto.

the class LtlParser method parseFormula.

protected Start parseFormula(final String formula) throws LtlParseException, IOException {
    StringReader reader = new StringReader(formula);
    PushbackReader r = new PushbackReader(reader);
    Lexer l = new LtlLexer(r);
    Parser p = new Parser(l);
    Start ast = null;
    try {
        ast = p.parse();
    } catch (ParserException e) {
        final UniversalToken token = UniversalToken.createToken(e.getToken());
        throw new LtlParseException(token, e.getLocalizedMessage());
    } catch (LexerException e) {
        throw new LtlParseException(null, e.getLocalizedMessage());
    }
    return ast;
}
Also used : ParserException(de.be4.ltl.core.parser.parser.ParserException) LtlLexer(de.be4.ltl.core.parser.internal.LtlLexer) Lexer(de.be4.ltl.core.parser.lexer.Lexer) UniversalToken(de.be4.ltl.core.parser.internal.UniversalToken) Start(de.be4.ltl.core.parser.node.Start) StringReader(java.io.StringReader) LexerException(de.be4.ltl.core.parser.lexer.LexerException) LtlLexer(de.be4.ltl.core.parser.internal.LtlLexer) PushbackReader(java.io.PushbackReader) Parser(de.be4.ltl.core.parser.parser.Parser)

Aggregations

PushbackReader (java.io.PushbackReader)7 StringReader (java.io.StringReader)7 IOException (java.io.IOException)6 Reader (java.io.Reader)4 LexerException (de.be4.eventb.core.parser.lexer.LexerException)2 LexerException (de.be4.eventbalg.core.parser.lexer.LexerException)2 LtlLexer (de.be4.ltl.core.parser.internal.LtlLexer)2 UniversalToken (de.be4.ltl.core.parser.internal.UniversalToken)2 Lexer (de.be4.ltl.core.parser.lexer.Lexer)2 Start (de.be4.ltl.core.parser.node.Start)2 Parser (de.be4.ltl.core.parser.parser.Parser)2 IParser (de.hhu.stups.sablecc.patch.IParser)2 IToken (de.hhu.stups.sablecc.patch.IToken)2 ITokenListContainer (de.hhu.stups.sablecc.patch.ITokenListContainer)2 PositionedNode (de.hhu.stups.sablecc.patch.PositionedNode)2 SourcePositions (de.hhu.stups.sablecc.patch.SourcePositions)2 SourcecodeRange (de.hhu.stups.sablecc.patch.SourcecodeRange)2 InputStreamReader (java.io.InputStreamReader)2 BParser (de.be4.classicalb.core.parser.BParser)1 DefinitionCollector (de.be4.classicalb.core.parser.analysis.checking.DefinitionCollector)1