Search in sources :

Example 21 with Start

use of de.be4.ltl.core.parser.node.Start in project probparsers by bendisposto.

the class PredicatesTest method getTreeAsString.

private String getTreeAsString(final String testMachine) throws BCompoundException {
    final Start startNode = parser.parse(testMachine, false);
    final Ast2String ast2String = new Ast2String();
    startNode.apply(ast2String);
    return ast2String.toString();
}
Also used : Start(de.be4.classicalb.core.parser.node.Start) Ast2String(util.Ast2String)

Example 22 with Start

use of de.be4.ltl.core.parser.node.Start in project probparsers by bendisposto.

the class SourcePositionsTest method testVariablesSourcePositions.

@Test
public void testVariablesSourcePositions() throws Exception {
    final String testMachine = "MACHINE test\n" + "VARIABLES\n" + "  xx,\n" + "    yy\n" + "INVARIANT xx:INT & yy:INT\n" + "INITIALISATION xx,yy:=0,0\n" + "END\n";
    final Start result = getAst(testMachine);
    final AAbstractMachineParseUnit machine = (AAbstractMachineParseUnit) result.getPParseUnit();
    AVariablesMachineClause variables = null;
    for (final PMachineClause clause : machine.getMachineClauses()) {
        if (clause instanceof AVariablesMachineClause) {
            variables = (AVariablesMachineClause) clause;
            break;
        }
    }
    if (variables == null) {
        fail("variables clause not found");
    }
    final LinkedList<PExpression> ids = variables.getIdentifiers();
    assertEquals(2, ids.size());
    final AIdentifierExpression x = (AIdentifierExpression) ids.get(0);
    final AIdentifierExpression y = (AIdentifierExpression) ids.get(1);
    // VARIABLES block
    assertEquals(2, variables.getStartPos().getLine());
    assertEquals(1, variables.getStartPos().getPos());
    assertEquals(4, variables.getEndPos().getLine());
    assertEquals(7, variables.getEndPos().getPos());
    // variable x declaration
    assertEquals(3, x.getStartPos().getLine());
    assertEquals(3, x.getStartPos().getPos());
    assertEquals(3, x.getEndPos().getLine());
    assertEquals(5, x.getEndPos().getPos());
    // variable y declaration
    assertEquals(4, y.getStartPos().getLine());
    assertEquals(5, y.getStartPos().getPos());
    assertEquals(4, y.getEndPos().getLine());
    assertEquals(7, y.getEndPos().getPos());
}
Also used : AVariablesMachineClause(de.be4.classicalb.core.parser.node.AVariablesMachineClause) Start(de.be4.classicalb.core.parser.node.Start) AIdentifierExpression(de.be4.classicalb.core.parser.node.AIdentifierExpression) AAbstractMachineParseUnit(de.be4.classicalb.core.parser.node.AAbstractMachineParseUnit) PMachineClause(de.be4.classicalb.core.parser.node.PMachineClause) PExpression(de.be4.classicalb.core.parser.node.PExpression) Test(org.junit.Test)

Example 23 with Start

use of de.be4.ltl.core.parser.node.Start 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 24 with Start

use of de.be4.ltl.core.parser.node.Start 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 25 with Start

use of de.be4.ltl.core.parser.node.Start in project probparsers by bendisposto.

the class PreParser method parse.

public void parse() throws PreParseException, IOException, BException, BCompoundException {
    final PreLexer preLexer = new PreLexer(pushbackReader);
    final Parser preParser = new Parser(preLexer);
    Start rootNode = null;
    try {
        rootNode = preParser.parse();
    } catch (final ParserException e) {
        if (e.getToken() instanceof de.be4.classicalb.core.preparser.node.TDefinitions) {
            final Token errorToken = e.getToken();
            final String message = "[" + errorToken.getLine() + "," + errorToken.getPos() + "] " + "Clause 'DEFINITIONS' is used more than once";
            throw new PreParseException(e.getToken(), message);
        } else {
            throw new PreParseException(e.getToken(), e.getLocalizedMessage());
        }
    } catch (final LexerException e) {
        throw new PreParseException(e.getLocalizedMessage());
    }
    final DefinitionPreCollector collector = new DefinitionPreCollector();
    rootNode.apply(collector);
    evaluateDefinitionFiles(collector.getFileDefinitions());
    List<Token> sortedDefinitionList = sortDefinitionsByTopologicalOrderAndCheckForCycles(collector.getDefinitions());
    evaluateTypes(sortedDefinitionList, collector.getDefinitions());
}
Also used : ParserException(de.be4.classicalb.core.preparser.parser.ParserException) Start(de.be4.classicalb.core.preparser.node.Start) Token(de.be4.classicalb.core.preparser.node.Token) DefinitionPreCollector(de.be4.classicalb.core.parser.analysis.checking.DefinitionPreCollector) Parser(de.be4.classicalb.core.preparser.parser.Parser) PreParseException(de.be4.classicalb.core.parser.exceptions.PreParseException) BLexerException(de.be4.classicalb.core.parser.exceptions.BLexerException) LexerException(de.be4.classicalb.core.preparser.lexer.LexerException)

Aggregations

Start (de.be4.classicalb.core.parser.node.Start)104 Test (org.junit.Test)84 BParser (de.be4.classicalb.core.parser.BParser)43 Ast2String (util.Ast2String)32 File (java.io.File)20 Start (de.be4.eventbalg.core.parser.node.Start)19 BCompoundException (de.be4.classicalb.core.parser.exceptions.BCompoundException)18 Start (de.be4.eventb.core.parser.node.Start)18 AMachineParseUnit (de.be4.eventb.core.parser.node.AMachineParseUnit)14 AMachineParseUnit (de.be4.eventbalg.core.parser.node.AMachineParseUnit)14 IOException (java.io.IOException)12 RecursiveMachineLoader (de.be4.classicalb.core.parser.analysis.prolog.RecursiveMachineLoader)11 PushbackReader (java.io.PushbackReader)11 StringReader (java.io.StringReader)11 ASTProlog (de.be4.classicalb.core.parser.analysis.prolog.ASTProlog)8 AExpressionParseUnit (de.be4.classicalb.core.parser.node.AExpressionParseUnit)7 AbstractParseMachineTest (util.AbstractParseMachineTest)7 ParsingBehaviour (de.be4.classicalb.core.parser.ParsingBehaviour)6 ClassicalPositionPrinter (de.be4.classicalb.core.parser.analysis.prolog.ClassicalPositionPrinter)6 ASTPrinter (de.be4.classicalb.core.parser.visualisation.ASTPrinter)6