Search in sources :

Example 1 with LtlParseException

use of de.be4.ltl.core.parser.LtlParseException 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
        }
    }
}
Also used : PrologTermOutput(de.prob.prolog.output.PrologTermOutput) IPrologTermOutput(de.prob.prolog.output.IPrologTermOutput) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) ProBParserBase(de.prob.parserbase.ProBParserBase) PrologTerm(de.prob.prolog.term.PrologTerm) IPrologTermOutput(de.prob.prolog.output.IPrologTermOutput) LtlParseException(de.be4.ltl.core.parser.LtlParseException)

Example 2 with LtlParseException

use of de.be4.ltl.core.parser.LtlParseException in project probparsers by bendisposto.

the class TemporalLogicParser method generatePrologTerm.

public PrologTerm generatePrologTerm(final String formula, final String stateID) throws LtlParseException {
    T ast;
    try {
        ast = parseFormula(formula);
    } catch (IOException e) {
        String msg = "StringReader should not cause IOExceptions";
        throw new IllegalStateException(msg);
    }
    StructuredPrologOutput pto = new StructuredPrologOutput();
    try {
        applyPrologGenerator(pto, stateID, specParser, ast);
    } catch (LtlAdapterException e) {
        throw e.getOriginalException();
    }
    pto.fullstop();
    return pto.getSentences().iterator().next();
}
Also used : StructuredPrologOutput(de.prob.prolog.output.StructuredPrologOutput) LtlAdapterException(de.be4.ltl.core.parser.internal.LtlAdapterException) IOException(java.io.IOException)

Example 3 with LtlParseException

use of de.be4.ltl.core.parser.LtlParseException 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;
}
Also used : LTLParseException(de.prob.typechecker.exceptions.LTLParseException) BParser(de.be4.classicalb.core.parser.BParser) BCompoundException(de.be4.classicalb.core.parser.exceptions.BCompoundException)

Example 4 with LtlParseException

use of de.be4.ltl.core.parser.LtlParseException in project probparsers by bendisposto.

the class CliBParser method parseTemporalFormula.

private static void parseTemporalFormula(BufferedReader in, final TemporalLogicParser<?> parser) throws IOException {
    String theFormula;
    PrologTermStringOutput strOutput = new PrologTermStringOutput();
    theFormula = in.readLine();
    try {
        final PrologTerm term = parser.generatePrologTerm(theFormula, null);
        strOutput.openTerm("ltl").printTerm(term).closeTerm();
    } catch (LtlParseException e) {
        strOutput.openTerm("syntax_error").printAtom(e.getLocalizedMessage()).closeTerm();
    }
    strOutput.fullstop();
    // A Friendly Reminder: strOutput includes a newline!
    print(strOutput.toString());
}
Also used : PrologTermStringOutput(de.prob.prolog.output.PrologTermStringOutput) PrologTerm(de.prob.prolog.term.PrologTerm) LtlParseException(de.be4.ltl.core.parser.LtlParseException)

Example 5 with LtlParseException

use of de.be4.ltl.core.parser.LtlParseException in project probparsers by bendisposto.

the class CtlParser method parseFormula.

protected Start parseFormula(final String formula) throws LtlParseException, IOException {
    StringReader reader = new StringReader(formula);
    PushbackReader r = new PushbackReader(reader);
    Lexer l = new CtlLexer(r);
    Parser p = new Parser(l);
    Start ast = null;
    try {
        ast = p.parse();
    } catch (ParserException e) {
        final UniversalToken token = UniversalToken.createToken(e.getToken());
        throw new LtlParseException(token, e.getLocalizedMessage());
    } catch (LexerException e) {
        throw new LtlParseException(null, e.getLocalizedMessage());
    }
    return ast;
}
Also used : ParserException(de.be4.ltl.core.ctlparser.parser.ParserException) CtlLexer(de.be4.ltl.core.parser.internal.CtlLexer) Lexer(de.be4.ltl.core.ctlparser.lexer.Lexer) UniversalToken(de.be4.ltl.core.parser.internal.UniversalToken) Start(de.be4.ltl.core.ctlparser.node.Start) StringReader(java.io.StringReader) CtlLexer(de.be4.ltl.core.parser.internal.CtlLexer) LexerException(de.be4.ltl.core.ctlparser.lexer.LexerException) PushbackReader(java.io.PushbackReader) Parser(de.be4.ltl.core.ctlparser.parser.Parser)

Aggregations

IOException (java.io.IOException)3 BCompoundException (de.be4.classicalb.core.parser.exceptions.BCompoundException)2 LtlParseException (de.be4.ltl.core.parser.LtlParseException)2 UniversalToken (de.be4.ltl.core.parser.internal.UniversalToken)2 LexerException (de.be4.ltl.core.parser.lexer.LexerException)2 ParserException (de.be4.ltl.core.parser.parser.ParserException)2 PrologTerm (de.prob.prolog.term.PrologTerm)2 LTLParseException (de.prob.typechecker.exceptions.LTLParseException)2 PushbackReader (java.io.PushbackReader)2 StringReader (java.io.StringReader)2 BParser (de.be4.classicalb.core.parser.BParser)1 AStringExpression (de.be4.classicalb.core.parser.node.AStringExpression)1 Lexer (de.be4.ltl.core.ctlparser.lexer.Lexer)1 LexerException (de.be4.ltl.core.ctlparser.lexer.LexerException)1 Start (de.be4.ltl.core.ctlparser.node.Start)1 Parser (de.be4.ltl.core.ctlparser.parser.Parser)1 ParserException (de.be4.ltl.core.ctlparser.parser.ParserException)1 CtlLexer (de.be4.ltl.core.parser.internal.CtlLexer)1 LtlAdapterException (de.be4.ltl.core.parser.internal.LtlAdapterException)1 LtlLexer (de.be4.ltl.core.parser.internal.LtlLexer)1