Skip to content
This repository was archived by the owner on Sep 11, 2023. It is now read-only.

Commit c39a63a

Browse files
committed
Add enum struct support
1 parent 45f8127 commit c39a63a

File tree

12 files changed

+1372
-800
lines changed

12 files changed

+1372
-800
lines changed

App/AssemblyInfo1.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,5 @@
3333
#if (DEBUG)
3434
[assembly: AssemblyVersion("1.13.*")]
3535
#else
36-
[assembly: AssemblyVersion("1.3.1.0")]
36+
[assembly: AssemblyVersion("1.3.2.0")]
3737
#endif

SourcepawnCondenser/CondenserTest/MainWindow.xaml.cs

+45-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public MainWindow()
2222
InitializeComponent();
2323
StringBuilder str = new StringBuilder();
2424
List<string> files = new List<string>();
25-
files.AddRange(Directory.GetFiles(@"C:\Users\Jelle\Desktop\coding\sm-scripting\_Sourcemod Plugins\1.7_5255", "*.inc", SearchOption.AllDirectories));
25+
files.AddRange(Directory.GetFiles(@"D:\AlliedModders\includes", "*.inc", SearchOption.AllDirectories));
2626
str.AppendLine(files.Count.ToString());
2727
foreach (var f in files)
2828
{
@@ -132,7 +132,7 @@ private void textBox_TextChanged(object sender, TextChangedEventArgs e)
132132
item.Items.Add(new TreeViewItem() { Header = "Length: " + cn.Length.ToString() });
133133
cItem.Items.Add(item);
134134
}
135-
termTree.Items.Add(cItem);
135+
// termTree.Items.Add(cItem);
136136
TreeViewItem mItem = new TreeViewItem() { Header = "methodmaps (" + def.Methodmaps.Count.ToString() + ")", IsExpanded = expand };
137137
foreach (var m in def.Methodmaps)
138138
{
@@ -177,6 +177,49 @@ private void textBox_TextChanged(object sender, TextChangedEventArgs e)
177177
mItem.Items.Add(item);
178178
}
179179
termTree.Items.Add(mItem);
180+
TreeViewItem eItem = new TreeViewItem() { Header = "EnumStructs (" + def.EnumStructs.Count.ToString() + ")", IsExpanded = expand };
181+
foreach (var m in def.EnumStructs)
182+
{
183+
TreeViewItem item = new TreeViewItem() { Header = m.Name, IsExpanded = expand };
184+
item.Tag = m;
185+
item.MouseLeftButtonUp += ItemMM_MouseLeftButtonUp;
186+
item.Items.Add(new TreeViewItem() { Header = "Index: " + m.Index.ToString(), Background = Brushes.LightGray });
187+
item.Items.Add(new TreeViewItem() { Header = "Length: " + m.Length.ToString() });
188+
// item.Items.Add(new TreeViewItem() { Header = "Type: " + m.Type, Background = Brushes.LightGray });
189+
TreeViewItem subItem = new TreeViewItem() { Header = "Methods", Background = Brushes.LightGray };
190+
for (int j = 0; j < m.Methods.Count; ++j)
191+
{
192+
TreeViewItem subSubItem = new TreeViewItem() { Header = m.Methods[j].Name, Background = (j % 2 == 0) ? Brushes.LightGray : Brushes.White };
193+
subSubItem.Items.Add(new TreeViewItem() { Header = "Index: " + m.Methods[j].Index.ToString() });
194+
subSubItem.Items.Add(new TreeViewItem() { Header = "Length: " + m.Methods[j].Length.ToString(), Background = Brushes.LightGray });
195+
subSubItem.Items.Add(new TreeViewItem() { Header = "Comment: >>" + m.Methods[j].CommentString + "<<" });
196+
subSubItem.Items.Add(new TreeViewItem() { Header = "Return: " + m.Methods[j].ReturnType, Background = Brushes.LightGray });
197+
int k = 0;
198+
for (; k < m.Methods[j].MethodKind.Length; ++k)
199+
{
200+
subSubItem.Items.Add(new TreeViewItem() { Header = "MethodKind" + (k + 1).ToString() + ": " + m.Methods[j].MethodKind[k], Background = (k % 2 == 0) ? Brushes.LightGray : Brushes.White });
201+
}
202+
for (int l = 0; l < m.Methods[j].Parameters.Length; ++l)
203+
{
204+
++k;
205+
subSubItem.Items.Add(new TreeViewItem() { Header = "Parameter" + (l + 1).ToString() + ": " + m.Methods[j].Parameters[l], Background = (k % 2 == 0) ? Brushes.LightGray : Brushes.White });
206+
}
207+
subItem.Items.Add(subSubItem);
208+
}
209+
item.Items.Add(subItem);
210+
subItem = new TreeViewItem() { Header = "Fields" };
211+
for (int j = 0; j < m.Fields.Count; ++j)
212+
{
213+
TreeViewItem subSubItem = new TreeViewItem() { Header = m.Fields[j].Name, Background = (j % 2 == 0) ? Brushes.LightGray : Brushes.White };
214+
subSubItem.Items.Add(new TreeViewItem() { Header = "Index: " + m.Fields[j].Index.ToString() });
215+
subSubItem.Items.Add(new TreeViewItem() { Header = "Length: " + m.Fields[j].Length.ToString(), Background = Brushes.LightGray });
216+
//subSubItem.Items.Add(new TreeViewItem() { Header = "Type: " + m.Fields[j].Type });
217+
subItem.Items.Add(subSubItem);
218+
}
219+
item.Items.Add(subItem);
220+
eItem.Items.Add(item);
221+
}
222+
termTree.Items.Add(eItem);
180223
}
181224

182225
private void ItemFunc_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)

SourcepawnCondenser/SourcepawnCondenser/Condenser.cs

+10
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,16 @@ public SMDefinition Condense()
4646
continue;
4747
}
4848
}
49+
50+
if (ct.Kind == TokenKind.EnumStruct)
51+
{
52+
int newIndex = ConsumeSMEnumStruct();
53+
if (newIndex != -1)
54+
{
55+
position = newIndex + 1;
56+
continue;
57+
}
58+
}
4959
if (ct.Kind == TokenKind.Enum)
5060
{
5161
int newIndex = ConsumeSMEnum();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,303 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using SourcepawnCondenser.SourcemodDefinition;
5+
using SourcepawnCondenser.Tokenizer;
6+
7+
namespace SourcepawnCondenser
8+
{
9+
public partial class Condenser
10+
{
11+
private int ConsumeSMEnumStruct()
12+
{
13+
var startIndex = t[position].Index;
14+
var iteratePosition = position + 1;
15+
if (position + 4 < length)
16+
{
17+
var enumStructName = string.Empty;
18+
var methods = new List<SMEnumStructMethod>();
19+
var fields = new List<SMEnumStructField>();
20+
if (t[iteratePosition].Kind == TokenKind.Identifier)
21+
{
22+
enumStructName = t[iteratePosition++].Value;
23+
}
24+
25+
var enteredBlock = false;
26+
var braceIndex = 0;
27+
var lastIndex = -1;
28+
for (; iteratePosition < length; ++iteratePosition)
29+
if (t[iteratePosition].Kind == TokenKind.BraceOpen)
30+
{
31+
++braceIndex;
32+
enteredBlock = true;
33+
}
34+
else if (t[iteratePosition].Kind == TokenKind.BraceClose)
35+
{
36+
--braceIndex;
37+
if (braceIndex <= 0)
38+
{
39+
lastIndex = iteratePosition;
40+
break;
41+
}
42+
}
43+
else if (enteredBlock)
44+
{
45+
if (t[iteratePosition].Kind == TokenKind.FunctionIndicator ||
46+
(t[iteratePosition].Kind == TokenKind.Identifier &&
47+
t[iteratePosition + 1].Kind == TokenKind.Identifier &&
48+
t[iteratePosition + 2].Kind == TokenKind.ParenthesisOpen) ||
49+
(t[iteratePosition].Kind == TokenKind.Identifier &&
50+
t[iteratePosition + 1].Kind == TokenKind.ParenthesisOpen))
51+
{
52+
var mStartIndex = t[iteratePosition].Index;
53+
var functionCommentString = string.Empty;
54+
var commentTokenIndex = BacktraceTestForToken(iteratePosition - 1,
55+
TokenKind.MultiLineComment, true, false);
56+
if (commentTokenIndex == -1)
57+
{
58+
commentTokenIndex = BacktraceTestForToken(iteratePosition - 1,
59+
TokenKind.SingleLineComment, true, false);
60+
if (commentTokenIndex != -1)
61+
{
62+
var strBuilder = new StringBuilder(t[commentTokenIndex].Value);
63+
while ((commentTokenIndex = BacktraceTestForToken(commentTokenIndex - 1,
64+
TokenKind.SingleLineComment, true, false)) != -1)
65+
{
66+
strBuilder.Insert(0, Environment.NewLine);
67+
strBuilder.Insert(0, t[commentTokenIndex].Value);
68+
}
69+
70+
functionCommentString = strBuilder.ToString();
71+
}
72+
}
73+
else
74+
{
75+
functionCommentString = t[commentTokenIndex].Value;
76+
}
77+
78+
var mEndIndex = mStartIndex;
79+
var functionIndicators = new List<string>();
80+
var parameters = new List<string>();
81+
var methodName = string.Empty;
82+
var methodReturnValue = string.Empty;
83+
var ParsingIndicators = true;
84+
var InCodeSection = false;
85+
var ParenthesisIndex = 0;
86+
var mBraceIndex = 0;
87+
var AwaitingName = true;
88+
var lastFoundParam = string.Empty;
89+
var foundCurentParameter = false;
90+
var InSearchForComma = false;
91+
for (var i = iteratePosition; i < length; ++i)
92+
if (InCodeSection)
93+
{
94+
if (t[i].Kind == TokenKind.BraceOpen)
95+
{
96+
++mBraceIndex;
97+
}
98+
else if (t[i].Kind == TokenKind.BraceClose)
99+
{
100+
--mBraceIndex;
101+
if (mBraceIndex <= 0)
102+
{
103+
iteratePosition = i;
104+
break;
105+
}
106+
}
107+
}
108+
else
109+
{
110+
if (ParsingIndicators)
111+
{
112+
if (t[i].Kind == TokenKind.FunctionIndicator)
113+
{
114+
functionIndicators.Add(t[i].Value);
115+
continue;
116+
}
117+
118+
ParsingIndicators = false;
119+
}
120+
121+
if (t[i].Kind == TokenKind.Identifier && AwaitingName)
122+
{
123+
if (i + 1 < length)
124+
{
125+
if (t[i + 1].Kind == TokenKind.Identifier)
126+
{
127+
methodReturnValue = t[i].Value;
128+
methodName = t[i + 1].Value;
129+
++i;
130+
}
131+
else
132+
{
133+
methodName = t[i].Value;
134+
}
135+
136+
AwaitingName = false;
137+
}
138+
139+
continue;
140+
}
141+
142+
if (t[i].Kind == TokenKind.ParenthesisOpen)
143+
{
144+
++ParenthesisIndex;
145+
continue;
146+
}
147+
148+
if (t[i].Kind == TokenKind.ParenthesisClose)
149+
{
150+
--ParenthesisIndex;
151+
if (ParenthesisIndex == 0)
152+
{
153+
if (foundCurentParameter)
154+
{
155+
parameters.Add(lastFoundParam);
156+
lastFoundParam = string.Empty;
157+
}
158+
159+
InCodeSection = true;
160+
if (i + 1 < length)
161+
{
162+
if (t[i + 1].Kind == TokenKind.Semicolon)
163+
{
164+
iteratePosition = i + 1;
165+
mEndIndex = t[i + 1].Index;
166+
break;
167+
}
168+
169+
iteratePosition = i;
170+
mEndIndex = t[i].Index;
171+
}
172+
}
173+
174+
continue;
175+
}
176+
177+
if (t[i].Kind == TokenKind.Identifier && !InSearchForComma)
178+
{
179+
lastFoundParam = t[i].Value;
180+
foundCurentParameter = true;
181+
continue;
182+
}
183+
184+
if (t[i].Kind == TokenKind.Comma)
185+
{
186+
parameters.Add(lastFoundParam);
187+
lastFoundParam = string.Empty;
188+
InSearchForComma = false;
189+
}
190+
else if (t[i].Kind == TokenKind.Assignment)
191+
{
192+
InSearchForComma = true;
193+
}
194+
}
195+
196+
if (mStartIndex < mEndIndex)
197+
methods.Add(new SMEnumStructMethod
198+
{
199+
Index = mStartIndex, Name = methodName, ReturnType = methodReturnValue,
200+
MethodKind = functionIndicators.ToArray(),
201+
Parameters = parameters.ToArray(),
202+
FullName = TrimFullname(source.Substring(mStartIndex, mEndIndex - mStartIndex + 1)),
203+
Length = mEndIndex - mStartIndex + 1,
204+
CommentString = TrimComments(functionCommentString), MethodmapName = enumStructName,
205+
File = FileName
206+
});
207+
}
208+
else if (t[iteratePosition].Kind == TokenKind.Identifier)
209+
{
210+
var fStartIndex = t[iteratePosition].Index;
211+
var fEndIndex = fStartIndex;
212+
if (iteratePosition - 1 >= 0)
213+
if (t[iteratePosition - 1].Kind == TokenKind.FunctionIndicator)
214+
fStartIndex = t[iteratePosition - 1].Index;
215+
var fieldName = string.Empty;
216+
var InPureSemicolonSearch = false;
217+
var fBracketIndex = 0;
218+
for (var j = iteratePosition; j < length; ++j)
219+
{
220+
if (t[j].Kind == TokenKind.Identifier && !InPureSemicolonSearch)
221+
{
222+
fieldName = t[j].Value;
223+
continue;
224+
}
225+
226+
if (t[j].Kind == TokenKind.Assignment)
227+
{
228+
InPureSemicolonSearch = true;
229+
continue;
230+
}
231+
232+
if (t[j].Kind == TokenKind.Semicolon)
233+
if (fStartIndex == fEndIndex && fBracketIndex == 0)
234+
{
235+
iteratePosition = j;
236+
fEndIndex = t[j].Index;
237+
break;
238+
}
239+
240+
if (t[j].Kind == TokenKind.BraceOpen)
241+
{
242+
if (!InPureSemicolonSearch)
243+
{
244+
InPureSemicolonSearch = true;
245+
fEndIndex = t[j].Index - 1;
246+
}
247+
248+
++fBracketIndex;
249+
}
250+
else if (t[j].Kind == TokenKind.BraceClose)
251+
{
252+
--fBracketIndex;
253+
if (fBracketIndex == 0)
254+
{
255+
if (j + 1 < length)
256+
{
257+
if (t[j + 1].Kind == TokenKind.Semicolon)
258+
iteratePosition = j + 1;
259+
else
260+
iteratePosition = j;
261+
}
262+
263+
break;
264+
}
265+
}
266+
}
267+
268+
if (fStartIndex < fEndIndex)
269+
fields.Add(new SMEnumStructField
270+
{
271+
Index = fStartIndex,
272+
Length = fEndIndex - fStartIndex + 1,
273+
Name = fieldName,
274+
File = FileName,
275+
MethodmapName = enumStructName,
276+
FullName = source.Substring(fStartIndex, fEndIndex - fStartIndex + 1)
277+
});
278+
}
279+
}
280+
281+
if (enteredBlock && braceIndex == 0)
282+
{
283+
var mm = new SMEnumStruct
284+
{
285+
Index = startIndex, Length = t[lastIndex].Index - startIndex + 1, Name = enumStructName,
286+
File = FileName,
287+
};
288+
mm.Methods.AddRange(methods);
289+
mm.Fields.AddRange(fields);
290+
def.EnumStructs.Add(mm);
291+
position = lastIndex;
292+
}
293+
}
294+
295+
return -1;
296+
}
297+
298+
private int ConsumeSMFunctionStruct()
299+
{
300+
return -1;
301+
}
302+
}
303+
}

0 commit comments

Comments
 (0)