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
|
/* This file is part of KDevelop
Copyright (C) 2002,2003 Roberto Raggi <roberto@kdevelop.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "lexer.h"
#include "lookup.h"
#include <kdebug.h>
#include <klocale.h>
#include <tqregexp.h>
#include <tqmap.h>
#include <tqvaluelist.h>
#if defined( KDEVELOP_BGPARSER )
#include <tqthread.h>
class KDevTread: public TQThread
{
public:
static void yield()
{
msleep( 0 );
}
};
inline void qthread_yield()
{
KDevTread::yield();
}
#endif
#define CREATE_TOKEN(type, start, len) Token( (type), (start), (len), m_source )
#define ADD_TOKEN(tk) m_tokens.insert( m_size++, new Token(tk) );
using namespace std;
struct LexerData
{
typedef TQMap<TQString, TQString> Scope;
typedef TQValueList<Scope> StaticChain;
StaticChain staticChain;
void beginScope()
{
Scope scope;
staticChain.push_front( scope );
}
void endScope()
{
staticChain.pop_front();
}
void bind( const TQString& name, const TQString& value )
{
Q_ASSERT( staticChain.size() > 0 );
staticChain.front().insert( name, value );
}
bool hasBind( const TQString& name ) const
{
StaticChain::ConstIterator it = staticChain.begin();
while( it != staticChain.end() ){
const Scope& scope = *it;
++it;
if( scope.contains(name) )
return true;
}
return false;
}
TQString apply( const TQString& name ) const
{
StaticChain::ConstIterator it = staticChain.begin();
while( it != staticChain.end() ){
const Scope& scope = *it;
++it;
if( scope.contains(name) )
return scope[ name ];
}
return TQString();
}
};
Lexer::Lexer( Driver* driver )
: d( new LexerData),
m_driver( driver ),
m_recordComments( true ),
m_recordWhiteSpaces( false ),
m_skipWordsEnabled( true ),
m_preprocessorEnabled( true ),
m_reportWarnings( false ),
m_reportMessages( false )
{
m_tokens.setAutoDelete( true );
reset();
d->beginScope();
}
Lexer::~Lexer()
{
d->endScope();
delete( d );
}
void Lexer::setSource( const TQString& source )
{
reset();
m_source = source;
m_ptr = offset( 0 );
m_endPtr = offset( m_source.length() );
m_inPreproc = false;
if( !source.isEmpty() ) {
m_currentChar = m_source[0];
} else {
m_currentChar = TQChar::null;
}
tokenize();
}
int Lexer::skippedLines() const {
return m_skippedLines;
}
void Lexer::reset()
{
m_skippedLines = 0;
m_index = 0;
m_size = 0;
m_tokens.clear();
m_source = TQString();
m_ptr = 0;
m_endPtr = 0;
m_startLine = false;
m_ifLevel = 0;
m_skipping.resize( 200 );
m_skipping.fill( 0 );
m_trueTest.resize( 200 );
m_trueTest.fill( 0 );
m_currentLine = 0;
m_currentColumn = 0;
}
// ### should all be done with a "long" type IMO
int Lexer::toInt( const Token& token )
{
TQString s = token.text();
if( token.type() == Token_number_literal ){
// hex literal ?
if( s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
return s.mid( 2 ).toInt( 0, 16 );
TQString n;
int i = 0;
while( i < int(s.length()) && s[i].isDigit() )
n += s[i++];
// ### respect more prefixes and suffixes ?
return n.toInt();
} else if( token.type() == Token_char_literal ){
int i = s[0] == 'L' ? 2 : 1; // wide char ?
if( s[i] == '\\' ){
// escaped char
int c = s[i+1].tqunicode();
switch( c ) {
case '0':
return 0;
case 'n':
return '\n';
// ### more
default:
return c;
}
} else {
return s[i].tqunicode();
}
} else {
return 0;
}
}
void Lexer::getTokenPosition( const Token& token, int* line, int* col )
{
token.getStartPosition( line, col );
}
void Lexer::nextToken( Token& tk, bool stopOnNewline )
{
int op = 0;
if( m_size == (int)m_tokens.size() ){
m_tokens.resize( m_tokens.size() + 5000 + 1 );
}
readWhiteSpaces( !stopOnNewline );
int startLine = m_currentLine;
int startColumn = m_currentColumn;
TQChar ch = currentChar();
TQChar ch1 = peekChar();
if( ch.isNull() || ch.isSpace() ){
/* skip */
} else if( m_startLine && ch == '#' ){
nextChar(); // skip #
readWhiteSpaces( false ); // skip white spaces
m_startLine = false;
int start = currentPosition();
readIdentifier(); // read the directive
TQString directive = m_source.mid( start, currentPosition() - start );
handleDirective( directive );
} else if( m_startLine && m_skipping[ m_ifLevel ] ){
// skip line and continue
m_startLine = false;
int ppe = preprocessorEnabled();
setPreprocessorEnabled( false );
while( !currentChar().isNull() && currentChar() != '\n' ){
Token tok(m_source);
nextToken( tok, true );
}
++m_skippedLines;
m_startLine = true;
setPreprocessorEnabled( ppe );
return;
} else if( ch == '/' && ch1 == '/' ){
int start = currentPosition();
readLineComment();
if( recordComments() ){
tk = CREATE_TOKEN( Token_comment, start, currentPosition() - start );
tk.setStartPosition( startLine, startColumn );
tk.setEndPosition( m_currentLine, m_currentColumn );
}
} else if( ch == '/' && ch1 == '*' ){
int start = currentPosition();
nextChar( 2 );
readMultiLineComment();
if( recordComments() ){
tk = CREATE_TOKEN( Token_comment, start, currentPosition() - start );
tk.setStartPosition( startLine, startColumn );
tk.setEndPosition( m_currentLine, m_currentColumn );
}
} else if( ch == '\'' || (ch == 'L' && ch1 == '\'') ){
int start = currentPosition();
readCharLiteral();
tk = CREATE_TOKEN( Token_char_literal, start, currentPosition() - start );
tk.setStartPosition( startLine, startColumn );
tk.setEndPosition( m_currentLine, m_currentColumn );
} else if( ch == '"' ){
int start = currentPosition();
readStringLiteral();
tk = CREATE_TOKEN( Token_string_literal, start, currentPosition() - start );
tk.setStartPosition( startLine, startColumn );
tk.setEndPosition( m_currentLine, m_currentColumn );
} else if( ch.isLetter() || ch == '_' ){
int start = currentPosition();
readIdentifier();
HashedString ide = m_source.mid( start, currentPosition() - start );
int k = Lookup::find( ide );
if( k == -1 && m_preprocessorEnabled ) m_driver->usingString( ide );
if( m_preprocessorEnabled && m_driver->hasMacro(ide) &&
(k == -1 || !m_driver->macro(ide).body().isEmpty()) ){
bool preproc = m_preprocessorEnabled;
m_preprocessorEnabled = false;
d->beginScope();
int svLine = currentLine();
int svColumn = currentColumn();
Macro m = m_driver->macro( ide );
m_driver->usingMacro( m );
TQString ellipsisArg;
if( m.hasArguments() ){
int endIde = currentPosition();
readWhiteSpaces();
if( currentChar() == '(' ){
nextChar();
int argIdx = 0;
int argCount = m.argumentList().size();
while( currentChar() && argIdx<argCount ){
readWhiteSpaces();
TQString argName = m.argumentList()[ argIdx ];
bool ellipsis = argName == "...";
TQString arg = readArgument();
if( !ellipsis )
d->bind( argName, arg );
else
ellipsisArg += arg;
if( currentChar() == ',' ){
nextChar();
if( !ellipsis ){
++argIdx;
} else {
ellipsisArg += ", ";
}
} else if( currentChar() == ')' ){
break;
}
}
if( currentChar() == ')' ){
// valid macro
nextChar();
}
} else {
tk = CREATE_TOKEN( Token_identifier, start, endIde - start );
tk.setStartPosition( svLine, svColumn );
tk.setEndPosition( svLine, svColumn + (endIde - start) );
m_startLine = false;
d->endScope(); // OPS!!
m_preprocessorEnabled = preproc;
return;
}
}
int argsEndAtLine = currentLine();
int argsEndAtColumn = currentColumn();
#if defined( KDEVELOP_BGPARSER )
qthread_yield();
#endif
insertCurrent( m.body() );
// tokenize the macro body
TQString textToInsert;
setEndPtr( offset( currentPosition() + m.body().length() ) );
while( currentChar() ){
readWhiteSpaces();
Token tok(m_source);
nextToken( tok );
bool stringify = !m_inPreproc && tok == '#';
bool merge = !m_inPreproc && tok == Token_concat;
if( stringify || merge )
nextToken( tok );
if( tok == Token_eof )
break;
TQString tokText = tok.text();
HashedString str = (tok == Token_identifier && d->hasBind(tokText)) ? d->apply( tokText ) : tokText;
if( str == ide ){
//Problem p( i18n("unsafe use of macro '%1', macro is ignored").tqarg(ide.str()), m_currentLine, m_currentColumn, Problem::Level_Warning );
//m_driver->addProblem( m_driver->currentFileName(), p );
m_driver->removeMacro( ide );
// str = TQString();
}
if( stringify ) {
textToInsert.append( TQString::fromLatin1("\"") + str.str() + TQString::fromLatin1("\" ") );
} else if( merge ){
textToInsert.truncate( textToInsert.length() - 1 );
textToInsert.append( str.str() + TQString::fromLatin1(" ") );
} else if( tok == Token_ellipsis && d->hasBind("...") ){
textToInsert.append( ellipsisArg );
} else {
textToInsert.append( str.str() + TQString::fromLatin1(" ") );
}
}
#if defined( KDEVELOP_BGPARSER )
qthread_yield();
#endif
insertCurrent( textToInsert ); //also corrects the end-pointer
d->endScope();
m_preprocessorEnabled = preproc;
//m_driver->addMacro( m );
m_currentLine = argsEndAtLine;
m_currentColumn = argsEndAtColumn;
} else if( k != -1 ){
tk = CREATE_TOKEN( k, start, currentPosition() - start );
tk.setStartPosition( startLine, startColumn );
tk.setEndPosition( m_currentLine, m_currentColumn );
} else if( m_skipWordsEnabled ){
__gnu_cxx::hash_map< HashedString, TQPair<SkipType, TQString> >::iterator pos = m_words.find( ide );
if( pos != m_words.end() ){
if( (*pos).second.first == SkipWordAndArguments ){
readWhiteSpaces();
if( currentChar() == '(' )
skip( '(', ')' );
}
if( !(*pos).second.second.isEmpty() ){
#if defined( KDEVELOP_BGPARSER )
qthread_yield();
#endif
insertCurrent( TQString(" ") + (*pos).second.second + TQString(" ") );
}
} else if( /*qt_rx.exactMatch(ide) ||*/
ide.str().endsWith("EXPORT") ||
(ide.str().startsWith("TQ_EXPORT") && ide.str() != "TQ_EXPORT_INTERFACE") ||
ide.str().startsWith("TQM_EXPORT") ||
ide.str().startsWith("TQM_TEMPLATE")){
readWhiteSpaces();
if( currentChar() == '(' )
skip( '(', ')' );
} else if( ide.str().startsWith("K_TYPELIST_") || ide.str().startsWith("TYPELIST_") ){
tk = CREATE_TOKEN( Token_identifier, start, currentPosition() - start );
tk.setStartPosition( startLine, startColumn );
tk.setEndPosition( m_currentLine, m_currentColumn );
readWhiteSpaces();
if( currentChar() == '(' )
skip( '(', ')' );
} else{
tk = CREATE_TOKEN( Token_identifier, start, currentPosition() - start );
tk.setStartPosition( startLine, startColumn );
tk.setEndPosition( m_currentLine, m_currentColumn );
}
} else {
tk = CREATE_TOKEN( Token_identifier, start, currentPosition() - start );
tk.setStartPosition( startLine, startColumn );
tk.setEndPosition( m_currentLine, m_currentColumn );
}
} else if( ch.isNumber() ){
int start = currentPosition();
readNumberLiteral();
tk = CREATE_TOKEN( Token_number_literal, start, currentPosition() - start );
tk.setStartPosition( startLine, startColumn );
tk.setEndPosition( m_currentLine, m_currentColumn );
} else if( -1 != (op = findOperator3()) ){
tk = CREATE_TOKEN( op, currentPosition(), 3 );
nextChar( 3 );
tk.setStartPosition( startLine, startColumn );
tk.setEndPosition( m_currentLine, m_currentColumn );
} else if( -1 != (op = findOperator2()) ){
tk = CREATE_TOKEN( op, currentPosition(), 2 );
nextChar( 2 );
tk.setStartPosition( startLine, startColumn );
tk.setEndPosition( m_currentLine, m_currentColumn );
} else {
tk = CREATE_TOKEN( ch, currentPosition(), 1 );
nextChar();
tk.setStartPosition( startLine, startColumn );
tk.setEndPosition( m_currentLine, m_currentColumn );
}
m_startLine = false;
}
void Lexer::tokenize()
{
m_startLine = true;
m_size = 0;
for( ;; ) {
Token tk(m_source);
nextToken( tk );
if( tk.type() != -1 )
ADD_TOKEN( tk );
if( currentChar().isNull() )
break;
}
Token tk = CREATE_TOKEN( Token_eof, currentPosition(), 0 );
tk.setStartPosition( m_currentLine, m_currentColumn );
tk.setEndPosition( m_currentLine, m_currentColumn );
ADD_TOKEN( tk );
}
void Lexer::resetSkipWords()
{
m_words.clear();
}
void Lexer::addSkipWord( const TQString& word, SkipType skipType, const TQString& str )
{
m_words[ word ] = tqMakePair( skipType, str );
}
void Lexer::skip( int l, int r )
{
int svCurrentLine = m_currentLine;
int svCurrentColumn = m_currentColumn;
int count = 0;
while( !eof() ){
Token tk(m_source);
nextToken( tk );
if( (int)tk == l )
++count;
else if( (int)tk == r )
--count;
if( count == 0 )
break;
}
m_currentLine = svCurrentLine;
m_currentColumn = svCurrentColumn;
}
TQString Lexer::readArgument()
{
int count = 0;
TQString arg;
readWhiteSpaces();
while( currentChar() ){
readWhiteSpaces();
TQChar ch = currentChar();
if( ch.isNull() || (!count && (ch == ',' || ch == ')')) )
break;
Token tk(m_source);
nextToken( tk );
if( tk == '(' ){
++count;
} else if( tk == ')' ){
--count;
}
if( tk != -1 )
arg += tk.text() + " ";
}
return arg.stripWhiteSpace();
}
void Lexer::handleDirective( const TQString& directive )
{
m_inPreproc = true;
bool skip = skipWordsEnabled();
bool preproc = preprocessorEnabled();
setSkipWordsEnabled( false );
setPreprocessorEnabled( false );
if( directive == "define" ){
if( !m_skipping[ m_ifLevel ] ){
Macro m;
processDefine( m );
}
} else if( directive == "else" ){
processElse();
} else if( directive == "elif" ){
processElif();
} else if( directive == "endif" ){
processEndif();
} else if( directive == "if" ){
processIf();
} else if( directive == "ifdef" ){
processIfdef();
} else if( directive == "ifndef" ){
processIfndef();
} else if( directive == "include" ){
if( !m_skipping[ m_ifLevel ] ){
processInclude();
}
} else if( directive == "undef" ){
if( !m_skipping[ m_ifLevel ] ){
processUndef();
}
}
// skip line
while( currentChar() && currentChar() != '\n' ){
Token tk(m_source);
nextToken( tk, true );
}
setSkipWordsEnabled( skip );
setPreprocessorEnabled( preproc );
m_inPreproc = false;
}
int Lexer::testIfLevel()
{
int rtn = !m_skipping[ m_ifLevel++ ];
m_skipping[ m_ifLevel ] = m_skipping[ m_ifLevel - 1 ];
return rtn;
}
int Lexer::macroDefined()
{
readWhiteSpaces( false );
int startWord = currentPosition();
readIdentifier();
HashedString word = m_source.mid( startWord, currentPosition() - startWord );
m_driver->usingString( word );
bool r = m_driver->hasMacro( word );
if( r ) m_driver->usingMacro( m_driver->macro( word ) );
return r;
}
void Lexer::processDefine( Macro& m )
{
m.setFileName( m_driver->currentFileName() );
m.setLine( m_currentLine );
m.setColumn( m_currentColumn );
readWhiteSpaces( false );
int startMacroName = currentPosition();
readIdentifier();
TQString macroName = m_source.mid( startMacroName, int(currentPosition()-startMacroName) );
m.setName( macroName );
if( currentChar() == '(' ){
m.setHasArguments( true );
nextChar();
readWhiteSpaces( false );
while( currentChar() && currentChar() != ')' ){
readWhiteSpaces( false );
int startArg = currentPosition();
if( currentChar() == '.' && peekChar() == '.' && peekChar(2) == '.' )
nextChar( 3 );
else
readIdentifier();
TQString arg = m_source.mid( startArg, int(currentPosition()-startArg) );
m.addArgument( Macro::Argument(arg) );
readWhiteSpaces( false );
if( currentChar() != ',' )
break;
nextChar(); // skip ','
}
if( currentChar() == ')' )
nextChar(); // skip ')'
}
setPreprocessorEnabled( true );
TQString body;
while( currentChar() && currentChar() != '\n' ){
if( currentChar().isSpace() ){
readWhiteSpaces( false );
body += " ";
} else {
Token tk(m_source);
nextToken( tk, true );
//Do not ignore c-style comments, those may be useful in the body, and ignoring them using this check causes problems
if( tk.type() != -1 && (tk.type() != Token_comment || ( tk.text().length() >= 2 && tk.text()[1] == '*') ) ){
TQString s = tk.text();
body += s;
}
}
}
m.setBody( body );
m_driver->addMacro( m );
}
void Lexer::processElse()
{
if( m_ifLevel == 0 )
/// @todo report error
return;
if( m_ifLevel > 0 && m_skipping[m_ifLevel-1] )
m_skipping[ m_ifLevel ] = m_skipping[ m_ifLevel - 1 ];
else
m_skipping[ m_ifLevel ] = m_trueTest[ m_ifLevel ];
}
void Lexer::processElif()
{
if( m_ifLevel == 0 )
/// @todo report error
return;
if( !m_trueTest[m_ifLevel] ){
/// @todo implement the correct semantic for elif!!
bool inSkip = m_ifLevel > 0 && m_skipping[ m_ifLevel-1 ];
m_trueTest[ m_ifLevel ] = macroExpression() != 0;
m_skipping[ m_ifLevel ] = inSkip ? inSkip : !m_trueTest[ m_ifLevel ];
}
else
m_skipping[ m_ifLevel ] = true;
}
void Lexer::processEndif()
{
if( m_ifLevel == 0 )
/// @todo report error
return;
m_skipping[ m_ifLevel ] = 0;
m_trueTest[ m_ifLevel-- ] = 0;
}
void Lexer::processIf()
{
bool inSkip = m_skipping[ m_ifLevel ];
if( testIfLevel() ) {
#if 0
int n;
if( (n = testDefined()) != 0 ) {
int isdef = macroDefined();
m_trueTest[ m_ifLevel ] = (n == 1 && isdef) || (n == -1 && !isdef);
} else
#endif
m_trueTest[ m_ifLevel ] = macroExpression() != 0;
m_skipping[ m_ifLevel ] = inSkip ? inSkip : !m_trueTest[ m_ifLevel ];
}
}
void Lexer::processIfdef()
{
bool inSkip = m_skipping[ m_ifLevel ];
if( testIfLevel() ){
m_trueTest[ m_ifLevel ] = macroDefined();
m_skipping[ m_ifLevel ] = inSkip ? inSkip : !m_trueTest[ m_ifLevel ];
}
}
void Lexer::processIfndef()
{
bool inSkip = m_skipping[ m_ifLevel ];
if( testIfLevel() ){
m_trueTest[ m_ifLevel ] = !macroDefined();
m_skipping[ m_ifLevel ] = inSkip ? inSkip : !m_trueTest[ m_ifLevel ];
}
}
void Lexer::processInclude()
{
if( m_skipping[m_ifLevel] )
return;
readWhiteSpaces( false );
if( currentChar() ){
TQChar ch = currentChar();
if( ch == '"' || ch == '<' ){
nextChar();
TQChar ch2 = ch == TQChar('"') ? TQChar('"') : TQChar('>');
int startWord = currentPosition();
while( currentChar() && currentChar() != ch2 )
nextChar();
if( currentChar() ){
TQString word = m_source.mid( startWord, int(currentPosition()-startWord) );
m_driver->addDependence( m_driver->currentFileName(),
Dependence(word, ch == '"' ? Dep_Local : Dep_Global) );
nextChar();
}
}
}
}
void Lexer::processUndef()
{
readWhiteSpaces();
int startWord = currentPosition();
readIdentifier();
TQString word = m_source.mid( startWord, currentPosition() - startWord );
Macro m( word, "" );
m.setFileName( m_driver->currentFileName() );
m.setUndef();
///Adds an undef-macro that shadows the previous macro
m_driver->addMacro( m );
}
int Lexer::macroPrimary()
{
readWhiteSpaces( false );
int result = 0;
switch( currentChar() ) {
case '(':
nextChar();
result = macroExpression();
if( currentChar() != ')' ){
/// @todo report error
return 0;
}
nextChar();
return result;
case '+':
case '-':
case '!':
case '~':
{
TQChar tk = currentChar();
nextChar();
int result = macroPrimary();
if( tk == '-' ) return -result;
else if( tk == '!' ) return !result;
else if( tk == '~' ) return ~result;
}
break;
default:
{
Token tk(m_source);
nextToken( tk, false );
switch( tk.type() ){
case Token_identifier:
if( tk.text() == "defined" ){
return macroPrimary();
}
/// @todo implement
{
HashedString h( tk.text() );
m_driver->usingString( h );
if( m_driver->hasMacro( h ) ) {
m_driver->usingMacro( m_driver->macro( h ) );
return true;
} else {
return false;
}
}
case Token_number_literal:
case Token_char_literal:
return toInt( tk );
default:
break;
} // end switch
} // end default
} // end switch
return 0;
}
int Lexer::macroMultiplyDivide()
{
int result = macroPrimary();
int iresult, op;
for (;;) {
readWhiteSpaces( false );
if( currentChar() == '*' )
op = 0;
else if( currentChar() == '/' && !(peekChar() == '*' || peekChar() == '/') )
op = 1;
else if( currentChar() == '%' )
op = 2;
else
break;
nextChar();
iresult = macroPrimary();
result = op == 0 ? (result * iresult) :
op == 1 ? (iresult == 0 ? 0 : (result / iresult)) :
(iresult == 0 ? 0 : (result % iresult)) ;
}
return result;
}
int Lexer::macroAddSubtract()
{
int result = macroMultiplyDivide();
int iresult, ad;
readWhiteSpaces( false );
while( currentChar() == '+' || currentChar() == '-') {
ad = currentChar() == '+';
nextChar();
iresult = macroMultiplyDivide();
result = ad ? (result+iresult) : (result-iresult);
}
return result;
}
int Lexer::macroRelational()
{
int result = macroAddSubtract();
int iresult;
readWhiteSpaces( false );
while( currentChar() == '<' || currentChar() == '>') {
int lt = currentChar() == '<';
nextChar();
if( currentChar() == '=') {
nextChar();
iresult = macroAddSubtract();
result = lt ? (result <= iresult) : (result >= iresult);
}
else {
iresult = macroAddSubtract();
result = lt ? (result < iresult) : (result > iresult);
}
}
return result;
}
int Lexer::macroEquality()
{
int result = macroRelational();
int iresult, eq;
readWhiteSpaces( false );
while ((currentChar() == '=' || currentChar() == '!') && peekChar() == '=') {
eq = currentChar() == '=';
nextChar( 2 );
iresult = macroRelational();
result = eq ? (result==iresult) : (result!=iresult);
}
return result;
}
int Lexer::macroBoolAnd()
{
int result = macroEquality();
readWhiteSpaces( false );
while( currentChar() == '&' && peekChar() != '&') {
nextChar();
result &= macroEquality();
}
return result;
}
int Lexer::macroBoolXor()
{
int result = macroBoolAnd();
readWhiteSpaces( false );
while( currentChar() == '^') {
nextChar();
result ^= macroBoolAnd();
}
return result;
}
int Lexer::macroBoolOr()
{
int result = macroBoolXor();
readWhiteSpaces( false );
while( currentChar() == '|' && peekChar() != '|') {
nextChar();
result |= macroBoolXor();
}
return result;
}
int Lexer::macroLogicalAnd()
{
int result = macroBoolOr();
readWhiteSpaces( false );
while( currentChar() == '&' && peekChar() == '&') {
nextChar( 2 );
int start = currentPosition();
result = macroBoolOr() && result;
TQString s = m_source.mid( start, currentPosition() - start );
}
return result;
}
int Lexer::macroLogicalOr()
{
int result = macroLogicalAnd();
readWhiteSpaces( false );
while( currentChar() == '|' && peekChar() == '|') {
nextChar( 2 );
result = macroLogicalAnd() || result;
}
return result;
}
int Lexer::macroExpression()
{
readWhiteSpaces( false );
return macroLogicalOr();
}
// *IMPORTANT*
// please, don't include lexer.tqmoc here, because Lexer isn't a TQObject class!!
// if you have problem while recompiling try to remove cppsupport/.deps,
// cppsupport/Makefile.in and rerun automake/autoconf
|