Search in sources :

Example 41 with ProjectSourceParser

use of org.eclipse.titan.designer.parsers.ProjectSourceParser in project titan.EclipsePlug-ins by eclipse.

the class AddImport method run.

@Override
public void run(final IAction action) {
    TITANDebugConsole.println("Add import called: ");
    if (targetEditor == null || !(targetEditor instanceof TTCN3Editor)) {
        return;
    }
    targetEditor.getEditorSite().getActionBars().getStatusLineManager().setErrorMessage(null);
    IFile file = (IFile) targetEditor.getEditorInput().getAdapter(IFile.class);
    if (file == null) {
        targetEditor.getEditorSite().getActionBars().getStatusLineManager().setErrorMessage(FILENOTIDENTIFIABLE);
        return;
    }
    if (!TITANNature.hasTITANNature(file.getProject())) {
        targetEditor.getEditorSite().getActionBars().getStatusLineManager().setErrorMessage(TITANNature.NO_TITAN_FILE_NATURE_FOUND);
        return;
    }
    IPreferencesService prefs = Platform.getPreferencesService();
    boolean reportDebugInformation = prefs.getBoolean(ProductConstants.PRODUCT_ID_DESIGNER, PreferenceConstants.DISPLAYDEBUGINFORMATION, true, null);
    int offset;
    if (!selection.isEmpty() && selection instanceof TextSelection && !"".equals(((TextSelection) selection).getText())) {
        if (reportDebugInformation) {
            TITANDebugConsole.println("text selected: " + ((TextSelection) selection).getText());
        }
        TextSelection tSelection = (TextSelection) selection;
        offset = tSelection.getOffset() + tSelection.getLength();
    } else {
        offset = ((TTCN3Editor) targetEditor).getCarretOffset();
    }
    DeclarationCollector declarationCollector = OpenDeclarationHelper.findVisibleDeclarations(targetEditor, new TTCN3ReferenceParser(false), ((TTCN3Editor) targetEditor).getDocument(), offset, false);
    if (declarationCollector == null) {
        return;
    }
    List<DeclarationCollectionHelper> collected = declarationCollector.getCollected();
    if (collected.isEmpty()) {
        // FIXME add semantic check guard on project level.
        ProjectSourceParser projectSourceParser = GlobalParser.getProjectSourceParser(file.getProject());
        if (reportDebugInformation) {
            TITANDebugConsole.println("No visible elements found");
        }
        for (String moduleName2 : projectSourceParser.getKnownModuleNames()) {
            Module module2 = projectSourceParser.getModuleByName(moduleName2);
            if (module2 != null) {
                // Visit each file in the project one by
                // one instead of
                // "module2.getAssignments().addDeclaration(declarationCollector)".
                Assignments assignments = module2.getAssignments();
                for (int i = 0; i < assignments.getNofAssignments(); i++) {
                    assignments.getAssignmentByIndex(i).addDeclaration(declarationCollector, 0);
                }
            }
        }
        if (declarationCollector.getCollectionSize() == 0) {
            targetEditor.getEditorSite().getActionBars().getStatusLineManager().setErrorMessage(NOTTTCN3DECLARATION);
            return;
        }
        if (reportDebugInformation) {
            TITANDebugConsole.println("Elements were only found in not visible modules");
        }
        DeclarationCollectionHelper resultToInsert = null;
        if (collected.size() == 1) {
            resultToInsert = collected.get(0);
        } else {
            OpenDeclarationLabelProvider labelProvider = new OpenDeclarationLabelProvider();
            ElementListSelectionDialog dialog = new ElementListSelectionDialog(null, labelProvider);
            dialog.setTitle("Add Import");
            dialog.setMessage("Choose element to generate an import statement for.");
            dialog.setElements(collected.toArray());
            if (dialog.open() == Window.OK) {
                if (reportDebugInformation) {
                    TITANDebugConsole.getConsole().newMessageStream().println("Selected: " + dialog.getFirstResult());
                }
                resultToInsert = (DeclarationCollectionHelper) dialog.getFirstResult();
            }
        }
        if (resultToInsert == null) {
            return;
        }
        IFile newfile = (IFile) resultToInsert.location.getFile();
        Module newModule = projectSourceParser.containedModule(newfile);
        if (newModule == null) {
            targetEditor.getEditorSite().getActionBars().getStatusLineManager().setErrorMessage("Could not identify the module in file " + newfile.getName());
            return;
        }
        String ttcnName = newModule.getIdentifier().getTtcnName();
        TITANDebugConsole.println("the new module to insert: " + ttcnName);
        final IFile actualFile = (IFile) targetEditor.getEditorInput().getAdapter(IFile.class);
        Module actualModule = projectSourceParser.containedModule(actualFile);
        int insertionOffset = ((TTCN3Module) actualModule).getAssignmentsScope().getLocation().getOffset() + 1;
        MultiTextEdit multiEdit = new MultiTextEdit(insertionOffset, 0);
        RewriteSessionEditProcessor processor = new RewriteSessionEditProcessor(((TTCN3Editor) targetEditor).getDocument(), multiEdit, TextEdit.UPDATE_REGIONS | TextEdit.CREATE_UNDO);
        multiEdit.addChild(new InsertEdit(insertionOffset, "\nimport from " + ttcnName + " all;\n"));
        try {
            processor.performEdits();
        } catch (BadLocationException e) {
            ErrorReporter.logExceptionStackTrace(e);
        }
    } else {
        if (reportDebugInformation) {
            for (DeclarationCollectionHelper foundDeclaration : collected) {
                TITANDebugConsole.println("declaration:" + foundDeclaration.location.getFile() + ": " + foundDeclaration.location.getOffset() + " - " + foundDeclaration.location.getEndOffset() + " is available");
            }
        }
    }
    Display.getDefault().asyncExec(new Runnable() {

        @Override
        public void run() {
            MessageDialog.openWarning(null, "Study feature", "Adding a missing importation is still under study");
        }
    });
}
Also used : TTCN3Editor(org.eclipse.titan.designer.editors.ttcn3editor.TTCN3Editor) InsertEdit(org.eclipse.text.edits.InsertEdit) IFile(org.eclipse.core.resources.IFile) DeclarationCollector(org.eclipse.titan.designer.editors.actions.DeclarationCollector) TTCN3ReferenceParser(org.eclipse.titan.designer.editors.ttcn3editor.TTCN3ReferenceParser) TextSelection(org.eclipse.jface.text.TextSelection) Assignments(org.eclipse.titan.designer.AST.Assignments) OpenDeclarationLabelProvider(org.eclipse.titan.designer.editors.actions.OpenDeclarationLabelProvider) IPreferencesService(org.eclipse.core.runtime.preferences.IPreferencesService) ProjectSourceParser(org.eclipse.titan.designer.parsers.ProjectSourceParser) DeclarationCollectionHelper(org.eclipse.titan.designer.editors.actions.DeclarationCollectionHelper) RewriteSessionEditProcessor(org.eclipse.jface.text.RewriteSessionEditProcessor) ElementListSelectionDialog(org.eclipse.ui.dialogs.ElementListSelectionDialog) Module(org.eclipse.titan.designer.AST.Module) TTCN3Module(org.eclipse.titan.designer.AST.TTCN3.definitions.TTCN3Module) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 42 with ProjectSourceParser

use of org.eclipse.titan.designer.parsers.ProjectSourceParser in project titan.EclipsePlug-ins by eclipse.

the class ImportModule method checkImports.

@Override
public /**
 * {@inheritDoc}
 */
void checkImports(final CompilationTimeStamp timestamp, final ModuleImportationChain referenceChain, final List<Module> moduleStack) {
    if (null != lastImportCheckTimeStamp && !lastImportCheckTimeStamp.isLess(timestamp)) {
        return;
    }
    symbols.checkUniqueness(timestamp);
    final ProjectSourceParser parser = GlobalParser.getProjectSourceParser(project);
    if (null == parser || null == identifier) {
        lastImportCheckTimeStamp = timestamp;
        // FIXME: is it correct? lastImportCheckTimeStamp will be set in extreme case only - very early running
        referredModule = null;
        return;
    }
    final Module temp = referredModule;
    referredModule = parser.getModuleByName(identifier.getName());
    if (temp != referredModule) {
        setUnhandledChange(true);
    }
    if (referredModule == null) {
        identifier.getLocation().reportSemanticError(MessageFormat.format(MISSINGMODULE, identifier.getDisplayName()));
    } else {
        if (!(referredModule instanceof ASN1Module)) {
            identifier.getLocation().reportSemanticError(MessageFormat.format(NOTASN1MODULE, identifier.getDisplayName()));
            lastImportCheckTimeStamp = timestamp;
            referredModule = null;
            return;
        }
        moduleStack.add(referredModule);
        if (!referenceChain.add(this)) {
            moduleStack.remove(moduleStack.size() - 1);
            lastImportCheckTimeStamp = timestamp;
            return;
        }
        referredModule.checkImports(timestamp, referenceChain, moduleStack);
        for (int i = 0; i < symbols.size(); i++) {
            final Identifier id = symbols.getNthElement(i);
            final List<ISubReference> list = new ArrayList<ISubReference>();
            list.add(new FieldSubReference(id));
            final Defined_Reference reference = new Defined_Reference(null, list);
            reference.setLocation(identifier.getLocation());
            if (null != referredModule.getAssBySRef(timestamp, reference)) {
                if (!((ASN1Module) referredModule).exportsSymbol(timestamp, id)) {
                    identifier.getLocation().reportSemanticError(MessageFormat.format(SYMBOLNOTEXPORTED, id.getDisplayName(), referredModule.getIdentifier().getDisplayName()));
                }
            }
        }
        moduleStack.remove(moduleStack.size() - 1);
    }
    lastImportCheckTimeStamp = timestamp;
}
Also used : ISubReference(org.eclipse.titan.designer.AST.ISubReference) Identifier(org.eclipse.titan.designer.AST.Identifier) Defined_Reference(org.eclipse.titan.designer.AST.ASN1.Defined_Reference) FieldSubReference(org.eclipse.titan.designer.AST.FieldSubReference) ArrayList(java.util.ArrayList) Module(org.eclipse.titan.designer.AST.Module) ProjectSourceParser(org.eclipse.titan.designer.parsers.ProjectSourceParser)

Example 43 with ProjectSourceParser

use of org.eclipse.titan.designer.parsers.ProjectSourceParser in project titan.EclipsePlug-ins by eclipse.

the class ASN1Module method getAssBySRef.

@Override
public /**
 * {@inheritDoc}
 */
Assignment getAssBySRef(final CompilationTimeStamp timestamp, final Reference reference, final IReferenceChain refChain) {
    Identifier moduleId = reference.getModuleIdentifier();
    final Identifier id = reference.getId();
    if (null == id) {
        return null;
    }
    Module module = null;
    if (null == moduleId || moduleId.getName().equals(identifier.getName())) {
        if (assignments.hasLocalAssignmentWithID(timestamp, id)) {
            return assignments.getLocalAssignmentByID(timestamp, id);
        }
        if (null != moduleId) {
            id.getLocation().reportSemanticError(MessageFormat.format(NOASSIGNMENT, id.getDisplayName(), identifier.getDisplayName()));
            return null;
        }
        if (imports.singularImportedSymbols_map.containsKey(id.getName())) {
            module = imports.singularImportedSymbols_map.get(id.getName());
            moduleId = module.getIdentifier();
            imports.getImportedModuleById(moduleId).setUsedForImportation();
        } else if (imports.pluralImportedSymbols.contains(id.getName())) {
            id.getLocation().reportSemanticError(MessageFormat.format(MORESYMBOLS, id.getDisplayName(), identifier.getDisplayName()));
            return null;
        } else {
            id.getLocation().reportSemanticError(MessageFormat.format(NOASSIGNMENTORSYMBOL, id.getDisplayName(), identifier.getDisplayName()));
            return null;
        }
    }
    if (null == module) {
        if (!imports.hasImportedModuleWithId(moduleId)) {
            moduleId.getLocation().reportSemanticError(MessageFormat.format(NOIMPORTEDMODULE, moduleId.getDisplayName()));
            return null;
        }
        if (!imports.getImportedModuleById(moduleId).hasSymbol(id)) {
            id.getLocation().reportSemanticError(MessageFormat.format(NOSYMBOLSIMPORTED, id.getDisplayName(), moduleId.getDisplayName()));
            return null;
        }
        imports.getImportedModuleById(moduleId).setUsedForImportation();
        final ProjectSourceParser parser = GlobalParser.getProjectSourceParser(project);
        if (null == parser) {
            return null;
        }
        module = parser.getModuleByName(moduleId.getName());
    }
    if (this == module || null == module) {
        return null;
    }
    final List<ISubReference> newSubreferences = new ArrayList<ISubReference>();
    newSubreferences.add(new FieldSubReference(id));
    final Defined_Reference finalReference = new Defined_Reference(null, newSubreferences);
    // FIXME add semantic check guard on project level.
    return module.getAssBySRef(timestamp, finalReference);
}
Also used : ISubReference(org.eclipse.titan.designer.AST.ISubReference) Identifier(org.eclipse.titan.designer.AST.Identifier) Defined_Reference(org.eclipse.titan.designer.AST.ASN1.Defined_Reference) FieldSubReference(org.eclipse.titan.designer.AST.FieldSubReference) ArrayList(java.util.ArrayList) Module(org.eclipse.titan.designer.AST.Module) ProjectSourceParser(org.eclipse.titan.designer.parsers.ProjectSourceParser)

Example 44 with ProjectSourceParser

use of org.eclipse.titan.designer.parsers.ProjectSourceParser in project titan.EclipsePlug-ins by eclipse.

the class Export_Debug_AST method run.

@Override
public void run(IAction action) {
    if (targetEditor == null)
        return;
    IFile file = (IFile) targetEditor.getEditorInput().getAdapter(IFile.class);
    ProjectSourceParser parser = GlobalParser.getProjectSourceParser(file.getProject());
    Module module = parser.containedModule(file);
    if (module == null) {
        TITANDebugConsole.getConsole().newMessageStream().println("No module was found");
    }
    TITANDebugConsole.getConsole().newMessageStream().println("*************************");
    TITANDebugConsole.getConsole().newMessageStream().println("Printing DEBUG information for module `" + module.getName() + "':");
    TITANDebugConsole.getConsole().newMessageStream().println("*************************");
    module.accept(new ASTVisitor() {

        private int padding = 0;

        @Override
        public int visit(IVisitableNode node) {
            if (node instanceof Assignment) {
                Assignment assignment = (Assignment) node;
                printInfoln(padding, assignment.getAssignmentName(), assignment.getFullName(), assignment.getLastTimeChecked(), assignment.getLocation());
            } else if (node instanceof Identifier) {
                printInfoln(padding, "identifier", ((Identifier) node).getDisplayName(), null, ((Identifier) node).getLocation());
            } else if (node instanceof Statement) {
                Statement statement = (Statement) node;
                printInfoln(padding, "statement", statement.getFullName(), statement.getLastTimeChecked(), statement.getLocation());
            } else if (node instanceof Reference) {
                Reference ref = (Reference) node;
                printInfoln(padding, "reference", ref.getFullName(), ref.getLastTimeChecked(), ref.getLocation());
                Assignment old = ref.getAssOld();
                if (old != null) {
                    printInfoln(padding + 1, "This reference was last pointing to " + old.getFullName() + " analyzed at " + old.getLastTimeChecked());
                }
            } else if (node instanceof ComponentTypeBody) {
                ComponentTypeBody body = (ComponentTypeBody) node;
                Map<String, Definition> map = body.getDefinitionMap();
                printInfoln(padding + 1, " contains definitions:");
                for (Map.Entry<String, Definition> entry : map.entrySet()) {
                    printInfoln(padding + 2, entry.getKey() + " was last checked at " + entry.getValue().getLastTimeChecked());
                }
            }
            if (node instanceof StatementBlock || node instanceof Definition) {
                padding++;
            }
            return super.visit(node);
        }

        @Override
        public int leave(IVisitableNode node) {
            if (node instanceof StatementBlock || node instanceof Definition) {
                padding--;
            }
            return super.leave(node);
        }
    });
    TITANDebugConsole.getConsole().newMessageStream().println("*************************");
    TITANDebugConsole.getConsole().newMessageStream().println("Printing DEBUG information for module `" + module.getName() + "' finished");
    TITANDebugConsole.getConsole().newMessageStream().println("*************************");
}
Also used : ComponentTypeBody(org.eclipse.titan.designer.AST.TTCN3.types.ComponentTypeBody) IFile(org.eclipse.core.resources.IFile) Statement(org.eclipse.titan.designer.AST.TTCN3.statements.Statement) Reference(org.eclipse.titan.designer.AST.Reference) Definition(org.eclipse.titan.designer.AST.TTCN3.definitions.Definition) ProjectSourceParser(org.eclipse.titan.designer.parsers.ProjectSourceParser) ASTVisitor(org.eclipse.titan.designer.AST.ASTVisitor) IVisitableNode(org.eclipse.titan.designer.AST.IVisitableNode) Assignment(org.eclipse.titan.designer.AST.Assignment) Identifier(org.eclipse.titan.designer.AST.Identifier) Module(org.eclipse.titan.designer.AST.Module) Map(java.util.Map) StatementBlock(org.eclipse.titan.designer.AST.TTCN3.statements.StatementBlock)

Example 45 with ProjectSourceParser

use of org.eclipse.titan.designer.parsers.ProjectSourceParser in project titan.EclipsePlug-ins by eclipse.

the class TITANJavaBuilder method build.

@Override
protected IProject[] build(final int kind, final Map<String, String> args, final IProgressMonitor monitor) throws CoreException {
    IProject project = getProject();
    if (!TITANInstallationValidator.check(true)) {
        return project.getReferencedProjects();
    }
    if (!LicenseValidator.check()) {
        return project.getReferencedProjects();
    }
    IPreferenceStore store = Activator.getDefault().getPreferenceStore();
    final boolean reportDebugInformation = store.getBoolean(PreferenceConstants.DISPLAYDEBUGINFORMATION);
    final SubMonitor progress = SubMonitor.convert(monitor);
    progress.beginTask("Build", 2);
    ProjectSourceParser sourceParser = GlobalParser.getProjectSourceParser(project);
    // TODO: this is temporary code!
    sourceParser.makefileCreatingAnalyzeAll();
    progress.worked(1);
    BuildTimestamp timestamp = BuildTimestamp.getNewBuildCounter();
    IProgressMonitor codeGeneratorMonitor = progress.newChild(1);
    codeGeneratorMonitor.beginTask("Checking prerequisites", sourceParser.getModules().size() + 1);
    for (Module module : sourceParser.getModules()) {
        TITANDebugConsole.println("Generating code for module `" + module.getIdentifier().getDisplayName() + "'");
        try {
            ProjectSourceCompiler.compile(timestamp, module, reportDebugInformation);
        } catch (Exception e) {
            ErrorReporter.logExceptionStackTrace("While generating Java code for module " + module.getIdentifier().getDisplayName(), e);
        }
        codeGeneratorMonitor.worked(1);
    }
    TITANDebugConsole.println("Generating code for single main");
    try {
        ProjectSourceCompiler.generateSingleMain(project, sourceParser.getModules(), reportDebugInformation);
        ProjectSourceCompiler.generateParallelMain(project, sourceParser.getModules(), reportDebugInformation);
    } catch (CoreException e) {
        ErrorReporter.logExceptionStackTrace("While generating Java code for main module ", e);
    }
    codeGeneratorMonitor.worked(1);
    codeGeneratorMonitor.done();
    return new IProject[0];
}
Also used : IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) CoreException(org.eclipse.core.runtime.CoreException) SubMonitor(org.eclipse.core.runtime.SubMonitor) BuildTimestamp(org.eclipse.titan.designer.compiler.BuildTimestamp) IPreferenceStore(org.eclipse.jface.preference.IPreferenceStore) Module(org.eclipse.titan.designer.AST.Module) IProject(org.eclipse.core.resources.IProject) ProjectSourceParser(org.eclipse.titan.designer.parsers.ProjectSourceParser) CoreException(org.eclipse.core.runtime.CoreException)

Aggregations

ProjectSourceParser (org.eclipse.titan.designer.parsers.ProjectSourceParser)72 Module (org.eclipse.titan.designer.AST.Module)51 IFile (org.eclipse.core.resources.IFile)34 ArrayList (java.util.ArrayList)23 WorkspaceJob (org.eclipse.core.resources.WorkspaceJob)23 IProject (org.eclipse.core.resources.IProject)19 TTCN3Module (org.eclipse.titan.designer.AST.TTCN3.definitions.TTCN3Module)14 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)12 IPreferencesService (org.eclipse.core.runtime.preferences.IPreferencesService)11 Identifier (org.eclipse.titan.designer.AST.Identifier)11 MultiTextEdit (org.eclipse.text.edits.MultiTextEdit)10 Location (org.eclipse.titan.designer.AST.Location)10 List (java.util.List)9 Reference (org.eclipse.titan.designer.AST.Reference)9 ImportModule (org.eclipse.titan.designer.AST.TTCN3.definitions.ImportModule)9 CoreException (org.eclipse.core.runtime.CoreException)8 TextSelection (org.eclipse.jface.text.TextSelection)8 TextFileChange (org.eclipse.ltk.core.refactoring.TextFileChange)8 HashMap (java.util.HashMap)7 HashSet (java.util.HashSet)7