Search in sources :

Example 81 with BParser

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

Example 82 with BParser

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

the class ClassicalBFactory method parseString.

private Start parseString(final String model, final BParser bparser) {
    try {
        logger.trace("Parsing file");
        Start ast = null;
        ast = bparser.parse(model, false);
        return ast;
    } catch (BCompoundException e) {
        throw new ProBError(e);
    }
}
Also used : Start(de.be4.classicalb.core.parser.node.Start) ProBError(de.prob.exception.ProBError) BCompoundException(de.be4.classicalb.core.parser.exceptions.BCompoundException)

Example 83 with BParser

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

the class TLAFactory method extract.

@Override
public ExtractedModel<ClassicalBModel> extract(final String fileName) throws IOException, ModelTranslationError {
    ClassicalBModel classicalBModel = modelCreator.get();
    File f = new File(fileName);
    if (!f.exists()) {
        throw new FileNotFoundException("The TLA Model" + fileName + " was not found.");
    }
    Translator translator;
    Start ast;
    try {
        translator = new Translator(f.getAbsolutePath());
        ast = translator.translate();
    } catch (TLA2BException e) {
        throw new ModelTranslationError("Translation Error: " + e.getMessage(), e);
    }
    BParser bparser = new BParser();
    bparser.getDefinitions().addDefinitions(translator.getBDefinitions());
    try {
        final RecursiveMachineLoader rml = parseAllMachines(ast, f, bparser);
        classicalBModel = classicalBModel.create(ast, rml, f, bparser);
    } catch (BCompoundException e) {
        throw new ModelTranslationError(e.getMessage(), e);
    }
    return new ExtractedModel<>(classicalBModel, classicalBModel.getMainMachine());
}
Also used : ClassicalBModel(de.prob.model.classicalb.ClassicalBModel) Translator(de.tla2bAst.Translator) Start(de.be4.classicalb.core.parser.node.Start) TLA2BException(de.tla2b.exceptions.TLA2BException) FileNotFoundException(java.io.FileNotFoundException) BParser(de.be4.classicalb.core.parser.BParser) RecursiveMachineLoader(de.be4.classicalb.core.parser.analysis.prolog.RecursiveMachineLoader) File(java.io.File) BCompoundException(de.be4.classicalb.core.parser.exceptions.BCompoundException)

Example 84 with BParser

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

the class LoadBProjectCommandTest method testWriteCommand.

@Test
public void testWriteCommand() throws Exception {
    ClassLoader classLoader = getClass().getClassLoader();
    URL resource = classLoader.getResource("examples/scheduler.mch");
    File f = new File(resource.toURI());
    StructuredPrologOutput prologTermOutput = new StructuredPrologOutput();
    ClassicalBFactory factory = new ClassicalBFactory(null);
    BParser bparser = new BParser();
    Start ast = factory.parseFile(f, bparser);
    RecursiveMachineLoader rml = factory.parseAllMachines(ast, f.getParent(), f, bparser.getContentProvider(), bparser);
    LoadBProjectCommand command = new LoadBProjectCommand(rml, f);
    command.writeCommand(prologTermOutput);
    prologTermOutput.fullstop().flush();
    Collection<PrologTerm> sentences = prologTermOutput.getSentences();
    PrologTerm next = sentences.iterator().next();
    assertNotNull(next);
    assertTrue(next instanceof CompoundPrologTerm);
    CompoundPrologTerm t = (CompoundPrologTerm) next;
    assertEquals("load_classical_b_from_list_of_facts", t.getFunctor());
    assertEquals(2, t.getArity());
    PrologTerm argument = t.getArgument(2);
    assertTrue(argument.isList());
}
Also used : StructuredPrologOutput(de.prob.prolog.output.StructuredPrologOutput) ClassicalBFactory(de.prob.scripting.ClassicalBFactory) Start(de.be4.classicalb.core.parser.node.Start) BParser(de.be4.classicalb.core.parser.BParser) RecursiveMachineLoader(de.be4.classicalb.core.parser.analysis.prolog.RecursiveMachineLoader) CompoundPrologTerm(de.prob.prolog.term.CompoundPrologTerm) File(java.io.File) URL(java.net.URL) CompoundPrologTerm(de.prob.prolog.term.CompoundPrologTerm) PrologTerm(de.prob.prolog.term.PrologTerm) Test(org.junit.Test)

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