Search in sources :

Example 1 with Token

use of de.be4.ltl.core.ctlparser.node.Token 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.eventb.core.parser.parser.ParserException) Start(de.be4.eventb.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.eventb.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.eventb.core.parser.lexer.LexerException) IParser(de.hhu.stups.sablecc.patch.IParser)

Example 2 with Token

use of de.be4.ltl.core.ctlparser.node.Token in project probparsers by bendisposto.

the class PrologCtlGenerator method caseAEnaCtl.

@Override
public void caseAEnaCtl(AEnaCtl node) {
    final Token token = node.getOperation();
    p.openTerm("ena");
    helper.parseTransitionPredicate(UniversalToken.createToken(token));
    node.getCont().apply(this);
    p.closeTerm();
}
Also used : Token(de.be4.ltl.core.ctlparser.node.Token)

Example 3 with Token

use of de.be4.ltl.core.ctlparser.node.Token in project probparsers by bendisposto.

the class RulesGrammar method createNewToken.

@Override
public Token createNewToken(Token token) {
    Class<? extends Token> clazz = map.get(token.getText());
    try {
        // default constructor
        Token newToken = clazz.newInstance();
        newToken.setLine(token.getLine());
        newToken.setPos(token.getPos());
        return newToken;
    } catch (InstantiationException e) {
        // if the class has not default constructor we call the
        // construct with a text string as the single argument
        // e.g. TKwPredicateOperator(token.getText)
        Class<?>[] cArg = new Class<?>[] { String.class };
        try {
            Token newInstance = clazz.getDeclaredConstructor(cArg).newInstance(token.getText());
            newInstance.setLine(token.getLine());
            newInstance.setPos(token.getPos());
            return newInstance;
        } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e1) {
            throw new AssertionError(INSTANTIATION_ERROR_MESSAGE + clazz.getName(), e1);
        }
    } catch (IllegalAccessException e) {
        throw new AssertionError(INSTANTIATION_ERROR_MESSAGE + clazz.getName(), e);
    }
}
Also used : Token(de.be4.classicalb.core.parser.node.Token)

Example 4 with Token

use of de.be4.ltl.core.ctlparser.node.Token in project probparsers by bendisposto.

the class PreParser method sortDefinitionsByTopologicalOrderAndCheckForCycles.

private List<Token> sortDefinitionsByTopologicalOrderAndCheckForCycles(Map<Token, Token> definitions) throws PreParseException {
    final Set<String> definitionNames = new HashSet<>();
    final Map<String, Token> definitionMap = new HashMap<>();
    for (Token token : definitions.keySet()) {
        final String definitionName = token.getText();
        definitionNames.add(definitionName);
        definitionMap.put(definitionName, token);
    }
    Map<String, Set<String>> dependencies = determineDependencies(definitionNames, definitions);
    List<String> sortedDefinitionNames = Utils.sortByTopologicalOrder(dependencies);
    if (sortedDefinitionNames.size() < definitionNames.size()) {
        Set<String> remaining = new HashSet<>(definitionNames);
        remaining.removeAll(sortedDefinitionNames);
        List<String> cycle = Utils.determineCycle(remaining, dependencies);
        StringBuilder sb = new StringBuilder();
        for (Iterator<String> iterator = cycle.iterator(); iterator.hasNext(); ) {
            sb.append(iterator.next());
            if (iterator.hasNext()) {
                sb.append(" -> ");
            }
        }
        final Token firstDefinitionToken = definitionMap.get(cycle.get(0));
        throw new PreParseException(firstDefinitionToken, "Cyclic references in definitions: " + sb.toString());
    } else {
        List<Token> sortedDefinitionTokens = new ArrayList<>();
        for (String name : sortedDefinitionNames) {
            sortedDefinitionTokens.add(definitionMap.get(name));
        }
        return sortedDefinitionTokens;
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Token(de.be4.classicalb.core.preparser.node.Token) PreParseException(de.be4.classicalb.core.parser.exceptions.PreParseException) HashSet(java.util.HashSet)

Example 5 with Token

use of de.be4.ltl.core.ctlparser.node.Token in project probparsers by bendisposto.

the class PreParser method determineType.

private DefinitionType determineType(final Token definition, final Token rhsToken, final Set<String> untypedDefinitions) throws PreParseException {
    final String definitionRhs = rhsToken.getText();
    de.be4.classicalb.core.parser.node.Start start;
    de.be4.classicalb.core.parser.node.Token errorToken = null;
    try {
        start = tryParsing(BParser.FORMULA_PREFIX, definitionRhs);
        // Predicate?
        PParseUnit parseunit = start.getPParseUnit();
        if (parseunit instanceof APredicateParseUnit) {
            return new DefinitionType(IDefinitions.Type.Predicate);
        }
        // Expression or Expression/Substituion (e.g. f(x))?
        AExpressionParseUnit expressionParseUnit = (AExpressionParseUnit) parseunit;
        PreParserIdentifierTypeVisitor visitor = new PreParserIdentifierTypeVisitor(untypedDefinitions);
        expressionParseUnit.apply(visitor);
        if (visitor.isKaboom()) {
            return new DefinitionType();
        }
        PExpression expression = expressionParseUnit.getExpression();
        if ((expression instanceof AIdentifierExpression) || (expression instanceof AFunctionExpression) || (expression instanceof ADefinitionExpression)) {
            return new DefinitionType(IDefinitions.Type.ExprOrSubst);
        }
        return new DefinitionType(IDefinitions.Type.Expression);
    } catch (de.be4.classicalb.core.parser.parser.ParserException e) {
        errorToken = e.getToken();
        try {
            tryParsing(BParser.SUBSTITUTION_PREFIX, definitionRhs);
            return new DefinitionType(IDefinitions.Type.Substitution, errorToken);
        } catch (de.be4.classicalb.core.parser.parser.ParserException ex) {
            final de.be4.classicalb.core.parser.node.Token errorToken2 = ex.getToken();
            if (errorToken.getLine() > errorToken2.getLine() || (errorToken.getLine() == errorToken2.getLine() && errorToken.getPos() >= errorToken2.getPos())) {
                final String newMessage = determineNewErrorMessageWithCorrectedPositionInformations(definition, rhsToken, errorToken, e.getMessage());
                return new DefinitionType(newMessage, errorToken);
            } else {
                final String newMessage = determineNewErrorMessageWithCorrectedPositionInformations(definition, rhsToken, errorToken2, ex.getMessage());
                return new DefinitionType(newMessage, errorToken2);
            }
        } catch (BLexerException e1) {
            errorToken = e1.getLastToken();
            final String newMessage = determineNewErrorMessageWithCorrectedPositionInformations(definition, rhsToken, errorToken, e.getMessage());
            throw new PreParseException(newMessage);
        } catch (de.be4.classicalb.core.parser.lexer.LexerException e3) {
            throw new PreParseException(determineNewErrorMessageWithCorrectedPositionInformationsWithoutToken(definition, rhsToken, e3.getMessage()));
        } catch (IOException e1) {
            throw new PreParseException(e.getMessage());
        }
    } catch (BLexerException e) {
        errorToken = e.getLastToken();
        final String newMessage = determineNewErrorMessageWithCorrectedPositionInformations(definition, rhsToken, errorToken, e.getMessage());
        throw new PreParseException(newMessage);
    } catch (de.be4.classicalb.core.parser.lexer.LexerException e) {
        throw new PreParseException(determineNewErrorMessageWithCorrectedPositionInformationsWithoutToken(definition, rhsToken, e.getMessage()));
    } catch (IOException e) {
        throw new PreParseException(e.getMessage());
    }
}
Also used : PParseUnit(de.be4.classicalb.core.parser.node.PParseUnit) AIdentifierExpression(de.be4.classicalb.core.parser.node.AIdentifierExpression) PExpression(de.be4.classicalb.core.parser.node.PExpression) BLexerException(de.be4.classicalb.core.parser.exceptions.BLexerException) AFunctionExpression(de.be4.classicalb.core.parser.node.AFunctionExpression) PreParseException(de.be4.classicalb.core.parser.exceptions.PreParseException) AExpressionParseUnit(de.be4.classicalb.core.parser.node.AExpressionParseUnit) ParserException(de.be4.classicalb.core.preparser.parser.ParserException) IOException(java.io.IOException) ADefinitionExpression(de.be4.classicalb.core.parser.node.ADefinitionExpression) APredicateParseUnit(de.be4.classicalb.core.parser.node.APredicateParseUnit) BLexerException(de.be4.classicalb.core.parser.exceptions.BLexerException) LexerException(de.be4.classicalb.core.preparser.lexer.LexerException)

Aggregations

Test (org.junit.Test)13 BCompoundException (de.be4.classicalb.core.parser.exceptions.BCompoundException)10 IOException (java.io.IOException)10 PushbackReader (java.io.PushbackReader)10 StringReader (java.io.StringReader)10 Ast2String (util.Ast2String)10 Token (de.be4.classicalb.core.parser.node.Token)8 PreParseException (de.be4.classicalb.core.parser.exceptions.PreParseException)7 Token (de.be4.classicalb.core.preparser.node.Token)7 Token (de.be4.ltl.core.parser.node.Token)7 EOF (de.be4.classicalb.core.parser.node.EOF)5 Start (de.be4.classicalb.core.parser.node.Start)5 IToken (de.hhu.stups.sablecc.patch.IToken)5 Reader (java.io.Reader)5 ArrayList (java.util.ArrayList)4 BLexer (de.be4.classicalb.core.parser.BLexer)3 BParser (de.be4.classicalb.core.parser.BParser)3 BLexerException (de.be4.classicalb.core.parser.exceptions.BLexerException)3 TIdentifierLiteral (de.be4.classicalb.core.parser.node.TIdentifierLiteral)3 ASTPrinter (de.be4.classicalb.core.parser.visualisation.ASTPrinter)3