Search in sources :

Example 11 with ParsingBehaviour

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

the class RulesMachineRun method parseAndTranslateRulesProject.

private boolean parseAndTranslateRulesProject() {
    this.rulesProject = new RulesProject();
    ParsingBehaviour parsingBehaviour = new ParsingBehaviour();
    parsingBehaviour.setAddLineNumbers(true);
    rulesProject.setParsingBehaviour(parsingBehaviour);
    rulesProject.parseProject(runnerFile);
    for (Entry<String, String> pair : constantValuesToBeInjected.entrySet()) {
        rulesProject.addConstantValue(pair.getKey(), pair.getValue());
    }
    /*
		 * parse errors and errors from semantic checks are stored in the
		 * rulesProject
		 */
    rulesProject.checkAndTranslateProject();
    if (rulesProject.hasErrors()) {
        BException bException = rulesProject.getBExceptionList().get(0);
        String message = bException.getMessage();
        logger.error("Parse error:  {}", message);
        this.errors.add(new Error(ERROR_TYPES.PARSE_ERROR, message, bException));
    }
    return rulesProject.hasErrors();
}
Also used : ProBError(de.prob.exception.ProBError) StateError(de.prob.animator.domainobjects.StateError) RulesProject(de.be4.classicalb.core.parser.rules.RulesProject) BException(de.be4.classicalb.core.parser.exceptions.BException) ParsingBehaviour(de.be4.classicalb.core.parser.ParsingBehaviour)

Example 12 with ParsingBehaviour

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

the class Helpers method fullParsing.

public static String fullParsing(String filename, ParsingBehaviour parsingBehaviour) {
    final File machineFile = new File(filename);
    final BParser parser = new BParser(machineFile.getAbsolutePath());
    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();
        }
    };
    PrintStream printStream = new PrintStream(output);
    parser.fullParsing(machineFile, parsingBehaviour, printStream, printStream);
    printStream.flush();
    printStream.close();
    return output.toString();
}
Also used : PrintStream(java.io.PrintStream) OutputStream(java.io.OutputStream) BParser(de.be4.classicalb.core.parser.BParser) File(java.io.File)

Example 13 with ParsingBehaviour

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

the class Helpers method fullParsing.

public static String fullParsing(String filename) {
    final ParsingBehaviour parsingBehaviour = new ParsingBehaviour();
    parsingBehaviour.setPrologOutput(true);
    parsingBehaviour.setUseIndention(false);
    parsingBehaviour.setAddLineNumbers(false);
    parsingBehaviour.setVerbose(true);
    parsingBehaviour.setMachineNameMustMatchFileName(true);
    return fullParsing(filename, parsingBehaviour);
}
Also used : ParsingBehaviour(de.be4.classicalb.core.parser.ParsingBehaviour)

Example 14 with ParsingBehaviour

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

the class Helpers method parseFile.

public 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);
        final ParsingBehaviour behaviour = new ParsingBehaviour();
        behaviour.setVerbose(true);
        PrintStream output = new PrintStream(probfilename);
        BParser.printASTasProlog(output, parser, machineFile, tree, behaviour, 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) File(java.io.File) ParsingBehaviour(de.be4.classicalb.core.parser.ParsingBehaviour)

Example 15 with ParsingBehaviour

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

the class CliBParser method runPRepl.

private static void runPRepl(final ParsingBehaviour behaviour) throws IOException, FileNotFoundException {
    PrintStream out;
    ServerSocket serverSocket = new ServerSocket(0, 50, InetAddress.getLoopbackAddress());
    // write port number as prolog term
    System.out.println(serverSocket.getLocalPort() + ".");
    socket = serverSocket.accept();
    socketOutputStream = socket.getOutputStream();
    BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream(), encoding));
    String line = "";
    MockedDefinitions context = new MockedDefinitions();
    boolean terminate = false;
    while (!terminate) {
        line = in.readLine();
        EPreplCommands command;
        String theFormula;
        if (line == null) {
            // the prob instance has been terminated. exit gracefully
            command = EPreplCommands.halt;
        } else {
            command = EPreplCommands.valueOf(line);
        }
        switch(command) {
            case version:
                print(CliBParser.getBuildRevision() + System.lineSeparator());
                break;
            case definition:
                String name = in.readLine();
                String type = in.readLine();
                String parameterCount = in.readLine();
                context.addMockedDefinition(name, type, parameterCount);
                break;
            case machine:
                String filename = in.readLine();
                String outFile = in.readLine();
                out = new PrintStream(outFile, encoding);
                final File bfile = new File(filename);
                int returnValue;
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                PrintStream ps = new PrintStream(baos);
                try {
                    final String fileName = bfile.getName();
                    final String extension = fileName.substring(fileName.lastIndexOf(".") + 1);
                    if (extension.equals("rmch")) {
                        returnValue = RulesProject.parseProject(bfile, behaviour, out, ps);
                    } else {
                        final BParser parser = new BParser(bfile.getAbsolutePath());
                        returnValue = parser.fullParsing(bfile, behaviour, out, ps);
                    }
                    context = new MockedDefinitions();
                } catch (Exception e) {
                    e.printStackTrace();
                    returnValue = -4;
                } finally {
                    if (true) {
                        out.close();
                    }
                }
                if (returnValue == 0) {
                    print("exit(" + returnValue + ")." + System.lineSeparator());
                } else {
                    String output = baos.toString().replace(System.lineSeparator(), " ").trim();
                    print(output + System.lineSeparator());
                }
                break;
            case formula:
                theFormula = "#FORMULA\n" + in.readLine();
                parseFormula(theFormula, context);
                break;
            case expression:
                theFormula = "#EXPRESSION\n" + in.readLine();
                parseFormula(theFormula, context);
                break;
            case predicate:
                theFormula = "#PREDICATE\n" + in.readLine();
                parseFormula(theFormula, context);
                break;
            case substitution:
                theFormula = "#SUBSTITUTION\n" + in.readLine();
                parseFormula(theFormula, context);
                break;
            case extendedformula:
                theFormula = "#FORMULA\n" + in.readLine();
                parseExtendedFormula(theFormula, context);
                break;
            case extendedexpression:
                theFormula = "#EXPRESSION\n" + in.readLine();
                parseExtendedFormula(theFormula, context);
                break;
            case extendedpredicate:
                theFormula = "#PREDICATE\n" + in.readLine();
                parseExtendedFormula(theFormula, context);
                break;
            case extendedsubstitution:
                theFormula = "#SUBSTITUTION\n" + in.readLine();
                parseExtendedFormula(theFormula, context);
                break;
            case ltl:
                String extension = in.readLine();
                final ProBParserBase extParser = LtlConsoleParser.getExtensionParser(extension);
                final TemporalLogicParser<?> parser = new LtlParser(extParser);
                parseTemporalFormula(in, parser);
                break;
            case ctl:
                String extension2 = in.readLine();
                final ProBParserBase extParser2 = LtlConsoleParser.getExtensionParser(extension2);
                final TemporalLogicParser<?> parser2 = new CtlParser(extParser2);
                parseTemporalFormula(in, parser2);
                break;
            case halt:
                socket.close();
                serverSocket.close();
                terminate = true;
                break;
            default:
                throw new UnsupportedOperationException("Unsupported Command " + line);
        }
    }
}
Also used : PrintStream(java.io.PrintStream) InputStreamReader(java.io.InputStreamReader) LtlParser(de.be4.ltl.core.parser.LtlParser) ServerSocket(java.net.ServerSocket) BParser(de.be4.classicalb.core.parser.BParser) ByteArrayOutputStream(java.io.ByteArrayOutputStream) 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) MockedDefinitions(de.be4.classicalb.core.parser.MockedDefinitions) BufferedReader(java.io.BufferedReader) CtlParser(de.be4.ltl.core.parser.CtlParser) File(java.io.File) ProBParserBase(de.prob.parserbase.ProBParserBase)

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