Search in sources :

Example 16 with TTCN3Module

use of org.eclipse.titan.designer.AST.TTCN3.definitions.TTCN3Module in project titan.EclipsePlug-ins by eclipse.

the class ImportSelectionDialog method organizeImportsEdit.

/**
 * Organize the imports according to the global preferences. If set,
 * <ul>
 * <li>Add imports necessary for missing references,</li>
 * <li>Remove unused imports,</li>
 * <li>Sort the import statements.</li>
 * </ul>
 * <p>
 * These changes are not applied in the function, just collected in a
 * <link>MultiTextEdit</link>, which is then returned.
 * </p>
 * TODO: notice and handle ambiguous references
 *
 * @param module
 *            The module which import statements are to organize.
 * @param document
 *            The document that contains the module.
 *
 * @return The edit, which contains the proper changes.
 */
private static MultiTextEdit organizeImportsEdit(final TTCN3Module module, final IDocument document) throws BadLocationException {
    final IProject prj = module.getProject();
    final String doc = document.get();
    final MultiTextEdit insertEdit = new MultiTextEdit();
    final MultiTextEdit removeEdit = new MultiTextEdit();
    final List<ImportText> newImports = new ArrayList<ImportText>();
    final List<ImportText> importsKept = new ArrayList<ImportText>();
    boolean needSorting = false;
    if (addImports) {
        // register the new needed imports
        final Set<String> importNamesAdded = new HashSet<String>();
        for (final Reference ref : module.getMissingReferences()) {
            final Location missLoc = findReferenceInProject(ref, prj);
            if (missLoc != null) {
                final IFile file = (IFile) missLoc.getFile();
                final ProjectSourceParser parser = GlobalParser.getProjectSourceParser(file.getProject());
                final Module addMod = parser.containedModule(file);
                final String importName = addMod.getIdentifier().getTtcnName();
                if (!importNamesAdded.contains(importName)) {
                    final StringBuilder impText = new StringBuilder("import from ").append(importName).append(" all;");
                    // if (importChangeMethod.equals(OrganizeImportPreferencePage.COMMENT_THEM)) {
                    impText.append(" // Added automatically to resolve ").append(ref.getDisplayName());
                    // }
                    newImports.add(new ImportText(importName, impText.toString() + NEWLINE));
                    importNamesAdded.add(importName);
                    if (reportDebug) {
                        final StringBuilder sb = new StringBuilder("For ").append(ref.getDisplayName()).append(": ");
                        sb.append(impText.toString());
                        TITANDebugConsole.println(sb.toString());
                    }
                }
            }
        }
        if (sortImports && !newImports.isEmpty()) {
            needSorting = true;
        }
    }
    if (!needSorting && sortImports) {
        // are the imports already sorted ?
        final List<ImportModule> oldImports = module.getImports();
        for (int size = oldImports.size(), i = 0; i < size - 1 && !needSorting; i++) {
            if (oldImports.get(i).getName().compareTo(oldImports.get(i + 1).getName()) > 0) {
                needSorting = true;
            }
            if (oldImports.get(i).getLocation().getOffset() > oldImports.get(i + 1).getLocation().getOffset()) {
                needSorting = true;
            }
        }
        if (!needSorting && oldImports.size() > 1) {
            // are the import strictly before the definitions ?
            final int lastImportOffset = oldImports.get(oldImports.size() - 1).getLocation().getOffset();
            final Definitions defs = module.getAssignmentsScope();
            if (defs.getNofAssignments() > 0 && !oldImports.isEmpty()) {
                for (int i = 0, size = defs.getNofAssignments(); i < size; ++i) {
                    final int temp = defs.getAssignmentByIndex(i).getLocation().getOffset();
                    if (temp < lastImportOffset) {
                        needSorting = true;
                    }
                }
            }
        }
    }
    if (needSorting || removeImports) {
        // remove the imports not needed, or every if sorting is required
        for (final ImportModule m : module.getImports()) {
            final Location delImp = m.getLocation();
            final IRegion startLineRegion = document.getLineInformationOfOffset(delImp.getOffset());
            final IRegion endLineRegion = document.getLineInformationOfOffset(delImp.getEndOffset());
            final String delimeter = document.getLineDelimiter(document.getLineOfOffset(delImp.getEndOffset()));
            final int delLength = delimeter == null ? 0 : delimeter.length();
            if (needSorting || (removeImports && !m.getUsedForImportation())) {
                if (reportDebug) {
                    final MessageConsoleStream stream = TITANDebugConsole.getConsole().newMessageStream();
                    TITANDebugConsole.println("Removing " + "'" + doc.substring(startLineRegion.getOffset(), endLineRegion.getOffset() + endLineRegion.getLength() + delLength) + "'", stream);
                    TITANDebugConsole.println("From " + startLineRegion.getOffset() + " till " + ((endLineRegion.getOffset() - startLineRegion.getOffset()) + endLineRegion.getLength() + delLength), stream);
                }
                /*if (importChangeMethod.equals(OrganizeImportPreferencePage.COMMENT_THEM)) {
						removeEdit.addChild(new InsertEdit(m.getLocation().getOffset(), "/*"));
						// hack to handle the semicolon
						removeEdit.addChild(new InsertEdit(m.getLocation().getEndOffset() + 1, "")); 
					} else {*/
                removeEdit.addChild(new DeleteEdit(startLineRegion.getOffset(), (endLineRegion.getOffset() - startLineRegion.getOffset()) + endLineRegion.getLength() + delLength));
            // }
            }
            if (needSorting && (!removeImports || m.getUsedForImportation())) {
                importsKept.add(new ImportText(m.getName(), doc.substring(startLineRegion.getOffset(), endLineRegion.getOffset() + endLineRegion.getLength() + delLength)));
            }
        }
    }
    if (!newImports.isEmpty() || (sortImports && needSorting)) {
        // always insert at the beginning of the file
        final int line = document.getLineOfOffset(module.getAssignmentsScope().getLocation().getOffset());
        final IRegion lineRegion = document.getLineInformation(line);
        final String delimeter = document.getLineDelimiter(line);
        final int delimeterLength = delimeter == null ? 0 : delimeter.length();
        final int startPos = lineRegion.getOffset() + lineRegion.getLength() + delimeterLength;
        if (sortImports) {
            if (needSorting || !newImports.isEmpty()) {
                final List<ImportText> results = new ArrayList<ImportText>();
                results.addAll(importsKept);
                results.addAll(newImports);
                Collections.sort(results);
                for (final ImportText i : results) {
                    insertEdit.addChild(new InsertEdit(startPos, i.getText()));
                }
            }
        } else {
            Collections.sort(newImports);
            for (final ImportText i : newImports) {
                insertEdit.addChild(new InsertEdit(startPos, i.getText()));
            }
        }
    }
    final MultiTextEdit resultEdit = new MultiTextEdit();
    if (insertEdit.hasChildren()) {
        resultEdit.addChild(insertEdit);
    }
    if (removeEdit.hasChildren()) {
        resultEdit.addChild(removeEdit);
    }
    return resultEdit;
}
Also used : InsertEdit(org.eclipse.text.edits.InsertEdit) IFile(org.eclipse.core.resources.IFile) Reference(org.eclipse.titan.designer.AST.Reference) Definitions(org.eclipse.titan.designer.AST.TTCN3.definitions.Definitions) ArrayList(java.util.ArrayList) MessageConsoleStream(org.eclipse.ui.console.MessageConsoleStream) DeleteEdit(org.eclipse.text.edits.DeleteEdit) IProject(org.eclipse.core.resources.IProject) ProjectSourceParser(org.eclipse.titan.designer.parsers.ProjectSourceParser) ImportModule(org.eclipse.titan.designer.AST.TTCN3.definitions.ImportModule) IRegion(org.eclipse.jface.text.IRegion) Module(org.eclipse.titan.designer.AST.Module) ImportModule(org.eclipse.titan.designer.AST.TTCN3.definitions.ImportModule) TTCN3Module(org.eclipse.titan.designer.AST.TTCN3.definitions.TTCN3Module) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) HashSet(java.util.HashSet) Location(org.eclipse.titan.designer.AST.Location)

Example 17 with TTCN3Module

use of org.eclipse.titan.designer.AST.TTCN3.definitions.TTCN3Module in project titan.EclipsePlug-ins by eclipse.

the class ImportSelectionDialog method createFileChange.

private Change createFileChange(final IFile toVisit) {
    if (toVisit == null) {
        return null;
    }
    final String designerId = ProductConstants.PRODUCT_ID_DESIGNER;
    final String displayDebugInfo = org.eclipse.titan.designer.preferences.PreferenceConstants.DISPLAYDEBUGINFORMATION;
    reportDebug = Platform.getPreferencesService().getBoolean(designerId, displayDebugInfo, false, null);
    final ProjectSourceParser sourceParser = GlobalParser.getProjectSourceParser(toVisit.getProject());
    final Module module = sourceParser.containedModule(toVisit);
    if (module == null || !(module instanceof TTCN3Module)) {
        return null;
    }
    final TextFileChange tfc = new TextFileChange(toVisit.getName(), toVisit);
    IDocument doc;
    final TTCN3Module tModule = (TTCN3Module) module;
    try {
        doc = tfc.getCurrentDocument(null);
        final MultiTextEdit resultEdit = organizeImportsEdit(tModule, doc);
        if (!resultEdit.hasChildren()) {
            return null;
        }
        tfc.setEdit(resultEdit);
    } catch (BadLocationException e) {
        ErrorReporter.logExceptionStackTrace("Error while organizing imports", e);
    } catch (CoreException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    return tfc;
}
Also used : TTCN3Module(org.eclipse.titan.designer.AST.TTCN3.definitions.TTCN3Module) CoreException(org.eclipse.core.runtime.CoreException) Module(org.eclipse.titan.designer.AST.Module) ImportModule(org.eclipse.titan.designer.AST.TTCN3.definitions.ImportModule) TTCN3Module(org.eclipse.titan.designer.AST.TTCN3.definitions.TTCN3Module) TextFileChange(org.eclipse.ltk.core.refactoring.TextFileChange) ProjectSourceParser(org.eclipse.titan.designer.parsers.ProjectSourceParser) IDocument(org.eclipse.jface.text.IDocument) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 18 with TTCN3Module

use of org.eclipse.titan.designer.AST.TTCN3.definitions.TTCN3Module in project titan.EclipsePlug-ins by eclipse.

the class Definitions method updateSyntax.

/**
 * Handles the incremental parsing of this list of definitions.
 *
 * @param reparser
 *                the parser doing the incremental parsing.
 * @param importedModules
 *                the list of module importations found in the same
 *                module.
 * @param friendModules
 *                the list of friend module declaration in the same
 *                module.
 * @param controlpart
 *                the control part found in the same module.
 * @throws ReParseException
 *                 if there was an error while refreshing the location
 *                 information and it could not be solved internally.
 */
public void updateSyntax(final TTCN3ReparseUpdater reparser, final List<ImportModule> importedModules, final List<FriendModule> friendModules, final ControlPart controlpart) throws ReParseException {
    // calculate damaged region
    int result = 0;
    boolean enveloped = false;
    int nofDamaged = 0;
    int leftBoundary = location.getOffset();
    int rightBoundary = location.getEndOffset();
    final int damageOffset = reparser.getDamageStart();
    final int damageEndOffset = reparser.getDamageEnd();
    IAppendableSyntax lastAppendableBeforeChange = null;
    IAppendableSyntax lastPrependableBeforeChange = null;
    boolean isControlPossible = controlpart == null;
    if (controlpart != null) {
        final Location tempLocation = controlpart.getLocation();
        if (reparser.envelopsDamage(tempLocation)) {
            enveloped = true;
        } else if (!reparser.isDamaged(tempLocation)) {
            if (tempLocation.getEndOffset() < damageOffset && tempLocation.getEndOffset() > leftBoundary) {
                leftBoundary = tempLocation.getEndOffset();
                lastAppendableBeforeChange = controlpart;
            }
            if (tempLocation.getOffset() > damageEndOffset && tempLocation.getOffset() < rightBoundary) {
                rightBoundary = tempLocation.getOffset();
                lastPrependableBeforeChange = controlpart;
            }
        }
    }
    for (int i = 0, size = groups.size(); i < size && !enveloped; i++) {
        final Group tempGroup = groups.get(i);
        final Location tempLocation = tempGroup.getLocation();
        if (reparser.envelopsDamage(tempLocation)) {
            enveloped = true;
            leftBoundary = tempLocation.getOffset();
            rightBoundary = tempLocation.getEndOffset();
        } else if (reparser.isDamaged(tempLocation)) {
            nofDamaged++;
        } else {
            if (tempLocation.getEndOffset() < damageOffset && tempLocation.getEndOffset() > leftBoundary) {
                leftBoundary = tempLocation.getEndOffset();
                lastAppendableBeforeChange = tempGroup;
            }
            if (tempLocation.getOffset() > damageEndOffset && tempLocation.getOffset() < rightBoundary) {
                rightBoundary = tempLocation.getOffset();
                lastPrependableBeforeChange = tempGroup;
            }
        }
    }
    if (!groups.isEmpty()) {
        isControlPossible &= groups.get(groups.size() - 1).getLocation().getEndOffset() < leftBoundary;
    }
    for (int i = 0, size = importedModules.size(); i < size && !enveloped; i++) {
        final ImportModule tempImport = importedModules.get(i);
        if (tempImport.getParentGroup() == null) {
            final Location tempLocation = tempImport.getLocation();
            if (reparser.envelopsDamage(tempLocation)) {
                enveloped = true;
                leftBoundary = tempLocation.getOffset();
                rightBoundary = tempLocation.getEndOffset();
            } else if (reparser.isDamaged(tempLocation)) {
                nofDamaged++;
            } else {
                if (tempLocation.getEndOffset() < damageOffset && tempLocation.getEndOffset() > leftBoundary) {
                    leftBoundary = tempLocation.getEndOffset();
                    lastAppendableBeforeChange = tempImport;
                }
                if (tempLocation.getOffset() > damageEndOffset && tempLocation.getOffset() < rightBoundary) {
                    rightBoundary = tempLocation.getOffset();
                    lastPrependableBeforeChange = tempImport;
                }
            }
        }
    }
    if (!importedModules.isEmpty()) {
        isControlPossible &= importedModules.get(importedModules.size() - 1).getLocation().getEndOffset() < leftBoundary;
    }
    for (int i = 0, size = friendModules.size(); i < size && !enveloped; i++) {
        final FriendModule tempFriend = friendModules.get(i);
        if (tempFriend.getParentGroup() == null) {
            final Location tempLocation = tempFriend.getLocation();
            if (reparser.envelopsDamage(tempLocation)) {
                enveloped = true;
                leftBoundary = tempLocation.getOffset();
                rightBoundary = tempLocation.getEndOffset();
            } else if (reparser.isDamaged(tempLocation)) {
                nofDamaged++;
            } else {
                if (tempLocation.getEndOffset() < damageOffset && tempLocation.getEndOffset() > leftBoundary) {
                    leftBoundary = tempLocation.getEndOffset();
                    lastAppendableBeforeChange = tempFriend;
                }
                if (tempLocation.getOffset() > damageEndOffset && tempLocation.getOffset() < rightBoundary) {
                    rightBoundary = tempLocation.getOffset();
                    lastPrependableBeforeChange = tempFriend;
                }
            }
        }
    }
    if (!friendModules.isEmpty()) {
        isControlPossible &= friendModules.get(friendModules.size() - 1).getLocation().getEndOffset() < leftBoundary;
    }
    for (final Iterator<Definition> iterator = definitions.iterator(); iterator.hasNext() && !enveloped; ) {
        final Definition temp = iterator.next();
        if (temp.getParentGroup() == null) {
            final Location tempLocation = temp.getLocation();
            final Location cumulativeLocation = temp.getCumulativeDefinitionLocation();
            if (tempLocation.equals(cumulativeLocation) && reparser.envelopsDamage(cumulativeLocation)) {
                enveloped = true;
                leftBoundary = cumulativeLocation.getOffset();
                rightBoundary = cumulativeLocation.getEndOffset();
            } else if (reparser.isDamaged(cumulativeLocation)) {
                nofDamaged++;
                if (reparser.getDamageStart() == cumulativeLocation.getEndOffset()) {
                    lastAppendableBeforeChange = temp;
                } else if (reparser.getDamageEnd() == cumulativeLocation.getOffset()) {
                    lastPrependableBeforeChange = temp;
                }
            } else {
                if (cumulativeLocation.getEndOffset() < damageOffset && cumulativeLocation.getEndOffset() > leftBoundary) {
                    leftBoundary = cumulativeLocation.getEndOffset();
                    lastAppendableBeforeChange = temp;
                }
                if (cumulativeLocation.getOffset() > damageEndOffset && cumulativeLocation.getOffset() < rightBoundary) {
                    rightBoundary = cumulativeLocation.getOffset();
                    lastPrependableBeforeChange = temp;
                }
            }
            final Location tempCommentLocation = temp.getCommentLocation();
            if (tempCommentLocation != null && reparser.isDamaged(tempCommentLocation)) {
                nofDamaged++;
                rightBoundary = tempLocation.getEndOffset();
            }
        }
    }
    if (!definitions.isEmpty()) {
        isControlPossible &= definitions.get(definitions.size() - 1).getLocation().getEndOffset() < leftBoundary;
    }
    // was not enveloped
    if (!enveloped && reparser.isDamaged(location)) {
        // the extension might be correct
        if (lastAppendableBeforeChange != null) {
            final boolean isBeingExtended = reparser.startsWithFollow(lastAppendableBeforeChange.getPossibleExtensionStarterTokens());
            if (isBeingExtended) {
                leftBoundary = lastAppendableBeforeChange.getLocation().getOffset();
                nofDamaged++;
                enveloped = false;
                reparser.extendDamagedRegion(leftBoundary, rightBoundary);
            }
        }
        if (lastPrependableBeforeChange != null) {
            final List<Integer> temp = lastPrependableBeforeChange.getPossiblePrefixTokens();
            if (temp != null && reparser.endsWithToken(temp)) {
                rightBoundary = lastPrependableBeforeChange.getLocation().getEndOffset();
                nofDamaged++;
                enveloped = false;
                reparser.extendDamagedRegion(leftBoundary, rightBoundary);
            }
        }
        if (nofDamaged != 0) {
            // remove damaged stuff
            removeStuffInRange(reparser, importedModules, friendModules);
            if (doubleDefinitions != null) {
                doubleDefinitions.clear();
            }
            lastUniquenessCheckTimeStamp = null;
            lastCompilationTimeStamp = null;
        }
        // extend damaged region till the neighbor definitions just here to avoid calculating something damaged or extended:
        // Perhaps it should be moved even farther:
        reparser.extendDamagedRegion(leftBoundary, rightBoundary);
    }
    // update what is left
    for (int i = 0; i < groups.size(); i++) {
        final Group temp = groups.get(i);
        final Location tempLocation = temp.getLocation();
        if (reparser.isAffected(tempLocation)) {
            try {
                temp.updateSyntax(reparser, importedModules, definitions, friendModules);
            } catch (ReParseException e) {
                if (e.getDepth() == 1) {
                    enveloped = false;
                    groups.remove(i);
                    i--;
                    reparser.extendDamagedRegion(tempLocation);
                    result = 1;
                } else {
                    if (doubleDefinitions != null) {
                        doubleDefinitions.clear();
                    }
                    lastUniquenessCheckTimeStamp = null;
                    e.decreaseDepth();
                    throw e;
                }
            }
        }
    }
    for (int i = 0; i < importedModules.size(); i++) {
        final ImportModule temp = importedModules.get(i);
        if (temp.getParentGroup() == null) {
            final Location tempLocation = temp.getLocation();
            if (reparser.isAffected(tempLocation)) {
                try {
                    final boolean isDamaged = enveloped && reparser.envelopsDamage(tempLocation);
                    temp.updateSyntax(reparser, enveloped && reparser.envelopsDamage(tempLocation));
                    if (isDamaged) {
                        ((TTCN3Module) parentScope).checkRoot();
                    }
                } catch (ReParseException e) {
                    if (e.getDepth() == 1) {
                        enveloped = false;
                        importedModules.remove(i);
                        i--;
                        reparser.extendDamagedRegion(tempLocation);
                        result = 1;
                    } else {
                        if (doubleDefinitions != null) {
                            doubleDefinitions.clear();
                        }
                        lastUniquenessCheckTimeStamp = null;
                        e.decreaseDepth();
                        throw e;
                    }
                }
            }
        }
    }
    for (int i = 0; i < friendModules.size(); i++) {
        final FriendModule temp = friendModules.get(i);
        if (temp.getParentGroup() == null) {
            final Location tempLocation = temp.getLocation();
            if (reparser.isAffected(tempLocation)) {
                try {
                    final boolean isDamaged = enveloped && reparser.envelopsDamage(tempLocation);
                    temp.updateSyntax(reparser, enveloped && reparser.envelopsDamage(tempLocation));
                    if (isDamaged) {
                        ((TTCN3Module) parentScope).checkRoot();
                    }
                } catch (ReParseException e) {
                    if (e.getDepth() == 1) {
                        enveloped = false;
                        friendModules.remove(i);
                        i--;
                        reparser.extendDamagedRegion(tempLocation);
                        result = 1;
                    } else {
                        if (doubleDefinitions != null) {
                            doubleDefinitions.clear();
                        }
                        lastUniquenessCheckTimeStamp = null;
                        e.decreaseDepth();
                        throw e;
                    }
                }
            }
        }
    }
    for (final Iterator<Definition> iterator = definitions.iterator(); iterator.hasNext(); ) {
        final Definition temp = iterator.next();
        if (temp.getParentGroup() == null) {
            final Location tempLocation = temp.getLocation();
            final Location cumulativeLocation = temp.getCumulativeDefinitionLocation();
            if (reparser.isAffected(cumulativeLocation)) {
                try {
                    final boolean isDamaged = enveloped && reparser.envelopsDamage(tempLocation);
                    temp.updateSyntax(reparser, isDamaged);
                    if (reparser.getNameChanged()) {
                        if (doubleDefinitions != null) {
                            doubleDefinitions.clear();
                        }
                        lastUniquenessCheckTimeStamp = null;
                        // to recheck the whole module
                        lastCompilationTimeStamp = null;
                        reparser.setNameChanged(false);
                    // This could also spread
                    }
                    if (isDamaged) {
                        // TODO lets move this into the definitions
                        temp.checkRoot();
                    }
                } catch (ReParseException e) {
                    if (e.getDepth() == 1) {
                        enveloped = false;
                        definitions.remove(temp);
                        reparser.extendDamagedRegion(cumulativeLocation);
                        result = 1;
                    } else {
                        if (doubleDefinitions != null) {
                            doubleDefinitions.clear();
                        }
                        lastUniquenessCheckTimeStamp = null;
                        e.decreaseDepth();
                        throw e;
                    }
                }
            }
        }
    }
    if (result == 1) {
        removeStuffInRange(reparser, importedModules, friendModules);
        if (doubleDefinitions != null) {
            doubleDefinitions.clear();
        }
        lastUniquenessCheckTimeStamp = null;
        lastCompilationTimeStamp = null;
    }
    for (int i = 0, size = groups.size(); i < size; i++) {
        final Group temp = groups.get(i);
        final Location tempLocation = temp.getLocation();
        if (reparser.isAffected(tempLocation)) {
            reparser.updateLocation(tempLocation);
        }
    }
    for (int i = 0, size = importedModules.size(); i < size; i++) {
        final ImportModule temp = importedModules.get(i);
        if (temp.getParentGroup() == null) {
            final Location tempLocation = temp.getLocation();
            if (reparser.isAffected(tempLocation)) {
                reparser.updateLocation(tempLocation);
            }
        }
    }
    for (int i = 0, size = friendModules.size(); i < size; i++) {
        final FriendModule temp = friendModules.get(i);
        if (temp.getParentGroup() == null) {
            final Location tempLocation = temp.getLocation();
            if (reparser.isAffected(tempLocation)) {
                reparser.updateLocation(tempLocation);
            }
        }
    }
    for (final Iterator<Definition> iterator = definitions.iterator(); iterator.hasNext(); ) {
        final Definition temp = iterator.next();
        if (temp.getParentGroup() == null) {
            final Location tempLocation = temp.getLocation();
            final Location cumulativeLocation = temp.getCumulativeDefinitionLocation();
            if (reparser.isAffected(tempLocation)) {
                if (tempLocation != cumulativeLocation) {
                    reparser.updateLocation(cumulativeLocation);
                }
                reparser.updateLocation(tempLocation);
            }
        }
    }
    final boolean tempIsControlPossible = isControlPossible;
    if (!enveloped) {
        if (reparser.envelopsDamage(location)) {
            reparser.extendDamagedRegion(leftBoundary, rightBoundary);
            result = reparse(reparser, tempIsControlPossible);
            result = Math.max(result - 1, 0);
            lastCompilationTimeStamp = null;
        } else {
            result = Math.max(result, 1);
        }
    }
    if (result == 0) {
        lastUniquenessCheckTimeStamp = null;
    } else {
        if (doubleDefinitions != null) {
            doubleDefinitions.clear();
        }
        lastUniquenessCheckTimeStamp = null;
        throw new ReParseException(result);
    }
}
Also used : IAppendableSyntax(org.eclipse.titan.designer.AST.TTCN3.IAppendableSyntax) ReParseException(org.eclipse.titan.designer.parsers.ttcn3parser.ReParseException) NULL_Location(org.eclipse.titan.designer.AST.NULL_Location) Location(org.eclipse.titan.designer.AST.Location)

Example 19 with TTCN3Module

use of org.eclipse.titan.designer.AST.TTCN3.definitions.TTCN3Module in project titan.EclipsePlug-ins by eclipse.

the class TTCN3Module method analyzeExtensionAttributes.

/**
 * Convert and check the version, requires and titan version extension attributes.
 *
 * @param timestamp
 *                the timestamp of the actual build cycle.
 */
private void analyzeExtensionAttributes(final CompilationTimeStamp timestamp) {
    if (withAttributesPath == null) {
        return;
    }
    final List<SingleWithAttribute> realAttributes = withAttributesPath.getRealAttributes(timestamp);
    SingleWithAttribute attribute;
    List<AttributeSpecification> specifications = null;
    for (int i = 0; i < realAttributes.size(); i++) {
        attribute = realAttributes.get(i);
        if (Attribute_Type.Extension_Attribute.equals(attribute.getAttributeType())) {
            final Qualifiers qualifiers = attribute.getQualifiers();
            if (qualifiers == null || qualifiers.getNofQualifiers() == 0) {
                if (specifications == null) {
                    specifications = new ArrayList<AttributeSpecification>();
                }
                specifications.add(attribute.getAttributeSpecification());
            }
        }
    }
    if (specifications == null) {
        return;
    }
    final List<ExtensionAttribute> attributes = new ArrayList<ExtensionAttribute>();
    AttributeSpecification specification;
    for (int i = 0; i < specifications.size(); i++) {
        specification = specifications.get(i);
        final ExtensionAttributeAnalyzer analyzer = new ExtensionAttributeAnalyzer();
        analyzer.parse(specification);
        final List<ExtensionAttribute> temp = analyzer.getAttributes();
        if (temp != null) {
            attributes.addAll(temp);
        }
    }
    ExtensionAttribute extensionAttribute;
    for (int i = 0; i < attributes.size(); i++) {
        extensionAttribute = attributes.get(i);
        switch(extensionAttribute.getAttributeType()) {
            case VERSION:
                {
                    final ModuleVersionAttribute moduleVersion = (ModuleVersionAttribute) extensionAttribute;
                    moduleVersion.parse();
                    if (versionNumber != null) {
                        moduleVersion.getLocation().reportSemanticError("Duplicate version attribute");
                    } else {
                        setVersion(moduleVersion.getVersionNumber());
                    }
                    break;
                }
            case REQUIRES:
                {
                    final VersionRequirementAttribute versionReq = (VersionRequirementAttribute) extensionAttribute;
                    versionReq.parse();
                    ImportModule theImport = null;
                    final String requiredModuleName = versionReq.getRequiredModule().getName();
                    for (final ImportModule impMod : importedModules) {
                        if (requiredModuleName.equals(impMod.getIdentifier().getName())) {
                            theImport = impMod;
                            break;
                        }
                    }
                    if (theImport == null) {
                        final String message = MessageFormat.format(ImportModule.MISSINGMODULE, versionReq.getRequiredModule().getDisplayName());
                        versionReq.getRequiredModule().getLocation().reportSemanticError(message);
                    } else {
                        final TTCN3Module theImportedModule = (TTCN3Module) theImport.getReferredModule();
                        // make sure the version attribute is parsed (if any)
                        theImportedModule.check(timestamp);
                        final ProductIdentity requiredVersion = versionReq.getVersionNumber();
                        if (requiredVersion != null && theImportedModule.versionNumber != null && theImportedModule.versionNumber.compareTo(requiredVersion) < 0) {
                            final String message = MessageFormat.format("Module `{0}'' requires version {1} of module `{2}'', but only version {3} is available", identifier.getDisplayName(), requiredVersion.toString(), theImportedModule.getIdentifier().getDisplayName(), theImportedModule.versionNumber.toString());
                            versionReq.getLocation().reportSemanticError(message);
                        }
                    }
                    break;
                }
            case TITANVERSION:
                {
                    final TitanVersionAttribute titanReq = (TitanVersionAttribute) extensionAttribute;
                    titanReq.parse();
                    final ProductIdentity requiredTITANVersion = titanReq.getVersionNumber();
                    final String temp = CompilerVersionInformationCollector.getCompilerProductNumber();
                    final ProductIdentity compilerVersion = ProductIdentityHelper.getProductIdentity(temp, null);
                    if (requiredTITANVersion != null && compilerVersion != null && compilerVersion.compareTo(requiredTITANVersion) < 0) {
                        final String message = MessageFormat.format("Module `{0}'' requires TITAN version {1}, but version {2} is used right now", identifier.getDisplayName(), requiredTITANVersion.toString(), compilerVersion.toString());
                        titanReq.getLocation().reportSemanticError(message);
                    }
                    if (requiredTITANVersion != null && GeneralConstants.ON_THE_FLY_ANALYZER_VERSION != null && GeneralConstants.ON_THE_FLY_ANALYZER_VERSION.compareTo(requiredTITANVersion) < 0) {
                        final String message = MessageFormat.format("Module `{0}'' requires TITAN version {1}, but the on-the-fly analyzer is of version {2}", identifier.getDisplayName(), requiredTITANVersion.toString(), GeneralConstants.ON_THE_FLY_ANALYZER_VERSION.toString());
                        titanReq.getLocation().reportSemanticError(message);
                    }
                    break;
                }
            default:
                // we don't care
                break;
        }
    }
}
Also used : ModuleVersionAttribute(org.eclipse.titan.designer.AST.TTCN3.attributes.ModuleVersionAttribute) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) SingleWithAttribute(org.eclipse.titan.designer.AST.TTCN3.attributes.SingleWithAttribute) ExtensionAttribute(org.eclipse.titan.designer.AST.TTCN3.attributes.ExtensionAttribute) AttributeSpecification(org.eclipse.titan.designer.AST.TTCN3.attributes.AttributeSpecification) VersionRequirementAttribute(org.eclipse.titan.designer.AST.TTCN3.attributes.VersionRequirementAttribute) ProductIdentity(org.eclipse.titan.common.product.ProductIdentity) ExtensionAttributeAnalyzer(org.eclipse.titan.designer.parsers.extensionattributeparser.ExtensionAttributeAnalyzer) TitanVersionAttribute(org.eclipse.titan.designer.AST.TTCN3.attributes.TitanVersionAttribute) Qualifiers(org.eclipse.titan.designer.AST.TTCN3.attributes.Qualifiers)

Example 20 with TTCN3Module

use of org.eclipse.titan.designer.AST.TTCN3.definitions.TTCN3Module in project titan.EclipsePlug-ins by eclipse.

the class Type method checkEncode.

/**
 * Checks the encodings supported by the type (when using new codec handling).
 * TTCN-3 types need to have an 'encode' attribute to support an encoding.
 * ASN.1 types automatically support BER, PER and JSON encodings, and XER
 * encoding, if set by the compiler option.
 */
public void checkEncode(final CompilationTimeStamp timestamp) {
    switch(getTypeRefdLast(timestamp).getTypetypeTtcn3()) {
        case TYPE_NULL:
        case TYPE_BOOL:
        case TYPE_INTEGER:
        case TYPE_REAL:
        case TYPE_TTCN3_ENUMERATED:
        case TYPE_BITSTRING:
        case TYPE_HEXSTRING:
        case TYPE_OCTETSTRING:
        case TYPE_CHARSTRING:
        case TYPE_UCHARSTRING:
        case TYPE_OBJECTID:
        case TYPE_TTCN3_CHOICE:
        case TYPE_SEQUENCE_OF:
        case TYPE_SET_OF:
        case TYPE_TTCN3_SEQUENCE:
        case TYPE_TTCN3_SET:
        case TYPE_VERDICT:
        case TYPE_ARRAY:
        case TYPE_ANYTYPE:
            if (!isAsn()) {
                final WithAttributesPath attributePath = getAttributePath();
                if (attributePath != null) {
                    final MultipleWithAttributes multipleWithAttributes = attributePath.getAttributes();
                    if (multipleWithAttributes != null) {
                        for (int i = 0; i < multipleWithAttributes.getNofElements(); i++) {
                            final SingleWithAttribute singleWithAttribute = multipleWithAttributes.getAttribute(i);
                            if (singleWithAttribute.getAttributeType() == Attribute_Type.Encode_Attribute) {
                                final Attribute_Modifier_type mod = singleWithAttribute.getModifier();
                                final Qualifiers qualifiers = singleWithAttribute.getQualifiers();
                                if (qualifiers != null && qualifiers.getNofQualifiers() > 0) {
                                    for (int j = 0; j < qualifiers.getNofQualifiers(); j++) {
                                        final Qualifier qualifier = qualifiers.getQualifierByIndex(j);
                                        final List<ISubReference> fieldsOrArrays = new ArrayList<ISubReference>();
                                        for (int k = 0; k < qualifier.getNofSubReferences(); k++) {
                                            fieldsOrArrays.add(qualifier.getSubReferenceByIndex(k));
                                        }
                                        final Reference reference = new Reference(null, fieldsOrArrays);
                                        final IType type = getFieldType(timestamp, reference, 0, Expected_Value_type.EXPECTED_CONSTANT, false);
                                        if (type != null) {
                                            if (type.getMyScope() != myScope) {
                                                qualifier.getLocation().reportSemanticWarning("Encode attribute is ignored, because it refers to a type from a different type definition");
                                            } else {
                                                type.addCoding(timestamp, singleWithAttribute.getAttributeSpecification().getSpecification(), mod, false);
                                            }
                                        }
                                    }
                                } else {
                                    addCoding(timestamp, singleWithAttribute.getAttributeSpecification().getSpecification(), mod, false);
                                }
                            }
                        }
                    }
                    if (ownerType != TypeOwner_type.OT_TYPE_DEF) {
                        return;
                    }
                    WithAttributesPath globalAttributesPath;
                    final Def_Type def = (Def_Type) owner;
                    final Group nearest_group = def.getParentGroup();
                    if (nearest_group == null) {
                        // no group, use the module
                        Module myModule = myScope.getModuleScope();
                        globalAttributesPath = ((TTCN3Module) myModule).getAttributePath();
                    } else {
                        globalAttributesPath = nearest_group.getAttributePath();
                    }
                    if (globalAttributesPath != null) {
                        boolean hasGlobalOverride = false;
                        boolean modifierConflict = false;
                        Attribute_Modifier_type firstModifier = Attribute_Modifier_type.MOD_NONE;
                        final List<SingleWithAttribute> realAttributes = globalAttributesPath.getRealAttributes(timestamp);
                        for (int i = 0; i < realAttributes.size(); i++) {
                            final SingleWithAttribute singleWithAttribute = realAttributes.get(i);
                            if (singleWithAttribute.getAttributeType() == Attribute_Type.Encode_Attribute) {
                                Attribute_Modifier_type modifier = singleWithAttribute.getModifier();
                                if (i == 0) {
                                    firstModifier = modifier;
                                } else if (!modifierConflict && modifier != firstModifier) {
                                    modifierConflict = true;
                                    singleWithAttribute.getLocation().reportSemanticError("All 'encode' attributes of a group or module must have the same modifier ('override', '@local' or none)");
                                }
                                if (modifier == Attribute_Modifier_type.MOD_OVERRIDE) {
                                    hasGlobalOverride = true;
                                }
                                if (hasGlobalOverride && modifierConflict) {
                                    break;
                                }
                            }
                        }
                        // make a list of the type and its field and element types that inherit
                        // the global 'encode' attributes
                        // overriding global attributes are inherited by types with no coding
                        // table (no 'encode' attributes) of their own
                        // non-overriding global attributes are inherited by types that have
                        // no coding table of their own and cannot use the coding table of any
                        // other type
                        final ArrayList<IType> typeList = new ArrayList<IType>();
                        getTypesWithNoCodingTable(timestamp, typeList, hasGlobalOverride);
                        if (!typeList.isEmpty()) {
                            for (int i = 0; i < realAttributes.size(); i++) {
                                final SingleWithAttribute singleWithAttribute = realAttributes.get(i);
                                if (singleWithAttribute.getAttributeType() == Attribute_Type.Encode_Attribute) {
                                    for (int j = typeList.size() - 1; j >= 0; j--) {
                                        typeList.get(j).addCoding(timestamp, singleWithAttribute.getAttributeSpecification().getSpecification(), Attribute_Modifier_type.MOD_NONE, true);
                                    }
                                }
                            }
                            typeList.clear();
                        }
                    }
                }
            } else {
                // ASN.1 types automatically have BER, PER, XER, OER and JSON encoding
                switch(ownerType) {
                    case OT_TYPE_ASS:
                    case OT_RECORD_OF:
                    case OT_COMP_FIELD:
                    case OT_SELTYPE:
                    case OT_FIELDSETTING:
                        // FIXME implement once PER, JSON, OER or XER gets supported
                        break;
                    default:
                        break;
                }
            }
            break;
        default:
            // the rest of the types can't have 'encode' attributes
            break;
    }
}
Also used : WithAttributesPath(org.eclipse.titan.designer.AST.TTCN3.attributes.WithAttributesPath) Group(org.eclipse.titan.designer.AST.TTCN3.definitions.Group) Def_Type(org.eclipse.titan.designer.AST.TTCN3.definitions.Def_Type) Attribute_Modifier_type(org.eclipse.titan.designer.AST.TTCN3.attributes.SingleWithAttribute.Attribute_Modifier_type) ArrayList(java.util.ArrayList) SingleWithAttribute(org.eclipse.titan.designer.AST.TTCN3.attributes.SingleWithAttribute) MultipleWithAttributes(org.eclipse.titan.designer.AST.TTCN3.attributes.MultipleWithAttributes) Qualifier(org.eclipse.titan.designer.AST.TTCN3.attributes.Qualifier) Qualifiers(org.eclipse.titan.designer.AST.TTCN3.attributes.Qualifiers) TTCN3Module(org.eclipse.titan.designer.AST.TTCN3.definitions.TTCN3Module)

Aggregations

TTCN3Module (org.eclipse.titan.designer.AST.TTCN3.definitions.TTCN3Module)22 Module (org.eclipse.titan.designer.AST.Module)17 ArrayList (java.util.ArrayList)13 ProjectSourceParser (org.eclipse.titan.designer.parsers.ProjectSourceParser)9 ImportModule (org.eclipse.titan.designer.AST.TTCN3.definitions.ImportModule)7 IFile (org.eclipse.core.resources.IFile)6 Qualifiers (org.eclipse.titan.designer.AST.TTCN3.attributes.Qualifiers)5 SingleWithAttribute (org.eclipse.titan.designer.AST.TTCN3.attributes.SingleWithAttribute)5 HashSet (java.util.HashSet)4 MultiTextEdit (org.eclipse.text.edits.MultiTextEdit)4 Location (org.eclipse.titan.designer.AST.Location)4 List (java.util.List)3 TreeSet (java.util.TreeSet)3 IProject (org.eclipse.core.resources.IProject)3 BadLocationException (org.eclipse.jface.text.BadLocationException)3 InsertEdit (org.eclipse.text.edits.InsertEdit)3 IType (org.eclipse.titan.designer.AST.IType)3 Reference (org.eclipse.titan.designer.AST.Reference)3 AttributeSpecification (org.eclipse.titan.designer.AST.TTCN3.attributes.AttributeSpecification)3 ExtensionAttribute (org.eclipse.titan.designer.AST.TTCN3.attributes.ExtensionAttribute)3