-
Notifications
You must be signed in to change notification settings - Fork 0
/
ActionsAndRules.cpp
executable file
·1264 lines (988 loc) · 37.8 KB
/
ActionsAndRules.cpp
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
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// ActionsAndRules.h - Added Customized rules for Pass 1 & Pass 2. //
// ver 1.2 //
// //
// Language : Visual C++, 2010 //
// Platform : Toshiba Satellite (R25-S3503), Windows 7 Professional //
// Application : CSE687 - Project #1 - CodeMetricsAnalyzer, Spring 2011 //
// Author : Harshal Bhakta, Syracuse University. //
// E-mail : harshal.c.bhakta@gmail.com //
// Cell : 714-876-4273 //
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
// ActionsAndRules.cpp - implements new parsing rules and actions //
// ver 1.1 //
// Language: Visual C++ 2008, SP1 //
// Platform: Dell Precision T7400, Vista Ultimate SP1 //
// Application: Prototype for CSE687 Pr1, Sp09 //
// Author: Jim Fawcett, CST 4-187, Syracuse University //
// (315) 443-3948, jfawcett@twcny.rr.com //
/////////////////////////////////////////////////////////////////////
#include "ActionsAndRules.h"
///////////////////////////////////////////////////////////////
// Class with Static Method to calculate number of lines.
int NumberOfLines::getLineNumber(ITokCollection* pTc,int currIndex, int prevLineNumber)
{
int tempNewLine = 0;
ITokCollection& tc = *pTc;
for ( int i = 0 ; i < currIndex ; i++ )
{
if ( tc[i] == "\n" )
tempNewLine++;
if ( tc[i].find_first_of("//") == 0 || tc[i].find_first_of("/*") == 0 )
{ // if semi is comment, check for multiple new line.
for ( int k = 0 ; k < (int)tc[i].length() ; k++)
{
if ( tc[i][k] == '\n')
tempNewLine++;
}
}
}
return tempNewLine + prevLineNumber;
}
/////////////////////////////////////////////////////////////////////////////////////
/////////////////////// Rules & Action for CodeMetricsAnalyzer //////////////////////
/////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////
// rule to count line number
bool NewLine::doTest(ITokCollection* pTc)
{
doActions(pTc);
return true;
}
///////////////////////////////////////////////////////////////
// action to increment the line number
///////////////////////////////////////////////////////////////
// Constructor to get previous number of lines
IncrementLineNumber::IncrementLineNumber(int& noOfLines) : pNumberOfLines(&noOfLines)
{ }
///////////////////////////////////////////////////////////////
// Function to increment number of lines by detecting new-line.
void IncrementLineNumber::doAction(ITokCollection* pTc)
{
ITokCollection& tc = *pTc;
if ( *pNumberOfLines == 0 && tc.length() != 0 )
{ *pNumberOfLines = 1; }
for ( int i = 0 ; i < tc.length() ; i++ )
{
if ( tc[i] == "\n" )
*pNumberOfLines = *pNumberOfLines + 1;
int z = tc[i].find("/*");
if ( tc[i].find_first_of("//") == 0 || tc[i].find_first_of("/*") == 0 )
{ // if semi is comment, look for multiple new line.
for ( int k = 0 ; k < (int)tc[i].length() ; k++)
{
if ( tc[i][k] == '\n')
*pNumberOfLines = *pNumberOfLines + 1;
}
}
}
DataStore::setCurrent_File_Size(*pNumberOfLines);
}
///////////////////////////////////////////////////////////////
// rule to detect class,enum , struct, namespace for analyzis
bool CheckBraceScope::doTest(ITokCollection* pTc)
{
ITokCollection& tc = *pTc;
if(tc[tc.length()-1] == "{" && tc.length() > 1 && tc[tc.length()-2] != ")" )
{
if ( tc.find("class") < tc.length() || tc.find("struct") < tc.length() || tc.find("enum") < tc.length() || tc.find("namespace") < tc.length() )
{
doActions(pTc);
return true;
}
}
return false;
}
///////////////////////////////////////////////////////////////
// action to detect Class, enum , struct , namespace
///////////////////////////////////////////////////////////////
// Constructor to get previous number of lines
DetectedBraceScope::DetectedBraceScope(int& noOfLines) : pNumberOfLines(&noOfLines)
{ }
///////////////////////////////////////////////////////////////
// ACtion to add class,enum,struct,namespace to stack.
void DetectedBraceScope::doAction(ITokCollection* pTc)
{
ITokCollection& tc = *pTc;
int len = tc.length()-1;
std::string type = "";
std::string classPart = "";
if ( tc.find("class") < tc.length() )
{ len = tc.find("class") ; type = "CLASS" ; }
else if ( tc.find("enum") < tc.length() )
{ len = tc.find("enum") ; type = "ENUM" ; }
else if ( tc.find("struct") < tc.length() )
{ len = tc.find("struct") ; type = "STRUCT" ;}
else if ( tc.find("namespace") < tc.length() )
{ len = tc.find("namespace") ; type = "NAMESPACE" ;}
classPart.append(tc[len+1]);
StackOperation::push(type,classPart,NumberOfLines::getLineNumber(pTc,tc.length()-1,*pNumberOfLines));
}
///////////////////////////////////////////////////////////////
// rule to detect function for analyzis
///////////////////////////////////////////////////////////////
// Function to check it a word is a special keyword.
bool CheckControlCount::isSpecialKeyWord(const std::string& tok)
{
const static std::string keys[]
= { "for", "while", "switch", "if", "else" };
for(int i=0; i<5; ++i)
if(tok == keys[i])
return true;
return false;
}
///////////////////////////////////////////////////////////////
// Check if a control keyword exists in the semi.
bool CheckControlCount::doTest(ITokCollection* pTc)
{
ITokCollection& tc = *pTc;
for ( int i = 0 ; i < tc.length() - 1 ; i++ )
{
if(isSpecialKeyWord(tc[i]))
{
doActions(pTc);
return true;
}
}
return false;
}
///////////////////////////////////////////////////////////////
// action to increment the line number
///////////////////////////////////////////////////////////////
// Constructor to get previous number of lines
ControlCountDetected::ControlCountDetected(int& noOfLines) : pNumberOfLines(&noOfLines)
{ }
///////////////////////////////////////////////////////////////
// Function to check it a word is a special keyword.
bool ControlCountDetected::isSpecialKeyWord(const std::string& tok)
{
const static std::string keys[]
= { "for", "while", "switch", "if", "else" };
for(int i=0; i<5; ++i)
if(tok == keys[i])
return true;
return false;
}
///////////////////////////////////////////////////////////////
// Function to increment cyclo count when control detected.
void ControlCountDetected::doAction(ITokCollection* pTc)
{
ITokCollection& tc = *pTc;
for ( int i = 0 ; i < tc.length() - 1 ; i++ )
{
if(isSpecialKeyWord(tc[i]))
{
if ( i == 0 )
{
DataStore::Increment_Cyclo();
}
else if ( tc[i-1] != "else" )
DataStore::Increment_Cyclo();
}
}
}
///////////////////////////////////////////////////////////////
// rule to detect function for analyzis
///////////////////////////////////////////////////////////////
// Function to check it a word is a special keyword.
bool CheckFunction::isSpecialKeyWord(const std::string& tok)
{
const static std::string keys[]
= { "for", "while", "switch", "if", "catch" , "each" };
for(int i=0; i<6; ++i)
if(tok == keys[i])
return true;
return false;
}
///////////////////////////////////////////////////////////////
// Function to check if a semi has function statement
bool CheckFunction::doTest(ITokCollection* pTc)
{
ITokCollection& tc = *pTc;
if(tc[tc.length()-1] == "{")
{
int len = tc.find("(");
if ( len > 0 )
{
if(len < tc.length() && !isSpecialKeyWord(tc[len-1]))
{
doActions(pTc);
return true;
}
}
}
return false;
}
///////////////////////////////////////////////////////////////
// action to increment the line number
///////////////////////////////////////////////////////////////
// Function to check it a word is a special operator.
bool FunctionDetected::isOperator(const std::string& tok)
{
const static std::string keys[] = { "+","-","*","/","%","^","&","|","~","!","=","<",">","(" ,")","[","]","new","delete" };
for(int i=0; i<19; ++i)
if(tok == keys[i])
return true;
return false;
}
///////////////////////////////////////////////////////////////
// Constructor to get previous line count.
FunctionDetected::FunctionDetected(int& noOfLines) : pNumberOfLines(&noOfLines)
{ }
///////////////////////////////////////////////////////////////
// Function Detected. Etract name and Add to Stack.
void FunctionDetected::doAction(ITokCollection* pTc)
{
ITokCollection& tc = *pTc;
int len = tc.find("(");
int operator_keyword = tc.find("operator");
std::string detectedFunctionName = "";
std::string functionSignature = "";
for ( int j = len ; j < tc.length() - 1 ; j++)
{
if ( tc[j] != "\n")
{ functionSignature = functionSignature + tc[j] ; }
}
if ( isOperator(tc[len-1]) && operator_keyword < tc.length()-1 )
{ // extract fully qualified name.
for ( int i = operator_keyword; i < len ; i++)
detectedFunctionName = detectedFunctionName + tc[i];
if ( ( ( (operator_keyword-1) >= 0 && tc[operator_keyword-1] == "::") || ( (operator_keyword-1) >= 0 && tc[operator_keyword-1]== "::~" ) || ( (operator_keyword-1) >= 0 && tc[operator_keyword-1] == "~") ) && ( (operator_keyword-1) >= 0 && (tc[operator_keyword-1] != "\n") ) )
detectedFunctionName = tc[operator_keyword-2] + tc[operator_keyword-1] + detectedFunctionName;
}
else if ( len > 2 )
{ // check if destructor symbol or scope resolution operator exist.
detectedFunctionName = tc[len-1];
if ( (tc[len-2] == "::" || tc[len-2] == "::~" || tc[len-2] == "~") && (tc[len-3] != "\n") )
detectedFunctionName = tc[len-3] + tc[len-2] + detectedFunctionName;
}
detectedFunctionName = detectedFunctionName + functionSignature;
StackOperation::push("FUNCTION",detectedFunctionName,NumberOfLines::getLineNumber(pTc,len,*pNumberOfLines));
}
///////////////////////////////////////////////////////////////
// rule to detect control statement for analyzis
///////////////////////////////////////////////////////////////
// Function to check it a word is a special keyword.
bool CheckControl::isControlKeyWord(const std::string& tok)
{
const static std::string keys[]
= { "for", "while", "switch", "if", "do", "else" , "try", "catch" ,"for each" };
for(int i=0; i<8; ++i)
if(tok == keys[i])
return true;
return false;
}
///////////////////////////////////////////////////////////////
// Function to check if a control exists.
bool CheckControl::doTest(ITokCollection* pTc)
{
ITokCollection& tc = *pTc;
if(tc[tc.length()-1] == "{" || tc[tc.length()-1] == ";")
{
for ( int i = 0 ; i < tc.length() ; i++)
{
if( isControlKeyWord(tc[i]) )
{
doActions(pTc);
return true;
}
}
}
return false;
}
///////////////////////////////////////////////////////////////
// action when Control is detected.
bool ControlDetected::isControlKeyWord(const std::string& tok)
{
const static std::string keys[]
= { "for", "while", "switch", "if", "do", "else" , "try", "catch" };
for(int i=0; i<8; ++i)
if(tok == keys[i])
return true;
return false;
}
///////////////////////////////////////////////////////////////
// Constructor to get previous number of lines.
ControlDetected::ControlDetected(std::vector<std::string>& forBuff,int& noOfLines) : pForBuffer(&forBuff),pNumberOfLines(&noOfLines)
{ }
// Actions combined for all control statments for handling : while ( true ) if ( true ) while ( true ) {
///////////////////////////////////////////////////////////////
// Function to handle a semi when control is detected.
void ControlDetected::doAction(ITokCollection* pTc)
{
ITokCollection& tc = *pTc;
for ( int i = 0 ; i < tc.length(); i++)
{
if ( tc[i] == "for" && tc[i+1] != "each")
handleFor(pTc,i);
if ( tc[i] == "while")
handleWhile(pTc,i);
if ( tc[i] == "if" && i != 0 && tc[i-1] != "else")
handleIf(pTc,i);
if ( tc[i] == "else")
handleElse(pTc,i);
if ( tc[i] == "do")
handleDo(pTc,i);
if ( tc[i] == "switch")
handleSwitch(pTc,i);
if ( tc[i] == "for" && tc[i+1] == "each" )
handleForEach(pTc,i+1);
CheckTryCatch(tc[i],pTc,i);
}
}
void ControlDetected::CheckTryCatch(std::string tok,ITokCollection* pTc,int loc)
{
if ( tok == "try")
handleTry(pTc,loc);
if ( tok == "catch")
handleCatch(pTc,loc);
}
///////////////////////////////////////////////////////////////
// Function to handle "for" loop.
void ControlDetected::handleFor(ITokCollection* pTc,int loc)
{
ITokCollection& tc = *pTc;
std::string forPart = "";
for ( int i = loc; i < tc.length(); i++)
{ forPart += tc[i] ; }
pForBuffer->clear();// Clear buffer when for starts.
pForBuffer->push_back(forPart);// Add starting part to buffer.
}
///////////////////////////////////////////////////////////////
// Function to handle "while" loop.
void ControlDetected::handleWhile(ITokCollection* pTc,int loc)
{
ITokCollection& tc = *pTc;
std::string whilePart = "";
int braceCount = 0;
// Build while loop statement. while ( ... )
for ( int i = loc; i < tc.length(); i++)
{
if ( tc[i] == "(" )
braceCount++;
else if ( tc[i] == ")")
braceCount--;
whilePart = whilePart + tc[i].c_str();
if (braceCount == 0 && tc[i] != "while" )
break;
}
if (!(tc[tc.length()-1] == ";"))
{
StackOperation::push("CONTROL",whilePart,NumberOfLines::getLineNumber(pTc,loc,*pNumberOfLines));
}
}
///////////////////////////////////////////////////////////////
// Function to handle "switch" statement
void ControlDetected::handleSwitch(ITokCollection* pTc,int loc)
{
ITokCollection& tc = *pTc;
std::string switchPart = "";
int braceCount = 0;
// Buile switch statement : switch (..)
for ( int i = loc; i < tc.length(); i++)
{
if ( tc[i] == "(" )
braceCount++;
else if ( tc[i] == ")")
braceCount--;
switchPart = switchPart + tc[i].c_str();
if (braceCount == 0 && tc[i] != "switch" )
break;
}
StackOperation::push("CONTROL",switchPart,NumberOfLines::getLineNumber(pTc,loc,*pNumberOfLines));
}
///////////////////////////////////////////////////////////////
// Function to handle try block
void ControlDetected::handleTry(ITokCollection* pTc,int loc)
{
ITokCollection& tc = *pTc;
StackOperation::push("TRY-CATCH","try",NumberOfLines::getLineNumber(pTc,loc,*pNumberOfLines));
}
///////////////////////////////////////////////////////////////
// Function to handle catch block.
void ControlDetected::handleCatch(ITokCollection* pTc,int loc)
{
ITokCollection& tc = *pTc;
std::string catchPart = "";
int braceCount = 0;
// Build catch statement. catch (..)
for ( int i = loc; i < tc.length(); i++)
{
if ( tc[i] == "(" )
braceCount++;
else if ( tc[i] == ")")
braceCount--;
catchPart = catchPart + tc[i].c_str();
if (braceCount == 0 && tc[i] != "catch" )
break;
}
StackOperation::push("TRY-CATCH",catchPart,NumberOfLines::getLineNumber(pTc,loc,*pNumberOfLines));
}
///////////////////////////////////////////////////////////////
// Function to handle If
void ControlDetected::handleIf(ITokCollection* pTc,int loc)
{
ITokCollection& tc = *pTc;
std::string ifPart = "";
int braceCount = 0;
// Build if statement if (..)
for ( int i = loc; i < tc.length(); i++)
{
if ( tc[i] == "(" )
braceCount++;
else if ( tc[i] == ")")
braceCount--;
ifPart = ifPart + tc[i].c_str();
if (braceCount == 0 && tc[i] != "if" )
break;
}
StackOperation::push("CONTROL",ifPart,NumberOfLines::getLineNumber(pTc,loc,*pNumberOfLines));
}
///////////////////////////////////////////////////////////////
// Function to handle For Each
void ControlDetected::handleForEach(ITokCollection* pTc,int loc)
{
ITokCollection& tc = *pTc;
std::string forEachPart = "";
int braceCount = 0;
// build For Each (..)
for ( int i = loc; i < tc.length(); i++)
{
if ( tc[i] == "(" )
braceCount++;
else if ( tc[i] == ")")
braceCount--;
forEachPart = forEachPart + tc[i].c_str();
if (braceCount == 0 && tc[i] != "each" )
break;
}
StackOperation::push("CONTROL","for " + forEachPart,NumberOfLines::getLineNumber(pTc,loc,*pNumberOfLines));
}
///////////////////////////////////////////////////////////////
// Function to handle Else
void ControlDetected::handleElse(ITokCollection* pTc,int loc)
{
ITokCollection& tc = *pTc;
StackOperation::push("CONTROL","else",NumberOfLines::getLineNumber(pTc,loc,*pNumberOfLines));
}
///////////////////////////////////////////////////////////////
// Function to handle Do
void ControlDetected::handleDo(ITokCollection* pTc,int loc)
{
ITokCollection& tc = *pTc;
StackOperation::push("CONTROL","do",NumberOfLines::getLineNumber(pTc,loc,*pNumberOfLines));
}
///////////////////////////////////////////////////////////////
// rule to build up for loop
///////////////////////////////////////////////////////////////
// Constructor to get previous number of line.
CheckBuildFor::CheckBuildFor(std::vector<std::string>& forBuff) : pForBuffer(&forBuff)
{ }
///////////////////////////////////////////////////////////////
// Function to build for loop when buffer is not empty.
bool CheckBuildFor::doTest(ITokCollection* pTc)
{
ITokCollection& tc = *pTc;
if ( pForBuffer->size() != 0 )
{
doActions(pTc);
return true;
}
return false;
}
///////////////////////////////////////////////////////////////
// Constructor to get previous line number
DoBuildFor::DoBuildFor(std::vector<std::string>& forBuff,int& noOfLines) : pForBuffer(&forBuff),pNumberOfLines(&noOfLines)
{ }
///////////////////////////////////////////////////////////////
// Function to build For statement using Buffer.
void DoBuildFor::doAction(ITokCollection* pTc)
{
ITokCollection& tc = *pTc;
std::string forPart = "";
if ( pForBuffer->size() == 1 )
{ // Add second part to buffer.
for ( int i = 0 ; i < tc.length() ; i++ )
forPart = forPart + tc[i].c_str();
pForBuffer->push_back(forPart);
}
else if ( pForBuffer->size() == 2 )
{ // Build For using all 3 parts. ( 1 detected + 2 buffer )
for ( int i = 0 ; i < (int)pForBuffer->size() ; i++ )
{ forPart = forPart + pForBuffer->at(i); }
int braceCount = 0;
for ( int j = 0 ; j < tc.length() ; j++ )
{
if ( tc[j] == "(" )
braceCount++;
else if ( tc[j] == ")")
braceCount--;
forPart = forPart + tc[j].c_str();
if (braceCount < 0)
break;
}
pForBuffer->clear(); // clear when for is complete.
StackOperation::push("CONTROL",forPart,(NumberOfLines::getLineNumber(pTc,tc.length()-1,*pNumberOfLines))-1);
}
}
///////////////////////////////////////////////////////////////
// Function to check if scope starts.
bool ScopeStart::doTest(ITokCollection* pTc)
{
ITokCollection& tc = *pTc;
if ( tc.length() > 0 )
{
if ( tc[tc.length() - 1] == "{" )
{
doActions(pTc);
return true;
}
}
return false;
}
///////////////////////////////////////////////////////////////
// constructor to get previous number of line
doScopeStart::doScopeStart(int& noOfLines) : pNumberOfLines(&noOfLines)
{}
///////////////////////////////////////////////////////////////
// Function to add scope start to stack.
void doScopeStart::doAction(ITokCollection* pTc)
{
ITokCollection& tc = *pTc;
std::pair<std::string, ScopeElement> item;
item.first = "SCOPE-START";
item.second.name = "{";
item.second.lineNumber = NumberOfLines::getLineNumber(pTc,tc.length() - 1,*pNumberOfLines);
ScopeStack<std::string,ScopeElement>::push(item);
}
///////////////////////////////////////////////////////////////
// Constructor to get previous line number.
EndInSemiColon::EndInSemiColon(std::vector<std::string>& forBuff) : pForBuffer(&forBuff)
{ }
///////////////////////////////////////////////////////////////
// Function to check if a semi ends in semi colon
bool EndInSemiColon::doTest(ITokCollection* pTc)
{
ITokCollection& tc = *pTc;
if ( ScopeStack<std::string,ScopeElement>::size() != 0 )
{
ScopeStack<std::string,ScopeElement>::iterator it = ScopeStack<std::string,ScopeElement>::end();
--it;
if ( tc[tc.length() - 1] == ";" && it->first != "SCOPE-START" && pForBuffer->size() == 0 )
{ // Ignore ; statements if top of stack is {
doActions(pTc);
return true;
}
}
return false;
}
///////////////////////////////////////////////////////////////
// Contructor to get previous number of lines.
doEndInSemiColon::doEndInSemiColon(int& NoOfLines) : pNumberOfLines(&NoOfLines)
{ }
///////////////////////////////////////////////////////////////
// Pop element if semi-colon statement ends scope.
void doEndInSemiColon::doAction(ITokCollection* pTc)
{
ITokCollection& tc = *pTc;
// Ignore if : For Buffer not empty.
StackOperation::pop_from_stack((NumberOfLines::getLineNumber(pTc,tc.length()-1,*pNumberOfLines))+1);
}
///////////////////////////////////////////////////////////////
// Function to check if semi ends in }
bool EndInBrace::doTest(ITokCollection* pTc)
{
ITokCollection& tc = *pTc;
if ( ScopeStack<std::string,ScopeElement>::size() != 0 )
{
if ( tc[tc.length() - 1] == "}" )
{
doActions(pTc);
return true;
}
}
return false;
}
///////////////////////////////////////////////////////////////
// Constructor to get previous line number.
doEndInBrace::doEndInBrace(int& NoOfLines) : pNumberOfLines(&NoOfLines)
{ }
///////////////////////////////////////////////////////////////
// Function to pop element as scope ends.
void doEndInBrace::doAction(ITokCollection* pTc)
{
ITokCollection& tc = *pTc;
StackOperation::pop_from_stack((NumberOfLines::getLineNumber(pTc,tc.length()-1,*pNumberOfLines)) + 1);
}
///////////////////////////////////////////////////////////////
// Function to detect object declaration
bool DetectObjectDeclaration::doTest(ITokCollection* pTc)
{
ITokCollection& tc = *pTc;
if ( ScopeStack<std::string,ScopeElement>::size() != 0 )
{
if ( tc[tc.length() - 1] == ";" )
{
doActions(pTc);
return true;
}
}
return false;
}
///////////////////////////////////////////////////////////////
// extract object declaration from semi ending in ;
// http://www.ecs.syr.edu/faculty/fawcett/handouts/CSE687/code/Project1HelpSp11/DetectingDeclarations.doc
std::vector<std::string> doDetectObjectDeclaration::removeAssignment(std::vector<std::string> semi)
{
std::vector<std::string>::iterator iter = std::find(semi.begin(),semi.end(),"=");
if ( iter != semi.end() )
{
std::vector<std::string>::iterator iter_end = semi.end();
semi.erase(iter,iter_end);
}
return semi;
}
///////////////////////////////////////////////////////////////////
// Remove qualifiers from the semi to detect object declaration
std::vector<std::string> doDetectObjectDeclaration::removeQualifiers(std::vector<std::string> semi)
{
std::vector<std::string>::iterator iter = semi.begin() ;
const static std::string keys[]
= { "short" , "long" , "const" , "register" , "static" , "unsigned" , "extern" , "\n" ,
"*" , "&" , "[" , "]" , "private" , ":" , "volatile" , "return" ,
"true" , "false", " " };
for(int i=0; i<19; ++i)
{
iter = find(semi.begin(), semi.end(), keys[i]);
while ( iter != semi.end() )
{
semi.erase(iter);
iter = find(semi.begin(), semi.end(), keys[i]);
}
}
return semi;
}
///////////////////////////////////////////////////////////////
// Constructor to get previous number of lines.
doDetectObjectDeclaration::doDetectObjectDeclaration(int& NoOfLines) : pNumberOfLines(&NoOfLines)
{ }
///////////////////////////////////////////////////////////////
// Action to be taken when object declaration detected.
void doDetectObjectDeclaration::doAction(ITokCollection* pTc)
{
ITokCollection& tc = *pTc;
std::vector<std::string> semi ;
for ( int i = 0; i < tc.length() - 1 ; i++ )
{ semi.push_back(tc[i]); }
semi = removeAssignment(semi);
semi = removeQualifiers(semi);
if ( semi.size() == 2 )
{ // Example : int temp;
TypeItem ti;
ti.setFileName(DataStore::getcurrentFileName());
ti.setpackageName(DataStore::getcurrentPackageName()) ;
ti.setLineNumber(NumberOfLines::getLineNumber(pTc,tc.find("("),*pNumberOfLines));
DataStore::addTypeToTypeTable(DataStore::getcurrentPackageName()+"::"+StackOperation::getCurrentScope()+"::"+semi[1],ti);
}
else if ( semi.size() > 1 && semi[semi.size()-2].at(semi[semi.size()-2].length()-1) == '>')
{ // Example : std::vector<std::string> temp;
TypeItem ti;
ti.setFileName(DataStore::getcurrentFileName());
ti.setpackageName(DataStore::getcurrentPackageName()) ;
ti.setLineNumber(NumberOfLines::getLineNumber(pTc,tc.find("("),*pNumberOfLines));
DataStore::addTypeToTypeTable(DataStore::getcurrentPackageName()+"::"+StackOperation::getCurrentScope()+"::"+semi[semi.size()-1],ti);
}
}
///////////////////////////////////////////////////////////////
// Check if a special keyword exist
bool CheckFunctionArgument::isSpecialKeyWord(const std::string& tok)
{
const static std::string keys[]
= { "for", "while", "switch", "if", "catch" };
for(int i=0; i<5; ++i)
if(tok == keys[i])
return true;
return false;
}
/////////////////////////////////////////////////////////////////////
// Cehck if a function statement for detecting argument declaration.
bool CheckFunctionArgument::doTest(ITokCollection* pTc)
{
ITokCollection& tc = *pTc;
if(tc[tc.length()-1] == "{")
{
int len = tc.find("(");
if ( len > 0 )
{
if(len < tc.length() && !isSpecialKeyWord(tc[len-1]))
{
doActions(pTc);
return true;
}
}
}
return false;
}
///////////////////////////////////////////////////////////////
// Remove assignement and eveything to right.
std::vector<std::string> DoCheckFunctionArgument::removeAssignment(std::vector<std::string> semi)
{
std::vector<std::string>::iterator iter = std::find(semi.begin(),semi.end(),"=");
if ( iter != semi.end() )
{
std::vector<std::string>::iterator iter_end = semi.end();
semi.erase(iter,iter_end);
}
return semi;
}
///////////////////////////////////////////////////////////////
// Remove special keywords.
std::vector<std::string> DoCheckFunctionArgument::removeQualifiers(std::vector<std::string> semi)
{
std::vector<std::string>::iterator iter = semi.begin() ;
const static std::string keys[]
= { "short" , "long" , "const" , "register" , "static" , "unsigned" , "extern" , "\n" ,
"*" , "&" , "[" , "]" , "private" , ":" , "volatile" , "return" ,
"true" , "false" };
for(int i=0; i<18; ++i)
{
iter = find(semi.begin(), semi.end(), keys[i]);
while ( iter != semi.end() )
{
semi.erase(iter);
iter = find(semi.begin(), semi.end(), keys[i]);
}
}
return semi;
}
///////////////////////////////////////////////////////////////
// Constructor to get previous number of lines
DoCheckFunctionArgument::DoCheckFunctionArgument(int& noOfLines) : pNumberOfLines(&noOfLines)
{ }
///////////////////////////////////////////////////////////////
// Extract object declaration from function argument list.
void DoCheckFunctionArgument::doAction(ITokCollection* pTc)
{
ITokCollection& tc = *pTc;
int len = tc.find("(") + 1;
std::string functionSignature = "";
std::vector<std::string> semi;
while ( len < tc.length() - 1 )
{
semi.clear();
while ( len < tc.length() - 1 && tc[len] != "," && tc[len] != ")" )
{
semi.push_back(tc[len]);
len++;
}
semi = removeAssignment(semi);
semi = removeQualifiers(semi);
if ( semi.size() == 2 )
{ // Example : int temp;
TypeItem ti;
ti.setFileName(DataStore::getcurrentFileName());
ti.setpackageName(DataStore::getcurrentPackageName()) ;
ti.setLineNumber(NumberOfLines::getLineNumber(pTc,tc.find("("),*pNumberOfLines));