use of org.eclipse.titan.designer.AST.TTCN3.definitions.Def_ModulePar in project titan.EclipsePlug-ins by eclipse.
the class All_From_Template method getNofTemplatesNotAnyornone.
/**
* Calculates the number of list members which are not the any or none
* symbol.
*
* @return the number calculated.
*/
public int getNofTemplatesNotAnyornone(final CompilationTimeStamp timestamp) {
int result = 0;
if (allFrom == null) {
ErrorReporter.INTERNAL_ERROR();
return result;
}
if (!Template_type.SPECIFIC_VALUE.equals(allFrom.getTemplatetype())) {
allFrom.getLocation().reportSemanticError(REFERENCEEXPECTED);
allFrom.setIsErroneous(true);
return result;
}
if (!((SpecificValue_Template) allFrom).isReference()) {
allFrom.getLocation().reportSemanticError(REFERENCEEXPECTED);
allFrom.setIsErroneous(true);
return result;
}
// isReference branch:
final Reference reference = ((SpecificValue_Template) allFrom).getReference();
final Assignment assignment = reference.getRefdAssignment(timestamp, true);
if (assignment == null) {
allFrom.getLocation().reportSemanticError("Assignment not found");
allFrom.setIsErroneous(true);
return result;
}
ITTCN3Template body = null;
switch(assignment.getAssignmentType()) {
case A_TEMPLATE:
body = ((Def_Template) assignment).getTemplate(timestamp);
break;
case A_VAR_TEMPLATE:
body = ((Def_Var_Template) assignment).getInitialValue();
break;
case A_CONST:
final IValue value = ((Def_Const) assignment).getValue();
return getNofValues(value, timestamp);
case A_MODULEPAR:
final IValue mvalue = ((Def_ModulePar) assignment).getDefaultValue();
return getNofValues(mvalue, timestamp);
case A_MODULEPAR_TEMPLATE:
body = ((Def_ModulePar_Template) assignment).getDefaultTemplate();
break;
default:
return result;
}
if (body == null) {
ErrorReporter.INTERNAL_ERROR();
return result;
}
if (!Template_type.TEMPLATE_LIST.equals(body.getTemplatetype())) {
allFrom.getLocation().reportSemanticError("Template must be a record of or a set of values");
allFrom.setIsErroneous(true);
return result;
}
result = ((Template_List) body).getNofTemplatesNotAnyornone(timestamp);
return result;
}
use of org.eclipse.titan.designer.AST.TTCN3.definitions.Def_ModulePar in project titan.EclipsePlug-ins by eclipse.
the class DependencyCollector method readDependencies.
public WorkspaceJob readDependencies() {
final WorkspaceJob job = new WorkspaceJob("ExtractModulePar: reading dependencies from source project") {
@Override
public IStatus runInWorkspace(final IProgressMonitor monitor) throws CoreException {
final ProjectSourceParser projectSourceParser = GlobalParser.getProjectSourceParser(sourceProj);
// find all dependencies of the 'selection' definition
Set<IResource> allFiles = new HashSet<IResource>();
Set<IResource> asnFiles = new HashSet<IResource>();
NavigableSet<ILocateableNode> dependencies = new TreeSet<ILocateableNode>(new LocationComparator());
for (Def_ModulePar def : selection) {
/*
* Def_ModulePars with mutliple entries in a single modulepar block have incorrect location info
* (all entries have a location info equal to the location of their parent block)
* that is why the dependencies must be collected into a set that is not sorted by location
*
* TODO fix grammar definitions
* */
Set<ILocateableNode> nonSortedTempSet = new HashSet<ILocateableNode>();
collectDependencies(def, nonSortedTempSet, allFiles, asnFiles);
dependencies.addAll(nonSortedTempSet);
allFiles.add(def.getLocation().getFile());
// adding the selection itself to the dependencies
if (!dependencies.contains(def)) {
dependencies.add(def);
}
// get imports and friends for all files
for (IResource r : allFiles) {
if (!(r instanceof IFile)) {
continue;
}
IFile f = (IFile) r;
Module m = projectSourceParser.containedModule(f);
ImportFinderVisitor impVisitor = new ImportFinderVisitor();
m.accept(impVisitor);
List<ImportModule> impDefs = impVisitor.getImportDefs();
List<FriendModule> friendDefs = impVisitor.getFriendDefs();
filterImportDefinitions(allFiles, impDefs, projectSourceParser);
filterFriendDefinitions(allFiles, friendDefs, projectSourceParser);
dependencies.addAll(impDefs);
dependencies.addAll(friendDefs);
// the dependencies are sorted by file & location to avoid reading through a file multiple times
}
}
// collect the text to insert into the new project
copyMap = new HashMap<IPath, StringBuilder>();
try {
InputStream is = null;
InputStreamReader isr = null;
IResource lastFile = null;
IResource currFile;
ILocateableNode lastD = null;
int currOffset = 0;
for (ILocateableNode d : dependencies) {
currFile = d.getLocation().getFile();
if (!(currFile instanceof IFile)) {
ErrorReporter.logError("ExtractModulePar/DependencyCollector: IResource `" + currFile.getName() + "' is not an IFile.");
continue;
}
if (currFile != lastFile) {
// started reading new file
lastD = null;
if (lastFile != null) {
addToCopyMap(lastFile.getProjectRelativePath(), MODULE_TRAILER);
}
if (isr != null) {
isr.close();
}
if (is != null) {
is.close();
}
is = ((IFile) currFile).getContents();
isr = new InputStreamReader(is);
currOffset = 0;
Module m = projectSourceParser.containedModule((IFile) currFile);
String moduleName = m.getIdentifier().getTtcnName();
addToCopyMap(currFile.getProjectRelativePath(), MessageFormat.format(MODULE_HEADER, moduleName));
}
int toSkip = getNodeOffset(d) - currOffset;
if (toSkip < 0) {
reportOverlappingError(lastD, d);
continue;
}
isr.skip(toSkip);
// separators
if (d instanceof ImportModule && lastD instanceof ImportModule) {
addToCopyMap(currFile.getProjectRelativePath(), SINGLE_SEPARATOR);
} else if (d instanceof FriendModule && lastD instanceof FriendModule) {
addToCopyMap(currFile.getProjectRelativePath(), SINGLE_SEPARATOR);
} else {
addToCopyMap(currFile.getProjectRelativePath(), DOUBLE_SEPARATOR);
}
//
insertDependency(d, currFile, isr);
currOffset = d.getLocation().getEndOffset();
lastFile = currFile;
lastD = d;
}
if (lastFile != null) {
addToCopyMap(lastFile.getProjectRelativePath(), MODULE_TRAILER);
}
if (isr != null) {
isr.close();
}
if (is != null) {
is.close();
}
// copy the full content of asn files without opening them
filesToCopy = new ArrayList<IFile>();
for (IResource r : asnFiles) {
if (!(r instanceof IFile)) {
ErrorReporter.logError("ExtractModulePar/DependencyCollector: IResource `" + r.getName() + "' is not an IFile.");
continue;
}
filesToCopy.add((IFile) r);
}
} catch (IOException ioe) {
ErrorReporter.logError("ExtractModulePar/DependencyCollector: Error while reading source project.");
return Status.CANCEL_STATUS;
}
return Status.OK_STATUS;
}
private void insertDependency(final ILocateableNode d, final IResource res, final InputStreamReader isr) throws IOException {
char[] content = new char[d.getLocation().getEndOffset() - getNodeOffset(d)];
isr.read(content, 0, content.length);
addToCopyMap(res.getProjectRelativePath(), new String(content));
}
};
job.setUser(true);
job.schedule();
return job;
}
use of org.eclipse.titan.designer.AST.TTCN3.definitions.Def_ModulePar in project titan.EclipsePlug-ins by eclipse.
the class ChangeCreator method createFileChange.
private Change createFileChange(final IFile toVisit) {
if (toVisit == null) {
return null;
}
final ProjectSourceParser sourceParser = GlobalParser.getProjectSourceParser(toVisit.getProject());
final Module module = sourceParser.containedModule(toVisit);
if (module == null) {
return null;
}
// find all locations in the module that should be edited
final DefinitionVisitor vis = new DefinitionVisitor();
module.accept(vis);
final NavigableSet<Definition> nodes = vis.getLocations();
if (nodes.isEmpty()) {
return null;
}
// calculate edit locations
final List<Definition> locations = new ArrayList<Definition>();
try {
final WorkspaceJob job1 = calculateEditLocations(nodes, toVisit, locations);
job1.join();
} catch (InterruptedException ie) {
ErrorReporter.logExceptionStackTrace(ie);
} catch (CoreException ce) {
ErrorReporter.logError("UngroupModuleparRefactoring/CreateChange.createFileChange(): " + "CoreException while calculating edit locations. ");
ErrorReporter.logExceptionStackTrace(ce);
}
if (locations.isEmpty()) {
return null;
}
// create a change for each edit location
final TextFileChange tfc = new TextFileChange(toVisit.getName(), toVisit);
final MultiTextEdit rootEdit = new MultiTextEdit();
tfc.setEdit(rootEdit);
int precedeOffset = -1;
final String fileContents = loadFileContent(toVisit);
for (Definition node : locations) {
final Location l = node.getCumulativeDefinitionLocation();
final Location typeLocation = node.getType(CompilationTimeStamp.getBaseTimestamp()).getLocation();
final Location identifierLocation = node.getIdentifier().getLocation();
if (precedeOffset != l.getOffset()) {
precedeOffset = l.getOffset();
final int len = l.getEndOffset() - l.getOffset();
rootEdit.addChild(new DeleteEdit(l.getOffset(), len + 1));
}
String typeText = fileContents.substring(typeLocation.getOffset(), typeLocation.getEndOffset()).trim();
String name = fileContents.substring(identifierLocation.getOffset(), identifierLocation.getEndOffset()).trim();
String newModulePar = "";
if (node instanceof Def_ModulePar) {
Def_ModulePar modulePar = (Def_ModulePar) node;
if (modulePar.getDefaultValue() != null) {
final Location valueLocation = modulePar.getDefaultValue().getLocation();
String valueText = fileContents.substring(valueLocation.getOffset(), valueLocation.getEndOffset()).trim();
newModulePar = "modulepar " + typeText + " " + name + " := " + valueText + ";\n";
} else {
newModulePar = "modulepar " + typeText + " " + name + ";\n";
}
} else if (node instanceof Def_ModulePar_Template) {
Def_ModulePar_Template modulePar = (Def_ModulePar_Template) node;
if (modulePar.getDefaultTemplate() != null) {
final Location valueLocation = modulePar.getDefaultTemplate().getLocation();
String temlateText = fileContents.substring(valueLocation.getOffset(), valueLocation.getEndOffset()).trim();
newModulePar = "modulepar template " + typeText + " " + name + " := " + temlateText + ";\n";
} else {
newModulePar = "modulepar template " + typeText + " " + name + ";\n";
}
}
rootEdit.addChild(new InsertEdit(l.getOffset(), newModulePar));
}
return tfc;
}
Aggregations