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
|
/*
parser/parser.cpp
This file is part of KSieve,
the KDE internet mail/usenet news message filtering library.
Copyright (c) 2002-2003 Marc Mutz <mutz@kde.org>
KSieve is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
KSieve 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
In addition, as a special exception, the copyright holders give
permission to link the code of this program with any edition of
the TQt library by Trolltech AS, Norway (or with modified versions
of TQt that use the same license as TQt), and distribute linked
combinations including the two. You must obey the GNU General
Public License in all respects for all of the code used other than
TQt. If you modify this file, you may extend this exception to
your version of the file, but you are not obligated to do so. If
you do not wish to do so, delete this exception statement from
your version.
*/
#include <config.h>
#include <ksieve/parser.h>
#include <impl/parser.h>
#include <ksieve/error.h>
#include <tqstring.h>
#include <assert.h>
#include <limits.h> // ULONG_MAX
#include <ctype.h> // isdigit
namespace KSieve {
//
//
// Parser Bridge implementation
//
//
Parser::Parser( const char * scursor, const char * const send, int options )
: i( 0 )
{
i = new Impl( scursor, send, options );
}
Parser::~Parser() {
delete i; i = 0;
}
void Parser::setScriptBuilder( ScriptBuilder * builder ) {
assert( i );
i->mBuilder = builder;
}
ScriptBuilder * Parser::scriptBuilder() const {
assert( i );
return i->mBuilder;
}
const Error & Parser::error() const {
assert( i );
return i->error();
}
bool Parser::parse() {
assert( i );
return i->parse();
}
}
static inline unsigned long factorForQuantifier( char ch ) {
switch ( ch ) {
case 'g':
case 'G':
return 1024*1024*1024;
case 'm':
case 'M':
return 1024*1024;
case 'k':
case 'K':
return 1024;
default:
assert( 0 ); // lexer should prohibit this
return 1; // make compiler happy
}
}
static inline bool willOverflowULong( unsigned long result, unsigned long add ) {
static const unsigned long maxULongByTen = (unsigned long)(ULONG_MAX / 10.0) ;
return result > maxULongByTen || ULONG_MAX - 10 * result < add ;
}
namespace KSieve {
//
//
// Parser Implementation
//
//
Parser::Impl::Impl( const char * scursor, const char * const send, int options )
: mToken( Lexer::None ),
lexer( scursor, send, options ),
mBuilder( 0 )
{
}
bool Parser::Impl::isStringToken() const {
return token() == Lexer::QuotedString ||
token() == Lexer::MultiLineString ;
}
bool Parser::Impl::isArgumentToken() const {
return isStringToken() ||
(token() == Lexer::Number) ||
(token() == Lexer::Tag) ||
((token() == Lexer::Special) && (mTokenValue == "[")) ;
}
bool Parser::Impl::obtainToken() {
while ( !mToken && !lexer.atEnd() && !lexer.error() ) {
mToken = lexer.nextToken( mTokenValue );
if ( lexer.error() )
break;
// comments and line feeds are semantically invisible and may
// appear anywhere, so we handle them here centrally:
switch ( token() ) {
case Lexer::HashComment:
if ( scriptBuilder() )
scriptBuilder()->hashComment( tokenValue() );
consumeToken();
break;
case Lexer::BracketComment:
if ( scriptBuilder() )
scriptBuilder()->bracketComment( tokenValue() );
consumeToken();
break;
case Lexer::LineFeeds:
for ( unsigned int i = 0, end = tokenValue().toUInt() ; i < end ; ++i )
if ( scriptBuilder() ) // better check every iteration, b/c
// we call out to ScriptBuilder,
// where nasty things might happen!
scriptBuilder()->lineFeed();
consumeToken();
break;
default: ; // make compiler happy
}
}
if ( lexer.error() && scriptBuilder() )
scriptBuilder()->error( lexer.error() );
return !lexer.error();
}
bool Parser::Impl::parse() {
// this is the entry point: START := command-list
if ( !parseCommandList() )
return false;
if ( !atEnd() ) {
makeUnexpectedTokenError( Error::ExpectedCommand );
return false;
}
if ( scriptBuilder() )
scriptBuilder()->finished();
return true;
}
bool Parser::Impl::parseCommandList() {
// our ABNF:
// command-list := *comand
while ( !atEnd() ) {
if ( !obtainToken() )
return false;
if ( token() == Lexer::None )
continue;
if ( token() != Lexer::Identifier )
return true;
if ( !parseCommand() ) {
assert( error() );
return false;
}
}
return true;
}
bool Parser::Impl::parseCommand() {
// command := identifier arguments ( ";" / block )
// arguments := *argument [ test / test-list ]
// block := "{" *command "}"
// our ABNF:
// block := "{" [ command-list ] "}"
if ( atEnd() )
return false;
//
// identifier
//
if ( !obtainToken() || token() != Lexer::Identifier )
return false;
if ( scriptBuilder() )
scriptBuilder()->commandStart( tokenValue() );
consumeToken();
//
// *argument
//
if ( !obtainToken() )
return false;
if ( atEnd() ) {
makeError( Error::MissingSemicolonOrBlock );
return false;
}
if ( isArgumentToken() && !parseArgumentList() ) {
assert( error() );
return false;
}
//
// test / test-list
//
if ( !obtainToken() )
return false;
if ( atEnd() ) {
makeError( Error::MissingSemicolonOrBlock );
return false;
}
if ( token() == Lexer::Special && tokenValue() == "(" ) { // test-list
if ( !parseTestList() ) {
assert( error() );
return false;
}
} else if ( token() == Lexer::Identifier ) { // should be test:
if ( !parseTest() ) {
assert( error() );
return false;
}
}
//
// ";" / block
//
if ( !obtainToken() )
return false;
if ( atEnd() ) {
makeError( Error::MissingSemicolonOrBlock );
return false;
}
if ( token() != Lexer::Special ) {
makeUnexpectedTokenError( Error::ExpectedBlockOrSemicolon );
return false;
}
if ( tokenValue() == ";" )
consumeToken();
else if ( tokenValue() == "{" ) { // block
if ( !parseBlock() )
return false; // it's an error since we saw '{'
} else {
makeError( Error::MissingSemicolonOrBlock );
return false;
}
if ( scriptBuilder() )
scriptBuilder()->commandEnd();
return true;
}
bool Parser::Impl::parseArgumentList() {
// our ABNF:
// argument-list := *argument
while ( !atEnd() ) {
if ( !obtainToken() )
return false;
if ( !isArgumentToken() )
return true;
if ( !parseArgument() )
return !error();
}
return true;
}
bool Parser::Impl::parseArgument() {
// argument := string-list / number / tag
if ( !obtainToken() || atEnd() )
return false;
if ( token() == Lexer::Number ) {
if ( !parseNumber() ) {
assert( error() );
return false;
}
return true;
} else if ( token() == Lexer::Tag ) {
if ( scriptBuilder() )
scriptBuilder()->taggedArgument( tokenValue() );
consumeToken();
return true;
} else if ( isStringToken() ) {
if ( scriptBuilder() )
scriptBuilder()->stringArgument( tokenValue(), token() == Lexer::MultiLineString, TQString() );
consumeToken();
return true;
} else if ( token() == Lexer::Special && tokenValue() == "[" ) {
if ( !parseStringList() ) {
assert( error() );
return false;
}
return true;
}
return false;
}
bool Parser::Impl::parseTestList() {
// test-list := "(" test *("," test) ")"
if ( !obtainToken() || atEnd() )
return false;
if ( token() != Lexer::Special || tokenValue() != "(" )
return false;
if ( scriptBuilder() )
scriptBuilder()->testListStart();
consumeToken();
// generic while/switch construct for comma-separated lists. See
// parseStringList() for another one. Any fix here is like to apply there, too.
bool lastWasComma = true;
while ( !atEnd() ) {
if ( !obtainToken() )
return false;
switch ( token() ) {
case Lexer::None:
break;
case Lexer::Special:
assert( tokenValue().length() == 1 );
assert( tokenValue()[0].latin1() );
switch ( tokenValue()[0].latin1() ) {
case ')':
consumeToken();
if ( lastWasComma ) {
makeError( Error::ConsecutiveCommasInTestList );
return false;
}
if ( scriptBuilder() )
scriptBuilder()->testListEnd();
return true;
case ',':
consumeToken();
if( lastWasComma ) {
makeError( Error::ConsecutiveCommasInTestList );
return false;
}
lastWasComma = true;
break;
default:
makeError( Error::NonStringInStringList );
return false;
}
break;
case Lexer::Identifier:
if ( !lastWasComma ) {
makeError( Error::MissingCommaInTestList );
return false;
} else {
lastWasComma = false;
if ( !parseTest() ) {
assert( error() );
return false;
}
}
break;
default:
makeUnexpectedTokenError( Error::NonTestInTestList );
return false;
}
}
makeError( Error::PrematureEndOfTestList );
return false;
}
bool Parser::Impl::parseTest() {
// test := identifier arguments
// arguments := *argument [ test / test-list ]
//
// identifier
//
if ( !obtainToken() || atEnd() )
return false;
if ( token() != Lexer::Identifier )
return false;
if ( scriptBuilder() )
scriptBuilder()->testStart( tokenValue() );
consumeToken();
//
// *argument
//
if ( !obtainToken() )
return false;
if ( atEnd() ) // a test w/o args
goto TestEnd;
if ( isArgumentToken() && !parseArgumentList() ) {
assert( error() );
return false;
}
//
// test / test-list
//
if ( !obtainToken() )
return false;
if ( atEnd() ) // a test w/o nested tests
goto TestEnd;
if ( token() == Lexer::Special && tokenValue() == "(" ) { // test-list
if ( !parseTestList() ) {
assert( error() );
return false;
}
} else if ( token() == Lexer::Identifier ) { // should be test:
if ( !parseTest() ) {
assert( error() );
return false;
}
}
TestEnd:
if ( scriptBuilder() )
scriptBuilder()->testEnd();
return true;
}
bool Parser::Impl::parseBlock() {
// our ABNF:
// block := "{" [ command-list ] "}"
if ( !obtainToken() || atEnd() )
return false;
if ( token() != Lexer::Special || tokenValue() != "{" )
return false;
if ( scriptBuilder() )
scriptBuilder()->blockStart();
consumeToken();
if ( !obtainToken() )
return false;
if ( atEnd() ) {
makeError( Error::PrematureEndOfBlock );
return false;
}
if ( token() == Lexer::Identifier ) {
if ( !parseCommandList() ) {
assert( error() );
return false;
}
}
if ( !obtainToken() )
return false;
if ( atEnd() ) {
makeError( Error::PrematureEndOfBlock );
return false;
}
if ( token() != Lexer::Special || tokenValue() != "}" ) {
makeError( Error::NonCommandInCommandList );
return false;
}
if ( scriptBuilder() )
scriptBuilder()->blockEnd();
consumeToken();
return true;
}
bool Parser::Impl::parseStringList() {
// string-list := "[" string *("," string) "]" / string
// ;; if there is only a single string, the brackets are optional
//
// However, since strings are already handled separately from
// string lists in parseArgument(), our ABNF is modified to:
// string-list := "[" string *("," string) "]"
if ( !obtainToken() || atEnd() )
return false;
if ( token() != Lexer::Special || tokenValue() != "[" )
return false;
if ( scriptBuilder() )
scriptBuilder()->stringListArgumentStart();
consumeToken();
// generic while/switch construct for comma-separated lists. See
// parseTestList() for another one. Any fix here is like to apply there, too.
bool lastWasComma = true;
while ( !atEnd() ) {
if ( !obtainToken() )
return false;
switch ( token() ) {
case Lexer::None:
break;
case Lexer::Special:
assert( tokenValue().length() == 1 );
switch ( tokenValue()[0].latin1() ) {
case ']':
consumeToken();
if ( lastWasComma ) {
makeError( Error::ConsecutiveCommasInStringList );
return false;
}
if ( scriptBuilder() )
scriptBuilder()->stringListArgumentEnd();
return true;
case ',':
consumeToken();
if ( lastWasComma ) {
makeError( Error::ConsecutiveCommasInStringList );
return false;
}
lastWasComma = true;
break;
default:
makeError( Error::NonStringInStringList );
return false;
}
break;
case Lexer::QuotedString:
case Lexer::MultiLineString:
if ( !lastWasComma ) {
makeError( Error::MissingCommaInStringList );
return false;
}
lastWasComma = false;
if ( scriptBuilder() )
scriptBuilder()->stringListEntry( tokenValue(), token() == Lexer::MultiLineString, TQString() );
consumeToken();
break;
default:
makeError( Error::NonStringInStringList );
return false;
}
}
makeError( Error::PrematureEndOfStringList );
return false;
}
bool Parser::Impl::parseNumber() {
// The lexer returns the number including the quantifier as a
// single token value. Here, we split is an check that the number
// is not out of range:
if ( !obtainToken() || atEnd() )
return false;
if ( token() != Lexer::Number )
return false;
// number:
unsigned long result = 0;
unsigned int i = 0;
const TQCString s = tokenValue().latin1();
for ( const unsigned int len = s.length() ; i < len && isdigit( s[i] ) ; ++i ) {
const unsigned long digitValue = s[i] - '0' ;
if ( willOverflowULong( result, digitValue ) ) {
makeError( Error::NumberOutOfRange );
return false;
} else {
result *= 10 ; result += digitValue ;
}
}
// optional quantifier:
char quantifier = '\0';
if ( i < s.length() ) {
assert( i + 1 == s.length() );
quantifier = s[i];
const unsigned long factor = factorForQuantifier( quantifier );
if ( result > double(ULONG_MAX) / double(factor) ) {
makeError( Error::NumberOutOfRange );
return false;
}
result *= factor;
}
if ( scriptBuilder() )
scriptBuilder()->numberArgument( result, quantifier );
consumeToken();
return true;
}
} // namespace KSieve
|