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);
}
}
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);
}
}
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();
}
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);
}
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);
}
Aggregations