summaryrefslogtreecommitdiffstats
path: root/qtsharp/src/generator/Parser.cs
blob: 1639b4b61a79e796a231e88a5edd049fa0647bc6 (plain)
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
320
321
322
323
// A Qt to C# binding generator.
//
// Copyright (C) 2002  Adam Treat (manyoso@yahoo.com)
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

using System;
using System.Collections;
using System.IO;
using System.Xml;

namespace QtCSharp {

	public class Parser {

		XmlTextReader xtr;
		TQType qtype;

		public Parser (string xmlFragment)
		{
			qtype = new TQType ();
			XmlNameTable nt = new NameTable ();
			XmlNamespaceManager nsMgr = new XmlNamespaceManager (nt);
			XmlParserContext parserContext = new XmlParserContext (null, nsMgr, null, XmlSpace.None);
			xtr = new XmlTextReader (xmlFragment, XmlNodeType.Document, parserContext);
			Parse ();
			OverLoad ();
			NewTQCtors ();
		}

		public TQType GetTQType ()
		{
			return qtype;
		}

		public void Parse ()
		{
			while(xtr.Read ()) {
				if (xtr.NodeType != XmlNodeType.EndElement) {
					switch (xtr.Name) {
						case "qtype":
							ParseTQType ();
							continue;
						case "qancestor":
							ParseTQAncestor ();
							continue;
						case "qenum":
							ParseTQEnum ();
							continue;
						case "qctor":
							ParseTQCtor ();
							continue;
						case "qdctor":
							ParseTQDCtor ();
							continue;
						case "qmethod":
							ParseTQMethod ();
							continue;
						default:
							continue;
					}
				}
			}
		}

		public void NewTQCtors ()
		{
			// A ctor for inherited classes
			TQCtor _qctor = new TQCtor();
			_qctor.Name = qtype.Name;
			_qctor.Access = "internal";
			_qctor.Inherited = true;
			qtype.AddTQCtor(_qctor);

			// A ctor for type boxing
			TQCtor __qctor = new TQCtor();
			__qctor.Name = qtype.Name;
			__qctor.Access = "internal";
			__qctor.Boxer = true;
			TQParam qparam = new TQParam();
			qparam.Type = "IntPtr";
			qparam.Name = "_ptr";
			__qctor.AddCSharpParam(qparam);
			qtype.AddTQCtor(__qctor);

			// A dummy ctor
			TQCtor ___qctor = new TQCtor();
			___qctor.Name = qtype.Name;
			___qctor.Access = "internal";
			___qctor.Dummy = true;
			TQParam ___qparam = new TQParam();
			___qparam.Type = "TQNull";
			___qparam.Name = "dummy";
			___qctor.AddCSharpParam(___qparam);
			qtype.AddTQCtor(___qctor);
		}

		public void OverLoad ()
		{
			ArrayList additions = new ArrayList ();

			foreach (TQCtor qctor in qtype.TQCtors){
				foreach (TQParam pinvokeparam in qctor.PinvokeParams) {
					if (pinvokeparam.Default != null) {
						TQCtor _qctor = new TQCtor();
						_qctor.Name = qctor.Name;
						_qctor.Access = qctor.Access;
						_qctor.Overload = true;
						for (int j = 0; j < qctor.PinvokeParams.IndexOf(pinvokeparam)+1; j++) {
							_qctor.AddPinvokeParam((TQParam)qctor.PinvokeParams[j]);
							_qctor.AddPinvokeCallParam((TQParam)qctor.PinvokeCallParams[j]);
							_qctor.AddCSharpParam((TQParam)qctor.CSharpParams[j]);
							_qctor.AddOverloadParam((TQParam) (( TQParam) qctor.CSharpParams[j]).Clone());
						}
						_qctor.CSharpParams.RemoveAt(_qctor.CSharpParams.Count-1);
						//qtype.AddTQCtor(_qctor);
						additions.Add (_qctor);
					}
				}
			}

			foreach (TQCtor ctor in additions) qtype.AddTQCtor (ctor);
			additions = new ArrayList ();

			foreach (TQMethod qmethod in qtype.TQMethods){
				foreach (TQParam pinvokeparam in qmethod.PinvokeParams) {
					if (pinvokeparam.Default != null) {
						TQMethod _qmethod = new TQMethod();
						_qmethod.Name = qmethod.Name;
						_qmethod.Access = qmethod.Access;
						_qmethod.Return = qmethod.Return;
						_qmethod.Overload = true;
						for (int j = 0; j < qmethod.PinvokeParams.IndexOf(pinvokeparam)+1; j++) {
							_qmethod.AddPinvokeParam((TQParam)qmethod.PinvokeParams[j]);
							_qmethod.AddPinvokeCallParam((TQParam)qmethod.PinvokeCallParams[j]);
							_qmethod.AddCSharpParam((TQParam)qmethod.CSharpParams[j]);
							_qmethod.AddOverloadParam((TQParam) ((TQParam) qmethod.CSharpParams[j]).Clone());

						}
						_qmethod.CSharpParams.RemoveAt(_qmethod.CSharpParams.Count-1);
						//qtype.AddTQMethod(_qmethod);
						additions.Add (_qmethod);
					}
				}
			}

			foreach (TQMethod method in additions) qtype.AddTQMethod (method);
		}

		public void ParseTQType ()
		{
			if (xtr.MoveToAttribute("name")) {
				qtype.Name = xtr.Value;
			}

			if (xtr.MoveToAttribute("access")) {
				qtype.Access = xtr.Value;
			}
		}

		public void ParseTQAncestor ()
		{
			TQAncestor qancestor = new TQAncestor();
			if (xtr.MoveToAttribute("name")) {
				qancestor.Name = xtr.Value;
			}
			qtype.AddTQAncestor(qancestor);
		}

		public void ParseTQEnum ()
		{
			bool match = false;
			TQEnum qenum = new TQEnum();
			if (xtr.MoveToAttribute("name"))
				qenum.Name = xtr.Value;
			if (xtr.MoveToAttribute("access"))
				qenum.Access = xtr.Value;
			while (xtr.Read()) {
				if (xtr.Name == "qitem") {
					TQItem qitem = ParseTQItem();
					qenum.AddTQItem(qitem);
					long parse = 0;
					try {
						parse = Int64.Parse(qitem.Value);
					} catch {
					}
					if (parse >= 2147483647)
						qenum.Type = "uint";
				} else if (xtr.Name == "") {
				} else {
					break;
				}
			}
			qtype.AddTQEnum(qenum);
		}

		public void ParseTQCtor ()
		{
			bool IsEmpty = xtr.IsEmptyElement;
			TQCtor qctor = new TQCtor();
			if (xtr.MoveToAttribute("name")) {
				qctor.Name = xtr.Value;
			}
			if (xtr.MoveToAttribute("access")) {
				qctor.Access = xtr.Value;
			}
			if (xtr.MoveToAttribute("id")) {
				qctor.Id = xtr.Value;
			}
			if (!IsEmpty) {
				while (xtr.Read()) {
					if (xtr.Name == "qparam") {
						qctor.AddPinvokeParam(ParseTQParam());
						qctor.AddPinvokeCallParam(ParseTQParam());
						qctor.AddCSharpParam(ParseTQParam());
					} else if (xtr.Name == ""){
					} else {
						break;
					}
				}
			}
			qtype.AddTQCtor(qctor);
		}

		public void ParseTQDCtor ()
		{
			bool IsEmpty = xtr.IsEmptyElement;
			TQDCtor qdctor = new TQDCtor();
			if (xtr.MoveToAttribute("name")) {
				qdctor.Name = xtr.Value;
			}
			if (xtr.MoveToAttribute("access")) {
				qdctor.Access = xtr.Value;
			}
			if (!IsEmpty) {
				while (xtr.Read()) {
					if (xtr.Name == "qparam") {
						qdctor.AddTQParam(ParseTQParam());
					} else if (xtr.Name == "") {
					} else {
						break;
					}
				}
			}
			qtype.AddTQDCtor(qdctor);
		}

		public void ParseTQMethod ()
		{
			bool IsEmpty = xtr.IsEmptyElement;
			TQMethod qmethod = new TQMethod();
			if (xtr.MoveToAttribute("name")) {
				qmethod.Name = xtr.Value;
			}
			if (xtr.MoveToAttribute("access")) {
				qmethod.Access = xtr.Value;
			}
			if (xtr.MoveToAttribute("return")) {
				qmethod.Return = xtr.Value;
			}
			if (xtr.MoveToAttribute("id")) {
				qmethod.Id = xtr.Value;
			}
			if (xtr.MoveToAttribute("throttle")) {
				if (String.Compare(xtr.Value, "true", true) == 0) {
					qmethod.Throttle = true;
				}
			}
			if (!IsEmpty) {
				while (xtr.Read()) {
					if (xtr.Name == "qparam") {
						qmethod.AddPinvokeParam(ParseTQParam());
						qmethod.AddPinvokeCallParam(ParseTQParam());
						qmethod.AddCSharpParam(ParseTQParam());
					} else if (xtr.Name == ""){
					} else {
						break;
					}
				}
			}
			qtype.AddTQMethod(qmethod);
		}

		public TQItem ParseTQItem ()
		{
			TQItem qitem = new TQItem();
			if (xtr.MoveToAttribute("name")) {
				qitem.Name = xtr.Value;
			}
			if (xtr.MoveToAttribute("value")) {
				qitem.Value = xtr.Value;
			}
			return qitem;
		}

		public TQParam ParseTQParam ()
		{
			TQParam qparam = new TQParam();
			if (xtr.MoveToAttribute("type")) {
				qparam.Type = xtr.Value;
			}
			if (xtr.MoveToAttribute("name")) {
				qparam.Name = xtr.Value;
			}
			if (xtr.MoveToAttribute("default")) {
				qparam.Default = xtr.Value;
			}
			return qparam;
		}
	}
}