Search in sources :

Example 26 with Definitions

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

the class PredVarsTest method getTreeAsString.

private String getTreeAsString(final String testMachine) throws BCompoundException, LexerException, IOException {
    final BParser parser = new BParser("testcase");
    Start ast = parser.eparse(testMachine, new Definitions());
    final Ast2String ast2String = new Ast2String();
    ast.apply(ast2String);
    final String string = ast2String.toString();
    return string;
}
Also used : Start(de.be4.classicalb.core.parser.node.Start) Definitions(de.be4.classicalb.core.parser.Definitions) BParser(de.be4.classicalb.core.parser.BParser) Ast2String(util.Ast2String) Ast2String(util.Ast2String)

Example 27 with Definitions

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

the class ASTBuilder method addPrintSubDefinitionToIdefinitions.

public static void addPrintSubDefinitionToIdefinitions(IDefinitions definitions) {
    if (definitions.containsDefinition(PRINT)) {
        return;
    }
    /*-
		 * PRINT(x) == skip; 
		 * EXTERNAL_SUBSTITUTION_PRINT(T) == T; /* declare as external for any type T
		 */
    ASubstitutionDefinitionDefinition printDef = new ASubstitutionDefinitionDefinition();
    printDef.setName(new TDefLiteralSubstitution(PRINT));
    printDef.setParameters(createIdentifierList("value"));
    printDef.setRhs(new ASkipSubstitution());
    definitions.addDefinition(printDef, IDefinitions.Type.Substitution);
    AExpressionDefinitionDefinition forceDefType = new AExpressionDefinitionDefinition();
    forceDefType.setName(new TIdentifierLiteral("EXTERNAL_SUBSTITUTION_" + PRINT));
    forceDefType.setParameters(createIdentifierList("T"));
    forceDefType.setRhs(createIdentifier("T"));
    definitions.addDefinition(forceDefType, IDefinitions.Type.Expression);
}
Also used : TDefLiteralSubstitution(de.be4.classicalb.core.parser.node.TDefLiteralSubstitution) ASubstitutionDefinitionDefinition(de.be4.classicalb.core.parser.node.ASubstitutionDefinitionDefinition) ASkipSubstitution(de.be4.classicalb.core.parser.node.ASkipSubstitution) AExpressionDefinitionDefinition(de.be4.classicalb.core.parser.node.AExpressionDefinitionDefinition) TIdentifierLiteral(de.be4.classicalb.core.parser.node.TIdentifierLiteral)

Example 28 with Definitions

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

the class OpSubstitutions method caseAIdentifierExpression.

@Override
public void caseAIdentifierExpression(final AIdentifierExpression node) {
    final String identifierString = Utils.getTIdentifierListAsString(node.getIdentifier());
    final Integer number = scopedVariables.get(identifierString);
    final Type type = definitions.getType(identifierString);
    if (number == null && type != Type.NoDefinition) {
        if (type == Type.Expression || type == Type.ExprOrSubst) {
            /*
				 * getFirst() is enough cause definitions cannot have composed
				 * identifiers
				 */
            replaceWithDefExpression(node, node.getIdentifier().getFirst(), null);
            if (type == Type.ExprOrSubst) {
                // type is determined now => set to Expression
                definitions.setDefinitionType(identifierString, Type.Expression);
            }
        } else {
            // finding some other type here is an error!
            throw new VisitorException(new CheckException("Expecting expression here but found definition with type '" + type + "'", node));
        }
    }
}
Also used : Type(de.be4.classicalb.core.parser.IDefinitions.Type) CheckException(de.be4.classicalb.core.parser.exceptions.CheckException) VisitorException(de.be4.classicalb.core.parser.exceptions.VisitorException)

Example 29 with Definitions

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

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

Aggregations

Test (org.junit.Test)19 Ast2String (util.Ast2String)17 BCompoundException (de.be4.classicalb.core.parser.exceptions.BCompoundException)16 BParser (de.be4.classicalb.core.parser.BParser)6 Token (de.be4.classicalb.core.preparser.node.Token)6 PreParseException (de.be4.classicalb.core.parser.exceptions.PreParseException)5 AExpressionDefinitionDefinition (de.be4.classicalb.core.parser.node.AExpressionDefinitionDefinition)5 IDefinitions (de.be4.classicalb.core.parser.IDefinitions)4 BLexerException (de.be4.classicalb.core.parser.exceptions.BLexerException)4 HashSet (java.util.HashSet)4 Definitions (de.be4.classicalb.core.parser.Definitions)3 Start (de.be4.classicalb.core.parser.node.Start)3 TIdentifierLiteral (de.be4.classicalb.core.parser.node.TIdentifierLiteral)3 File (java.io.File)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 CheckException (de.be4.classicalb.core.parser.exceptions.CheckException)2 AIdentifierExpression (de.be4.classicalb.core.parser.node.AIdentifierExpression)2 APredicateDefinitionDefinition (de.be4.classicalb.core.parser.node.APredicateDefinitionDefinition)2 ASubstitutionDefinitionDefinition (de.be4.classicalb.core.parser.node.ASubstitutionDefinitionDefinition)2