-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileParser.cs
319 lines (269 loc) · 10.2 KB
/
FileParser.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Xml;
namespace Intellua
{
internal class FileParserResult {
public AutoCompleteData result;
public string msg;
}
internal class FileParser
{
public AutoCompleteData result;
public string msg = "";
private static List<string> s_extensions = new List<string>();
private static Dictionary<string, FileParser> s_files = new Dictionary<string, FileParser>();
private static List<string> s_includePaths = new List<string>();
private static Regex s_requreReg = new Regex(@"(?<![\.:]\s*)(?:\s+|^)require\s*\(\s*\""(.*)\""\s*\)", RegexOptions.Compiled);
private System.DateTime m_lastCheckTime;
private System.DateTime m_lastWriteTime;
private Dictionary<string, int> m_required = new Dictionary<string, int>();
private IntelluaSource m_source;
static FileParser()
{
s_extensions.Add("");
//s_extensions.Add(".lua");
try
{
XmlTextReader reader = new XmlTextReader(AppDomain.CurrentDomain.BaseDirectory + "intelluaconfig.xml");
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
if (reader.Name == "path")
{
reader.Read();
s_includePaths.Add(reader.Value);
}
else if (reader.Name == "ext")
{
reader.Read();
s_extensions.Add(reader.Value);
}
}
}
}
catch
{
}
}
public FileParser(IntelluaSource source)
{
m_source = source;
result = new AutoCompleteData();
result.setParent(source.m_intellua.AutoCompleteData.getParent());
//result.Variables.scope = source.m_intellua.parseScope(0, source.m_intellua.Lines.Count - 1);
m_lastCheckTime = System.DateTime.Now;
if (source.FilePath.Length != 0)
{
m_lastWriteTime = System.IO.File.GetLastWriteTime(m_source.FilePath);
}
m_required[source.FilePath] = 0;
}
public void doWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
parse(false);
FileParserResult rst = new FileParserResult();
rst.result = result;
rst.msg = msg;
e.Result = rst;
}
private static FileParser getFile(string filename, Intellua parent, Dictionary<string, int> required)
{
if (s_files.ContainsKey(filename))
{
FileParser afp = s_files[filename];
if (!afp.modified())
{
return afp;
}
else
{
System.Diagnostics.Debug.Print("modified");
}
}
System.Diagnostics.Debug.Print("parse file " + filename);
IntelluaSource source = new IntelluaSource(filename, parent);
FileParser fp = new FileParser(source);
foreach (var kv in required)
{
fp.m_required[kv.Key] = kv.Value;
}
fp.parse(true);
s_files[filename] = fp;
return fp;
}
private void addExtention(List<string> paths, string fn)
{
List<string> extensions = new List<string>();
foreach (string str in s_extensions)
{
paths.Add(fn + str);
}
}
private string getFilename(string filename, string filebasepath)
{
string exebasepath = AppDomain.CurrentDomain.BaseDirectory;
List<string> paths = new List<string>();
if (filebasepath != null)
{
addExtention(paths, filebasepath + filename);
}
addExtention(paths, exebasepath + filename);
foreach (string str in s_includePaths)
{
addExtention(paths, exebasepath + str + filename);
}
addExtention(paths, filename);
foreach (string str in paths)
{
if (System.IO.File.Exists(str))
{
System.IO.FileInfo fi = new System.IO.FileInfo(str);
return fi.FullName;
}
}
return null;
}
private bool modified()
{
if (m_source.FilePath.Length == 0) return false;
if (m_lastWriteTime == null) return true;
if ((System.DateTime.Now - m_lastCheckTime) < System.TimeSpan.FromSeconds(5)) return false;
if (!System.IO.File.Exists(m_source.FilePath)) return true;
m_lastCheckTime = System.DateTime.Now;
if (m_lastWriteTime != System.IO.File.GetLastWriteTime(m_source.FilePath))
{
return true;
}
return false;
}
private void parse(bool importMode)
{
parseRequire();
parseDeclaration();
if (importMode) return;
parseVariables();
}
private void parseDeclaration()
{
DeclParser dp = new DeclParser();
dp.parse(m_source.text);
msg = dp.msg;
dp.apply(result);
result.Types.removeEmptyNamespace();
result.Variables.removeEmptyNamespace();
}
private void parseRequire()
{
MatchCollection rst = s_requreReg.Matches(m_source.text);
string filebasepath = null;
if (m_source.FilePath.Length != 0)
{
System.IO.FileInfo fi = new System.IO.FileInfo(m_source.FilePath);
filebasepath = fi.DirectoryName + "\\";
}
foreach (Match m in rst)
{
string fn = getFilename(m.Groups[1].Value, filebasepath);
if (fn != null)
{
if (m_required.ContainsKey(fn))
{
continue;
}
m_required[fn] = 0;
FileParser fp = getFile(fn, m_source.m_intellua, m_required);
result.Requires.Add(fp.result);
}
}
}
private Scope parseScope(int start, int end)
{
/*int level = m_source.Lines[start].FoldLevel;
Scope rst = new Scope();
rst.StartPos = Lines[start].StartPosition;
rst.EndPos = Lines[end].EndPosition;
for (int i = start; i <= end; i++)
{
if (Lines[i].FoldLevel != level)
{
int s = i;
while (i <= end && Lines[i].FoldLevel != level)
{
i++;
}
i--;
int e = i;
Scope c = parseScope(s, e);
c.Parent = rst;
rst.Childs.Add(c);
}
}
return rst;*/
return null;
}
private void parseVariables()
{
{
LuaTokenizer lt = new LuaTokenizer(m_source.RawText, 0);
List<LuaToken> tokens = new List<LuaToken>();
while (true)
{
LuaToken t = lt.getToken();
tokens.Add(t);
if (t.Type == LuaTokenType.EOF) break;
}
//System.Diagnostics.Debug.Print("=========");
LuaParser lp = new LuaParser(tokens);
LuaAST chunk = lp.parse();
//chunk.print(0);
LuaASTWalker walker = new LuaASTWalker();
if (lp.errMsg != null)
{
msg = lp.errMsg;
}
walker.walk(chunk, result);
}
/*
int pos = 0;
Byte[] str = m_source.RawText;
for (; pos < str.Length; pos++)
{
char c = Convert.ToChar(str[pos]);
//search for assignment operator
if (!Parser.isCode(m_source, pos))
{
continue;
}
if (c != '=') continue;
if (pos > 0)
{
if (Convert.ToChar(str[pos - 1]) == '=') continue;
}
if (pos < str.Length - 1)
{
if (Convert.ToChar(str[pos + 1]) == '=') continue;
}
MemberChain v = MemberChain.ParseBackward(m_source, pos - 1);
if (v.Elements.Count > 1 || v.Elements.Count == 0) continue;
string varName = v.getLastElement();
Variable var = result.Variables.getVariable(varName);
// if (var != null) continue;
MemberChain elem = MemberChain.ParseFoward(m_source, pos + 1);
if (elem == null) continue;
Type t = elem.getType(result);
if (t == null) continue;
//if (t.displa == "") continue;
//System.Diagnostics.Debug.Print(varName + " added");
var = new Variable(varName);
var.IsStatic = false;
var.Type = t;
var.StartPos = v.StartPos;
var.EndPos = elem.EndPos;
result.Variables.add(var);
}*/
}
}
}