Search in sources :

Example 11 with BParser

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

the class Helpers method parseFile2.

public static String parseFile2(String filename) throws IOException {
    final File machineFile = new File(filename);
    final BParser parser = new BParser(machineFile.getAbsolutePath());
    Start tree;
    OutputStream output = new OutputStream() {

        private StringBuilder string = new StringBuilder();

        @Override
        public void write(int b) throws IOException {
            this.string.append((char) b);
        }

        public String toString() {
            return this.string.toString();
        }
    };
    try {
        tree = parser.parseFile(machineFile, false);
        PrintStream printStream = new PrintStream(output);
        BParser.printASTasProlog(printStream, parser, machineFile, tree, new ParsingBehaviour(), parser.getContentProvider());
        output.close();
        return output.toString();
    } catch (BCompoundException e) {
        e.printStackTrace();
        PrologExceptionPrinter.printException(output, e);
        return output.toString();
    }
}
Also used : PrintStream(java.io.PrintStream) Start(de.be4.classicalb.core.parser.node.Start) OutputStream(java.io.OutputStream) BParser(de.be4.classicalb.core.parser.BParser) File(java.io.File) ParsingBehaviour(de.be4.classicalb.core.parser.ParsingBehaviour) BCompoundException(de.be4.classicalb.core.parser.exceptions.BCompoundException)

Example 12 with BParser

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

the class Helpers method getMachineAsPrologTerm.

public static String getMachineAsPrologTerm(String input) {
    final BParser parser = new BParser("Test");
    Start start;
    try {
        start = parser.parse(input, true);
    } catch (BCompoundException e) {
        throw new RuntimeException(e);
    }
    OutputStream output = new OutputStream() {

        private StringBuilder string = new StringBuilder();

        @Override
        public void write(int b) throws IOException {
            this.string.append((char) b);
        }

        public String toString() {
            return this.string.toString();
        }
    };
    final IPrologTermOutput pout = new PrologTermOutput(output, false);
    printAsProlog(start, pout);
    return output.toString();
}
Also used : Start(de.be4.classicalb.core.parser.node.Start) PrologTermOutput(de.prob.prolog.output.PrologTermOutput) IPrologTermOutput(de.prob.prolog.output.IPrologTermOutput) OutputStream(java.io.OutputStream) BParser(de.be4.classicalb.core.parser.BParser) BCompoundException(de.be4.classicalb.core.parser.exceptions.BCompoundException) IPrologTermOutput(de.prob.prolog.output.IPrologTermOutput)

Example 13 with BParser

use of de.be4.classicalb.core.parser.BParser 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 14 with BParser

use of de.be4.classicalb.core.parser.BParser 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 15 with BParser

use of de.be4.classicalb.core.parser.BParser 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)

Aggregations

Start (de.be4.classicalb.core.parser.node.Start)63 BParser (de.be4.classicalb.core.parser.BParser)53 Test (org.junit.Test)39 File (java.io.File)23 Ast2String (util.Ast2String)21 BCompoundException (de.be4.classicalb.core.parser.exceptions.BCompoundException)18 RecursiveMachineLoader (de.be4.classicalb.core.parser.analysis.prolog.RecursiveMachineLoader)11 ParsingBehaviour (de.be4.classicalb.core.parser.ParsingBehaviour)7 PrintStream (java.io.PrintStream)7 AbstractParseMachineTest (util.AbstractParseMachineTest)7 IOException (java.io.IOException)6 ASTPrinter (de.be4.classicalb.core.parser.visualisation.ASTPrinter)5 ClassicalBModel (de.prob.model.classicalb.ClassicalBModel)4 OutputStream (java.io.OutputStream)4 BLexer (de.be4.classicalb.core.parser.BLexer)3 IDefinitions (de.be4.classicalb.core.parser.IDefinitions)3 LexerException (de.be4.classicalb.core.parser.lexer.LexerException)3 EOF (de.be4.classicalb.core.parser.node.EOF)3 Token (de.be4.classicalb.core.parser.node.Token)3 ProBError (de.prob.exception.ProBError)3