Lines 9-129
Link Here
|
9 |
import org.eclipse.dltk.internal.core.ModelManager; |
9 |
import org.eclipse.dltk.internal.core.ModelManager; |
10 |
|
10 |
|
11 |
public class SourceParserUtil { |
11 |
public class SourceParserUtil { |
12 |
private static final Object AST = "ast"; //$NON-NLS-1$ |
12 |
private static final String AST = "ast"; //$NON-NLS-1$ |
13 |
private static final Object FLAGS = "flags"; //$NON-NLS-1$ |
13 |
private static final int DEFAULT_FLAGS = ISourceParserConstants.DEFAULT; |
14 |
|
14 |
|
15 |
/** |
15 |
/** |
16 |
* Used to retrive module declaration from source module. |
16 |
* Used to retrieve module declaration from source module. |
17 |
*/ |
17 |
*/ |
18 |
public static ModuleDeclaration getModuleDeclaration(ISourceModule module, |
18 |
public static ModuleDeclaration getModuleDeclaration(ISourceModule module, |
19 |
IProblemReporter reporter) { |
19 |
IProblemReporter reporter) { |
20 |
return getModuleDeclaration(module, reporter, ISourceParserConstants.DEFAULT); |
20 |
return getModuleDeclaration(module, reporter, DEFAULT_FLAGS); |
21 |
} |
21 |
} |
|
|
22 |
|
22 |
public static ModuleDeclaration getModuleDeclaration(ISourceModule module, |
23 |
public static ModuleDeclaration getModuleDeclaration(ISourceModule module, |
23 |
IProblemReporter reporter, int flags) { |
24 |
IProblemReporter reporter, int flags) { |
24 |
ISourceModuleInfoCache sourceModuleInfoCache = ModelManager |
25 |
ISourceModuleInfoCache cache = ModelManager.getModelManager() |
25 |
.getModelManager().getSourceModuleInfoCache(); |
26 |
.getSourceModuleInfoCache(); |
26 |
ISourceModuleInfo sourceModuleInfo = sourceModuleInfoCache.get(module); |
27 |
ISourceModuleInfo cacheEntry = cache.get(module); |
27 |
return getModuleDeclaration(module, reporter, sourceModuleInfo, flags); |
28 |
ModuleDeclaration declaration = getFromCache(cacheEntry, flags); |
|
|
29 |
if (declaration == null) { |
30 |
final char[] content = getModuleContent(module); |
31 |
if (content != null) { |
32 |
declaration = parseModule(module, content, reporter, flags); |
33 |
if (declaration != null) { |
34 |
putToCache(cacheEntry, declaration, flags); |
35 |
} |
36 |
} |
37 |
} |
38 |
return declaration; |
39 |
} |
40 |
|
41 |
private static ISourceParser getSourceParser(ISourceModule module) { |
42 |
IDLTKLanguageToolkit toolkit = DLTKLanguageManager |
43 |
.getLanguageToolkit(module); |
44 |
return DLTKLanguageManager.getSourceParser(toolkit.getNatureId()); |
45 |
} |
46 |
|
47 |
private static String getCacheKey(int flags) { |
48 |
if (flags == DEFAULT_FLAGS) { |
49 |
return AST; |
50 |
} else { |
51 |
return AST + flags; |
52 |
} |
28 |
} |
53 |
} |
29 |
|
54 |
|
30 |
/** |
55 |
/** |
31 |
* Used to retrive module declaration from source module. |
56 |
* This is for use in parsers. |
32 |
*/ |
57 |
*/ |
|
|
58 |
private static ModuleDeclaration getFromCache(ISourceModuleInfo cacheEntry, |
59 |
int flags) { |
60 |
if (cacheEntry != null) { |
61 |
return (ModuleDeclaration) cacheEntry.get(getCacheKey(flags)); |
62 |
} |
63 |
return null; |
64 |
} |
33 |
|
65 |
|
34 |
public static ModuleDeclaration getModuleDeclaration(ISourceModule module, |
66 |
private static void putToCache(ISourceModuleInfo cacheEntry, |
35 |
IProblemReporter reporter, ISourceModuleInfo mifo) { |
67 |
ModuleDeclaration declaration, int flags) { |
36 |
return getModuleDeclaration(module, reporter, mifo, ISourceParserConstants.DEFAULT); |
68 |
if (cacheEntry != null) { |
|
|
69 |
cacheEntry.put(getCacheKey(flags), declaration); |
70 |
} |
37 |
} |
71 |
} |
38 |
public static ModuleDeclaration getModuleDeclaration(ISourceModule module, |
|
|
39 |
IProblemReporter reporter, ISourceModuleInfo mifo, int flags) { |
40 |
|
72 |
|
41 |
IDLTKLanguageToolkit toolkit; |
73 |
private static char[] getModuleContent(ISourceModule module) { |
42 |
toolkit = DLTKLanguageManager.getLanguageToolkit(module); |
74 |
try { |
43 |
ModuleDeclaration moduleDeclaration = null; |
75 |
return module.getSourceAsCharArray(); |
44 |
Integer flag; |
76 |
} catch (Exception e) { |
45 |
if (mifo != null) { |
77 |
DLTKCore.error("Error parsing " + module.getPath(), e); |
46 |
moduleDeclaration = (ModuleDeclaration) mifo.get(AST); |
78 |
return null; |
47 |
flag = (Integer) mifo.get(FLAGS); |
|
|
48 |
if( flag != null && flag.intValue() != flags ) { |
49 |
moduleDeclaration = null; |
50 |
} |
51 |
} |
79 |
} |
52 |
if (moduleDeclaration == null) { |
80 |
} |
53 |
if (reporter != null) { |
81 |
|
54 |
reporter.clearMarkers(); |
82 |
private static ModuleDeclaration parseModule(ISourceModule module, |
55 |
} |
83 |
char[] content, IProblemReporter reporter, int flags) { |
56 |
ISourceParser sourceParser = null; |
84 |
if (reporter != null) { |
57 |
sourceParser = DLTKLanguageManager.getSourceParser(toolkit |
85 |
reporter.clearMarkers(); |
58 |
.getNatureId()); |
86 |
} |
59 |
if (sourceParser != null) { |
87 |
final ISourceParser parser = getSourceParser(module); |
60 |
if (sourceParser instanceof ISourceParserExtension) { |
88 |
if (parser instanceof ISourceParserExtension) { |
61 |
((ISourceParserExtension) sourceParser).setFlags(flags); |
89 |
((ISourceParserExtension) parser).setFlags(flags); |
62 |
} |
|
|
63 |
try { |
64 |
moduleDeclaration = sourceParser.parse(module.getPath() |
65 |
.toString().toCharArray(), module |
66 |
.getSourceAsCharArray(), reporter); |
67 |
} catch (ModelException e) { |
68 |
if (DLTKCore.DEBUG) { |
69 |
e.printStackTrace(); |
70 |
} |
71 |
} |
72 |
if (moduleDeclaration != null && mifo != null) { |
73 |
mifo.put(AST, moduleDeclaration); |
74 |
mifo.put(FLAGS, new Integer(flags)); |
75 |
} |
76 |
} |
77 |
} |
90 |
} |
78 |
return moduleDeclaration; |
91 |
final String filename = module.getPath().toString(); |
|
|
92 |
return parser.parse(filename.toCharArray(), content, reporter); |
79 |
} |
93 |
} |
80 |
|
94 |
|
81 |
public static ModuleDeclaration getModuleDeclaration(char[] filename, |
95 |
public static void parseSourceModule( |
82 |
char[] content, String nature, IProblemReporter reporter, |
96 |
ISourceElementParser sourceElementParser, ISourceModule sourceModule) { |
83 |
ISourceModuleInfo mifo) { |
97 |
final ISourceModuleInfo cacheEntry = ModelManager.getModelManager() |
84 |
return getModuleDeclaration(filename, content, nature, reporter, mifo, |
98 |
.getSourceModuleInfoCache().get(sourceModule); |
85 |
ISourceParserConstants.DEFAULT); |
99 |
parseSourceModule(sourceElementParser, sourceModule, cacheEntry); |
86 |
} |
100 |
} |
87 |
|
101 |
|
88 |
public static ModuleDeclaration getModuleDeclaration(char[] filename, |
102 |
public static void parseSourceModule(ISourceElementParser elementParser, |
89 |
char[] content, String nature, IProblemReporter reporter, |
103 |
ISourceModule module, ISourceModuleInfo cacheEntry) { |
90 |
ISourceModuleInfo mifo, int flags) { |
104 |
if (cacheEntry != null |
91 |
ISourceParser sourceParser;// = new SourceParser(this.fReporter); |
105 |
&& elementParser instanceof ISourceElementASTCachingParser) { |
92 |
sourceParser = DLTKLanguageManager.getSourceParser(nature); |
106 |
final ISourceElementASTCachingParser cachingParser = (ISourceElementASTCachingParser) elementParser; |
93 |
if (sourceParser instanceof ISourceParserExtension) { |
107 |
final int flags = DEFAULT_FLAGS; |
94 |
((ISourceParserExtension) sourceParser).setFlags(flags); |
108 |
ModuleDeclaration declaration = getFromCache(cacheEntry, flags); |
95 |
} |
109 |
if (declaration == null) { |
96 |
ModuleDeclaration moduleDeclaration = SourceParserUtil |
110 |
final char[] content = getModuleContent(module); |
97 |
.getModuleFromCache(mifo, flags); |
111 |
if (content == null) { |
98 |
if (moduleDeclaration == null) { |
112 |
return; |
99 |
if (reporter != null) { |
113 |
} |
100 |
reporter.clearMarkers(); |
114 |
IProblemReporter reporter = cachingParser.getProblemReporter(); |
|
|
115 |
declaration = parseModule(module, content, reporter, flags); |
116 |
if (declaration != null) { |
117 |
putToCache(cacheEntry, declaration, flags); |
118 |
cachingParser.processModule(module, declaration, content); |
119 |
} |
120 |
} else { |
121 |
cachingParser.processModule(module, declaration, null); |
122 |
} |
123 |
} else { |
124 |
final char[] content = getModuleContent(module); |
125 |
if (content == null) { |
126 |
return; |
101 |
} |
127 |
} |
102 |
moduleDeclaration = sourceParser.parse(filename, content, reporter); |
128 |
elementParser.parseSourceModule(content, cacheEntry, module |
103 |
SourceParserUtil.putModuleToCache(mifo, moduleDeclaration, flags); |
129 |
.getPath().toString().toCharArray()); |
104 |
} |
130 |
} |
105 |
return moduleDeclaration; |
|
|
106 |
} |
131 |
} |
107 |
|
132 |
|
108 |
/** |
133 |
/** |
109 |
* This is for use in parsers. |
134 |
* @param filename |
|
|
135 |
* @param content |
136 |
* @param natureId |
137 |
* @param object |
138 |
* @param object2 |
139 |
* @param runtimeModel |
140 |
* @return |
110 |
*/ |
141 |
*/ |
111 |
public static ModuleDeclaration getModuleFromCache(ISourceModuleInfo mifo, int flags) { |
142 |
public static ModuleDeclaration getModuleDeclaration(char[] filename, |
112 |
if (mifo != null) { |
143 |
char[] content, String natureId, IProblemReporter reporter, |
113 |
Integer flag = (Integer) mifo.get(FLAGS); |
144 |
ISourceModuleInfo cacheEntry, int flags) { |
114 |
if( flag != null && flag.intValue() != flags ) { |
145 |
ModuleDeclaration declaration = getFromCache(cacheEntry, flags); |
115 |
return null; |
146 |
if (declaration == null) { |
|
|
147 |
ISourceParser parser = DLTKLanguageManager |
148 |
.getSourceParser(natureId); |
149 |
if (parser instanceof ISourceParserExtension) { |
150 |
((ISourceParserExtension) parser).setFlags(flags); |
151 |
} |
152 |
declaration = parser.parse(filename, content, reporter); |
153 |
if (declaration != null) { |
154 |
putToCache(cacheEntry, declaration, flags); |
116 |
} |
155 |
} |
117 |
return (ModuleDeclaration) mifo.get(AST); |
|
|
118 |
} |
156 |
} |
119 |
return null; |
157 |
return declaration; |
120 |
} |
158 |
} |
121 |
|
159 |
|
122 |
public static void putModuleToCache(ISourceModuleInfo info, |
|
|
123 |
ModuleDeclaration module, int flags) { |
124 |
if (info != null) { |
125 |
info.put(AST, module); |
126 |
info.put(FLAGS, new Integer(flags)); |
127 |
} |
128 |
} |
129 |
} |
160 |
} |