Search in sources :

Example 16 with BException

use of de.be4.classicalb.core.parser.exceptions.BException in project probparsers by bendisposto.

the class LexerTest method testStringLabeledElements.

@Test
public void testStringLabeledElements() throws BException {
    final Start rootNode = parseInput("machine Test invariants \n\t@inv1 asdf \n fdsa \n\t@inv2 qwer: \t rewq \nend", false);
    final AMachineParseUnit parseUnit = (AMachineParseUnit) rootNode.getPParseUnit();
    final LinkedList<PInvariant> invariants = parseUnit.getInvariants();
    AInvariant invariant = (AInvariant) invariants.get(0);
    // correct invariant label?
    assertEquals("inv1", invariant.getName().getText());
    // correct string representation for predicate?
    assertEquals("asdf \n fdsa", invariant.getPredicate().getText());
    invariant = (AInvariant) invariants.get(1);
    // correct invariant label?
    assertEquals("inv2", invariant.getName().getText());
    // correct string representation for predicate?
    assertEquals("qwer: \t rewq", invariant.getPredicate().getText());
}
Also used : Start(de.be4.eventb.core.parser.node.Start) PInvariant(de.be4.eventb.core.parser.node.PInvariant) AInvariant(de.be4.eventb.core.parser.node.AInvariant) AMachineParseUnit(de.be4.eventb.core.parser.node.AMachineParseUnit) Test(org.junit.Test)

Example 17 with BException

use of de.be4.classicalb.core.parser.exceptions.BException in project probparsers by bendisposto.

the class LexerTest method testStringVariant.

@Test
public void testStringVariant() throws BException {
    final Start rootNode = parseInput("machine Test\nvariant y-x\nend", false);
    final AMachineParseUnit parseUnit = (AMachineParseUnit) rootNode.getPParseUnit();
    final AVariant variantClause = (AVariant) parseUnit.getVariant();
    assertEquals("y-x", variantClause.getExpression().getText());
}
Also used : Start(de.be4.eventb.core.parser.node.Start) AVariant(de.be4.eventb.core.parser.node.AVariant) AMachineParseUnit(de.be4.eventb.core.parser.node.AMachineParseUnit) Test(org.junit.Test)

Example 18 with BException

use of de.be4.classicalb.core.parser.exceptions.BException in project probparsers by bendisposto.

the class LexerTest method testStringVariant.

@Test
public void testStringVariant() throws BException {
    final Start rootNode = parseInput("machine Test\nvariant y-x\nend", false);
    final AMachineParseUnit parseUnit = (AMachineParseUnit) rootNode.getPParseUnit();
    final AVariant variantClause = (AVariant) parseUnit.getVariant();
    assertEquals("y-x", variantClause.getExpression().getText());
}
Also used : Start(de.be4.eventbalg.core.parser.node.Start) AVariant(de.be4.eventbalg.core.parser.node.AVariant) AMachineParseUnit(de.be4.eventbalg.core.parser.node.AMachineParseUnit) Test(org.junit.Test)

Example 19 with BException

use of de.be4.classicalb.core.parser.exceptions.BException in project probparsers by bendisposto.

the class BParser method parse.

/**
 * Parses the input string.
 *
 * @param input
 *            The {@link String} to be parsed
 * @param debugOutput
 *            output debug messages on standard out?
 * @param contentProvider
 *            A {@link IFileContentProvider} that is able to load content of
 *            referenced files during the parsing process. The content
 *            provider is used for referenced definition files for example.
 * @return the root node of the AST
 * @throws BCompoundException
 *             The {@link BCompoundException} class stores all
 *             {@link BException}s occurred during the parsing process. 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 source code 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 PreParseException}: This exception contains errors
 *             that occur during the preparsing. If possible it supplies a
 *             token, where the error occurred.</li>
 *             <li>{@link BLexerException}: 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 source code
 *             position where the error occurred. 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 source code position of the last characters that
 *             were read from the input.</li>
 *             <li>{@link BParseException}: This exception is thrown in two
 *             situations. On the one hand if the parser throws a
 *             {@link ParserException} we convert it into a
 *             {@link BParseException}. On the other hand it can be thrown
 *             if any error is found during the AST transformations after
 *             the parser has finished.</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 duplicate machine clauses,
 *             we will list all occurrences in the exception.</li>
 *             </ul>
 */
public Start parse(final String input, final boolean debugOutput, final IFileContentProvider contentProvider) throws BCompoundException {
    final Reader reader = new StringReader(input);
    try {
        // PreParsing
        final DefinitionTypes defTypes = preParsing(debugOutput, reader, contentProvider, directory);
        /*
			 * The definition types are used in the lexer in order to replace an
			 * identifier token by a definition call token. This is required if
			 * the definition is a predicate because an identifier can not be
			 * parsed as a predicate. For example "... SELECT def THEN ... "
			 * would yield to a parse error. The lexer will replace the
			 * identifier token "def" by a TDefLiteralPredicate which will be
			 * excepted by the parser
			 * 
			 */
        defTypes.addAll(definitions.getTypes());
        /*
			 * Main parser
			 */
        final BLexer lexer = new BLexer(new PushbackReader(reader, BLexer.PUSHBACK_BUFFER_SIZE), defTypes);
        lexer.setParseOptions(parseOptions);
        SabbleCCBParser parser = new SabbleCCBParser(lexer);
        final Start rootNode = parser.parse();
        final List<BException> bExceptionList = new ArrayList<>();
        /*
			 * Collect available definition declarations. Needs to be done now
			 * cause they are needed by the following transformations.
			 */
        final DefinitionCollector collector = new DefinitionCollector(defTypes, this.definitions);
        collector.collectDefinitions(rootNode);
        List<CheckException> definitionsCollectorExceptions = collector.getExceptions();
        for (CheckException checkException : definitionsCollectorExceptions) {
            bExceptionList.add(new BException(getFileName(), checkException));
        }
        // perfom AST transformations that can't be done by SableCC
        try {
            applyAstTransformations(rootNode);
        } catch (CheckException e) {
            throw new BCompoundException(new BException(getFileName(), e));
        }
        // perform some semantic checks which are not done in the parser
        List<CheckException> checkExceptions = performSemanticChecks(rootNode);
        for (CheckException checkException : checkExceptions) {
            bExceptionList.add(new BException(getFileName(), checkException));
        }
        if (!bExceptionList.isEmpty()) {
            throw new BCompoundException(bExceptionList);
        }
        return rootNode;
    } catch (final LexerException e) {
        throw new BCompoundException(new BException(getFileName(), e));
    } catch (final BParseException e) {
        throw new BCompoundException(new BException(getFileName(), e));
    } catch (final IOException e) {
        throw new BCompoundException(new BException(getFileName(), e));
    } catch (final PreParseException e) {
        throw new BCompoundException(new BException(getFileName(), e));
    } catch (final ParserException e) {
        final Token token = e.getToken();
        String msg = getImprovedErrorMessageBasedOnTheErrorToken(token);
        if (msg == null) {
            msg = e.getLocalizedMessage();
        }
        final String realMsg = e.getRealMsg();
        throw new BCompoundException(new BException(getFileName(), new BParseException(token, msg, realMsg, e)));
    } catch (BException e) {
        throw new BCompoundException(e);
    }
}
Also used : DefinitionCollector(de.be4.classicalb.core.parser.analysis.checking.DefinitionCollector) ParserException(de.be4.classicalb.core.parser.parser.ParserException) Start(de.be4.classicalb.core.parser.node.Start) ArrayList(java.util.ArrayList) Reader(java.io.Reader) StringReader(java.io.StringReader) PushbackReader(java.io.PushbackReader) Token(de.be4.classicalb.core.parser.node.Token) IOException(java.io.IOException) PushbackReader(java.io.PushbackReader) StringReader(java.io.StringReader) LexerException(de.be4.classicalb.core.parser.lexer.LexerException)

Example 20 with BException

use of de.be4.classicalb.core.parser.exceptions.BException in project prob2 by bendisposto.

the class RulesMachineRun method parseAndTranslateRulesProject.

private boolean parseAndTranslateRulesProject() {
    this.rulesProject = new RulesProject();
    ParsingBehaviour parsingBehaviour = new ParsingBehaviour();
    parsingBehaviour.setAddLineNumbers(true);
    rulesProject.setParsingBehaviour(parsingBehaviour);
    rulesProject.parseProject(runnerFile);
    for (Entry<String, String> pair : constantValuesToBeInjected.entrySet()) {
        rulesProject.addConstantValue(pair.getKey(), pair.getValue());
    }
    /*
		 * parse errors and errors from semantic checks are stored in the
		 * rulesProject
		 */
    rulesProject.checkAndTranslateProject();
    if (rulesProject.hasErrors()) {
        BException bException = rulesProject.getBExceptionList().get(0);
        String message = bException.getMessage();
        logger.error("Parse error:  {}", message);
        this.errors.add(new Error(ERROR_TYPES.PARSE_ERROR, message, bException));
    }
    return rulesProject.hasErrors();
}
Also used : ProBError(de.prob.exception.ProBError) StateError(de.prob.animator.domainobjects.StateError) RulesProject(de.be4.classicalb.core.parser.rules.RulesProject) BException(de.be4.classicalb.core.parser.exceptions.BException) ParsingBehaviour(de.be4.classicalb.core.parser.ParsingBehaviour)

Aggregations

BException (de.be4.classicalb.core.parser.exceptions.BException)22 CheckException (de.be4.classicalb.core.parser.exceptions.CheckException)15 IOException (java.io.IOException)9 ArrayList (java.util.ArrayList)9 Start (de.be4.eventbalg.core.parser.node.Start)7 Test (org.junit.Test)7 BCompoundException (de.be4.classicalb.core.parser.exceptions.BCompoundException)6 Start (de.be4.eventb.core.parser.node.Start)6 PreParseException (de.be4.classicalb.core.parser.exceptions.PreParseException)4 AIdentifierExpression (de.be4.classicalb.core.parser.node.AIdentifierExpression)4 BLexerException (de.be4.classicalb.core.parser.exceptions.BLexerException)3 LexerException (de.be4.classicalb.core.parser.lexer.LexerException)3 TIdentifierLiteral (de.be4.classicalb.core.parser.node.TIdentifierLiteral)3 PrettyPrinter (de.be4.classicalb.core.parser.util.PrettyPrinter)3 AMachineParseUnit (de.be4.eventb.core.parser.node.AMachineParseUnit)3 File (java.io.File)3 ParsingBehaviour (de.be4.classicalb.core.parser.ParsingBehaviour)2 BParseException (de.be4.classicalb.core.parser.exceptions.BParseException)2 Token (de.be4.classicalb.core.preparser.node.Token)2 AInvariant (de.be4.eventb.core.parser.node.AInvariant)2