summaryrefslogtreecommitdiffstats
path: root/src/kvirc/kvs/kvi_kvs_parser_lside.cpp
blob: d6437c7da129c936b165b6c81749a2183543f421 (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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
//=============================================================================
//
//   File : kvi_kvs_parser_lside.cpp
//   Creation date : Thu 03 Nov 2003 13.11 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"


/*
	@doc: operators
	@title:
		Operators
	@keyterms:
		operator,operators,assignment,assign
	@type:
		language
	@short:
		Variable operators , assignments & co.
	@body:
		Operator constructs are commands just like the other ones.
		All the operators work on local or global variables and dictionaries.[br]
		The generic operator syntax is:[br]
		[br]
		[b]<left_operand> <operator> [right_operand][/b][br]
		[br]
		where <left_operand> and [right_operand] depend on the <operator>.[br]
		Some operators have no [right_operand] and these are called [b]unary operators[/b]:
		they operate directly on <left_operand>.[br]

		[br]
		[big]= (Assignment)[/big][br]
		[br]

		The assignment is the "plainest" of the binary operators: it works just like in any other programming language.[br]
		[br]
		If the <left_operand> is a variable, then the <right_operand> is assigned to it;
		if the variable doesn't exists yet , it is created.[br]
		If <right_operand> evaluates to an empty value then the variable is unset.[br]
		[example]
		[comment]# Assigning a constant to the global variable %Tmp[/comment]
		%Tmp = 1
		[comment]# Assigning a string constant to the global variable %Tmp[/comment]
		%Tmp = some string
		[comment]# Assigning a string constant to the local variable %tmp[/comment]
		%tmp = "some string with whitespace &nbsp; &nbsp; &nbsp; &nbsp; preserved"
		[comment]# Assigning a variable to another variable copies its contents[/comment]
		%tmp = %somevariable
		[comment]# Assigning a variable string to the global variable %Z[/comment]
		%Z = my eyes are %color
		[comment]# Assigning a variable string (with a function call inside) to the local variable %x[/comment]
		%x = the system os is [fnc]$system.osname[/fnc]
		[comment]# Assigning an empty string to the local variable %y unsets %y[/comment]
		%y =
		[comment]# This is equivalent to the above[/comment]
		%y = ""
		[comment]# This is equivalent too, if $function evalutates to an empty string[/comment]
		%y = $function()
		[/example]
		[br]
		If the <left_operand> is a dictionary/array entry, then the <right_operand> is assigned to it;
		if the dictionary/array entry doesn't exist (or the whole dictionary/array doesn't exists) it is created.[br]
		If <right_operand> evaluates to an empty value then the dictionary/array entry (and eventually the whole
		dictionary/array, if there are no other entries) is unset.[br]
		[example]
		[comment]# Assigning a variable string to a global dictionary entry[/comment]
		%Dict[key] = [fnc]$system.osname[/fnc]\ian
		[comment]# Unsetting a local dictionary entry[/comment]
		%mydict[23] = ""
		[/example]
		[br]
		If the <left_operand> is an array reference then the semantics depend on the <right_operand> type.
		If <right_operand> is an array reference then its contents are copied to the <left_operand>.[br]
		If <right_operand> is a dictionary (keys) reference then its values are copied to to <left_operand>.[br]
		If <right_operand> is a scalar then the value is assigned to every entry of <left_operand>.[br]
		This is an easy way of unsetting a whole array: just assign an empty string.[br]
		If the <left_operand> is a dictionary reference then the semantics depend on the <right_operand> type.
		If <right_operand> is a dictionary reference then its contents are copied to the <left_operand>.[br]
		If <right_operand> is an array reference then its contents are copied to to <left_operand> using
		the array indexes as keys.[br]
		If <right_operand> is a dictionary key reference then the keys are copied to the <left_operand>
		using numeric indexes starting from 0 as keys.[br]
		If <right_operand> is a scalar then the value is assigned to every key of <left_operand>.[br]
		This is an easy way of unsetting a whole dictionary: just assign an empty string to all its keys.[br]
		(If you play with huge dictionaries/arrays it might be a good idea to unset them when no longer needed)
		[example]
		[comment]# Assigning a dictionary to another: %mydict[] becomes a copy of %anotherdict[][/comment]
		%mydict[] = %anotherdict[]
		[comment]# %mydict[] gets the values of the dict returned by $features[/comment]
		%mydict[] = [fnc]$features[/fnc]
		[comment]# Assigning a string to ALL the keys of %mydict[/comment]
		%mydict[] = "some default value"
		[comment]# Unsetting a whole dictionary[/comment]
		%mydict[] =
		%AnotherGlobalDict[] = ""
		[/example]


		[br]
		[big]=~ (Binding operator)[/big][br]
		[br]

		This operator is a really ugly, poor and clueless attempt to reach at least 1% of the
		power of the perl =~ operator :D[br]
		It allows some complex string operations to be performed efficently by operating directly
		on the left operand (in fact this is a lot faster in KVIrc since at least one step of parsing is skipped).[br]
		Its basic syntax is:[br]
		[b]<left_operand> =~ <operation>[parameters][/b][br]
		Where <operation> may be one of 't','s' and parameters depend on it.[br]
		<left_operand> is the target of the <operation>.[br]
		If <left_operand> is an array or dictionary, the <operation> is executed on each item they contain.[br]
		Operation 't' is the transliteration.[br]
		The complete syntax with parameters is:[br]
		[b]<left_operand> =~ t/<search characters>/<replacement characters>/[/b][br]
		where <search characters> is a string of characters that are replaced with the corresponding
		characters in <replacement characters>.[br]
		This operation can be also named 'y' or 'tr' (to preserve some compatibility with other languages).[br]
		[example]
		%A=This is a test string
		echo %A
		%A=~ tr/abcdefghi/ABCDEFGHI/
		echo %A
		[/example]
		Operation 's' is the substitution.[br]
		The complete syntax with parameters is:[br]
		[b]<left_operand> =~ s/<search pattern>/<replacement pattern>/[flags][/b][br]
		where <search pattern> is an extended regular expression to be matched in the <left_operand>
		and <replacement string> is a special pattern that will replace any occurence found.[br]
		<search pattern> may contain tqparentheses to capture parts of the matched text.
		<replacement string> can contain the escape sequences \\N where N is a number between 1 and 9
		to be replaced by the captured text.[br]
		(We use \\N because KVIrc will first unquote the string when parsing...)[br]
		\\0 is a special escape that will be replaced by the entire match (is always valid!).[br]
		WARNING: the "capture-text" feature is not available if KVIrc has been compiled
		with qt older than 3.0.0. You can find out if the feature is available by
		looking for the string "TQt3" in the array returned by [fnc]$features[/fnc].[br]
		[flags] may be a combination of the letters 'g','i' and 'w'.[br]
		'g' causes the search to be global and not stop after the first occurence of <search pattern>.[br]
		'i' causes the search to be case insensitive.[br]
		'w' causes the search pattern to be interpreted as a simple wildcard regular expression.[br]
		'm' causes the matching to be "minimal" instead of "greedy" (default). Greedy matches
		find the longest possible match in the string while minimal (non-greedy) matches the shortest possible.[br]
		[example]
		%A=This is a test string
		echo %A
		%A=~ s/([a-z])i([a-z])/\\1I\\2/
		echo %A
		%A=~ s/([a-z])i([a-z])/\\1@\\2/gi
		echo %A
		[/example]

		[br]
		[big]X= (Arithmetic Self-operators)[/big][br]
		[br]

		The general syntax is:[br]
		[b]<left_operand> <operation> <right_operand>[/b][br]
		Where <left_operand> and <right_operand> must evaluate to numbers.[br]
		All these operators perform the operation on <left_operand> and <right_operand> and then
		store the result in <left_operand> (which therefore must be a variable, an array entry or a dictionary entry).[br]
		<operation> may be one of:[br]
		+= : sums the <right_operand> to <left_operand>[br]
		-= : subtracts <right_operand> from <left_operand>[br]
		*= : multiplies <left_operand> by <right_operand>[br]
		%= : calculates <left_operand> modulus <right_operand>[br]
		|= : calculates <left_operand> bitwise-or <right_operand>[br]
		&= : calculates <left_operand> bitwise-and <right_operand>[br]
		/= : divides <left_operand> by <right_operand>[br]

		[br]
		[big]++ and -- (Increment and Decrement)[/big][br]
		[br]

		These two operators work only on numeric operands.[br]
		The general syntax is:[br]
		[b]<left_operand> <operator>[/b][br]
		There is no <right_operand>.[br]
		++ increments <left_operand> by one, -- decrements <left_operand> by one.[br]
		These are equivalent to += 1 and -= 1.[br]

		[br]
		[big].= , << , <+ , <, (String concatenation operators)[/big][br]
		[br]

		All these operators work also on whole arrays and dictionaries.[br]
		Operator [b].=[/b] : APPENDS the <right_operand> to the <left_operand>[br]
		Operator [b]<+[/b] is a synonim for .= (backward compatibility)[br]
		Operator [b]<<[/b] : appends <right_operand> to <left_operand>
		separating the two strings by a single space if and only if <left_operand> and <right_operand>
		are non-empty.[br]
		Operator [b]<,[/b] : is similar to '<<' ; appends , separating with a single ',' with the same condition.[br]

	@examples:
		First set the variable %var
		[example]
		%var = Ciao ciao
		[/example]
		Then append a nickname...
		[example]
		%var << Pragma
		[/example]
		%var now tqcontains "Ciao ciao Pragma"[br]
		Append a '!' character
		[example]
		%var <+ !
		[/example]
		%var now tqcontains "Ciao ciao Pragma!"
		Now reset it.
		[example]
		%var =
		[/example]
		Now %var is unset.[br]
		Reset it with a comma separated list of items
		[example]
		%var = Pragma,Diabl0,Arter|o	
		%var <, MalboroLi
		[/example]
		%var now tqcontains "Pragma,Diabl0,Arter|o,MalboroLi"[br]
		[br]
		Now a longer example.
		[example]
		%var = l
		[cmd]echo[/cmd] It's name starts with the letter %var!
		%var &lt;+ inux
		[cmd]echo[/cmd] Yes , it is %var!
		%var &lt;&lt; OS
		[cmd]echo[/cmd] Use %var!
		%var &lt;, Mac OS
		[cmd]echo[/cmd] There are two items in this list : %var
		%var = [fnc]$strlen[/fnc](%var)
		[cmd]echo[/cmd] And it is %var characters long (including the comma)
		%var--
		[cmd]echo[/cmd] Excluding the comma : %var
		%var+=%var
		[cmd]echo[/cmd] Now it is doubled : %var
		%var =
		[cmd]echo[/cmd] Now the var is unset (empty): (%var) !
		[/example]
*/


KviKvsTreeNodeData * KviKvsParser::parseOperationRightSide(bool bPreferNumeric)
{
	KviPointerList<KviKvsTreeNodeData> * l = new KviPointerList<KviKvsTreeNodeData>();
	l->setAutoDelete(true);

	const TQChar * pBegin = KVSP_curCharPointer;
	
	for(;;)
	{
		switch(KVSP_curCharUnicode)
		{
			case 0:
				goto end_of_the_param;
			break;
			case '\n':
			case '\r':
			case ';':
				KVSP_skipChar;
				goto end_of_the_param;
			break;
			case ' ':
			case '\t':
				skipSpaces();
				if(KVSP_curCharIsEndOfCommand)
				{
					goto end_of_the_param;
				} else {
					// separate by single spaces
					bPreferNumeric = false; // this can't be a number
					l->append(new KviKvsTreeNodeConstantData(KVSP_curCharPointer,new KviKvsVariant(TQString(" "))));
				}
			break;
			default:
				// anything else is a parameter
				KviKvsTreeNodeData * p = parseCommandParameter(bPreferNumeric);
				if(!p)
				{
					// this is an error
					delete l;
					return 0;
				}
				l->append(p);
			break;
		}
	}

end_of_the_param:
	if(l->count() > 1)
	{
		// complex parameter needed
		return new KviKvsTreeNodeCompositeData(pBegin,l);
	} else {
		if(l->count() > 0)
		{
			// a single parameter in the list
			l->setAutoDelete(false);
			KviKvsTreeNodeData * p = l->first();
			delete l;
			return p;
		} else {
			// empty (this should NEVER happen anyway)
			delete l;
			return new KviKvsTreeNodeConstantData(pBegin,new KviKvsVariant(TQString("")));
		}
	}
	// never reached
	return 0;
}

/*
	@doc: assignment
	@title:
		Assignment operation
	@keyterms:
		assignment
	@type:
		language
	@short:
		Assignment operation
	@body:
		The assignment is the "plainest" of the operators: it works just like in any other programming language.[br]
		The syntax is:[br]
		[br]
		[b]<target> = <source>[/b]
		[br]
		<target> must be a variable, <source> can be any parameter.[br]
		If the <target> variable doesn't exist, it is created.
		If it already exists, it is eventually converted to the type of <souce> (scalar, hash or array).[br]
		If <source> evaluates to an empty value then the <target> variable is unset.[br]
		[example]
		[comment]# Assigning a constant to the variable %Tmp[/comment]
		%Tmp = 1
		[cmd]echo[/cmd] %Tmp
		[comment]# Assigning a string constant to the variable %Tmp[/comment]
		%Tmp = some string
		[cmd]echo[/cmd] %Tmp
		[comment]# Assigning a string constant to the variable %Tmp[/comment]
		%Tmp = "some string with whitespace &nbsp; &nbsp; &nbsp; &nbsp; preserved"
		[cmd]echo[/cmd] %Tmp
		[comment]# Assigning a variable to another variable copies its contents[/comment]
		%Someothervariable = "Contents"
		%Tmp = %Someothervariable
		[cmd]echo[/cmd] %Tmp
		[comment]# Assigning a variable string to the variable %z[/comment]
		%color = blue
		%z = my eyes are %color
		[cmd]echo[/cmd] %z
		[comment]# Assigning a variable string (with a function call inside) to the variable %x[/comment]
		%x = the system os is [fnc]$system.osname[/fnc]
		[cmd]echo[/cmd] %x
		[comment]# Assigning an empty string to the local variable %y unsets %y[/comment]
		%x =
		[cmd]echo[/cmd] %y
		[comment]# This is equivalent to the above[/comment]
		%y = ""
		[comment]# This is equivalent too, if $function evalutates to an empty string[/comment]
		%y = $function()
		[comment]# Assigning a variable string to a hash entry[/comment]
		%Dict{key} = [fnc]$system.osname[/fnc]\ian
		[comment]# Unsetting an array entry[/comment]
		%mydict[23] = ""
		[comment]# Assigning a hash to another: %mydict[] becomes a copy of %anotherdict[][/comment]
		%anotherdict{"The key"} = "Some dummy value"
		%mydict = %anotherdict
		[cmd]echo[/cmd]%mydict{"The key"}
		[comment]# This will convert %mydict to be a scalar variable (deleting all the %mydict contents!)[/comment]
		%mydict = "some default value"
		[comment]# Unsetting a whole hash[/comment]
		%anotherdict =
		[/example]
		[/p]
*/


/*
	@doc: incrementdecrement
	@title:
		Increment and decrement operations
	@keyterms:
		increment, decrement
	@type:
		language
	@short:
		Increment and decrement operations
	@body:
		These two operators work only on numeric operands.[br]
		The syntax is:[br]
		[br]
		[b]<target>++[/b][br]
		[b]<target>--[/b][br]
		[br]
		++ increments <target> by one, -- decrements <target> by one.[br]
		These are equivalent to += 1 and -= 1.[br]
		<target> must be an existing variable and contain an integer value.[br]
		If <target> contains a real value then the real is truncated to the nearest
		integer and then incremented or decremented.[br]
	@examples:
		[example]
		%a=10
		[cmd]echo[/cmd] "Incrementing"
		[cmd]while[/cmd](%a < 20)
		{
			[cmd]echo[/cmd] %a
			[b]%a++[/b]
		}
		[cmd]echo[/cmd] "Decrementing"
		[cmd]while[/cmd](%a > 10)
		{
			[cmd]echo[/cmd] %a
			[b]%a--[/b]
		}
		[cmd]echo[/cmd] "Testing for loop"
		[cmd]for[/cmd](%a=0;%a < 10;[b]%a++[/b])
		{
			[cmd]echo[/cmd] %a
		}
		[example]
	@seealso:
		[doc:operators]Operators[/doc]
*/


/*
	@doc: selfarithmetic
	@title:
		Arithmetic self-operators
	@type:
		language
	@short:
		Arithmetic self-operators
	@body:
		These operators work only on numeric operands.[br]
		The syntax is:[br]
		[br]
		[b]<target> += <right_operand>[/b][br]
		[b]<target> -= <right_operand>[/b][br]
		[b]<target> *= <right_operand>[/b][br]
		[b]<target> /= <right_operand>[/b][br]
		[b]<target> %= <right_operand>[/b][br]
		[br]
		<target> must be an existing variable and contain a numeric value.
		<right_operand> must evaluate to a numeric value.
		Note that if you want <right_operand> to be a result of an expression, you must
		enclose it in the $(*) expression evaluation call.[br]
		Operator += sums the <right_operand> value to the <target> value and stores the result in <target>.[br]
		Operator -= subtracts <right_operand> from <target> and stores the result in <target>.[br]
		Operator *= multiplies <target> by <right_operand> and stores the result in <target>.[br]
		Operator /= divides <target> by <right_operand> and stores the result in <target>.[br]
		Operator %= computes <target> modulus <right_operand> and stores the result in <target>.[br]
		The division and modulus operators fail with an error if <right_operand> is 0.[br]
		If both <target> and <right_operand> are integer values then the results of the division
		and modulus are integers (truncated for the division).[br]
		If <target> or <right_operand> or both are floating point values then the result is a floating point value.[br]
	@examples:
		[example]
			%a=10
			[cmd]echo[/cmd] %a
			%a+=20
			[cmd]echo[/cmd] %a
			%a-=$(%a - 1)
			[cmd]echo[/cmd] %a
			%a *= 10
			[cmd]echo[/cmd] %a
			%a /= 21
			[cmd]echo[/cmd] %a
			%a *= 20
			[cmd]echo[/cmd] %a
			%a /= 21.0
			[cmd]echo[/cmd] %a
			%b = 10.0
			%a %= %b
			[cmd]echo[/cmd] %a
			%a = 10
			%b = 3
			[comment]# nice trick[/comment]
			%a /= %b.0
			[cmd]echo[/cmd] %a
		[example]
	@seealso:
		[doc:operators]Operators[/doc]
*/


/*
	@doc: selfbitwise
	@title:
		Bitwise self-operators
	@type:
		language
	@short:
		Bitwise self-operators
	@body:
		These operators work only on integer operands.[br]
		The syntax is:[br]
		[br]
		[b]<target> |= <right_operand>[/b][br]
		[b]<target> &= <right_operand>[/b][br]
		[b]<target> ^= <right_operand>[/b][br]
		[b]<target> >>= <right_operand>[/b][br]
		[b]<target> <<= <right_operand>[/b][br]
		[br]
		<target> must be an existing variable and contain a numeric value.
		<right_operand> must evaluate to a numeric value.
		If <target> or <right_operand> are floating point values then they are truncated
		and converted to integers.[br]
		Note that if you want <right_operand> to be a result of an expression, you must
		enclose it in the $(*) expression evaluation call.[br]
		Operator |= computes <target> bitwise-or <right_operand> and stores the result in <target>.[br]
		Operator &= computes <target> bitwise-and <right_operand> and stores the result in <target>.[br]
		Operator ^= computes <target> bitwise-xor <right_operand> and stores the result in <target>.[br]
		Operator >>= shifts <target> <right_operand> bits to the right and stores the result int <target>.[br]
		Operator <<= shifts <target> <right_operand> bits to the left and stores the result int <target>.[br]
		Note that "!=" is not available. You must use %a = $(!%b) to implement it.[br]
		For operators >>= and <<= <right_operand> must be a positive integer.[br]
	@examples:
		[example]
			%a = 1
			[cmd]echo[/cmd] %a
			%a |= 2
			[cmd]echo[/cmd] %a
			%a &= 2
			[cmd]echo[/cmd] %a
			%a ^= 1
			[cmd]echo[/cmd] %a
			%a >>= 2
			[cmd]echo[/cmd] %a
			%a <<= 1
			[cmd]echo[/cmd] %a
		[example]
	@seealso:
		[doc:operators]Operators[/doc]
*/


/*
	@doc: stringconcatenation
	@title:
		String concatenation operators
	@type:
		language
	@short:
		String concatenation operators
	@body:
		These operators concatenate strings.
		The syntax is:[br]
		[br]
		[b]<target> .= <right_operand>[/b][br]
		[b]<target> << <right_operand>[/b][br]
		[b]<target> <, <right_operand>[/b][br]
		[br]
		Operator .= appends <right_operand> to <target>.
		Operator << appends a space followed by <right_operand> to <target> if <target> is non empty,
		otherwise sets <target> to <right_operand>.
		Operator <, is similar to << but uses a comma to separate the two variable contents.
		The last two operators are useful in creating space-separated or comma-separated lists.
	@examples:
		[example]
			%a = ""
			%a << free
			[cmd]echo[/cmd] %a
			%a .= bsd
			[cmd]echo[/cmd] %a
			%a << rox
			[cmd]echo[/cmd] %a
			%a <, but linux is better!
			[cmd]echo[/cmd] %a
		[example]
	@seealso:
		[doc:operators]Operators[/doc]
*/


/*
	@doc: arrayconcatenation
	@title:
		Array concatenation operator
	@type:
		language
	@short:
		Array concatenation operator
	@body:
		This operator concatenates arrays
		The syntax is:[br]
		[br]
		[b]<target> <+ <right_operand>[/b][br]
		[br]
		If <target> is not an array, it is converted to one first.
		After that, if <right_operand> is a scalar then it is appended
		to the end of the <target> array. If <right_operand> is an array
		then all of its items are appended to the end of the <target> array.
		If <right_operand> is a hash then all of its value items
		are appended to the end of the <target> array.
	@seealso:
		[doc:operators]Operators[/doc]
*/



/*
	@doc: binding
	@title:
		Binding operator
	@type:
		language
	@short:
		Binding operator
	@body:
		This operator is a really ugly, poor and clueless attempt to reach at least 1% of the
		power of the perl =~ operator :D[br]
		It allows some complex string operations to be performed efficently by operating directly
		on the left operand (in fact this is a lot faster in KVIrc since at least one step of parsing is skipped).[br]
		Its basic syntax is:[br]
		[b]<left_operand> =~ <operation>[parameters][/b][br]
		Where <operation> may be one of 't','s' and parameters depend on it.[br]
		<left_operand> is the target of the <operation>.[br]
		If <left_operand> is an array or dictionary, the <operation> is executed on each item they contain.[br]
		Operation 't' is the transliteration.[br]
		The complete syntax with parameters is:[br]
		[b]<left_operand> =~ t/<search characters>/<replacement characters>/[/b][br]
		where <search characters> is a string of characters that are replaced with the corresponding
		characters in <replacement characters>.[br]
		This operation can be also named 'y' or 'tr' (to preserve some compatibility with other languages).[br]
		[example]
		%A=This is a test string
		echo %A
		%A=~ tr/abcdefghi/ABCDEFGHI/
		echo %A
		[/example]
		Operation 's' is the substitution.[br]
		The complete syntax with parameters is:[br]
		[b]<left_operand> =~ s/<search pattern>/<replacement pattern>/[flags][/b][br]
		where <search pattern> is an extended regular expression to be matched in the <left_operand>
		and <replacement string> is a special pattern that will replace any occurence found.[br]
		<search pattern> may contain tqparentheses to capture parts of the matched text.
		<replacement string> can contain the escape sequences \\N where N is a number between 1 and 9
		to be replaced by the captured text.[br]
		(We use \\N because KVIrc will first unquote the string when parsing...)[br]
		\\0 is a special escape that will be replaced by the entire match (is always valid!).[br]
		[flags] may be a combination of the letters 'g','i' and 'w'.[br]
		'g' causes the search to be global and not stop after the first occurence of <search pattern>.[br]
		'i' causes the search to be case insensitive.[br]
		'w' causes the search pattern to be interpreted as a simple wildcard regular expression.[br]
		[example]
		%A=This is a test string
		echo %A
		%A=~ s/([a-z])i([a-z])/\\1I\\2/
		echo %A
		%A=~ s/([a-z])i([a-z])/\\1@\\2/gi
		echo %A
		[/example]
	@examples:
		[example]
			%a = ""
			%a << free
			[cmd]echo[/cmd] %a
			%a .= bsd
			[cmd]echo[/cmd] %a
			%a << rox
			[cmd]echo[/cmd] %a
			%a <, but linux is better!
			[cmd]echo[/cmd] %a
		[example]
	@seealso:
		[doc:operators]Operators[/doc]
*/



/*
	@doc: operators
	@title:
		Operators
	@keyterms:
		operator,operators,assignment
	@type:
		language
	@short:
		Variable operators , assignments & co.
	@body:
		[p]
		Operator constructs are commands just like the other ones.
		All the operators work on local or global variables.[br]
		The generic operator syntax is:[br]
		[br]
		&nbsp; &nbsp; &nbsp; &nbsp; [b]<left_operand> <operator> [right_operand][/b][br]
		[br]
		where <left_operand> is a variable and [right_operand] is a variable , a constant or a complex expression.[br]
		Some operators do not use [right_operand] and do their job directly on <left_operand>[br]
		[/p]

		[table]
			[tr][td]Operator[/td][td]document[/td][/td]
			[tr][td]=[/td][td][doc:assignment]assignment operator[/doc][/td][/tr]
			[tr][td]++[/td][td][doc:incrementdecrement]Increment and decrement operators[/doc][/td][/tr]
			[tr][td]--[/td][td][doc:incrementdecrement]Increment and decrement operators[/doc][/td][/tr]
			[tr][td]+=[/td][td][doc:selfarithmetic]Arithmetic self-operators[/doc][/td][/tr]
			[tr][td]-=[/td][td][doc:selfarithmetic]Arithmetic self-operators[/doc][/td][/tr]
			[tr][td]*=[/td][td][doc:selfarithmetic]Arithmetic self-operators[/doc][/td][/tr]
			[tr][td]/=[/td][td][doc:selfarithmetic]Arithmetic self-operators[/doc][/td][/tr]
			[tr][td]%=[/td][td][doc:selfarithmetic]Arithmetic self-operators[/doc][/td][/tr]
			[tr][td]|=[/td][td][doc:selfbitwise]Bitwise self-operators[/doc][/td][/tr]
			[tr][td]&=[/td][td][doc:selfbitwise]Bitwise self-operators[/doc][/td][/tr]
			[tr][td]^=[/td][td][doc:selfbitwise]Bitwise self-operators[/doc][/td][/tr]
			[tr][td]<<=[/td][td][doc:selfbitwise]Bitwise self-operators[/doc][/td][/tr]
			[tr][td]>>=[/td][td][doc:selfbitwise]Bitwise self-operators[/doc][/td][/tr]
			[tr][td].=[/td][td][doc:stringconcatenation]String concatenation operators[/doc][/td][/tr]
			[tr][td]<<[/td][td][doc:stringconcatenation]String concatenation operators[/doc][/td][/tr]
			[tr][td]<,[/td][td][doc:stringconcatenation]String concatenation operators[/doc][/td][/tr]
			[tr][td]<+[/td][td][doc:arrayconcatenation]Array concatenation[/doc][/td][/tr]
			[tr][td]=~[/td][td][doc:binding]Binding operator[/doc][/td][/tr]
		[/table]
	*/

KviKvsTreeNodeData * KviKvsParser::parseBindingOperationParameter()
{
	KviPointerList<KviKvsTreeNodeData> * l = new KviPointerList<KviKvsTreeNodeData>;
	l->setAutoDelete(true);

	const TQChar * pBegin = KVSP_curCharPointer;

	for(;;)
	{
		switch(KVSP_curCharUnicode)
		{
			case 0:
			case '/':
			case '\n':
			case '\r':
				// not a part of a parameter
				goto end_of_function_parameter;
			break;
			case '$':
			case '%':
			{
				// this may be a data reference
				KviKvsTreeNodeData * p = parseParameterPercentOrDollar();
				if(!p)
				{
					// this is an error
					delete l;
					return 0;
				}
				l->append(p);
			}
			break;
			case '"':
			{
				// this is a string
				KviKvsTreeNodeData * p = parseStringParameter();
				if(!p)
				{
					// this is an error
					delete l;
					return 0;
				}
				l->append(p);
			}
			break;
			default:
			{
				// anything else is a literal
				l->append(parseBindingOperationLiteralParameter());
			}
			break;
		}
	}
end_of_function_parameter:
	if(l->count() > 1)
	{
		// complex parameter needed
		return new KviKvsTreeNodeCompositeData(pBegin,l);
	} else {
		// a single parameter in the list or empty list at all
		l->setAutoDelete(false);
		KviKvsTreeNodeData * p = l->first();
		delete l;
		if(!p)p = new KviKvsTreeNodeConstantData(KVSP_curCharPointer,new KviKvsVariant(TQString("")));
		return p;
	}
	// never reached
	return 0;
}



KviKvsTreeNodeOperation * KviKvsParser::parseBindingOperation()
{
	// t or tr or y
	// s
	const TQChar * pBegin = KVSP_curCharPointer;
	
	while(KVSP_curCharIsLetter)KVSP_skipChar;
	
	TQString szOp = TQString(pBegin,KVSP_curCharPointer - pBegin).lower();

	skipSpaces();
	
	if(KVSP_curCharUnicode != '/')
	{
		error(KVSP_curCharPointer,__tr2qs("Found character '%q' (tqunicode %x) where a slash '/' was expected"),KVSP_curCharPointer,KVSP_curCharUnicode);
		return 0;
	}

	KVSP_skipChar;

	KviKvsTreeNodeData * pFirst = parseBindingOperationParameter();
	if(!pFirst)return 0;
	
	if(KVSP_curCharIsEndOfCommand)
	{
		error(KVSP_curCharPointer,__tr2qs("Unexpected end of command in binding operation, at least two slashes are missing"));
		delete pFirst;
		return 0;
	}

	if(KVSP_curCharUnicode != '/')
	{
		error(KVSP_curCharPointer,__tr2qs("Found character '%q' (tqunicode %x) where a slash '/' was expected"),KVSP_curCharPointer,KVSP_curCharUnicode);
		delete pFirst;
		return 0;
	}

	KVSP_skipChar;

	KviKvsTreeNodeData * pSecond = parseBindingOperationParameter();
	if(!pSecond)
	{
		delete pFirst;
		return 0;
	}

	if(KVSP_curCharIsEndOfCommand)
	{
		error(KVSP_curCharPointer,__tr2qs("Unexpected end of command in binding operation, at least one slash is missing"));
		delete pFirst;
		return 0;
	}

	if(KVSP_curCharUnicode != '/')
	{
		error(KVSP_curCharPointer,__tr2qs("Found character '%q' (tqunicode %x) where a slash '/' was expected"),KVSP_curCharPointer,KVSP_curCharUnicode);
		delete pFirst;
		return 0;
	}

	KVSP_skipChar;
	
	KviKvsTreeNodeData * pThird = parseCommandParameter();
	if(!pThird)
	{
		if(error())
		{
			delete pFirst;
			delete pSecond;
			return 0;
		}

		pThird = new KviKvsTreeNodeConstantData(KVSP_curCharPointer,new KviKvsVariant(TQString("")));
	}
	
	while(!KVSP_curCharIsEndOfCommand)KVSP_skipChar;
	if(!KVSP_curCharIsEndOfBuffer)KVSP_skipChar;

	if((szOp == "t") || (szOp == "tr") || (szOp == "y"))
	{
		// transliteration  tr/szFirst/szSecond/szFlags
		return new KviKvsTreeNodeOperationStringTransliteration(pBegin,pFirst,pSecond,pThird);
	} else if(szOp == "s")
	{
		// regexp substitution s/szFirst/szSecond/szFlags
		return new KviKvsTreeNodeOperationStringSubstitution(pBegin,pFirst,pSecond,pThird);
	}
	
	error(KVSP_curCharPointer,__tr2qs("Unknown binding operation '%Q'"),&szOp);
	return 0;
}


KviKvsTreeNodeOperation * KviKvsParser::parseOperation()
{
	// find the operator
	const TQChar * pBegin = KVSP_curCharPointer;
	
	switch(KVSP_curCharUnicode)
	{
		case '=':
		{
			KVSP_skipChar;
			if(KVSP_curCharUnicode == '~')
			{
				KVSP_skipChar;
				skipSpaces();
				if(KVSP_curCharIsEndOfCommand)
				{
					error(KVSP_curCharPointer,__tr2qs("Missing right side operand for the binding operator '=~'"));
					return 0;
				}
				return parseBindingOperation();
			} else {
				skipSpaces();
				KviKvsTreeNodeData * d = parseOperationRightSide(true);
				if(!d)return 0; // error
				return new KviKvsTreeNodeOperationAssignment(pBegin,d);
			}
		}
		break;
		case '+':
			KVSP_skipChar;
			switch(KVSP_curCharUnicode)
			{
				case '+':
					// operator ++
					KVSP_skipChar;
					skipSpaces();
					if(!KVSP_curCharIsEndOfCommand)
					{
						warning(KVSP_curCharPointer,__tr2qs("Trailing garbage ignored after operator '++'"));
					}
					while(!KVSP_curCharIsEndOfCommand)KVSP_skipChar;
					if(!KVSP_curCharIsEndOfBuffer)KVSP_skipChar;
					return new KviKvsTreeNodeOperationIncrement(pBegin);
				break;
				case '=':
					// operator +=
					KVSP_skipChar;
					skipSpaces();
					if(KVSP_curCharIsEndOfCommand)
					{
						error(KVSP_curCharPointer,__tr2qs("Missing right operand for operator '+='"));
						return 0;
					}
					KviKvsTreeNodeData * d = parseOperationRightSide(true);
					if(!d)return 0; // error
					return new KviKvsTreeNodeOperationSelfSum(pBegin,d);
				break;
			}
		break;
		case '-':
			KVSP_skipChar;
			switch(KVSP_curCharUnicode)
			{
				case '-':
					KVSP_skipChar;
					// operator --
					skipSpaces();
					if(!KVSP_curCharIsEndOfCommand)
					{
						warning(KVSP_curCharPointer,__tr2qs("Trailing garbage ignored after operator '--'"));
					}
					while(!KVSP_curCharIsEndOfCommand)KVSP_skipChar;
					if(!KVSP_curCharIsEndOfBuffer)KVSP_skipChar;
					return new KviKvsTreeNodeOperationDecrement(pBegin);
				break;
				case '>':
					warning(KVSP_curCharPointer,__tr2qs("This looks a lot like an object handle dereferencing operator '->' but in fact it isn't. Maybe you forgot a '$' just after ?"));
				break;
				case '=':
					// operator -=
					KVSP_skipChar;
					skipSpaces();
					if(KVSP_curCharIsEndOfCommand)
					{
						error(KVSP_curCharPointer,__tr2qs("Missing right operand for operator '-='"));
						return 0;
					}
					KviKvsTreeNodeData * d = parseOperationRightSide(true);
					if(!d)return 0; // error
					return new KviKvsTreeNodeOperationSelfSubtraction(pBegin,d);
				break;
			}
		break;
		case '<':
			KVSP_skipChar;
			switch(KVSP_curCharUnicode)
			{
				case '<':
					KVSP_skipChar;
					if(KVSP_curCharUnicode == '=')
					{
						KVSP_skipChar;
						skipSpaces();
						if(KVSP_curCharIsEndOfCommand)
						{
							error(KVSP_curCharPointer,__tr2qs("Missing right operand for operator '<<='"));
							return 0;
						}
						KviKvsTreeNodeData * d = parseOperationRightSide(true);
						if(!d)return 0; // error
						return new KviKvsTreeNodeOperationSelfShl(pBegin,d);
					} else {
						skipSpaces();
						if(KVSP_curCharIsEndOfCommand)
						{
							error(KVSP_curCharPointer,__tr2qs("Missing right operand for operator '<<'"));
							return 0;
						}
						KviKvsTreeNodeData * d = parseOperationRightSide();
						if(!d)return 0; // error
						return new KviKvsTreeNodeOperationStringAppendWithSpace(pBegin,d);
					}
				break;
				case ',':
				{
					KVSP_skipChar;
					skipSpaces();
					if(KVSP_curCharIsEndOfCommand)
					{
						error(KVSP_curCharPointer,__tr2qs("Missing right operand for operator '<,'"));
						return 0;
					}
					KviKvsTreeNodeData * d = parseOperationRightSide();
					if(!d)return 0; // error
					return new KviKvsTreeNodeOperationStringAppendWithComma(pBegin,d);
				}
				break;
				case '+':
				{
					KVSP_skipChar;
					skipSpaces();
					if(KVSP_curCharIsEndOfCommand)
					{
						error(KVSP_curCharPointer,__tr2qs("Missing right operand for operator '<+'"));
						return 0;
					}
					KviKvsTreeNodeData * d = parseOperationRightSide();
					if(!d)return 0; // error
					return new KviKvsTreeNodeOperationArrayAppend(pBegin,d);
				}
				break;
			}
		break;
		case '>':
			KVSP_skipChar;
			switch(KVSP_curCharUnicode)
			{
				case '>':
					KVSP_skipChar;
					if(KVSP_curCharUnicode == '=')
					{
						KVSP_skipChar;
						skipSpaces();
						if(KVSP_curCharIsEndOfCommand)
						{
							error(KVSP_curCharPointer,__tr2qs("Missing right operand for operator '>>='"));
							return 0;
						}
						KviKvsTreeNodeData * d = parseOperationRightSide(true);
						if(!d)return 0; // error
						return new KviKvsTreeNodeOperationSelfShr(pBegin,d);
					}
				break;
			}
		break;
		case '.':
			KVSP_skipChar;
			switch(KVSP_curCharUnicode)
			{
				case '=':
					KVSP_skipChar;
					skipSpaces();
					if(KVSP_curCharIsEndOfCommand)
					{
						error(KVSP_curCharPointer,__tr2qs("Missing right operand for operator '.='"));
						return 0;
					}
					KviKvsTreeNodeData * d = parseOperationRightSide();
					if(!d)return 0; // error
					return new KviKvsTreeNodeOperationStringAppend(pBegin,d);
				break;
			}
		break;
#define SELF_OPERATOR(__opchar,__opstr,__class) \
		case __opchar: \
			KVSP_skipChar; \
			switch(KVSP_curCharUnicode) \
			{ \
				case '=': \
					KVSP_skipChar; \
					skipSpaces(); \
					if(KVSP_curCharIsEndOfCommand) \
					{ \
						error(KVSP_curCharPointer,__tr2qs("Missing right operand for operator '" __opstr "='")); \
						return 0; \
					} \
					KviKvsTreeNodeData * d = parseOperationRightSide(true); \
					if(!d)return 0; \
					return new __class(pBegin,d); \
				break; \
			} \
		break;
		SELF_OPERATOR('*',"*",KviKvsTreeNodeOperationSelfMultiplication)
		SELF_OPERATOR('/',"/",KviKvsTreeNodeOperationSelfDivision)
		SELF_OPERATOR('%',"%",KviKvsTreeNodeOperationSelfModulus)
		SELF_OPERATOR('|',"|",KviKvsTreeNodeOperationSelfOr)
		SELF_OPERATOR('&',"&",KviKvsTreeNodeOperationSelfAnd)
		SELF_OPERATOR('^',"^",KviKvsTreeNodeOperationSelfXor)
	}

	error(pBegin,__tr2qs("Unknown operator"));
	return 0;
}

KviKvsTreeNodeInstruction * KviKvsParser::parseVoidFunctionCallOrOperation()
{
	KVSP_ASSERT((KVSP_curCharUnicode == '$') || (KVSP_curCharUnicode == '%') || (KVSP_curCharUnicode == '@'));

	const TQChar * pBegin = KVSP_curCharPointer;

	KviKvsTreeNodeData * r = parsePercentOrDollar();

	if(!r)
	{
		// must be an error
		return 0;
	}

	skipSpaces();

	if(KVSP_curCharIsEndOfCommand)
	{
		// the end of the command
		if(!r->isFunctionCall())
		{
			if(r->isReadOnly())
			{
				warning(pBegin,__tr2qs("Unexpected (and senseless) read-only data evaluation"));
				error(KVSP_curCharPointer,__tr2qs("Syntax error: confused by earlier errors: bailing out"));
			} else {
				error(KVSP_curCharPointer,__tr2qs("Unexpected end of script after a variable reference: expected operator"));
			}
			delete r;
			return 0;
		} else {
			if(!KVSP_curCharIsEndOfBuffer)KVSP_skipChar;
			return new KviKvsTreeNodeVoidFunctionCall(r->location(),(KviKvsTreeNodeFunctionCall *)r);
		}
	}

	// not the end of a command : an operation
	if(r->isReadOnly())
	{
		// must be followed by the end of a command
		if(r->isFunctionCall())
		{
			error(KVSP_curCharPointer,__tr2qs("Unexpected character '%q' (tqunicode %x) after a void function call: end of instruction expected"),KVSP_curCharPointer,KVSP_curCharUnicode);
		} else {
			warning(pBegin,__tr2qs("Unexpected (and senseless) read-only data evaluation"));
			warning(pBegin,__tr2qs("Unexpected character '%q' (tqunicode %x)"),KVSP_curCharPointer,KVSP_curCharUnicode);
			error(KVSP_curCharPointer,__tr2qs("Syntax error: confused by earlier errors: bailing out"));
		}
		delete r;
		return 0;
	}

	// ok.. parse the operation
	KviKvsTreeNodeOperation * op = parseOperation();
	if(!op)
	{
		delete r;
		return 0;
	}

	op->setTargetVariableReference(r);
	return op;
}