use of de.be4.eventbalg.core.parser.parser.Parser in project probparsers by bendisposto.
the class LtlConsoleParser method main.
public static void main(final String[] args) {
ConsoleOptions options = new ConsoleOptions();
options.addOption(CLI_LANG, "set language for atomic propositions, etc. (e.g. none, B)", 1);
options.addOption(CLI_OUT, "set output file, use stdout if omitted", 1);
options.addOption(CLI_LTL, "use LTL (default)");
options.addOption(CLI_CTL, "use CTL instead of LTL");
options.setIntro("usage: LtlConsoleParser [options] <LTL file>\n\n" + "If the file is omitted, stdin is used\n" + "Available options are:");
options.addOption(CLI_HELP, "print this message");
options.parseOptions(args);
if (options.isOptionSet(CLI_HELP)) {
options.printUsage(System.out);
return;
}
String[] params = options.getRemainingOptions();
if (params.length > 1) {
options.printUsage(System.out);
System.exit(-1);
return;
}
if (options.isOptionSet(CLI_LTL) && options.isOptionSet(CLI_CTL)) {
System.err.println("Incopatible options -ltl and -ctl given.");
System.exit(-1);
return;
}
final Mode mode = options.isOptionSet(CLI_CTL) ? Mode.CTL : Mode.LTL;
// please note: createOutputStream might call System.exit()
final OutputStream out = createOutputStream(options);
final String lang = options.isOptionSet(CLI_LANG) ? options.getOptions(CLI_LANG)[0] : null;
final ProBParserBase extParser = getExtensionParser(lang);
final IPrologTermOutput pto = new PrologTermOutput(out, false);
final String input = createInputStream(params, pto);
if (input != null) {
final String[] formulas = input.split("###");
final TemporalLogicParser<?> parser = createParser(extParser, mode);
pto.openList();
for (final String formula : formulas) {
try {
final PrologTerm term = parser.generatePrologTerm(formula, null);
pto.openTerm("ltl").printTerm(term).closeTerm();
} catch (LtlParseException e) {
pto.openTerm("syntax_error").printAtom(e.getLocalizedMessage()).closeTerm();
}
}
pto.closeList();
}
pto.fullstop();
pto.flush();
if (options.isOptionSet(CLI_OUT)) {
try {
out.close();
} catch (IOException e) {
// ignore
}
}
}
use of de.be4.eventbalg.core.parser.parser.Parser 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.eventbalg.core.parser.parser.Parser in project probparsers by bendisposto.
the class AbstractTest method parseInput.
protected Start parseInput(final String input, final boolean debugOutput) throws BException {
if (debugOutput) {
System.out.println();
System.out.println();
}
final EventBParser parser = new EventBParser();
final Start rootNode = parser.parse(input, debugOutput);
return rootNode;
}
use of de.be4.eventbalg.core.parser.parser.Parser in project probparsers by bendisposto.
the class LTLFormulaVisitor method parseBPredicate.
private de.be4.classicalb.core.parser.node.Start parseBPredicate(String text) {
String bPredicate = "#PREDICATE " + text;
BParser parser = new BParser("Testing");
de.be4.classicalb.core.parser.node.Start start = null;
try {
start = parser.parse(bPredicate, false);
} catch (BCompoundException e) {
throw new LTLParseException(e.getMessage());
}
return start;
}
use of de.be4.eventbalg.core.parser.parser.Parser 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