Search in sources :

Example 16 with IDefinitions

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

the class RulesProject method createMainMachine.

private BMachine createMainMachine(List<PDefinition> mainDefinitions) {
    BMachine mainMachine = new BMachine(MAIN_MACHINE_NAME);
    mainMachine.addIncludesClause(COMPOSITION_MACHINE_NAME);
    mainMachine.addPromotesClause(getPromotesList());
    mainMachine.addPropertiesPredicates(this.constantStringValues);
    IDefinitions idefinitions = new Definitions();
    if (mainDefinitions != null) {
        for (PDefinition pDefinition : mainDefinitions) {
            idefinitions.addDefinition(pDefinition);
        }
    }
    addToStringDefinition(idefinitions);
    addSortDefinition(idefinitions);
    addFormatToStringDefinition(idefinitions);
    addChooseDefinition(idefinitions);
    addBooleanPreferenceDefinition(idefinitions, "SET_PREF_ALLOW_LOCAL_OPERATION_CALLS", true);
    mainMachine.replaceDefinition(idefinitions);
    return mainMachine;
}
Also used : PDefinition(de.be4.classicalb.core.parser.node.PDefinition) Definitions(de.be4.classicalb.core.parser.Definitions) IDefinitions(de.be4.classicalb.core.parser.IDefinitions) IDefinitions(de.be4.classicalb.core.parser.IDefinitions)

Example 17 with IDefinitions

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

the class RulesTransformation method outARuleFailSubSubstitution.

@Override
public void outARuleFailSubSubstitution(ARuleFailSubSubstitution node) {
    addForceDefinition(iDefinitions);
    Node newNode = null;
    if (!node.getIdentifiers().isEmpty()) {
        newNode = createPositinedNode(createCounterExampleSubstitutions(node.getIdentifiers(), node.getWhen(), null, node.getMessage(), node.getErrorType()), node);
    } else {
        // default value if no value is provided
        int errorType = 1;
        if (node.getErrorType() != null) {
            errorType = Integer.parseInt(node.getErrorType().getText());
        }
        PSubstitution sub = createCounterExampleSubstitution(errorType, createSetOfPExpression(node.getMessage(), node.getMessage()), false);
        if (node.getWhen() != null) {
            // there is a when predicate but no parameters
            newNode = new AIfSubstitution(node.getWhen(), sub, new ArrayList<PSubstitution>(), null);
        } else {
            // no parameters and no when predicate
            newNode = sub;
        }
    }
    node.replaceBy(newNode);
}
Also used : NodeCloner.cloneNode(de.be4.classicalb.core.parser.util.NodeCloner.cloneNode) ArrayList(java.util.ArrayList)

Example 18 with IDefinitions

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

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

the class BParser method eparse.

public Start eparse(String input, IDefinitions context) throws BCompoundException, LexerException, IOException {
    final Reader reader = new StringReader(input);
    Start ast = null;
    List<String> ids = new ArrayList<>();
    final DefinitionTypes defTypes = new DefinitionTypes();
    defTypes.addAll(context.getTypes());
    BLexer bLexer = new BLexer(new PushbackReader(reader, BLexer.PUSHBACK_BUFFER_SIZE), defTypes);
    bLexer.setParseOptions(parseOptions);
    Token t;
    do {
        t = bLexer.next();
        if (t instanceof TIdentifierLiteral) {
            if (!ids.contains(t.getText())) {
                ids.add(t.getText());
            }
        }
    } while (!(t instanceof EOF));
    Parser p = new Parser(new EBLexer(input, BigInteger.ZERO, ids, defTypes));
    boolean ok;
    try {
        ast = p.parse();
        ok = true;
    } catch (Exception e) {
        handleException(e);
        ok = false;
    }
    BigInteger b = new BigInteger("2");
    b = b.pow(ids.size());
    b = b.subtract(BigInteger.ONE);
    while (!ok && b.compareTo(BigInteger.ZERO) > 0) {
        p = new Parser(new EBLexer(input, b, ids, defTypes));
        try {
            ast = p.parse();
            ok = true;
        } catch (ParserException e) {
            b = b.subtract(BigInteger.ONE);
            handleException(e);
        }
    }
    return ast;
}
Also used : ParserException(de.be4.classicalb.core.parser.parser.ParserException) Start(de.be4.classicalb.core.parser.node.Start) ArrayList(java.util.ArrayList) Reader(java.io.Reader) StringReader(java.io.StringReader) PushbackReader(java.io.PushbackReader) Token(de.be4.classicalb.core.parser.node.Token) TIdentifierLiteral(de.be4.classicalb.core.parser.node.TIdentifierLiteral) LexerException(de.be4.classicalb.core.parser.lexer.LexerException) IOException(java.io.IOException) ParserException(de.be4.classicalb.core.parser.parser.ParserException) PushbackReader(java.io.PushbackReader) Parser(de.be4.classicalb.core.parser.parser.Parser) StringReader(java.io.StringReader) BigInteger(java.math.BigInteger) EOF(de.be4.classicalb.core.parser.node.EOF)

Aggregations

AExpressionDefinitionDefinition (de.be4.classicalb.core.parser.node.AExpressionDefinitionDefinition)9 TIdentifierLiteral (de.be4.classicalb.core.parser.node.TIdentifierLiteral)8 BParser (de.be4.classicalb.core.parser.BParser)5 ATotalFunctionExpression (de.be4.classicalb.core.parser.node.ATotalFunctionExpression)5 ArrayList (java.util.ArrayList)5 IDefinitions (de.be4.classicalb.core.parser.IDefinitions)4 BCompoundException (de.be4.classicalb.core.parser.exceptions.BCompoundException)4 IOException (java.io.IOException)4 AStringExpression (de.be4.classicalb.core.parser.node.AStringExpression)3 Start (de.be4.classicalb.core.parser.node.Start)3 TStringLiteral (de.be4.classicalb.core.parser.node.TStringLiteral)3 File (java.io.File)3 Test (org.junit.Test)3 ASTProlog (de.be4.classicalb.core.parser.analysis.prolog.ASTProlog)2 ClassicalPositionPrinter (de.be4.classicalb.core.parser.analysis.prolog.ClassicalPositionPrinter)2 NodeIdAssignment (de.be4.classicalb.core.parser.analysis.prolog.NodeIdAssignment)2 BException (de.be4.classicalb.core.parser.exceptions.BException)2 CheckException (de.be4.classicalb.core.parser.exceptions.CheckException)2 LexerException (de.be4.classicalb.core.parser.lexer.LexerException)2 AIdentifierExpression (de.be4.classicalb.core.parser.node.AIdentifierExpression)2