summaryrefslogtreecommitdiffstats
path: root/src/kvirc/kvs/kvi_kvs_parser_dollar.cpp
blob: 027fd7f04c3e767c62bcff25c9d39a8dc973e147 (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
324
325
326
327
328
329
330
331
//=============================================================================
//
//   File : kvi_kvs_parser_dollar.cpp
//   Creation date : Thu 5 Oct 2003 20.20 CEST by Szymon Stefanek
//
//   This file is part of the KVirc irc client distribution
//   Copyright (C) 2003 Szymon Stefanek (pragma at kvirc dot net)
//
//   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 opinion) 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.
//
//=============================================================================

#define __KVIRC__


#include "kvi_kvs_parser.h"
#include "kvi_kvs_treenode.h"

#include "kvi_kvs_report.h"
#include "kvi_kvs_kernel.h"

#include "kvi_kvs_parser_macros.h"

#include "kvi_locale.h"




KviKvsTreeNodeData * KviKvsParser::parseDollar(bool bInObjScope)
{
	KVSP_ASSERT(KVSP_curCharUnicode == '$');

	const TQChar * pDollarBegin = KVSP_curCharPointer;
	const TQChar * pBegin = KVSP_curCharPointer;

	KVSP_skipChar;

	if(!KVSP_curCharIsFunctionStart)
	{
		if(KVSP_curCharUnicode == 0)warning(KVSP_curCharPointer,__tr2qs("Unexpected end of script after '$' function call prefix"));
		else warning(KVSP_curCharPointer,__tr2qs("Unexpected character %q (unicode %x) after '$' function call prefix"),KVSP_curCharPointer,KVSP_curCharUnicode);
		error(KVSP_curCharPointer,__tr2qs("Syntax error after '$' function call prefix. If you want to use a plain '$' in the code you need to escape it"));
		return 0;
	}

	if(KVSP_curCharUnicode == '(')
	{
		// expression eval
		if(bInObjScope)
		{
			error(KVSP_curCharPointer,__tr2qs("Invalid expression evaluation in object scope"));
			return 0;
		}

		KVSP_skipChar;
		skipSpaces();

		return parseExpression(')');
	}

	if(KVSP_curCharUnicode == '{')
	{
		// command block eval <--- senseless ???
		if(bInObjScope)
		{
			error(KVSP_curCharPointer,__tr2qs("Invalid command evaluation in object scope"));
			return 0;
		}

		KviKvsTreeNodeInstruction * i = parseInstructionBlock();
		if(!i)
		{
			if(error())return 0;
			// trigger an error anyway: this is abused syntax :D
			error(KVSP_curCharPointer,__tr2qs("Empty instruction block for command evaluation"));
			return 0;
		}

		return new KviKvsTreeNodeCommandEvaluation(pDollarBegin,i);
	}

	if(KVSP_curCharIsNumber)
	{
		// this is a parameter identifier
		// $1-4 $1- $3

		if(bInObjScope)
		{
			error(KVSP_curCharPointer,__tr2qs("Parameter identifiers are forbidden in object scope (after the '->' operator)"));
			return 0;
		}

		pBegin = KVSP_curCharPointer;

		while(KVSP_curCharIsNumber)
			KVSP_skipChar;

		TQString szNum1(pBegin,KVSP_curCharPointer - pBegin);
		bool bOk;
		int iNum1 = szNum1.toInt(&bOk);
		if(!bOk)tqDebug("Ops... a non-number made by numbers ?");

		if(KVSP_curCharUnicode != '-')
		{
			// end
			return new KviKvsTreeNodeSingleParameterIdentifier(pDollarBegin,iNum1);
		}

		// dash... make sure it's not $N->$something
		KVSP_skipChar;
		if(KVSP_curCharUnicode == '>')
		{
			// object scope operator in fact...
			// go back to the - and return a single parameter identifier
			KVSP_backChar;
			return new KviKvsTreeNodeSingleParameterIdentifier(pDollarBegin,iNum1);
		}

		if(!KVSP_curCharIsNumber)
		{
			// from iNum1 to the end
			return new KviKvsTreeNodeMultipleParameterIdentifier(pDollarBegin,iNum1,-1);
		}

		pBegin = KVSP_curCharPointer;
		while(KVSP_curCharIsNumber)
			KVSP_skipChar;

		TQString szNum2(pBegin,KVSP_curCharPointer - pBegin);
		int iNum2 = szNum2.toInt(&bOk);
		if(!bOk)tqDebug("Ops... a non-number made by numbers (2) ?");

		if(iNum1 < iNum2)return new KviKvsTreeNodeMultipleParameterIdentifier(pDollarBegin,iNum1,iNum2);
		else {
			warning(pBegin,__tr2qs("Ending index of a multiple parameter identifier is lower or equal to the starting index. This will evaluate to a single parameter identifier."));
			return new KviKvsTreeNodeSingleParameterIdentifier(pDollarBegin,iNum1);
		}
	}

	pBegin = KVSP_curCharPointer;

	//KVSP_skipChar;

	bool bHasNamespaceNotInObjScopeSoMustBeAlias = false; // ;D
	bool bIsThis = false;
	
	if(KVSP_curCharUnicode == '$')
	{
		if(bInObjScope)
		{
			error(KVSP_curCharPointer,__tr2qs("Syntax error: invalid $$ ($this) function call in object scope"));
			return 0;
		}
		// handle $$
		KVSP_skipChar;
	} else {
		while((KVSP_curCharIsLetterOrNumber) || (KVSP_curCharUnicode == '_'))KVSP_skipChar;
		if(!bInObjScope)
		{
			while(KVSP_curCharUnicode == ':')
			{
				// check for namespaces
	
				// here we allow the syntax of the form
				// <namespace>::{<namespace>::}<alias_name>
		
				bHasNamespaceNotInObjScopeSoMustBeAlias = true; // ;D
		
				KVSP_skipChar;
				if(KVSP_curCharUnicode == ':')
				{
					KVSP_skipChar;
		
					if(!KVSP_curCharIsLetter)
					{
						warning(KVSP_curCharPointer - 1,__tr2qs("Stray '::' sequence or invalid following alias name"));
						error(KVSP_curCharPointer,__tr2qs("Syntax error: malformed alias function call identifier"));
						return 0;
					}
		
					KVSP_skipChar;
					while(KVSP_curCharIsLetterOrNumber || (KVSP_curCharUnicode == '_'))KVSP_skipChar;
				} else {
					warning(KVSP_curCharPointer - 1,__tr2qs("Stray ':' character: did you mean '...<namespace>::<alias_name>' ?"));
					error(KVSP_curCharPointer,__tr2qs("Syntax error: malformed (alias?) function call identifier"));
					return 0;
				}
			}
		}
	}

	TQString szIdentifier1(pBegin,KVSP_curCharPointer - pBegin);

	const TQChar * pId2 = 0;
	int iId2Len = 0;
	bool bModuleFunctionCall = false;

	if(!bHasNamespaceNotInObjScopeSoMustBeAlias)
	{
		if(!bInObjScope)
		{
			if(KVSP_curCharUnicode == '.')
			{
				KVSP_skipChar;
				if(KVSP_curCharIsLetter)
				{
					pId2 = KVSP_curCharPointer;
					while((KVSP_curCharIsLetterOrNumber) || (KVSP_curCharUnicode == '_'))
						KVSP_skipChar;
					iId2Len = KVSP_curCharPointer - pId2;
					bModuleFunctionCall = true;
				} else {
					KVSP_backChar;
				}
			}
		} else {
			// object scope, check for "class name" namespace
			// the class name namespace has the format "<namespace>::<namespace>::..::<classname>
			// so the last :: is the delimiter of the function name
			const TQChar * pOriginalEndOfId1 = KVSP_curCharPointer;
			const TQChar * pEndOfId1 = pOriginalEndOfId1;
			
			while(KVSP_curCharUnicode == ':')
			{
				const TQChar * pEndOfId1 = KVSP_curCharPointer;
				// base class function call ?
				KVSP_skipChar;
				if(KVSP_curCharUnicode == ':')
				{
					KVSP_skipChar;
					if(KVSP_curCharIsLetter)
					{
						pId2 = KVSP_curCharPointer;
						while((KVSP_curCharIsLetterOrNumber) || (KVSP_curCharUnicode == '_'))
							KVSP_skipChar;
						iId2Len = KVSP_curCharPointer - pId2;
					} else {
						KVSP_setCurCharPointer(pOriginalEndOfId1);
						pId2 = 0;
						iId2Len = 0;
						break;
					}
				} else {
					KVSP_setCurCharPointer(pOriginalEndOfId1);
					pId2 = 0;
					iId2Len = 0;
					break;
				}
			}
			if(pId2)
			{
				// yes, that's fine: reset it
				szIdentifier1.setUnicode(pBegin,pEndOfId1 - pBegin);
			} 
		}
	}

	KviKvsTreeNodeDataList * l;

	if(KVSP_curCharUnicode != '(')
	{
		// no parameters passed
		//KVSP_setCurCharPointer(pBegin);
		// will get an empty data list
		l = new KviKvsTreeNodeDataList(pDollarBegin);
	} else {
		// check for the special syntax ()
		KVSP_skipChar;
		if(KVSP_curCharUnicode == ')')
		{
			// $call(), assume no parameters passed
			l = new KviKvsTreeNodeDataList(pDollarBegin);
			KVSP_skipChar;
		} else {
			KVSP_backChar;
			l = parseCommaSeparatedParameterList();
			if(!l)return 0; // error
		}
	}

	if(bHasNamespaceNotInObjScopeSoMustBeAlias)
	{
		// namespace::alias function call
		return new KviKvsTreeNodeAliasFunctionCall(pDollarBegin,szIdentifier1,l);
	} else if(bModuleFunctionCall)
	{
		// module function call
		return new KviKvsTreeNodeModuleFunctionCall(pDollarBegin,szIdentifier1,TQString(pId2,iId2Len),l);
	} else {
		
		if(bInObjScope)
		{
			// object function call (our parent will be a scope operator)
			if(pId2)
			{
				// base class object function call
				return new KviKvsTreeNodeBaseObjectFunctionCall(pDollarBegin,szIdentifier1,TQString(pId2,iId2Len),l);
			} else {
				// plain object function call
				return new KviKvsTreeNodeThisObjectFunctionCall(pDollarBegin,szIdentifier1,l);
			}
		} else {
			// core or alias function call
			KviKvsCoreFunctionExecRoutine * r = KviKvsKernel::instance()->findCoreFunctionExecRoutine(szIdentifier1);
			if(r)
			{
				// core function call
				return new KviKvsTreeNodeCoreFunctionCall(pDollarBegin,szIdentifier1,r,l);
			} else {
				// root namespace alias function call
				return new KviKvsTreeNodeAliasFunctionCall(pDollarBegin,szIdentifier1,l);
			}
		}
	}

	// not reached
	KVSP_ASSERT(false);
	return 0;
}