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());
}
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());
}
}
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());
}
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;
}
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());
}
Aggregations