use of de.be4.classicalb.core.parser.node.AIdentifierExpression 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.AIdentifierExpression 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.AIdentifierExpression in project probparsers by bendisposto.
the class DefinitionFilesTest method testRecursiveReference.
// TODO test two files
/*
* test recursive references from def file to def file
*/
@Test
public void testRecursiveReference() throws Exception {
final String testMachine = "MACHINE Test\nDEFINITIONS \"DefFile1\"; def1 == xx; def02 == aa\nEND";
final BParser parser = new BParser("testcase");
parser.parse(testMachine, false, this);
final IDefinitions definitions = parser.getDefinitions();
final AExpressionDefinitionDefinition def1 = (AExpressionDefinitionDefinition) definitions.getDefinition("def1");
assertEquals("def1", def1.getName().getText());
assertEquals(0, def1.getParameters().size());
assertTrue(def1.getRhs() instanceof AIdentifierExpression);
String ident = Utils.getTIdentifierListAsString(((AIdentifierExpression) def1.getRhs()).getIdentifier());
assertEquals("xx", ident);
final AExpressionDefinitionDefinition def2 = (AExpressionDefinitionDefinition) definitions.getDefinition("def2");
assertEquals("def2", def2.getName().getText());
assertEquals(0, def2.getParameters().size());
assertTrue(def2.getRhs() instanceof AIdentifierExpression);
ident = Utils.getTIdentifierListAsString(((AIdentifierExpression) def2.getRhs()).getIdentifier());
assertEquals("yy", ident);
final AExpressionDefinitionDefinition def3 = (AExpressionDefinitionDefinition) definitions.getDefinition("def3");
assertEquals("def3", def3.getName().getText());
assertEquals(0, def3.getParameters().size());
assertTrue(def3.getRhs() instanceof AIdentifierExpression);
ident = Utils.getTIdentifierListAsString(((AIdentifierExpression) def3.getRhs()).getIdentifier());
// definition in outer def file should overwrite the one in referenced
// def file
assertEquals("bb", ident);
}
use of de.be4.classicalb.core.parser.node.AIdentifierExpression in project probparsers by bendisposto.
the class DefinitionFilesTest method testOneDefinitionFile.
@Test
public void testOneDefinitionFile() throws BCompoundException {
final String testMachine = "MACHINE Test\nDEFINITIONS \"DefFile\"; def1 == xx\nINVARIANT def2 = def3\nEND";
final BParser parser = new BParser("testcase");
parser.parse(testMachine, true, this);
final IDefinitions definitions = parser.getDefinitions();
final AExpressionDefinitionDefinition def1 = (AExpressionDefinitionDefinition) definitions.getDefinition("def1");
assertEquals("def1", def1.getName().getText());
assertEquals(0, def1.getParameters().size());
assertTrue(def1.getRhs() instanceof AIdentifierExpression);
final AExpressionDefinitionDefinition def2 = (AExpressionDefinitionDefinition) definitions.getDefinition("def2");
assertEquals("def2", def2.getName().getText());
assertEquals(0, def2.getParameters().size());
assertTrue(def2.getRhs() instanceof AIdentifierExpression);
}
use of de.be4.classicalb.core.parser.node.AIdentifierExpression in project probparsers by bendisposto.
the class ASTBuilder method createAIdentifierExpression.
public static AIdentifierExpression createAIdentifierExpression(TIdentifierLiteral identifierLiteral) {
final String name = identifierLiteral.getText();
ArrayList<TIdentifierLiteral> list = new ArrayList<>();
TIdentifierLiteral literal = new TIdentifierLiteral(name);
list.add(literal);
AIdentifierExpression result = new AIdentifierExpression(list);
result.setStartPos(identifierLiteral.getStartPos());
result.setEndPos(identifierLiteral.getEndPos());
return result;
}
Aggregations