Search in sources :

Example 21 with Parser

use of de.be4.classicalb.core.parser.parser.Parser in project probparsers by bendisposto.

the class CliBParser method parseFormula.

private static void parseFormula(String theFormula, IDefinitions context) {
    try {
        BParser parser = new BParser();
        parser.setDefinitions(context);
        Start start = parser.parse(theFormula, false);
        PrologTermStringOutput strOutput = new PrologTermStringOutput();
        NodeIdAssignment na = new NodeIdAssignment();
        start.apply(na);
        ClassicalPositionPrinter pprinter = new ClassicalPositionPrinter(na, -1, 0);
        ASTProlog printer = new ASTProlog(strOutput, pprinter);
        start.apply(printer);
        strOutput.fullstop();
        // A Friendly Reminder: strOutput includes a newline!
        String output = strOutput.toString();
        print(output);
    } catch (NullPointerException e) {
        // Not Parseable - Sadly, calling e.getLocalizedMessage() on the
        // NullPointerException returns NULL itself, thus triggering another
        // NullPointerException in the catch statement. Therefore we need a
        // second catch statement with a special case for the
        // NullPointerException instead of catching a general Exception
        // print("EXCEPTION NullPointerException" + System.lineSeparator());
        PrologTermStringOutput strOutput = new PrologTermStringOutput();
        strOutput.openTerm("exception").printAtom("NullPointerException").closeTerm();
        strOutput.fullstop();
        strOutput.flush();
        String output = strOutput.toString();
        print(output);
    } catch (BCompoundException e) {
        PrologExceptionPrinter.printException(socketOutputStream, e, false, true);
    }
}
Also used : ClassicalPositionPrinter(de.be4.classicalb.core.parser.analysis.prolog.ClassicalPositionPrinter) ASTProlog(de.be4.classicalb.core.parser.analysis.prolog.ASTProlog) Start(de.be4.classicalb.core.parser.node.Start) PrologTermStringOutput(de.prob.prolog.output.PrologTermStringOutput) BParser(de.be4.classicalb.core.parser.BParser) NodeIdAssignment(de.be4.classicalb.core.parser.analysis.prolog.NodeIdAssignment) BCompoundException(de.be4.classicalb.core.parser.exceptions.BCompoundException)

Example 22 with Parser

use of de.be4.classicalb.core.parser.parser.Parser in project probparsers by bendisposto.

the class CliBParser method doFileParsing.

private static int doFileParsing(final ParsingBehaviour behaviour, final PrintStream out, final PrintStream err, final boolean closeStream, final File bfile) {
    int returnValue;
    try {
        final String fileName = bfile.getName();
        final String extension = fileName.substring(fileName.lastIndexOf(".") + 1);
        if (extension.equals("rmch")) {
            returnValue = RulesProject.parseProject(bfile, behaviour, out, err);
        } else {
            final BParser parser = new BParser(bfile.getAbsolutePath());
            returnValue = parser.fullParsing(bfile, behaviour, out, err);
        }
    } catch (Exception e) {
        e.printStackTrace();
        returnValue = -4;
    } finally {
        if (closeStream) {
            out.close();
        }
    }
    return returnValue;
}
Also used : BParser(de.be4.classicalb.core.parser.BParser) LtlParseException(de.be4.ltl.core.parser.LtlParseException) LexerException(de.be4.classicalb.core.parser.lexer.LexerException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) BCompoundException(de.be4.classicalb.core.parser.exceptions.BCompoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 23 with Parser

use of de.be4.classicalb.core.parser.parser.Parser in project probparsers by bendisposto.

the class CliBParser method parseExtendedFormula.

private static void parseExtendedFormula(String theFormula, IDefinitions context) {
    try {
        BParser parser = new BParser();
        parser.setDefinitions(context);
        Start start = parser.eparse(theFormula, context);
        PrologTermStringOutput strOutput = new PrologTermStringOutput();
        NodeIdAssignment na = new NodeIdAssignment();
        start.apply(na);
        ClassicalPositionPrinter pprinter = new ClassicalPositionPrinter(na, -1, 0);
        ASTProlog printer = new ASTProlog(strOutput, pprinter);
        start.apply(printer);
        strOutput.fullstop();
        // A Friendly Reminder: strOutput includes a newline!
        print(strOutput.toString());
    } catch (NullPointerException e) {
        // Not Parseable - Sadly, calling e.getLocalizedMessage() on the
        // NullPointerException returns NULL itself, thus triggering another
        // NullPointerException in the catch statement. Therefore we need a
        // second catch statement with a special case for the
        // NullPointerException instead of catching a general Exception
        // print("EXCEPTION NullPointerException" + System.lineSeparator());
        PrologTermStringOutput strOutput = new PrologTermStringOutput();
        strOutput.openTerm("exception").printAtom("NullPointerException").closeTerm();
        strOutput.fullstop();
        strOutput.flush();
        print(strOutput.toString());
    } catch (BCompoundException e) {
        PrologExceptionPrinter.printException(socketOutputStream, e, false, true);
    } catch (LexerException e) {
        PrologTermStringOutput strOutput = new PrologTermStringOutput();
        strOutput.openTerm("exception").printAtom(e.getLocalizedMessage()).closeTerm();
        strOutput.fullstop();
        strOutput.flush();
        print(strOutput.toString());
    } catch (IOException e) {
        PrologExceptionPrinter.printException(socketOutputStream, e, theFormula, false, true);
    }
}
Also used : ClassicalPositionPrinter(de.be4.classicalb.core.parser.analysis.prolog.ClassicalPositionPrinter) ASTProlog(de.be4.classicalb.core.parser.analysis.prolog.ASTProlog) Start(de.be4.classicalb.core.parser.node.Start) PrologTermStringOutput(de.prob.prolog.output.PrologTermStringOutput) BParser(de.be4.classicalb.core.parser.BParser) IOException(java.io.IOException) LexerException(de.be4.classicalb.core.parser.lexer.LexerException) NodeIdAssignment(de.be4.classicalb.core.parser.analysis.prolog.NodeIdAssignment) BCompoundException(de.be4.classicalb.core.parser.exceptions.BCompoundException)

Example 24 with Parser

use of de.be4.classicalb.core.parser.parser.Parser in project probparsers by bendisposto.

the class FilePragmaTest method parseFile.

private static void parseFile(final String filename) throws IOException, BCompoundException {
    final int dot = filename.lastIndexOf('.');
    if (dot >= 0) {
        final File machineFile = new File(filename);
        final String probfilename = filename.substring(0, dot) + ".prob";
        BParser parser = new BParser(filename);
        Start tree = parser.parseFile(machineFile, false);
        PrintStream output = new PrintStream(probfilename);
        BParser.printASTasProlog(output, parser, machineFile, tree, new ParsingBehaviour(), parser.getContentProvider());
        output.close();
    } else
        throw new IllegalArgumentException("Filename '" + filename + "' has no extension");
}
Also used : PrintStream(java.io.PrintStream) Start(de.be4.classicalb.core.parser.node.Start) BParser(de.be4.classicalb.core.parser.BParser) Ast2String(util.Ast2String) File(java.io.File) ParsingBehaviour(de.be4.classicalb.core.parser.ParsingBehaviour)

Example 25 with Parser

use of de.be4.classicalb.core.parser.parser.Parser in project probparsers by bendisposto.

the class FilePragmaTest method getTreeAsString.

private String getTreeAsString(final String testMachine) throws BCompoundException {
    // System.out.println("Parsing \"" + testMachine + "\"");
    final BParser parser = new BParser("testcase");
    parser.getOptions().setGrammar(RulesGrammar.getInstance());
    final Start startNode = parser.parse(testMachine, false);
    // startNode.apply(new ASTPrinter());
    final Ast2String ast2String = new Ast2String();
    startNode.apply(ast2String);
    final String string = ast2String.toString();
    // System.out.println(string);
    return string;
}
Also used : Start(de.be4.classicalb.core.parser.node.Start) BParser(de.be4.classicalb.core.parser.BParser) Ast2String(util.Ast2String) Ast2String(util.Ast2String)

Aggregations

Start (de.be4.classicalb.core.parser.node.Start)51 BParser (de.be4.classicalb.core.parser.BParser)41 Test (org.junit.Test)31 Ast2String (util.Ast2String)20 File (java.io.File)15 IOException (java.io.IOException)13 BCompoundException (de.be4.classicalb.core.parser.exceptions.BCompoundException)10 StringReader (java.io.StringReader)9 PushbackReader (java.io.PushbackReader)8 AbstractParseMachineTest (util.AbstractParseMachineTest)7 PrintStream (java.io.PrintStream)6 LexerException (de.be4.classicalb.core.parser.lexer.LexerException)5 ParsingBehaviour (de.be4.classicalb.core.parser.ParsingBehaviour)4 PreParseException (de.be4.classicalb.core.parser.exceptions.PreParseException)4 Reader (java.io.Reader)4 ArrayList (java.util.ArrayList)4 IDefinitions (de.be4.classicalb.core.parser.IDefinitions)3 NodeIdAssignment (de.be4.classicalb.core.parser.analysis.prolog.NodeIdAssignment)3 Start (de.be4.eventbalg.core.parser.node.Start)3 LtlParseException (de.be4.ltl.core.parser.LtlParseException)3