use of de.be4.classicalb.core.parser.node.ADefinitionExpression 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.ADefinitionExpression in project probparsers by bendisposto.
the class PreParser method determineType.
private DefinitionType determineType(final Token definition, final Token rhsToken, final Set<String> untypedDefinitions) throws PreParseException {
final String definitionRhs = rhsToken.getText();
de.be4.classicalb.core.parser.node.Start start;
de.be4.classicalb.core.parser.node.Token errorToken = null;
try {
start = tryParsing(BParser.FORMULA_PREFIX, definitionRhs);
// Predicate?
PParseUnit parseunit = start.getPParseUnit();
if (parseunit instanceof APredicateParseUnit) {
return new DefinitionType(IDefinitions.Type.Predicate);
}
// Expression or Expression/Substituion (e.g. f(x))?
AExpressionParseUnit expressionParseUnit = (AExpressionParseUnit) parseunit;
PreParserIdentifierTypeVisitor visitor = new PreParserIdentifierTypeVisitor(untypedDefinitions);
expressionParseUnit.apply(visitor);
if (visitor.isKaboom()) {
return new DefinitionType();
}
PExpression expression = expressionParseUnit.getExpression();
if ((expression instanceof AIdentifierExpression) || (expression instanceof AFunctionExpression) || (expression instanceof ADefinitionExpression)) {
return new DefinitionType(IDefinitions.Type.ExprOrSubst);
}
return new DefinitionType(IDefinitions.Type.Expression);
} catch (de.be4.classicalb.core.parser.parser.ParserException e) {
errorToken = e.getToken();
try {
tryParsing(BParser.SUBSTITUTION_PREFIX, definitionRhs);
return new DefinitionType(IDefinitions.Type.Substitution, errorToken);
} catch (de.be4.classicalb.core.parser.parser.ParserException ex) {
final de.be4.classicalb.core.parser.node.Token errorToken2 = ex.getToken();
if (errorToken.getLine() > errorToken2.getLine() || (errorToken.getLine() == errorToken2.getLine() && errorToken.getPos() >= errorToken2.getPos())) {
final String newMessage = determineNewErrorMessageWithCorrectedPositionInformations(definition, rhsToken, errorToken, e.getMessage());
return new DefinitionType(newMessage, errorToken);
} else {
final String newMessage = determineNewErrorMessageWithCorrectedPositionInformations(definition, rhsToken, errorToken2, ex.getMessage());
return new DefinitionType(newMessage, errorToken2);
}
} catch (BLexerException e1) {
errorToken = e1.getLastToken();
final String newMessage = determineNewErrorMessageWithCorrectedPositionInformations(definition, rhsToken, errorToken, e.getMessage());
throw new PreParseException(newMessage);
} catch (de.be4.classicalb.core.parser.lexer.LexerException e3) {
throw new PreParseException(determineNewErrorMessageWithCorrectedPositionInformationsWithoutToken(definition, rhsToken, e3.getMessage()));
} catch (IOException e1) {
throw new PreParseException(e.getMessage());
}
} catch (BLexerException e) {
errorToken = e.getLastToken();
final String newMessage = determineNewErrorMessageWithCorrectedPositionInformations(definition, rhsToken, errorToken, e.getMessage());
throw new PreParseException(newMessage);
} catch (de.be4.classicalb.core.parser.lexer.LexerException e) {
throw new PreParseException(determineNewErrorMessageWithCorrectedPositionInformationsWithoutToken(definition, rhsToken, e.getMessage()));
} catch (IOException e) {
throw new PreParseException(e.getMessage());
}
}
use of de.be4.classicalb.core.parser.node.ADefinitionExpression in project probparsers by bendisposto.
the class OpSubstitutions method replaceWithDefExpression.
private ADefinitionExpression replaceWithDefExpression(final Node node, TIdentifierLiteral identifier, final List<PExpression> paramList) {
final ADefinitionExpression newNode = new ADefinitionExpression();
newNode.setDefLiteral(identifier);
if (paramList != null) {
newNode.setParameters(paramList);
}
final PositionedNode posNode = node;
final PositionedNode newPosNode = newNode;
newPosNode.setStartPos(posNode.getStartPos());
newPosNode.setEndPos(posNode.getEndPos());
node.replaceBy(newNode);
return newNode;
}
use of de.be4.classicalb.core.parser.node.ADefinitionExpression in project probparsers by bendisposto.
the class OpSubstitutions method caseAFunctionExpression.
@Override
public void caseAFunctionExpression(final AFunctionExpression node) {
if (node.getIdentifier() != null) {
node.getIdentifier().apply(this);
}
if (node.getIdentifier() instanceof ADefinitionExpression && ((ADefinitionExpression) node.getIdentifier()).getParameters().isEmpty()) {
final LinkedList<PExpression> paramList = new LinkedList<>(node.getParameters());
final TIdentifierLiteral identifier = ((ADefinitionExpression) node.getIdentifier()).getDefLiteral();
if (paramList.size() <= definitions.getParameterCount(identifier.getText())) {
/*
* The parameters seem to belong to this definition, so we need
* to replace the FunctionExpression by a
* DefinitionFunctionExpression. If not enough parameters were
* given this will be found by a later check, i.e.
* DefinitionUsageCheck.
*/
final ADefinitionExpression newNode = replaceWithDefExpression(node, identifier, paramList);
final List<PExpression> copy = newNode.getParameters();
for (final PExpression e : copy) {
e.apply(this);
}
return;
}
}
/*
* Reached in case that: Identifier of this FunctionExpression is not a
* definition or there were more parameters than the definition needs
* (by declaration), so we asume the parameters belong to some other
* construct (for example a function a level higher in the AST).
*/
final List<PExpression> copy = node.getParameters();
for (final PExpression e : copy) {
e.apply(this);
}
}
Aggregations