Search in sources :

Example 1 with AExpressionParseUnit

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

the class SourcePositionsTest method testAddExpression.

@Test
public void testAddExpression() throws Exception {
    final String testMachine = "#EXPRESSION xx + 5";
    final Start result = getAst(testMachine);
    final AExpressionParseUnit exprParseUnit = (AExpressionParseUnit) result.getPParseUnit();
    assertEquals(1, exprParseUnit.getStartPos().getLine());
    assertEquals(1, exprParseUnit.getStartPos().getPos());
    assertEquals(1, exprParseUnit.getEndPos().getLine());
    assertEquals(19, exprParseUnit.getEndPos().getPos());
    final AAddExpression addExpression = (AAddExpression) exprParseUnit.getExpression();
    assertEquals(1, addExpression.getStartPos().getLine());
    assertEquals(13, addExpression.getStartPos().getPos());
    assertEquals(1, addExpression.getEndPos().getLine());
    assertEquals(19, addExpression.getEndPos().getPos());
    final AIdentifierExpression varExpression = (AIdentifierExpression) addExpression.getLeft();
    assertEquals(1, varExpression.getStartPos().getLine());
    assertEquals(13, varExpression.getStartPos().getPos());
    assertEquals(1, varExpression.getEndPos().getLine());
    assertEquals(15, varExpression.getEndPos().getPos());
    final AIntegerExpression intExpression = (AIntegerExpression) addExpression.getRight();
    assertEquals(1, intExpression.getStartPos().getLine());
    assertEquals(18, intExpression.getStartPos().getPos());
    assertEquals(1, intExpression.getEndPos().getLine());
    assertEquals(19, intExpression.getEndPos().getPos());
}
Also used : AExpressionParseUnit(de.be4.classicalb.core.parser.node.AExpressionParseUnit) AIntegerExpression(de.be4.classicalb.core.parser.node.AIntegerExpression) AAddExpression(de.be4.classicalb.core.parser.node.AAddExpression) Start(de.be4.classicalb.core.parser.node.Start) AIdentifierExpression(de.be4.classicalb.core.parser.node.AIdentifierExpression) Test(org.junit.Test)

Example 2 with AExpressionParseUnit

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

Example 3 with AExpressionParseUnit

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

the class PositionAspectTest method testSimpleNode.

@Test
public void testSimpleNode() throws Exception {
    final String testMachine = "#EXPRESSION x";
    final BParser parser = new BParser("testcase");
    final Start startNode = parser.parse(testMachine, true);
    final PExpression expression = ((AExpressionParseUnit) startNode.getPParseUnit()).getExpression();
    final SourcePosition startPos = expression.getStartPos();
    final SourcePosition endPos = expression.getEndPos();
    assertNotNull(startNode);
    assertNotNull(endPos);
    assertEquals(1, startPos.getLine());
    assertEquals(13, startPos.getPos());
    assertEquals(1, endPos.getLine());
    assertEquals(14, endPos.getPos());
}
Also used : AExpressionParseUnit(de.be4.classicalb.core.parser.node.AExpressionParseUnit) Start(de.be4.classicalb.core.parser.node.Start) SourcePosition(de.hhu.stups.sablecc.patch.SourcePosition) BParser(de.be4.classicalb.core.parser.BParser) PExpression(de.be4.classicalb.core.parser.node.PExpression) Test(org.junit.Test)

Example 4 with AExpressionParseUnit

use of de.be4.classicalb.core.parser.node.AExpressionParseUnit in project prob2 by bendisposto.

the class DomBuilder method createExpressionAST.

private Start createExpressionAST(final PExpression expression) {
    Start start = new Start();
    AExpressionParseUnit node = new AExpressionParseUnit();
    start.setPParseUnit(node);
    start.setEOF(EOF);
    node.setExpression((PExpression) expression.clone());
    node.getExpression().apply(new RenameIdentifiers());
    return start;
}
Also used : AExpressionParseUnit(de.be4.classicalb.core.parser.node.AExpressionParseUnit) Start(de.be4.classicalb.core.parser.node.Start)

Example 5 with AExpressionParseUnit

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

the class PositionAspectTest method testComposedNode.

@Test
public void testComposedNode() throws Exception {
    final String testMachine = "#EXPRESSION x+1";
    final BParser parser = new BParser("testcase");
    final Start startNode = parser.parse(testMachine, true);
    // test top node
    final PExpression expression = ((AExpressionParseUnit) startNode.getPParseUnit()).getExpression();
    SourcePosition startPos = expression.getStartPos();
    SourcePosition endPos = expression.getEndPos();
    assertNotNull(startNode);
    assertNotNull(endPos);
    assertEquals(1, startPos.getLine());
    assertEquals(13, startPos.getPos());
    assertEquals(1, endPos.getLine());
    assertEquals(16, endPos.getPos());
    // test left child: 13-14
    final PExpression leftExpr = ((AAddExpression) expression).getLeft();
    startPos = leftExpr.getStartPos();
    endPos = leftExpr.getEndPos();
    assertNotNull(startNode);
    assertNotNull(endPos);
    assertEquals(1, startPos.getLine());
    assertEquals(13, startPos.getPos());
    assertEquals(1, endPos.getLine());
    assertEquals(14, endPos.getPos());
    // test right child: 15-16
    final PExpression rightExpr = ((AAddExpression) expression).getRight();
    startPos = rightExpr.getStartPos();
    endPos = rightExpr.getEndPos();
    assertNotNull(startNode);
    assertNotNull(endPos);
    assertEquals(1, startPos.getLine());
    assertEquals(15, startPos.getPos());
    assertEquals(1, endPos.getLine());
    assertEquals(16, endPos.getPos());
}
Also used : AExpressionParseUnit(de.be4.classicalb.core.parser.node.AExpressionParseUnit) AAddExpression(de.be4.classicalb.core.parser.node.AAddExpression) Start(de.be4.classicalb.core.parser.node.Start) SourcePosition(de.hhu.stups.sablecc.patch.SourcePosition) BParser(de.be4.classicalb.core.parser.BParser) PExpression(de.be4.classicalb.core.parser.node.PExpression) Test(org.junit.Test)

Aggregations

AExpressionParseUnit (de.be4.classicalb.core.parser.node.AExpressionParseUnit)7 Start (de.be4.classicalb.core.parser.node.Start)6 Test (org.junit.Test)5 AAddExpression (de.be4.classicalb.core.parser.node.AAddExpression)4 AIntegerExpression (de.be4.classicalb.core.parser.node.AIntegerExpression)3 PExpression (de.be4.classicalb.core.parser.node.PExpression)3 BParser (de.be4.classicalb.core.parser.BParser)2 AIdentifierExpression (de.be4.classicalb.core.parser.node.AIdentifierExpression)2 SourcePosition (de.hhu.stups.sablecc.patch.SourcePosition)2 BLexerException (de.be4.classicalb.core.parser.exceptions.BLexerException)1 PreParseException (de.be4.classicalb.core.parser.exceptions.PreParseException)1 ADefinitionExpression (de.be4.classicalb.core.parser.node.ADefinitionExpression)1 AFunctionExpression (de.be4.classicalb.core.parser.node.AFunctionExpression)1 APredicateParseUnit (de.be4.classicalb.core.parser.node.APredicateParseUnit)1 PParseUnit (de.be4.classicalb.core.parser.node.PParseUnit)1 TIntegerLiteral (de.be4.classicalb.core.parser.node.TIntegerLiteral)1 LexerException (de.be4.classicalb.core.preparser.lexer.LexerException)1 ParserException (de.be4.classicalb.core.preparser.parser.ParserException)1 PositionedNode (de.hhu.stups.sablecc.patch.PositionedNode)1 IOException (java.io.IOException)1