Search in sources :

Example 1 with CheckException

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

the class EventBParser method parse.

/**
 * Parses the input string.
 *
 * @param input
 *            the {@link String} to be parsed
 * @param debugOutput
 *            output debug messages on standard out?
 * @return the root node of the AST
 * @throws BException
 *             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 sourcecode 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 EventBLexerException}: 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 sourcecode
 *             position where the error occured. 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 sourcecode position of the last characters that
 *             were read from the input.</li>
 *             <li> {@link EventBParseException}: This exception is thrown in
 *             two situations. On the one hand if the parser throws a
 *             {@link ParserException} we convert it into a
 *             {@link EventBParseException}. On the other hand it can be
 *             thrown if any error is found during the AST transformations
 *             after the parser has finished. We try to provide a token if a
 *             single token is involved in the error. Otherwise a
 *             {@link SourcecodeRange} is provided, which can be used to
 *             retrieve detailed position information from the
 *             {@link SourcePositions} (s. {@link #getSourcePositions()}).</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 dublicate machine clauses,
 *             we will list all occurances in the exception.</li>
 *             </ul>
 */
public Start parse(final String input, final boolean debugOutput) throws BException {
    final Reader reader = new StringReader(input);
    try {
        /*
			 * Main parser
			 */
        final EventBLexer lexer = new EventBLexer(new PushbackReader(reader, 99));
        lexer.setDebugOutput(debugOutput);
        Parser parser = new Parser(lexer);
        final Start rootNode = parser.parse();
        final List<IToken> tokenList = ((ITokenListContainer) lexer).getTokenList();
        /*
			 * Retrieving sourcecode positions which were found by ParserAspect
			 */
        final Map<PositionedNode, SourcecodeRange> positions = ((IParser) parser).getMapping();
        sourcePositions = new SourcePositions(tokenList, positions);
        parser = null;
        return rootNode;
    } catch (final LexerException e) {
        /*
			 * Actually it's supposed to be a EventBLexerException because the
			 * aspect 'LexerAspect' replaces any LexerException to provide
			 * sourcecode position information in the BLexerException.
			 */
        throw new BException(e);
    } catch (final ParserException e) {
        throw new BException(createEventBParseException(e));
    } catch (final EventBParseException e) {
        throw new BException(e);
    } catch (final IOException e) {
        // shouldn't happen and if, we cannot handle it
        throw new BException(e);
    }
}
Also used : ParserException(de.be4.eventb.core.parser.parser.ParserException) Start(de.be4.eventb.core.parser.node.Start) SourcePositions(de.hhu.stups.sablecc.patch.SourcePositions) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) StringReader(java.io.StringReader) PushbackReader(java.io.PushbackReader) PositionedNode(de.hhu.stups.sablecc.patch.PositionedNode) IOException(java.io.IOException) PushbackReader(java.io.PushbackReader) IParser(de.hhu.stups.sablecc.patch.IParser) Parser(de.be4.eventb.core.parser.parser.Parser) IToken(de.hhu.stups.sablecc.patch.IToken) StringReader(java.io.StringReader) ITokenListContainer(de.hhu.stups.sablecc.patch.ITokenListContainer) SourcecodeRange(de.hhu.stups.sablecc.patch.SourcecodeRange) LexerException(de.be4.eventb.core.parser.lexer.LexerException) IParser(de.hhu.stups.sablecc.patch.IParser)

Example 2 with CheckException

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

the class RulesMachineChecker method caseAIdentifierExpression.

@Override
public void caseAIdentifierExpression(AIdentifierExpression node) {
    List<TIdentifierLiteral> copy = new ArrayList<>(node.getIdentifier());
    if (copy.size() > 1) {
        this.errorList.add(new CheckException("Identifier renaming is not allowed in a RULES_MACHINE.", node));
    }
    final String name = copy.get(0).getText();
    if (currentOperation != null) {
        currentOperation.addReadVariable(node);
    }
    if (!this.identifierScope.contains(name)) {
        addReadIdentifier(node);
    }
}
Also used : CheckException(de.be4.classicalb.core.parser.exceptions.CheckException) ArrayList(java.util.ArrayList) TIdentifierLiteral(de.be4.classicalb.core.parser.node.TIdentifierLiteral)

Example 3 with CheckException

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

the class RulesMachineChecker method caseARuleFailSubSubstitution.

@Override
public void caseARuleFailSubSubstitution(ARuleFailSubSubstitution node) {
    if (!isInRule()) {
        errorList.add(new CheckException("RULE_FAIL used outside of a RULE operation", node));
        return;
    }
    checkErrorType(node.getErrorType());
    if (!node.getIdentifiers().isEmpty() && node.getWhen() == null) {
        this.errorList.add(new CheckException("The WHEN predicate must be provided if RULE_FAIL has at least one parameter.", node));
        return;
    }
    this.identifierScope.createNewScope(new LinkedList<PExpression>(node.getIdentifiers()));
    if (node.getWhen() != null) {
        node.getWhen().apply(this);
    }
    node.getMessage().apply(this);
    this.identifierScope.removeScope();
}
Also used : CheckException(de.be4.classicalb.core.parser.exceptions.CheckException) PExpression(de.be4.classicalb.core.parser.node.PExpression)

Example 4 with CheckException

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

the class RulesMachineChecker method checkOperationPredicateAttribute.

private void checkOperationPredicateAttribute(OccurredAttributes occurredAttributes, POperationAttribute pOperationAttribute) throws AssertionError {
    APredicateAttributeOperationAttribute attr = (APredicateAttributeOperationAttribute) pOperationAttribute;
    PPredicate predicate = attr.getPredicate();
    final String attrName = attr.getName().getText();
    occurredAttributes.add(attrName, pOperationAttribute);
    switch(attrName) {
        case RulesGrammar.ACTIVATION:
            if (currentOperation instanceof FunctionOperation) {
                errorList.add(new CheckException("ACTIVATION is not a valid attribute of a FUNCTION operation.", pOperationAttribute));
            } else {
                currentOperation.setActivationPredicate(predicate);
            }
            break;
        case RulesGrammar.PRECONDITION:
            if (currentOperation instanceof FunctionOperation) {
                FunctionOperation func = (FunctionOperation) currentOperation;
                func.setPreconditionPredicate(predicate);
            } else {
                errorList.add(new CheckException("PRECONDITION clause is not allowed for a RULE or COMPUTATION operation", pOperationAttribute));
            }
            break;
        case RulesGrammar.POSTCONDITION:
            if (currentOperation instanceof RuleOperation) {
                errorList.add(new CheckException("POSTCONDITION attribute is not allowed for a RULE operation", pOperationAttribute));
            } else {
                currentOperation.setPostcondition(predicate);
            }
            break;
        default:
            throw new AssertionError("Unexpected operation attribute: " + attrName);
    }
    predicate.apply(this);
}
Also used : APredicateAttributeOperationAttribute(de.be4.classicalb.core.parser.node.APredicateAttributeOperationAttribute) ARuleOperation(de.be4.classicalb.core.parser.node.ARuleOperation) AFunctionOperation(de.be4.classicalb.core.parser.node.AFunctionOperation) CheckException(de.be4.classicalb.core.parser.exceptions.CheckException) PPredicate(de.be4.classicalb.core.parser.node.PPredicate)

Example 5 with CheckException

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

the class RulesMachineChecker method caseADefinitionExpression.

@Override
public void caseADefinitionExpression(ADefinitionExpression node) {
    node.getDefLiteral().apply(this);
    final String defName = node.getDefLiteral().getText();
    if ("READ_XML_FROM_STRING".equals(defName)) {
        if (node.getParameters().size() != 1) {
            errorList.add(new CheckException("The external function 'READ_XML_FROM_STRING' requires exactly one argrument.", node));
            return;
        }
        PExpression pExpression = node.getParameters().get(0);
        if (pExpression instanceof AStringExpression) {
            AStringExpression aStringExpr = (AStringExpression) pExpression;
            TStringLiteral content = aStringExpr.getContent();
            String text = content.getText();
            int xmlStartIndex = text.indexOf("<?");
            if (xmlStartIndex == -1) {
                return;
            }
            String testString = text.substring(0, xmlStartIndex);
            int numberOfNewLines = testString.length() - testString.replace("\n", "").length();
            try {
                InputSource inputSource = new InputSource(new StringReader(text.trim()));
                SAXParserFactory factory = SAXParserFactory.newInstance();
                SAXParser saxParser = factory.newSAXParser();
                Locale newLocale = new Locale("en", "GB");
                // Surprisingly, we need both of the following two lines in
                // order to obtain all error messages in English.
                java.util.Locale.setDefault(newLocale);
                saxParser.setProperty("http://apache.org/xml/properties/locale", newLocale);
                saxParser.parse(inputSource, new DefaultHandler());
            } catch (SAXParseException e) {
                final int line = content.getLine() + numberOfNewLines + e.getLineNumber() - 1;
                final int column = (numberOfNewLines == 0 && e.getLineNumber() == 1) ? content.getPos() + e.getColumnNumber() : e.getColumnNumber();
                TStringLiteral dummy = new TStringLiteral("", line, column);
                String message = e.getMessage();
                errorList.add(new CheckException(message, dummy, e));
            } catch (SAXException e) {
                String message = e.getMessage();
                errorList.add(new CheckException(message, aStringExpr, e));
            } catch (ParserConfigurationException | IOException e) {
            /*
					 * We do nothing. The error is not handled by the parser but
					 * will be handled by the ProB prolog kernel.
					 */
            }
        }
    }
    super.caseADefinitionExpression(node);
}
Also used : Locale(java.util.Locale) InputSource(org.xml.sax.InputSource) CheckException(de.be4.classicalb.core.parser.exceptions.CheckException) AStringExpression(de.be4.classicalb.core.parser.node.AStringExpression) IOException(java.io.IOException) PExpression(de.be4.classicalb.core.parser.node.PExpression) DefaultHandler(org.xml.sax.helpers.DefaultHandler) SAXException(org.xml.sax.SAXException) SAXParseException(org.xml.sax.SAXParseException) StringReader(java.io.StringReader) SAXParser(javax.xml.parsers.SAXParser) TStringLiteral(de.be4.classicalb.core.parser.node.TStringLiteral) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Aggregations

CheckException (de.be4.classicalb.core.parser.exceptions.CheckException)78 BCompoundException (de.be4.classicalb.core.parser.exceptions.BCompoundException)19 AIdentifierExpression (de.be4.classicalb.core.parser.node.AIdentifierExpression)17 PExpression (de.be4.classicalb.core.parser.node.PExpression)16 Test (org.junit.Test)15 BException (de.be4.classicalb.core.parser.exceptions.BException)14 ArrayList (java.util.ArrayList)13 TPragmaIdOrString (de.be4.classicalb.core.parser.node.TPragmaIdOrString)12 Ast2String (util.Ast2String)10 VisitorException (de.be4.classicalb.core.parser.exceptions.VisitorException)9 Node (de.be4.classicalb.core.parser.node.Node)8 TIdentifierLiteral (de.be4.classicalb.core.parser.node.TIdentifierLiteral)8 File (java.io.File)8 HashSet (java.util.HashSet)8 IOException (java.io.IOException)7 ARuleOperation (de.be4.classicalb.core.parser.node.ARuleOperation)4 PositionedNode (de.hhu.stups.sablecc.patch.PositionedNode)4 Helpers.getTreeAsString (util.Helpers.getTreeAsString)4 Type (de.be4.classicalb.core.parser.IDefinitions.Type)3 AIntegerExpression (de.be4.classicalb.core.parser.node.AIntegerExpression)3