Search in sources :

Example 11 with PExpression

use of de.be4.classicalb.core.parser.node.PExpression in project probparsers by bendisposto.

the class SetType method createASTNode.

public PExpression createASTNode(Typechecker typechecker) {
    APowSubsetExpression node = new APowSubsetExpression(subtype.createASTNode(typechecker));
    typechecker.setType(node, this);
    return node;
}
Also used : APowSubsetExpression(de.be4.classicalb.core.parser.node.APowSubsetExpression)

Example 12 with PExpression

use of de.be4.classicalb.core.parser.node.PExpression in project probparsers by bendisposto.

the class StringType method createASTNode.

public PExpression createASTNode(Typechecker typechecker) {
    AStringSetExpression node = new AStringSetExpression();
    typechecker.setType(node, new SetType(this));
    return node;
}
Also used : AStringSetExpression(de.be4.classicalb.core.parser.node.AStringSetExpression)

Example 13 with PExpression

use of de.be4.classicalb.core.parser.node.PExpression in project probparsers by bendisposto.

the class StructType method createASTNode.

public PExpression createASTNode(Typechecker typechecker) {
    ArrayList<PRecEntry> list = new ArrayList<PRecEntry>();
    Set<Entry<String, BType>> entrySet = this.types.entrySet();
    for (Entry<String, BType> entry : entrySet) {
        String name = entry.getKey();
        BType type = entry.getValue();
        TIdentifierLiteral literal = new TIdentifierLiteral(name);
        ArrayList<TIdentifierLiteral> idList = new ArrayList<TIdentifierLiteral>();
        idList.add(literal);
        AIdentifierExpression id = new AIdentifierExpression(idList);
        ARecEntry recEntry = new ARecEntry(id, type.createASTNode(typechecker));
        list.add(recEntry);
    }
    AStructExpression node = new AStructExpression(list);
    typechecker.setType(node, new SetType(this));
    return node;
}
Also used : AIdentifierExpression(de.be4.classicalb.core.parser.node.AIdentifierExpression) ARecEntry(de.be4.classicalb.core.parser.node.ARecEntry) ArrayList(java.util.ArrayList) TIdentifierLiteral(de.be4.classicalb.core.parser.node.TIdentifierLiteral) AStructExpression(de.be4.classicalb.core.parser.node.AStructExpression) ARecEntry(de.be4.classicalb.core.parser.node.ARecEntry) PRecEntry(de.be4.classicalb.core.parser.node.PRecEntry) Entry(java.util.Map.Entry) PRecEntry(de.be4.classicalb.core.parser.node.PRecEntry)

Example 14 with PExpression

use of de.be4.classicalb.core.parser.node.PExpression 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 15 with PExpression

use of de.be4.classicalb.core.parser.node.PExpression 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

PExpression (de.be4.classicalb.core.parser.node.PExpression)50 AIdentifierExpression (de.be4.classicalb.core.parser.node.AIdentifierExpression)28 ArrayList (java.util.ArrayList)27 CheckException (de.be4.classicalb.core.parser.exceptions.CheckException)21 TIdentifierLiteral (de.be4.classicalb.core.parser.node.TIdentifierLiteral)12 Test (org.junit.Test)6 Node (de.be4.classicalb.core.parser.node.Node)5 LinkedList (java.util.LinkedList)5 AExpressionParseUnit (de.be4.classicalb.core.parser.node.AExpressionParseUnit)4 AFunctionExpression (de.be4.classicalb.core.parser.node.AFunctionExpression)4 AIntegerExpression (de.be4.classicalb.core.parser.node.AIntegerExpression)4 PSubstitution (de.be4.classicalb.core.parser.node.PSubstitution)4 VisitorException (de.be4.classicalb.core.parser.exceptions.VisitorException)3 ADefinitionExpression (de.be4.classicalb.core.parser.node.ADefinitionExpression)3 AStringExpression (de.be4.classicalb.core.parser.node.AStringExpression)3 Start (de.be4.classicalb.core.parser.node.Start)3 BParser (de.be4.classicalb.core.parser.BParser)2 AExpressionDefinitionDefinition (de.be4.classicalb.core.parser.node.AExpressionDefinitionDefinition)2 AMachineReference (de.be4.classicalb.core.parser.node.AMachineReference)2 AOpSubstitution (de.be4.classicalb.core.parser.node.AOpSubstitution)2