Search in sources :

Example 66 with Parser

use of de.be4.ltl.core.parser.parser.Parser in project probparsers by bendisposto.

the class ParserBugTest method test.

@Test
public void test() throws Exception {
    BParser parser = new BParser();
    Start ast = parser.parse(s, true);
    ast.apply(new ASTPrinter());
    assertTrue(true);
}
Also used : Start(de.be4.classicalb.core.parser.node.Start) ASTPrinter(de.be4.classicalb.core.parser.visualisation.ASTPrinter) BParser(de.be4.classicalb.core.parser.BParser) Test(org.junit.Test)

Example 67 with Parser

use of de.be4.ltl.core.parser.parser.Parser in project probparsers by bendisposto.

the class RulesProject method printProjectAsPrologTerm.

public void printProjectAsPrologTerm(final IPrologTermOutput pout) {
    // parser version
    pout.openTerm("parser_version");
    pout.printAtom(BParser.getBuildRevision());
    pout.closeTerm();
    pout.fullstop();
    // machine
    pout.openTerm("classical_b");
    pout.printAtom(MAIN_MACHINE_NAME);
    pout.openList();
    for (IModel iModel : bModels) {
        pout.printAtom(iModel.getPath());
    }
    pout.closeList();
    pout.closeTerm();
    pout.fullstop();
    NodeIdAssignment tempNodeIdAssignment = parsingBehaviour.isAddLineNumbers() ? this.nodeIdAssignment : new NodeIdAssignment();
    for (IModel iModel : bModels) {
        iModel.printAsProlog(pout, tempNodeIdAssignment);
    }
}
Also used : NodeIdAssignment(de.be4.classicalb.core.parser.analysis.prolog.NodeIdAssignment)

Example 68 with Parser

use of de.be4.ltl.core.parser.parser.Parser in project probparsers by bendisposto.

the class RulesParseUnit method parse.

public void parse() {
    if (this.bCompoundException != null) {
        return;
    }
    try {
        bParser = null;
        if (machineFile != null) {
            bParser = new BParser(machineFile.getPath());
            bParser.setDirectory(machineFile.getParentFile());
        } else {
            bParser = new BParser();
        }
        ParseOptions parseOptions = new ParseOptions();
        parseOptions.setGrammar(RulesGrammar.getInstance());
        bParser.setParseOptions(parseOptions);
        start = bParser.parse(content, debugOuput, new CachingDefinitionFileProvider());
        refFinder = new RulesReferencesFinder(machineFile, start);
        refFinder.findReferencedMachines();
        this.machineReferences = refFinder.getReferences();
        this.machineName = refFinder.getName();
        this.rulesMachineChecker = new RulesMachineChecker(machineFile, bParser.getFileName(), machineReferences, start);
        rulesMachineChecker.runChecks();
        this.operationList.addAll(rulesMachineChecker.getOperations());
    } catch (BCompoundException e) {
        // store parser exceptions
        this.bCompoundException = e;
    }
}
Also used : CachingDefinitionFileProvider(de.be4.classicalb.core.parser.CachingDefinitionFileProvider) ParseOptions(de.be4.classicalb.core.parser.ParseOptions) BParser(de.be4.classicalb.core.parser.BParser) BCompoundException(de.be4.classicalb.core.parser.exceptions.BCompoundException)

Example 69 with Parser

use of de.be4.ltl.core.parser.parser.Parser in project probparsers by bendisposto.

the class PreParser method evaluateDefinitionFiles.

private void evaluateDefinitionFiles(final List<Token> list) throws PreParseException, BException, BCompoundException {
    IDefinitionFileProvider cache = null;
    if (contentProvider instanceof IDefinitionFileProvider) {
        cache = (IDefinitionFileProvider) contentProvider;
    }
    for (final Token fileNameToken : list) {
        final List<String> newDoneList = new ArrayList<String>(doneDefFiles);
        try {
            final String fileName = fileNameToken.getText();
            if (doneDefFiles.contains(fileName)) {
                StringBuilder sb = new StringBuilder();
                for (String string : doneDefFiles) {
                    sb.append(string).append(" -> ");
                }
                sb.append(fileName);
                throw new PreParseException(fileNameToken, "Cyclic references in definition files: " + sb.toString());
            }
            IDefinitions definitions;
            if (cache != null && cache.getDefinitions(fileName) != null) {
                definitions = cache.getDefinitions(fileName);
            } else {
                final String content = contentProvider.getFileContent(directory, fileName);
                newDoneList.add(fileName);
                final File file = contentProvider.getFile(directory, fileName);
                String filePath = fileName;
                if (file != null) {
                    filePath = file.getCanonicalPath();
                }
                final BParser parser = new BParser(filePath, parseOptions);
                parser.setDirectory(directory);
                parser.setDoneDefFiles(newDoneList);
                parser.setDefinitions(new Definitions(file));
                parser.parse(content, debugOutput, contentProvider);
                definitions = parser.getDefinitions();
                if (cache != null) {
                    cache.storeDefinition(fileName, definitions);
                }
            }
            defFileDefinitions.addDefinitions(definitions);
            definitionTypes.addAll(definitions.getTypes());
        } catch (final IOException e) {
            throw new PreParseException(fileNameToken, "Definition file cannot be read: " + e.getLocalizedMessage());
        } finally {
        }
    }
}
Also used : ArrayList(java.util.ArrayList) Token(de.be4.classicalb.core.preparser.node.Token) IOException(java.io.IOException) PreParseException(de.be4.classicalb.core.parser.exceptions.PreParseException) File(java.io.File)

Example 70 with Parser

use of de.be4.ltl.core.parser.parser.Parser in project probparsers by bendisposto.

the class PreParser method evaluateTypes.

private void evaluateTypes(final List<Token> sortedDefinitionList, final Map<Token, Token> definitions) throws PreParseException {
    // use linked list as we rely on pop() and push()
    final LinkedList<Token> remainingDefinitions = new LinkedList<>(sortedDefinitionList);
    final LinkedList<Token> currentlyUnparseableDefinitions = new LinkedList<Token>();
    Set<String> todoDefs = new HashSet<String>();
    for (Token token : remainingDefinitions) {
        todoDefs.add(token.getText());
    }
    // use main parser for the rhs of each definition to determine type
    // if a definition can not be typed this way, it may be due to another
    // definition that is not yet parser (because it appears later in the
    // source code)
    // in this case, the definition is appended to the list again
    // the algorithm terminates if the queue is empty or if no definition
    // has been parsed
    boolean oneParsed = true;
    while (oneParsed) {
        oneParsed = false;
        while (!remainingDefinitions.isEmpty()) {
            final Token definition = remainingDefinitions.pop();
            final Token defRhs = definitions.get(definition);
            Definitions.Type type = null;
            DefinitionType definitionType = determineType(definition, defRhs, todoDefs);
            type = definitionType.type;
            if (type != null) {
                todoDefs.remove(definition.getText());
                oneParsed = true;
                definitionTypes.addTyping(definition.getText(), type);
            // types.addTyping(definition.getText(), type);
            } else {
                currentlyUnparseableDefinitions.push(definition);
            }
        }
        remainingDefinitions.addAll(currentlyUnparseableDefinitions);
        currentlyUnparseableDefinitions.clear();
    }
    if (!remainingDefinitions.isEmpty()) {
        final Token definition = remainingDefinitions.pop();
        final Token defRhs = definitions.get(definition);
        DefinitionType definitionType = determineType(definition, defRhs, todoDefs);
        if (definitionType.errorMessage != null) {
            throw new PreParseException(definitionType.errorMessage + " in file: " + modelFileName);
        // throw new BParseException(definitionType.errorToken,
        // definitionType.errorMessage + " in file: "
        // + modelFileName);
        } else {
            // fall back message
            throw new PreParseException(definition, "[" + definition.getLine() + "," + definition.getPos() + "] expecting wellformed expression, predicate or substitution as DEFINITION body (DEFINITION arguments assumed to be expressions)");
        }
    }
}
Also used : Token(de.be4.classicalb.core.preparser.node.Token) PreParseException(de.be4.classicalb.core.parser.exceptions.PreParseException) LinkedList(java.util.LinkedList) HashSet(java.util.HashSet)

Aggregations

Start (de.be4.classicalb.core.parser.node.Start)51 BParser (de.be4.classicalb.core.parser.BParser)41 Test (org.junit.Test)31 Ast2String (util.Ast2String)20 File (java.io.File)15 IOException (java.io.IOException)13 BCompoundException (de.be4.classicalb.core.parser.exceptions.BCompoundException)10 StringReader (java.io.StringReader)9 PushbackReader (java.io.PushbackReader)8 AbstractParseMachineTest (util.AbstractParseMachineTest)7 PrintStream (java.io.PrintStream)6 LexerException (de.be4.classicalb.core.parser.lexer.LexerException)5 ParsingBehaviour (de.be4.classicalb.core.parser.ParsingBehaviour)4 PreParseException (de.be4.classicalb.core.parser.exceptions.PreParseException)4 Reader (java.io.Reader)4 ArrayList (java.util.ArrayList)4 IDefinitions (de.be4.classicalb.core.parser.IDefinitions)3 NodeIdAssignment (de.be4.classicalb.core.parser.analysis.prolog.NodeIdAssignment)3 Start (de.be4.eventbalg.core.parser.node.Start)3 LtlParseException (de.be4.ltl.core.parser.LtlParseException)3