Search in sources :

Example 1 with UtilityFunctions.getAbsNature

use of org.absmodels.abs.plugin.util.UtilityFunctions.getAbsNature in project abstools by abstools.

the class JavaJob method generateJavaCode.

/**
 * Generates .java files (no .class files).
 * If 'product' is set, will flatten accordingly.
 * @param monitor - must not be null
 * @param path - where to add the modules / java-files
 * @param project - the ABS project
 * @throws IOException, if unable to create java files
 * @throws AbsJobException, if unable to generate java files
 * @throws JavaCodeGenerationException, if unable to generate java files
 * @throws CoreException
 * @throws NoModelException
 */
private void generateJavaCode(IProgressMonitor monitor, Path path, IProject project) throws AbsJobException, IOException, JavaCodeGenerationException, CoreException, NoModelException {
    assert monitor != null;
    monitor.subTask("Creating java source files");
    AbsNature nat = UtilityFunctions.getAbsNature(project);
    synchronized (nat.modelLock) {
        Model model = nat.getCompleteModel();
        if (model == null)
            throw new NoModelException();
        JavaCode javaCode = new JavaCode(path.toFile());
        if (product != null) {
            /* [stolz] Flattening for a product will mangle the model according to [ramus]...
                 */
            // work on a copy:
            model = model.treeCopyNoTransform();
            model.flushTreeCache();
            String productN = product.getName();
            try {
                model.flattenForProduct(productN);
                model.flushCache();
                if (model.hasErrors() || model.hasTypeErrors()) {
                    nat.createMarkers(model);
                    throw new AbsJobException("An ABS file in the project has type errors after applying deltas");
                }
            } catch (WrongProgramArgumentException e) {
                throw new AbsJobException(e);
            } catch (DeltaModellingException e) {
                throw new AbsJobException(e);
            }
        }
        // TODO: the second parameter toggles listener code creation (3
        // Java method calls per ABS statement); make this configurable
        model.generateJavaCode(javaCode, true);
        int countUnits = model.getNumCompilationUnit();
        if (countUnits == 0)
            throw new AbsJobException("No compilation unit found");
    }
}
Also used : NoModelException(org.absmodels.abs.plugin.internal.NoModelException) Model(abs.frontend.ast.Model) WrongProgramArgumentException(abs.common.WrongProgramArgumentException) JavaCode(abs.backend.java.codegeneration.JavaCode) AbsNature(org.absmodels.abs.plugin.builder.AbsNature) UtilityFunctions.getAbsNature(org.absmodels.abs.plugin.util.UtilityFunctions.getAbsNature) DeltaModellingException(abs.frontend.delta.DeltaModellingException) AbsJobException(org.absmodels.abs.plugin.exceptions.AbsJobException)

Example 2 with UtilityFunctions.getAbsNature

use of org.absmodels.abs.plugin.util.UtilityFunctions.getAbsNature in project abstools by abstools.

the class JavaJob method getModelFromProject.

/**
 * @Deprecated According to Yannick, you need to hold {@link AbsNature#modelLock} to safely do anything with the model,
 * which means you need the nature first, and then this helper is just a fancy wrapper around {@link AbsNature#getCompleteModel()}...
 * @see AbsNature#getCompleteModel()
 */
public static Model getModelFromProject(IProject project) throws AbsJobException {
    AbsNature nature = UtilityFunctions.getAbsNature(project);
    if (nature == null) {
        throw new AbsJobException("The file is not in an ABS project!");
    }
    Model model = nature.getCompleteModel();
    // if (model==null && curFile!=null ) model = abs.frontend.parser.Main.parse(new File(curFile.getLocation().toString()), withStdLib);
    if (model == null) {
        throw new AbsJobException("No ABS model found");
    }
    if (model.hasParserErrors() || model.hasErrors() || model.hasTypeErrors()) {
        // just to be sure
        throw new AbsJobException("An ABS file in the project has type or parser errors");
    }
    return model;
}
Also used : Model(abs.frontend.ast.Model) AbsNature(org.absmodels.abs.plugin.builder.AbsNature) UtilityFunctions.getAbsNature(org.absmodels.abs.plugin.util.UtilityFunctions.getAbsNature) AbsJobException(org.absmodels.abs.plugin.exceptions.AbsJobException)

Example 3 with UtilityFunctions.getAbsNature

use of org.absmodels.abs.plugin.util.UtilityFunctions.getAbsNature in project abstools by abstools.

the class NavigatorContentProvider method getChildren.

@Override
public Object[] getChildren(Object parentElement) {
    if (parentElement instanceof InternalASTNode) {
        return outlineProvider.getChildren(parentElement);
    } else if (parentElement instanceof IProject) {
        if (((IProject) parentElement).isOpen()) {
            AbsNature nature = UtilityFunctions.getAbsNature((IProject) parentElement);
            assert nature != null;
            return ModulePath.getRootHierarchy(nature).toArray();
        }
    } else if (parentElement instanceof ModulePath) {
        ModulePath mPath = (ModulePath) parentElement;
        ArrayList<Object> children = new ArrayList<Object>();
        children.addAll(mPath.getChildModulePathsAndModuleDecls());
        InternalASTNode<ModuleDecl> intNode = mPath.getModuleDecl();
        // if the module path has a matching module declaration unfold its content..
        if (intNode != null) {
            ModuleDecl md = intNode.getASTNode();
            ArrayList<ASTNode<?>> chld = getChildrenOf(md);
            ASTNode<?>[] chldArr = chld.toArray(new ASTNode<?>[chld.size()]);
            List<InternalASTNode<ASTNode<?>>> wrapASTNodes = InternalASTNode.wrapASTNodes(chldArr, mPath.getNature());
            children.addAll(wrapASTNodes);
            return (children.toArray());
        }
        return children.toArray();
    }
    return super.getChildren(parentElement);
}
Also used : InternalASTNode(org.absmodels.abs.plugin.util.InternalASTNode) ArrayList(java.util.ArrayList) IProject(org.eclipse.core.resources.IProject) InternalASTNode(org.absmodels.abs.plugin.util.InternalASTNode) ASTNode(abs.frontend.ast.ASTNode) ModuleDecl(abs.frontend.ast.ModuleDecl) UtilityFunctions.getAbsNature(org.absmodels.abs.plugin.util.UtilityFunctions.getAbsNature) AbsNature(org.absmodels.abs.plugin.builder.AbsNature)

Example 4 with UtilityFunctions.getAbsNature

use of org.absmodels.abs.plugin.util.UtilityFunctions.getAbsNature in project abstools by abstools.

the class JavaJob method searchForMainBlockInCurrentFile.

/**
 * @return a module with a main block from the current file or null if no such module was found
 * @throws AbsJobException
 */
private ModuleDecl searchForMainBlockInCurrentFile() throws AbsJobException {
    if (currentFile == null) {
        return null;
    }
    AbsNature nature = UtilityFunctions.getAbsNature(project);
    if (nature == null) {
        throw new AbsJobException("Could not start the debugger, because selected file (" + currentFile.getName() + ") is not in an ABS project!");
    }
    synchronized (nature.modelLock) {
        CompilationUnit unit = nature.getCompilationUnit(currentFile);
        List<ModuleDecl> modules = findModulesWithMain(unit);
        // TODO: is the module still valid once you have returned the lock?
        return modules.size() == 0 ? null : modules.get(0);
    }
}
Also used : CompilationUnit(abs.frontend.ast.CompilationUnit) ModuleDecl(abs.frontend.ast.ModuleDecl) AbsNature(org.absmodels.abs.plugin.builder.AbsNature) UtilityFunctions.getAbsNature(org.absmodels.abs.plugin.util.UtilityFunctions.getAbsNature) AbsJobException(org.absmodels.abs.plugin.exceptions.AbsJobException)

Example 5 with UtilityFunctions.getAbsNature

use of org.absmodels.abs.plugin.util.UtilityFunctions.getAbsNature in project abstools by abstools.

the class JavaJob method getModuleByName.

/**
 * finds a module with a given name in the current
 * @param moduleName
 * @return
 * @throws AbsJobException
 */
private ModuleDecl getModuleByName(String moduleName) throws AbsJobException {
    AbsNature nature = UtilityFunctions.getAbsNature(project);
    if (nature == null) {
        throw new AbsJobException("Could not start the debugger, because selected file (" + currentFile.getName() + ") is not in an ABS project!");
    }
    synchronized (nature.modelLock) {
        Model model = nature.getCompleteModel();
        ModuleDecl result = model.lookupModule(moduleName);
        if (result == null) {
            throw new AbsJobException("Could not find a module with name " + moduleName + ".");
        }
        return result;
    }
}
Also used : Model(abs.frontend.ast.Model) ModuleDecl(abs.frontend.ast.ModuleDecl) AbsNature(org.absmodels.abs.plugin.builder.AbsNature) UtilityFunctions.getAbsNature(org.absmodels.abs.plugin.util.UtilityFunctions.getAbsNature) AbsJobException(org.absmodels.abs.plugin.exceptions.AbsJobException)

Aggregations

AbsNature (org.absmodels.abs.plugin.builder.AbsNature)5 UtilityFunctions.getAbsNature (org.absmodels.abs.plugin.util.UtilityFunctions.getAbsNature)5 AbsJobException (org.absmodels.abs.plugin.exceptions.AbsJobException)4 Model (abs.frontend.ast.Model)3 ModuleDecl (abs.frontend.ast.ModuleDecl)3 JavaCode (abs.backend.java.codegeneration.JavaCode)1 WrongProgramArgumentException (abs.common.WrongProgramArgumentException)1 ASTNode (abs.frontend.ast.ASTNode)1 CompilationUnit (abs.frontend.ast.CompilationUnit)1 DeltaModellingException (abs.frontend.delta.DeltaModellingException)1 ArrayList (java.util.ArrayList)1 NoModelException (org.absmodels.abs.plugin.internal.NoModelException)1 InternalASTNode (org.absmodels.abs.plugin.util.InternalASTNode)1 IProject (org.eclipse.core.resources.IProject)1