use of de.be4.classicalb.core.parser.node.Node 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);
}
}
use of de.be4.classicalb.core.parser.node.Node 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();
}
use of de.be4.classicalb.core.parser.node.Node 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);
}
use of de.be4.classicalb.core.parser.node.Node in project probparsers by bendisposto.
the class ASTPrologTest method printAST.
private String printAST(final Node node) {
final StringWriter swriter = new StringWriter();
NodeIdAssignment nodeids = new NodeIdAssignment();
node.apply(nodeids);
IPrologTermOutput pout = new PrologTermOutput(new PrintWriter(swriter), false);
PositionPrinter pprinter = new ClassicalPositionPrinter(nodeids);
ASTProlog prolog = new ASTProlog(pout, pprinter);
node.apply(prolog);
swriter.flush();
System.out.println(swriter.toString());
return swriter.toString();
}
use of de.be4.classicalb.core.parser.node.Node in project probparsers by bendisposto.
the class ASTBuilder method createExpressionList.
public static List<PExpression> createExpressionList(PExpression... pExpressions) {
final List<PExpression> list = new ArrayList<>();
for (int i = 0; i < pExpressions.length; i++) {
PExpression oldNode = pExpressions[i];
PExpression node = cloneNode(pExpressions[i]);
node.setStartPos(oldNode.getStartPos());
node.setEndPos(oldNode.getEndPos());
list.add(node);
}
return list;
}
Aggregations