Search in sources :

Example 1 with ParsingBehaviour

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

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

the class CliBParser method main.

public static void main(final String[] args) throws IOException {
    // System.out.println("Ready. Press enter");
    // System.in.read();
    // System.out.println("Starting");
    final ConsoleOptions options = createConsoleOptions(args);
    if (options.isOptionSet(CLI_SWITCH_VERSION)) {
        System.out.println(CliBParser.getBuildRevision());
        System.exit(0);
    }
    final String[] arguments = options.getRemainingOptions();
    if (!options.isOptionSet(CLI_SWITCH_PREPL) && arguments.length != 1) {
        options.printUsage(System.err);
        System.exit(-1);
    }
    final ParsingBehaviour behaviour = new ParsingBehaviour();
    final File f;
    PrintStream out;
    if (options.isOptionSet(CLI_SWITCH_OUTPUT)) {
        final String filename = options.getOptions(CLI_SWITCH_OUTPUT)[0];
        f = new File(filename);
        try {
            out = new PrintStream(filename);
        } catch (final FileNotFoundException e) {
            if (options.isOptionSet(CLI_SWITCH_PROLOG)) {
                PrologExceptionPrinter.printException(System.err, e, f.getAbsolutePath());
            } else {
                System.err.println("Unable to create file '" + filename + "'");
            }
            System.exit(-1);
            // Unreachable, but needed
            return;
        }
    } else {
        out = System.out;
        f = null;
    }
    behaviour.setOut(out);
    behaviour.setOutputFile(f);
    behaviour.setPrintTime(options.isOptionSet(CLI_SWITCH_TIME));
    behaviour.setPrologOutput(options.isOptionSet(CLI_SWITCH_PROLOG));
    behaviour.setAddLineNumbers(options.isOptionSet(CLI_SWITCH_PROLOG_LINES));
    behaviour.setUseIndention(options.isOptionSet(CLI_SWITCH_INDENTION));
    behaviour.setDisplayGraphically(options.isOptionSet(CLI_SWITCH_UI));
    behaviour.setPrintAST(options.isOptionSet(CLI_SWITCH_AST));
    behaviour.setVerbose(options.isOptionSet(CLI_SWITCH_VERBOSE));
    behaviour.setFastPrologOutput(options.isOptionSet(CLI_SWITCH_FASTPROLOG));
    behaviour.setMachineNameMustMatchFileName(options.isOptionSet(CLI_SWITCH_NAME_CHECK));
    if (options.isOptionSet(CLI_SWITCH_PREPL)) {
        runPRepl(behaviour);
    } else {
        // parsed correctly
        if (options.getRemainingOptions().length != 1) {
            options.printUsage(System.err);
            System.exit(-1);
        }
        String filename = options.getRemainingOptions()[0];
        final File bfile = new File(filename);
        int returnValue;
        if (options.isOptionSet(CLI_SWITCH_OUTPUT)) {
            returnValue = doFileParsing(behaviour, out, System.err, true, bfile);
        } else {
            returnValue = doFileParsing(behaviour, out, System.err, false, bfile);
        }
        System.exit(returnValue);
    }
}
Also used : PrintStream(java.io.PrintStream) FileNotFoundException(java.io.FileNotFoundException) ParsingBehaviour(de.be4.classicalb.core.parser.ParsingBehaviour) File(java.io.File)

Example 3 with ParsingBehaviour

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

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

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

the class RulesUtil method getFileAsPrologTerm.

public static String getFileAsPrologTerm(final String file) {
    ParsingBehaviour pb = new ParsingBehaviour();
    pb.setAddLineNumbers(false);
    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();
        }
    };
    RulesProject.parseProject(new File(file), pb, new PrintStream(output), new PrintStream(output));
    return output.toString();
}
Also used : PrintStream(java.io.PrintStream) OutputStream(java.io.OutputStream) ParsingBehaviour(de.be4.classicalb.core.parser.ParsingBehaviour) File(java.io.File)

Aggregations

ParsingBehaviour (de.be4.classicalb.core.parser.ParsingBehaviour)19 File (java.io.File)16 PrintStream (java.io.PrintStream)12 BParser (de.be4.classicalb.core.parser.BParser)9 OutputStream (java.io.OutputStream)7 Start (de.be4.classicalb.core.parser.node.Start)6 Test (org.junit.Test)6 BCompoundException (de.be4.classicalb.core.parser.exceptions.BCompoundException)5 RecursiveMachineLoader (de.be4.classicalb.core.parser.analysis.prolog.RecursiveMachineLoader)4 LexerException (de.be4.classicalb.core.parser.lexer.LexerException)3 ProBError (de.prob.exception.ProBError)3 FileNotFoundException (java.io.FileNotFoundException)3 IOException (java.io.IOException)3 BException (de.be4.classicalb.core.parser.exceptions.BException)2 RulesProject (de.be4.classicalb.core.parser.rules.RulesProject)2 LtlParseException (de.be4.ltl.core.parser.LtlParseException)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 Ast2String (util.Ast2String)2 MockedDefinitions (de.be4.classicalb.core.parser.MockedDefinitions)1 ParserException (de.be4.classicalb.core.parser.parser.ParserException)1