Search in sources :

Example 1 with PositionedNode

use of de.hhu.stups.sablecc.patch.PositionedNode 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 PositionedNode

use of de.hhu.stups.sablecc.patch.PositionedNode in project probparsers by bendisposto.

the class OpSubstitutions method caseAFuncOpSubstitution.

@Override
public void caseAFuncOpSubstitution(final AFuncOpSubstitution node) {
    final PExpression expression = node.getFunction();
    PExpression idExpr = null;
    LinkedList<PExpression> parameters = null;
    Type type = null;
    TIdentifierLiteral idToken = null;
    String idString = null;
    if (expression instanceof AFunctionExpression) {
        // the operation was parsed as a function expression
        final AFunctionExpression function = (AFunctionExpression) expression;
        final PExpression funcId = function.getIdentifier();
        if (funcId instanceof AIdentifierExpression) {
            final AIdentifierExpression identifier = (AIdentifierExpression) funcId;
            idString = Utils.getTIdentifierListAsString(identifier.getIdentifier());
            idToken = identifier.getIdentifier().get(0);
            type = definitions.getType(idString);
        } else {
            type = Type.NoDefinition;
        }
        idExpr = function.getIdentifier();
        parameters = new LinkedList<>(function.getParameters());
    } else if (expression instanceof AIdentifierExpression) {
        // the operation was parsed as an identifier expression
        final AIdentifierExpression identifier = (AIdentifierExpression) expression;
        idString = Utils.getTIdentifierListAsString(identifier.getIdentifier());
        idToken = identifier.getIdentifier().get(0);
        type = definitions.getType(idString);
        idExpr = expression;
        parameters = new LinkedList<>();
    } else {
        // some other expression was parsed (NOT allowed)
        throw new BParseException(null, "Expecting operation");
    }
    if (type != Type.NoDefinition && idToken != null) {
        if (type == Type.Substitution || type == Type.ExprOrSubst) {
            // create DefinitionSubstitution
            final ADefinitionSubstitution defSubst = new ADefinitionSubstitution(new TDefLiteralSubstitution(idToken.getText(), idToken.getLine(), idToken.getPos()), parameters);
            if (type == Type.ExprOrSubst) {
                // type is determined now => set to Substitution
                setTypeSubstDef(node, idString);
            }
            // transfer position information
            final PositionedNode posNode = node;
            final PositionedNode newPosNode = defSubst;
            newPosNode.setStartPos(posNode.getStartPos());
            newPosNode.setEndPos(posNode.getEndPos());
            node.replaceBy(defSubst);
            defSubst.apply(this);
        } else {
            // finding some other type here is an error!
            throw new VisitorException(new CheckException("Expecting substitution here but found definition with type '" + type + "'", node));
        }
    } else {
        // no def, no problem ;-)
        final AOpSubstitution opSubst = new AOpSubstitution(idExpr, parameters);
        opSubst.setStartPos(idExpr.getStartPos());
        opSubst.setEndPos(idExpr.getEndPos());
        node.replaceBy(opSubst);
        opSubst.apply(this);
    }
}
Also used : TDefLiteralSubstitution(de.be4.classicalb.core.parser.node.TDefLiteralSubstitution) CheckException(de.be4.classicalb.core.parser.exceptions.CheckException) AIdentifierExpression(de.be4.classicalb.core.parser.node.AIdentifierExpression) BParseException(de.be4.classicalb.core.parser.exceptions.BParseException) PositionedNode(de.hhu.stups.sablecc.patch.PositionedNode) PExpression(de.be4.classicalb.core.parser.node.PExpression) TIdentifierLiteral(de.be4.classicalb.core.parser.node.TIdentifierLiteral) LinkedList(java.util.LinkedList) Type(de.be4.classicalb.core.parser.IDefinitions.Type) AFunctionExpression(de.be4.classicalb.core.parser.node.AFunctionExpression) ADefinitionSubstitution(de.be4.classicalb.core.parser.node.ADefinitionSubstitution) VisitorException(de.be4.classicalb.core.parser.exceptions.VisitorException) AOpSubstitution(de.be4.classicalb.core.parser.node.AOpSubstitution)

Example 3 with PositionedNode

use of de.hhu.stups.sablecc.patch.PositionedNode in project probparsers by bendisposto.

the class SourcePositionsTest method testGetEndLine.

@Test
public void testGetEndLine() throws Exception {
    final Start root = parser.parse("machine\nTestMachine\n\nvariables\nx\n\nend", false);
    final AMachineParseUnit parseUnit = (AMachineParseUnit) root.getPParseUnit();
    assertEquals(7, ((PositionedNode) parseUnit).getEndPos().getLine());
    final LinkedList<PVariable> variables = parseUnit.getVariables();
    assertEquals(5, ((PositionedNode) variables.get(0)).getEndPos().getLine());
}
Also used : Start(de.be4.eventbalg.core.parser.node.Start) PositionedNode(de.hhu.stups.sablecc.patch.PositionedNode) PVariable(de.be4.eventbalg.core.parser.node.PVariable) AMachineParseUnit(de.be4.eventbalg.core.parser.node.AMachineParseUnit) Test(org.junit.Test)

Example 4 with PositionedNode

use of de.hhu.stups.sablecc.patch.PositionedNode in project probparsers by bendisposto.

the class PositionAspectTest method testNodeSubclassOfPositionedNode.

@Test
public void testNodeSubclassOfPositionedNode() throws Exception {
    final String testMachine = "#EXPRESSION 1+2";
    final BParser parser = new BParser("testcase");
    final Start startNode = parser.parse(testMachine, true);
    assertTrue(startNode instanceof PositionedNode);
}
Also used : Start(de.be4.classicalb.core.parser.node.Start) BParser(de.be4.classicalb.core.parser.BParser) PositionedNode(de.hhu.stups.sablecc.patch.PositionedNode) Test(org.junit.Test)

Example 5 with PositionedNode

use of de.hhu.stups.sablecc.patch.PositionedNode in project probparsers by bendisposto.

the class OpSubstitutions method replaceWithDefExpression.

private ADefinitionExpression replaceWithDefExpression(final Node node, TIdentifierLiteral identifier, final List<PExpression> paramList) {
    final ADefinitionExpression newNode = new ADefinitionExpression();
    newNode.setDefLiteral(identifier);
    if (paramList != null) {
        newNode.setParameters(paramList);
    }
    final PositionedNode posNode = node;
    final PositionedNode newPosNode = newNode;
    newPosNode.setStartPos(posNode.getStartPos());
    newPosNode.setEndPos(posNode.getEndPos());
    node.replaceBy(newNode);
    return newNode;
}
Also used : ADefinitionExpression(de.be4.classicalb.core.parser.node.ADefinitionExpression) PositionedNode(de.hhu.stups.sablecc.patch.PositionedNode)

Aggregations

PositionedNode (de.hhu.stups.sablecc.patch.PositionedNode)8 Test (org.junit.Test)4 Start (de.be4.classicalb.core.parser.node.Start)2 Start (de.be4.eventb.core.parser.node.Start)2 Start (de.be4.eventbalg.core.parser.node.Start)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 SourcePositions (de.hhu.stups.sablecc.patch.SourcePositions)2 SourcecodeRange (de.hhu.stups.sablecc.patch.SourcecodeRange)2 IOException (java.io.IOException)2 InputStreamReader (java.io.InputStreamReader)2 PushbackReader (java.io.PushbackReader)2 Reader (java.io.Reader)2 StringReader (java.io.StringReader)2 BParser (de.be4.classicalb.core.parser.BParser)1 Type (de.be4.classicalb.core.parser.IDefinitions.Type)1 BParseException (de.be4.classicalb.core.parser.exceptions.BParseException)1 CheckException (de.be4.classicalb.core.parser.exceptions.CheckException)1 VisitorException (de.be4.classicalb.core.parser.exceptions.VisitorException)1