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