Search in sources :

Example 1 with AStringExpression

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

Example 2 with AStringExpression

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

the class ASTBuilder method addFormatToStringDefinition.

public static void addFormatToStringDefinition(IDefinitions iDefinitions) {
    if (iDefinitions.containsDefinition(FORMAT_TO_STRING)) {
        return;
    }
    /*-
		 * FORMAT_TO_STRING(MyFormatString,ListOfValues) == "0";
		 * EXTERNAL_FUNCTION_FORMAT_TO_STRING(TO_STRING_TYPE) == STRING*seq(TO_STRING_TYPE) --> STRING;
		 */
    AExpressionDefinitionDefinition formatDef = new AExpressionDefinitionDefinition();
    formatDef.setName(new TIdentifierLiteral(FORMAT_TO_STRING));
    formatDef.setParameters(createExpressionList("S", "T"));
    formatDef.setRhs(new AStringExpression(new TStringLiteral("abc")));
    iDefinitions.addDefinition(formatDef, IDefinitions.Type.Expression);
    AExpressionDefinitionDefinition formatType = new AExpressionDefinitionDefinition();
    formatType.setName(new TIdentifierLiteral("EXTERNAL_FUNCTION_FORMAT_TO_STRING"));
    formatType.setParameters(createExpressionList("T"));
    formatType.setRhs(new ATotalFunctionExpression(new AMultOrCartExpression(new AStringSetExpression(), new ASeqExpression(createIdentifier("T"))), new AStringSetExpression()));
    iDefinitions.addDefinition(formatType, IDefinitions.Type.Expression);
}
Also used : AStringSetExpression(de.be4.classicalb.core.parser.node.AStringSetExpression) AMultOrCartExpression(de.be4.classicalb.core.parser.node.AMultOrCartExpression) ASeqExpression(de.be4.classicalb.core.parser.node.ASeqExpression) AExpressionDefinitionDefinition(de.be4.classicalb.core.parser.node.AExpressionDefinitionDefinition) AStringExpression(de.be4.classicalb.core.parser.node.AStringExpression) TStringLiteral(de.be4.classicalb.core.parser.node.TStringLiteral) TIdentifierLiteral(de.be4.classicalb.core.parser.node.TIdentifierLiteral) ATotalFunctionExpression(de.be4.classicalb.core.parser.node.ATotalFunctionExpression)

Example 3 with AStringExpression

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

the class ASTBuilder method addToStringDefinition.

public static void addToStringDefinition(IDefinitions definitions) {
    if (definitions.containsDefinition(TO_STRING)) {
        return;
    }
    /*-
		 * TO_STRING(S) == "0"; 
		 * EXTERNAL_FUNCTION_TO_STRING(X) == X -->STRING;
		 */
    AExpressionDefinitionDefinition toStringDef = new AExpressionDefinitionDefinition();
    toStringDef.setName(new TIdentifierLiteral(TO_STRING));
    toStringDef.setParameters(createIdentifierList("S"));
    toStringDef.setRhs(new AStringExpression(new TStringLiteral("0")));
    definitions.addDefinition(toStringDef, IDefinitions.Type.Expression);
    AExpressionDefinitionDefinition toStringTypeDef = new AExpressionDefinitionDefinition();
    toStringTypeDef.setName(new TIdentifierLiteral("EXTERNAL_FUNCTION_TO_STRING"));
    toStringTypeDef.setParameters(createIdentifierList("X"));
    toStringTypeDef.setRhs(new ATotalFunctionExpression(createIdentifier("X"), new AStringSetExpression()));
    definitions.addDefinition(toStringTypeDef, IDefinitions.Type.Expression);
}
Also used : AStringSetExpression(de.be4.classicalb.core.parser.node.AStringSetExpression) AExpressionDefinitionDefinition(de.be4.classicalb.core.parser.node.AExpressionDefinitionDefinition) AStringExpression(de.be4.classicalb.core.parser.node.AStringExpression) TStringLiteral(de.be4.classicalb.core.parser.node.TStringLiteral) TIdentifierLiteral(de.be4.classicalb.core.parser.node.TIdentifierLiteral) ATotalFunctionExpression(de.be4.classicalb.core.parser.node.ATotalFunctionExpression)

Example 4 with AStringExpression

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

the class ASTBuilder method createEqualPredicate.

public static AEqualPredicate createEqualPredicate(TIdentifierLiteral old, final String value) {
    TIdentifierLiteral e = NodeCloner.cloneNode(old);
    AIdentifierExpression aIdentifier = createAIdentifierExpression(e);
    final AEqualPredicate equal = new AEqualPredicate(aIdentifier, new AStringExpression(new TStringLiteral(value)));
    equal.setStartPos(e.getStartPos());
    equal.setEndPos(e.getEndPos());
    return equal;
}
Also used : AEqualPredicate(de.be4.classicalb.core.parser.node.AEqualPredicate) AIdentifierExpression(de.be4.classicalb.core.parser.node.AIdentifierExpression) AStringExpression(de.be4.classicalb.core.parser.node.AStringExpression) TStringLiteral(de.be4.classicalb.core.parser.node.TStringLiteral) TIdentifierLiteral(de.be4.classicalb.core.parser.node.TIdentifierLiteral)

Example 5 with AStringExpression

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

the class BMachine method addPropertiesPredicates.

public void addPropertiesPredicates(Map<String, String> constantStringValues) {
    if (constantStringValues.size() == 0) {
        return;
    }
    APropertiesMachineClause clause = new APropertiesMachineClause();
    this.parseUnit.getMachineClauses().add(clause);
    List<PPredicate> predList = new ArrayList<>();
    for (Entry<String, String> entry : constantStringValues.entrySet()) {
        AIdentifierExpression identifier = createIdentifier(entry.getKey());
        AStringExpression value = new AStringExpression(new TStringLiteral(entry.getValue()));
        AEqualPredicate equal = new AEqualPredicate(identifier, value);
        predList.add(equal);
    }
    clause.setPredicates(createConjunction(predList));
}
Also used : AEqualPredicate(de.be4.classicalb.core.parser.node.AEqualPredicate) AIdentifierExpression(de.be4.classicalb.core.parser.node.AIdentifierExpression) ArrayList(java.util.ArrayList) AStringExpression(de.be4.classicalb.core.parser.node.AStringExpression) TStringLiteral(de.be4.classicalb.core.parser.node.TStringLiteral) PPredicate(de.be4.classicalb.core.parser.node.PPredicate) APropertiesMachineClause(de.be4.classicalb.core.parser.node.APropertiesMachineClause)

Aggregations

AStringExpression (de.be4.classicalb.core.parser.node.AStringExpression)10 TStringLiteral (de.be4.classicalb.core.parser.node.TStringLiteral)7 TIdentifierLiteral (de.be4.classicalb.core.parser.node.TIdentifierLiteral)4 AExpressionDefinitionDefinition (de.be4.classicalb.core.parser.node.AExpressionDefinitionDefinition)3 AIdentifierExpression (de.be4.classicalb.core.parser.node.AIdentifierExpression)3 ATotalFunctionExpression (de.be4.classicalb.core.parser.node.ATotalFunctionExpression)3 CheckException (de.be4.classicalb.core.parser.exceptions.CheckException)2 AEqualPredicate (de.be4.classicalb.core.parser.node.AEqualPredicate)2 AStringSetExpression (de.be4.classicalb.core.parser.node.AStringSetExpression)2 PExpression (de.be4.classicalb.core.parser.node.PExpression)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 BCompoundException (de.be4.classicalb.core.parser.exceptions.BCompoundException)1 AConcatExpression (de.be4.classicalb.core.parser.node.AConcatExpression)1 AMultOrCartExpression (de.be4.classicalb.core.parser.node.AMultOrCartExpression)1 APowSubsetExpression (de.be4.classicalb.core.parser.node.APowSubsetExpression)1 APropertiesMachineClause (de.be4.classicalb.core.parser.node.APropertiesMachineClause)1 ASeqExpression (de.be4.classicalb.core.parser.node.ASeqExpression)1 PPredicate (de.be4.classicalb.core.parser.node.PPredicate)1 TMultilineStringContent (de.be4.classicalb.core.parser.node.TMultilineStringContent)1