Search in sources :

Example 1 with IDefinitions

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

the class ASTBuilder method addFormatToStringDefinition.

public static void addFormatToStringDefinition(IDefinitions iDefinitions) {
    if (iDefinitions.containsDefinition(FORMAT_TO_STRING)) {
        return;
    }
    /*-
		 * FORMAT_TO_STRING(MyFormatString,ListOfValues) == "0";
		 * EXTERNAL_FUNCTION_FORMAT_TO_STRING(TO_STRING_TYPE) == STRING*seq(TO_STRING_TYPE) --> STRING;
		 */
    AExpressionDefinitionDefinition formatDef = new AExpressionDefinitionDefinition();
    formatDef.setName(new TIdentifierLiteral(FORMAT_TO_STRING));
    formatDef.setParameters(createExpressionList("S", "T"));
    formatDef.setRhs(new AStringExpression(new TStringLiteral("abc")));
    iDefinitions.addDefinition(formatDef, IDefinitions.Type.Expression);
    AExpressionDefinitionDefinition formatType = new AExpressionDefinitionDefinition();
    formatType.setName(new TIdentifierLiteral("EXTERNAL_FUNCTION_FORMAT_TO_STRING"));
    formatType.setParameters(createExpressionList("T"));
    formatType.setRhs(new ATotalFunctionExpression(new AMultOrCartExpression(new AStringSetExpression(), new ASeqExpression(createIdentifier("T"))), new AStringSetExpression()));
    iDefinitions.addDefinition(formatType, IDefinitions.Type.Expression);
}
Also used : AStringSetExpression(de.be4.classicalb.core.parser.node.AStringSetExpression) AMultOrCartExpression(de.be4.classicalb.core.parser.node.AMultOrCartExpression) ASeqExpression(de.be4.classicalb.core.parser.node.ASeqExpression) AExpressionDefinitionDefinition(de.be4.classicalb.core.parser.node.AExpressionDefinitionDefinition) AStringExpression(de.be4.classicalb.core.parser.node.AStringExpression) TStringLiteral(de.be4.classicalb.core.parser.node.TStringLiteral) TIdentifierLiteral(de.be4.classicalb.core.parser.node.TIdentifierLiteral) ATotalFunctionExpression(de.be4.classicalb.core.parser.node.ATotalFunctionExpression)

Example 2 with IDefinitions

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

the class ASTBuilder method addToStringDefinition.

public static void addToStringDefinition(IDefinitions definitions) {
    if (definitions.containsDefinition(TO_STRING)) {
        return;
    }
    /*-
		 * TO_STRING(S) == "0"; 
		 * EXTERNAL_FUNCTION_TO_STRING(X) == X -->STRING;
		 */
    AExpressionDefinitionDefinition toStringDef = new AExpressionDefinitionDefinition();
    toStringDef.setName(new TIdentifierLiteral(TO_STRING));
    toStringDef.setParameters(createIdentifierList("S"));
    toStringDef.setRhs(new AStringExpression(new TStringLiteral("0")));
    definitions.addDefinition(toStringDef, IDefinitions.Type.Expression);
    AExpressionDefinitionDefinition toStringTypeDef = new AExpressionDefinitionDefinition();
    toStringTypeDef.setName(new TIdentifierLiteral("EXTERNAL_FUNCTION_TO_STRING"));
    toStringTypeDef.setParameters(createIdentifierList("X"));
    toStringTypeDef.setRhs(new ATotalFunctionExpression(createIdentifier("X"), new AStringSetExpression()));
    definitions.addDefinition(toStringTypeDef, IDefinitions.Type.Expression);
}
Also used : AStringSetExpression(de.be4.classicalb.core.parser.node.AStringSetExpression) AExpressionDefinitionDefinition(de.be4.classicalb.core.parser.node.AExpressionDefinitionDefinition) AStringExpression(de.be4.classicalb.core.parser.node.AStringExpression) TStringLiteral(de.be4.classicalb.core.parser.node.TStringLiteral) TIdentifierLiteral(de.be4.classicalb.core.parser.node.TIdentifierLiteral) ATotalFunctionExpression(de.be4.classicalb.core.parser.node.ATotalFunctionExpression)

Example 3 with IDefinitions

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

the class ASTBuilder method addSortDefinition.

public static void addSortDefinition(IDefinitions iDefinitions) {
    if (iDefinitions.containsDefinition(SORT)) {
        return;
    }
    /*- SORT
		 *  SORT(X) == [];
		 *  EXTERNAL_FUNCTION_SORT(T) == POW(T)-->seq(T);
		 */
    AExpressionDefinitionDefinition sortDef = new AExpressionDefinitionDefinition();
    sortDef.setName(new TIdentifierLiteral(SORT));
    sortDef.setParameters(createExpressionList("X"));
    sortDef.setRhs(new AEmptySequenceExpression());
    iDefinitions.addDefinition(sortDef, IDefinitions.Type.Expression);
    AExpressionDefinitionDefinition sortType = new AExpressionDefinitionDefinition();
    sortType.setName(new TIdentifierLiteral("EXTERNAL_FUNCTION_SORT"));
    sortType.setParameters(createExpressionList("T"));
    sortType.setRhs(new ATotalFunctionExpression(new APowSubsetExpression(createIdentifier("T")), new ASeqExpression(createIdentifier("T"))));
    iDefinitions.addDefinition(sortType, IDefinitions.Type.Expression);
}
Also used : AEmptySequenceExpression(de.be4.classicalb.core.parser.node.AEmptySequenceExpression) APowSubsetExpression(de.be4.classicalb.core.parser.node.APowSubsetExpression) ASeqExpression(de.be4.classicalb.core.parser.node.ASeqExpression) AExpressionDefinitionDefinition(de.be4.classicalb.core.parser.node.AExpressionDefinitionDefinition) TIdentifierLiteral(de.be4.classicalb.core.parser.node.TIdentifierLiteral) ATotalFunctionExpression(de.be4.classicalb.core.parser.node.ATotalFunctionExpression)

Example 4 with IDefinitions

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

the class RulesTransformation method runTransformation.

public void runTransformation() throws BCompoundException {
    start.apply(this);
    DefinitionInjector.injectDefinitions(start, iDefinitions);
    MissingPositionsAdder.injectPositions(start);
    if (!this.errorList.isEmpty()) {
        List<BException> list = new ArrayList<>();
        for (CheckException checkException : this.errorList) {
            list.add(new BException(this.rulesMachineChecker.getFileName(), checkException));
        }
        throw new BCompoundException(list);
    }
}
Also used : CheckException(de.be4.classicalb.core.parser.exceptions.CheckException) ArrayList(java.util.ArrayList) BException(de.be4.classicalb.core.parser.exceptions.BException) BCompoundException(de.be4.classicalb.core.parser.exceptions.BCompoundException)

Example 5 with IDefinitions

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

the class RecursiveMachineLoader method recursivlyLoadMachine.

private void recursivlyLoadMachine(final File machineFile, final Start currentAst, final List<String> ancestors, final boolean isMain, File directory, final IDefinitions definitions) throws BCompoundException {
    // make a copy of the referencing machines
    List<String> newAncestors = new ArrayList<>(ancestors);
    ReferencedMachines refMachines = new ReferencedMachines(machineFile, currentAst, parsingBehaviour.isMachineNameMustMatchFileName());
    try {
        refMachines.findReferencedMachines();
    } catch (BException e) {
        throw new BCompoundException(e);
    }
    final String name = refMachines.getName();
    if (name == null) {
        /*
			 * the parsed file is a definition file, hence the name of the
			 * machine is null
			 */
        throw new BCompoundException(new BException(machineFile.getName(), "Expecting a B machine but was a definition file in file: '" + machineFile.getName() + "\'", null));
    }
    machineFilesLoaded.add(machineFile);
    final int fileNumber = machineFilesLoaded.indexOf(machineFile) + 1;
    getNodeIdMapping().assignIdentifiers(fileNumber, currentAst);
    definitions.assignIdsToNodes(getNodeIdMapping(), machineFilesLoaded);
    injectDefinitions(currentAst, definitions);
    getParsedMachines().put(name, currentAst);
    parsedFiles.put(name, machineFile);
    if (name != null) {
        newAncestors.add(name);
    }
    if (isMain) {
        main = name;
    }
    final Set<String> referencesSet = refMachines.getSetOfReferencedMachines();
    try {
        checkForCycles(newAncestors, referencesSet);
    } catch (BException e) {
        throw new BCompoundException(e);
    }
    final List<MachineReference> references = refMachines.getReferences();
    for (final MachineReference refMachine : references) {
        try {
            final String filePragma = refMachine.getPath();
            File file = null;
            if (filePragma == null) {
                file = lookupFile(directory, refMachine, newAncestors, refMachines.getPathList());
            } else {
                File p = new File(filePragma);
                if (p.isAbsolute()) {
                    file = p;
                } else {
                    file = new File(directory, filePragma);
                }
            }
            if (file.exists() && parsedFiles.containsKey(refMachine.getName()) && !parsedFiles.get(refMachine.getName()).getCanonicalPath().equals(file.getCanonicalPath())) {
                final String message = "Two files with the same name are referenced:\n" + parsedFiles.get(refMachine.getName()).getCanonicalPath() + "\n" + file.getCanonicalPath();
                throw new BException(machineFile.getCanonicalPath(), new CheckException(message, refMachine.getNode()));
            }
            if (!getParsedMachines().containsKey(refMachine.getName())) {
                try {
                    loadMachine(newAncestors, file);
                } catch (IOException e) {
                    throw new BException(machineFile.getCanonicalPath(), new CheckException(e.getMessage(), refMachine.getNode(), e));
                }
            }
        } catch (final BException e) {
            // we do not longer wrap a B Exception in a B Exception
            throw new BCompoundException(e);
        } catch (final IOException e) {
            throw new BCompoundException(new BException(machineFile.getAbsolutePath(), e));
        } catch (final CheckException e) {
            throw new BCompoundException(new BException(machineFile.getAbsolutePath(), e));
        }
    }
}
Also used : CheckException(de.be4.classicalb.core.parser.exceptions.CheckException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) BException(de.be4.classicalb.core.parser.exceptions.BException) File(java.io.File) BCompoundException(de.be4.classicalb.core.parser.exceptions.BCompoundException)

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