Search in sources :

Example 21 with ParsingBehaviour

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

the class BatchParser 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) File(java.io.File) ParsingBehaviour(de.be4.classicalb.core.parser.ParsingBehaviour)

Example 22 with ParsingBehaviour

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

the class BParser method printASTasProlog.

public static void printASTasProlog(final PrintStream out, final BParser parser, final File bfile, final Start tree, final ParsingBehaviour parsingBehaviour, IDefinitionFileProvider contentProvider) throws BCompoundException {
    final RecursiveMachineLoader rml = new RecursiveMachineLoader(bfile.getParent(), contentProvider, parsingBehaviour);
    rml.loadAllMachines(bfile, tree, parser.getDefinitions());
    rml.printAsProlog(new PrintWriter(out));
}
Also used : RecursiveMachineLoader(de.be4.classicalb.core.parser.analysis.prolog.RecursiveMachineLoader) PrintWriter(java.io.PrintWriter)

Example 23 with ParsingBehaviour

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

the class BParser method getASTasFastProlog.

private static String getASTasFastProlog(final BParser parser, final File bfile, final Start tree, final ParsingBehaviour parsingBehaviour, IDefinitionFileProvider contentProvider) throws BCompoundException {
    final RecursiveMachineLoader rml = new RecursiveMachineLoader(bfile.getParent(), contentProvider, parsingBehaviour);
    rml.loadAllMachines(bfile, tree, parser.getDefinitions());
    StructuredPrologOutput structuredPrologOutput = new StructuredPrologOutput();
    rml.printAsProlog(structuredPrologOutput);
    Collection<PrologTerm> sentences = structuredPrologOutput.getSentences();
    StructuredPrologOutput output = new StructuredPrologOutput();
    output.openList();
    for (PrologTerm term : sentences) {
        output.printTerm(term);
    }
    output.closeList();
    output.fullstop();
    FastReadTransformer transformer = new FastReadTransformer(output);
    return transformer.write();
}
Also used : StructuredPrologOutput(de.prob.prolog.output.StructuredPrologOutput) RecursiveMachineLoader(de.be4.classicalb.core.parser.analysis.prolog.RecursiveMachineLoader) PrologTerm(de.prob.prolog.term.PrologTerm)

Example 24 with ParsingBehaviour

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

the class Api method brules_load.

/**
 * Load a B rules machine from the given file.
 *
 * @param file the path of the file to load
 * @param prefs the preferences to use
 * @return the {@link StateSpace} for the loaded machine
 */
public StateSpace brules_load(final String file, final Map<String, String> prefs) throws IOException {
    final RulesModelFactory bRulesFactory = modelFactoryProvider.getBRulesFactory();
    final RulesProject rulesProject = new RulesProject();
    final ParsingBehaviour parsingBehaviour = new ParsingBehaviour();
    parsingBehaviour.setAddLineNumbers(true);
    rulesProject.setParsingBehaviour(parsingBehaviour);
    rulesProject.parseProject(new File(file));
    rulesProject.checkAndTranslateProject();
    if (rulesProject.hasErrors()) {
        throw new ProBError(new BCompoundException(rulesProject.getBExceptionList()));
    }
    return bRulesFactory.extract(new File(file), rulesProject).load(prefs);
}
Also used : RulesModelFactory(de.prob.model.brules.RulesModelFactory) RulesProject(de.be4.classicalb.core.parser.rules.RulesProject) ParsingBehaviour(de.be4.classicalb.core.parser.ParsingBehaviour) File(java.io.File) ProBError(de.prob.exception.ProBError) BCompoundException(de.be4.classicalb.core.parser.exceptions.BCompoundException)

Example 25 with ParsingBehaviour

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

the class ClassicalBFactory method parseAllMachines.

/**
 * Given an {@link Start} ast, {@link File} f, and {@link BParser} bparser, all
 * machines are loaded.
 *
 * @param ast
 *            {@link Start} representing the abstract syntax tree for the
 *            machine
 * @param directory
 *            the directory relative to which machines should be loaded
 * @param f
 *            {@link File} containing machine
 * @param contentProvider
 *            the content provider to use
 * @param bparser
 *            {@link BParser} for parsing
 * @return {@link RecursiveMachineLoader} rml with all loaded machines
 * @throws ProBError
 *             if the model could not be loaded
 */
public RecursiveMachineLoader parseAllMachines(final Start ast, final String directory, final File f, final IDefinitionFileProvider contentProvider, final BParser bparser) {
    try {
        ParsingBehaviour parsingBehaviour = new ParsingBehaviour();
        parsingBehaviour.setAddLineNumbers(true);
        final RecursiveMachineLoader rml = new RecursiveMachineLoader(directory, contentProvider, parsingBehaviour);
        rml.loadAllMachines(f, ast, bparser.getDefinitions());
        logger.trace("Done parsing '{}'", f.getAbsolutePath());
        return rml;
    } catch (BCompoundException e) {
        throw new ProBError(e);
    }
}
Also used : RecursiveMachineLoader(de.be4.classicalb.core.parser.analysis.prolog.RecursiveMachineLoader) ParsingBehaviour(de.be4.classicalb.core.parser.ParsingBehaviour) ProBError(de.prob.exception.ProBError) BCompoundException(de.be4.classicalb.core.parser.exceptions.BCompoundException)

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