File: CxxVariableScanner.cpp

package info (click to toggle)
codelite 17.0.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 136,204 kB
  • sloc: cpp: 491,547; ansic: 280,393; php: 10,259; sh: 8,930; lisp: 7,664; vhdl: 6,518; python: 6,020; lex: 4,920; yacc: 3,123; perl: 2,385; javascript: 1,715; cs: 1,193; xml: 1,110; makefile: 804; cobol: 741; sql: 709; ruby: 620; f90: 566; ada: 534; asm: 464; fortran: 350; objc: 289; tcl: 258; java: 157; erlang: 61; pascal: 51; ml: 49; awk: 44; haskell: 36
file content (932 lines) | stat: -rw-r--r-- 27,414 bytes parent folder | download | duplicates (2)
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
#include "CxxVariableScanner.h"

#include "CxxScannerTokens.h"
#include "file_logger.h"

#include <algorithm>
#include <unordered_set>

CxxVariableScanner::CxxVariableScanner(const wxString& buffer, eCxxStandard standard, const wxStringTable_t& macros,
                                       bool isFuncSignature)
    : m_scanner(NULL)
    , m_buffer(buffer)
    , m_eof(false)
    , m_parenthesisDepth(0)
    , m_standard(standard)
    , m_macros(macros)
    , m_isFuncSignature(isFuncSignature)
{
    if(!m_buffer.IsEmpty() && m_buffer[0] == '(') {
        m_buffer.Remove(0, 1);
    }
    m_nativeTypes.insert(T_AUTO);
    m_nativeTypes.insert(T_BOOL);
    m_nativeTypes.insert(T_CHAR);
    m_nativeTypes.insert(T_CHAR16_T);
    m_nativeTypes.insert(T_CHAR32_T);
    m_nativeTypes.insert(T_DOUBLE);
    m_nativeTypes.insert(T_FLOAT);
    m_nativeTypes.insert(T_INT);
    m_nativeTypes.insert(T_LONG);
    m_nativeTypes.insert(T_SHORT);
    m_nativeTypes.insert(T_SIGNED);
    m_nativeTypes.insert(T_UNSIGNED);
    m_nativeTypes.insert(T_VOID);
    m_nativeTypes.insert(T_WCHAR_T);
    // optimize the buffer
    DoOptimizeBuffer();
}

CxxVariableScanner::~CxxVariableScanner() {}

CxxVariable::Vec_t CxxVariableScanner::GetVariables(bool sort)
{
    // this call does nothing if the buffer was already optimized
    CxxVariable::Vec_t vars = DoGetVariables(GetOptimizeBuffer(), sort);
    if(sort) {
        std::sort(vars.begin(), vars.end(),
                  [&](CxxVariable::Ptr_t a, CxxVariable::Ptr_t b) { return a->GetName() < b->GetName(); });
    }
    return vars;
}

bool CxxVariableScanner::ReadType(CxxVariable::LexerToken::Vec_t& vartype, bool& isAuto)
{
    isAuto = false;
    int depth = 0;
    CxxLexerToken token;
    while(GetNextToken(token)) {
        if(depth == 0) {
            if(vartype.empty()) {
                // a type can start the following tokens
                switch(token.GetType()) {
                case T_AUTO:
                    isAuto = true;
                // fall
                case T_CLASS:
                case T_STRUCT:
                case T_IDENTIFIER:
                case T_DOUBLE_COLONS:
                case T_BOOL:
                case T_CHAR:
                case T_CHAR16_T:
                case T_CHAR32_T:
                case T_CONST:
                case T_CONSTEXPR:
                case T_DOUBLE:
                case T_FLOAT:
                case T_INT:
                case T_LONG:
                case T_MUTABLE:
                case T_REGISTER:
                case T_SHORT:
                case T_SIGNED:
                case T_STATIC:
                case T_UNSIGNED:
                case T_VOLATILE:
                case T_VOID:
                case T_USING:
                case T_WCHAR_T: {
                    vartype.push_back(CxxVariable::LexerToken(token, depth));
                    break;
                }
                default:
                    // Not a type definition
                    return false;
                }
            } else {
                const CxxVariable::LexerToken& lastToken = vartype.back();
                switch(token.GetType()) {
                case T_IDENTIFIER: {
                    if(TypeHasIdentifier(vartype) && (vartype.back().type != T_DOUBLE_COLONS)) {
                        // We already found the identifier for this type, its probably part of the name
                        UngetToken(token);
                        return true;
                    } else if(HasNativeTypeInList(vartype) && (vartype.back().type != T_DOUBLE_COLONS)) {
                        UngetToken(token);
                        return true;
                    }
                    // Found an identifier
                    // We consider this part of the type only if the previous token was "::" or
                    // if it was T_CONST
                    switch(lastToken.type) {
                    case T_CLASS:
                    case T_STRUCT:
                    case T_DOUBLE_COLONS:
                    case T_CONST:
                    case T_CONSTEXPR:
                    case T_REGISTER:
                    case T_MUTABLE:
                    case T_VOLATILE:
                    case T_STATIC:
                        vartype.push_back(CxxVariable::LexerToken(token, depth));
                        break;
                    default:
                        UngetToken(token);
                        return true;
                    }
                    break;
                case T_AUTO:
                    isAuto = true;
                // fall
                case T_DOUBLE_COLONS:
                case T_BOOL:
                case T_CHAR:
                case T_CHAR16_T:
                case T_CHAR32_T:
                case T_CONST:
                case T_CONSTEXPR:
                case T_DOUBLE:
                case T_FLOAT:
                case T_INT:
                case T_LONG:
                case T_SHORT:
                case T_SIGNED:
                case T_UNSIGNED:
                case T_VOID:
                case T_WCHAR_T: {
                    vartype.push_back(CxxVariable::LexerToken(token, depth));
                    break;
                }
                }
                case '<':
                case '[':
                    vartype.push_back(CxxVariable::LexerToken(token, depth));
                    depth++;
                    break;
                case '*':
                case '&':
                case '@':       // AngelScript
                case T_AND_AND: // C++11 reference rvalue
                    // Part of the name
                    UngetToken(token);
                    return true;
                default:
                    return false;
                }
            }
        } else {
            // Depth > 0
            vartype.push_back(CxxVariable::LexerToken(token, depth));
            if(token.GetType() == '>' || token.GetType() == ']') {
                --depth;
            } else if(token.GetType() == '<' || token.GetType() == '[') {
                ++depth;
            }
        }
    }
    return false;
}

thread_local std::unordered_set<int> s_validLocalTerminators;
bool CxxVariableScanner::ReadName(wxString& varname, wxString& pointerOrRef, int& line_number,
                                  wxString& varInitialization)
{
    CxxLexerToken token;
    while(GetNextToken(token)) {
        if(token.GetType() == '@') {
            // AngelScript. @ is similar to * in C/C++
            // @see https://github.com/eranif/codelite/issues/1839
            if(!GetNextToken(token) || token.GetType() != T_IDENTIFIER) {
                varname.Clear();
                return false;
            }
            varname = token.GetWXString();
            line_number = token.GetLineNumber();
            varInitialization.Clear();
            pointerOrRef = "@";
            return true;
        } else if(token.GetType() == T_IDENTIFIER) {
            varname = token.GetWXString();

            // When parsing function signature, we don't have multiple arguments
            // tied to the same TYPE
            if(m_isFuncSignature) {
                return false;
            }

            // Peek at the next token
            // We can expect "=", "," "(", ";" or ")"
            // Examples:
            // TYPE name = 1;
            // TYPE name, secondVariable;
            // TYPE name(10);
            // TYPE name;
            if(s_validLocalTerminators.empty()) {
                s_validLocalTerminators.insert((int)',');
                s_validLocalTerminators.insert((int)'=');
                s_validLocalTerminators.insert((int)';');
                s_validLocalTerminators.insert((int)')');
                s_validLocalTerminators.insert((int)'(');
                s_validLocalTerminators.insert((int)'{'); // C++11 initialization, e.g: vector<int> v {1,2,3};
                s_validLocalTerminators.insert((int)'['); // Array
            }

            // Now that we got the name, check if have more variables to expect
            if(!GetNextToken(token)) {
                // We reached EOF, but we do got the variable name
                // se we return false ("don't continue") but we dont clear the name
                return false;
            }

            // Always return the token
            UngetToken(token);

            if(s_validLocalTerminators.count(token.GetType()) == 0) {
                varname.Clear();
                return false;
            }

            ConsumeInitialization(varInitialization);

            // Now that we got the name, check if have more variables to expect
            if(!GetNextToken(token)) {
                return false;
            }

            if((token.GetType() == '{') && !varInitialization.IsEmpty()) {
                // Don't collect functions and consider them as variables
                UngetToken(token);
                varname.clear();
                return false;
            }

            if(!varInitialization.empty()) {
                varInitialization.RemoveLast();
            }

            // If we found comma, return true. Unless we are parsing a function signature
            if(!m_isFuncSignature && (token.GetType() == ',')) {
                return true;
            } else {
                UngetToken(token);
                return false;
            }
        } else if(token.GetType() == '*' || token.GetType() == '&' || token.GetType() == T_AND_AND) {
            pointerOrRef << token.GetWXString();
        } else {
            return false;
        }
    }
    return false;
}

void CxxVariableScanner::ConsumeInitialization(wxString& consumed)
{
    CxxLexerToken token;
    wxString dummy;
    if(!GetNextToken(token))
        return;

    int type = wxNOT_FOUND;
    int tokType = token.GetType();
    if(tokType == '=') {
        CxxLexerToken next_token;
        if(!GetNextToken(next_token))
            return;

        if(next_token.GetType() == '[') {
            // ... = [ -> we expect lambda to come
            return;
        } else if(next_token.GetType() == ']') {
            // ... =] -> lambda body is expected
            return;
        } else {
            UngetToken(next_token);
        }
    }

    if(tokType == '(') {
        // Read the initialization
        std::unordered_set<int> delims;
        delims.insert(')');
        if(ReadUntil(delims, token, consumed) == wxNOT_FOUND) {
            return;
        }
        consumed.Prepend("(");
        // Now read until the delimiter
        delims.clear();
        delims.insert(';');
        delims.insert(',');
        delims.insert('{');
        type = ReadUntil(delims, token, dummy);

    } else if(tokType == '[') {
        // Array
        std::unordered_set<int> delims;
        delims.insert(']');
        if(ReadUntil(delims, token, consumed) == wxNOT_FOUND) {
            return;
        }
        consumed.Prepend("[");
        // Now read until the delimiter
        delims.clear();
        delims.insert(';');
        delims.insert(',');
        type = ReadUntil(delims, token, dummy);

    } else if(tokType == '{') {
        // Read the initialization
        std::unordered_set<int> delims;
        delims.insert('}');
        if(ReadUntil(delims, token, consumed) == wxNOT_FOUND) {
            return;
        }
        consumed.Prepend("{");
        // Now read until the delimiter
        delims.clear();
        delims.insert(';');
        delims.insert(',');
        type = ReadUntil(delims, token, dummy);

    } else if(tokType == '=') {
        std::unordered_set<int> delims;
        delims.insert(';');
        delims.insert(',');
        type = ReadUntil(delims, token, consumed);
    } else {
        UngetToken(token);
        consumed.clear();
        std::unordered_set<int> delims;
        delims.insert(';');
        delims.insert(',');
        delims.insert('{');
        type = ReadUntil(delims, token, dummy);
    }

    if(type == ',' || type == (int)'{' || type == ';') {
        UngetToken(token);
    }
}

int CxxVariableScanner::ReadUntil(const std::unordered_set<int>& delims, CxxLexerToken& token, wxString& consumed)
{
    // loop until we find the open brace
    CxxVariable::LexerToken::Vec_t v;
    int depth = 0;
    while(GetNextToken(token)) {
        v.push_back(CxxVariable::LexerToken(token, depth));
        if(depth == 0) {
            if(delims.count(token.GetType())) {
                consumed = CxxVariable::PackType(v, m_standard);
                return token.GetType();
            } else {
                switch(token.GetType()) {
                case '<':
                case '{':
                case '[':
                case '(':
                    depth++;
                    break;
                default:
                    // ignore it
                    break;
                }
            }
        } else {
            switch(token.GetType()) {
            case '>':
            case '}':
            case ']':
            case ')':
                depth--;
                break;
            default:
                // ignore it
                break;
            }
        }
    }
    return wxNOT_FOUND;
}

bool CxxVariableScanner::GetNextToken(CxxLexerToken& token)
{
    bool res = false;

    while(true) {
        res = ::LexerNext(m_scanner, token);
        if(!res)
            break;

        // Ignore any T_IDENTIFIER which is declared as macro
        if((token.GetType() == T_IDENTIFIER) && m_macros.count(token.GetWXString())) {
            continue;
        }
        break;
    }

    m_eof = !res;
    switch(token.GetType()) {
    case '(':
        ++m_parenthesisDepth;
        break;
    case ')':
        --m_parenthesisDepth;
        break;
    default:
        break;
    }
    return res;
}

void CxxVariableScanner::DoOptimizeBuffer()
{
    if(m_buffer_optimized) {
        return;
    }

    Scanner_t sc = ::LexerNew(m_buffer);
    if(!sc) {
        clWARNING() << "CxxVariableScanner::DoOptimizeBuffer(): failed to create Scanner_t" << clEndl;
        return; // Failed to allocate scanner
    }

    CppLexerUserData* userData = ::LexerGetUserData(sc);
    CxxLexerToken tok;
    CxxLexerToken lastToken;

    // Cleanup
    m_buffers.clear();
    PushBuffer();
    int parenthesisDepth = 0;
    while(::LexerNext(sc, tok)) {
        // Skip prep processing state
        if(userData && userData->IsInPreProcessorSection()) {
            continue;
        }

        // Return the working buffer, which depends on the current state
        wxString& buffer = Buffer();

        // Outer switch: state based
        switch(tok.GetType()) {
        case T_PP_STATE_EXIT:
            break;
        case T_FOR: {
            wxString variable_definition;
            if(OnForLoop(sc, variable_definition)) {
                // move the variable to the next scope
                Buffer() << "for () {";
                PushBuffer();
                Buffer() << variable_definition;
            } else {
                // single line for()
                Buffer() << "for ()";
            }
        } break;
        case T_CATCH:
            OnCatch(sc);
            break;
        case T_DECLTYPE:
            OnDeclType(sc);
            break;
        case T_WHILE:
            OnWhile(sc);
            break;
        case '(':
            buffer << tok.GetWXString();
            if(skip_parenthesis_block(sc)) {
                buffer << ")";
            }
            break;
        case '{':
            buffer << tok.GetWXString();
            PushBuffer();
            break;
        case '}':
            buffer = PopBuffer();
            // The closing curly bracket is added *after* we switch buffers
            buffer << tok.GetWXString();
            break;
        case ')':
            --parenthesisDepth;
            buffer = PopBuffer();
            buffer << ")";
            break;
        default:
            buffer << tok.GetWXString() << " ";
            break;
        }
        lastToken = tok;
    }
    ::LexerDestroy(&sc);

    // Merge the buffers
    std::for_each(m_buffers.rbegin(), m_buffers.rend(), [&](const wxString& buffer) {
        // append the buffers in reverse order
        m_optimized_buffer << buffer;
    });
    m_buffer_optimized = true;
}

CxxVariable::Vec_t CxxVariableScanner::DoGetVariables(const wxString& buffer, bool sort)
{
    // First, we strip all parenthesis content from the buffer
    m_scanner = ::LexerNew(buffer);
    m_eof = false;
    m_parenthesisDepth = 0;
    if(!m_scanner)
        return CxxVariable::Vec_t(); // Empty list

    CxxVariable::Vec_t vars;

    // Read the variable type
    while(!IsEof()) {
        bool isAuto;
        CxxVariable::LexerToken::Vec_t vartype;
        if(!ReadType(vartype, isAuto))
            continue;

        // Get the variable(s) name
        wxString varname, pointerOrRef, varInitialization;
        bool cont = false;
        do {
            int line_number = wxNOT_FOUND;
            cont = ReadName(varname, pointerOrRef, line_number, varInitialization);
            CxxVariable::Ptr_t var(new CxxVariable(m_standard));
            var->SetName(varname);
            var->SetType(vartype);
            var->SetDefaultValue(varInitialization);
            var->SetPointerOrReference(pointerOrRef);
            var->SetIsAuto(isAuto);
            var->SetLine(line_number);

            // the below condition fixes this type:
            // if(something && GetCtrl(). -> we can mistaken this as: VarType: something, VarName: GetCtrl, And
            // pointerOrRef: &&
            bool not_ok = varInitialization.Contains("(") && pointerOrRef == "&&";
            if(not_ok) {
                break;
            } else if(var->IsOk()) {
                vars.push_back(var);
            } else if(!varInitialization.IsEmpty()) {
                // This means that the above was a function call
                // Parse the siganture which is placed inside the varInitialization
                CxxVariableScanner scanner(varInitialization, m_standard, m_macros, true);
                CxxVariable::Vec_t args = scanner.GetVariables(sort);
                vars.insert(vars.end(), args.begin(), args.end());
                break;
            }
        } while(cont && (m_parenthesisDepth == 0) /* not inside a function */);
    }

    ::LexerDestroy(&m_scanner);
    return vars;
}

bool CxxVariableScanner::TypeHasIdentifier(const CxxVariable::LexerToken::Vec_t& type)
{
    // do we have an identifier in the type?
    CxxVariable::LexerToken::Vec_t::const_iterator iter =
        std::find_if(type.begin(), type.end(),
                     [&](const CxxVariable::LexerToken& token) { return (token.GetType() == T_IDENTIFIER); });
    return (iter != type.end());
}

CxxVariable::Map_t CxxVariableScanner::GetVariablesMap()
{
    CxxVariable::Vec_t l = GetVariables(true);
    CxxVariable::Map_t m;
    std::for_each(l.begin(), l.end(), [&](CxxVariable::Ptr_t v) {
        if(m.count(v->GetName()) == 0) {
            m.insert(std::make_pair(v->GetName(), v));
        }
    });
    return m;
}

bool CxxVariableScanner::HasNativeTypeInList(const CxxVariable::LexerToken::Vec_t& type) const
{
    CxxVariable::LexerToken::Vec_t::const_iterator iter =
        std::find_if(type.begin(), type.end(), [&](const CxxVariable::LexerToken& token) {
            return ((token._depth == 0) && (m_nativeTypes.count(token.GetType()) != 0));
        });
    return (iter != type.end());
}

CxxVariable::Vec_t CxxVariableScanner::DoParseFunctionArguments(const wxString& buffer)
{
    m_scanner = ::LexerNew(buffer);
    m_eof = false;
    m_parenthesisDepth = 0;
    if(!m_scanner)
        return CxxVariable::Vec_t(); // Empty list

    CxxVariable::Vec_t vars;

    // Read the variable type
    while(!IsEof()) {
        bool isAuto;
        CxxVariable::LexerToken::Vec_t vartype;
        if(!ReadType(vartype, isAuto))
            continue;

        // Get the variable(s) name
        int line_number = wxNOT_FOUND;
        wxString varname, pointerOrRef, varInitialization;
        ReadName(varname, pointerOrRef, line_number, varInitialization);
        CxxVariable::Ptr_t var(new CxxVariable(m_standard));
        var->SetName(varname);
        var->SetType(vartype);
        var->SetDefaultValue(varInitialization);
        var->SetPointerOrReference(pointerOrRef);
        var->SetIsAuto(isAuto);
        var->SetLine(line_number);
        vars.push_back(var);
    }
    ::LexerDestroy(&m_scanner);
    return vars;
}

CxxVariable::Vec_t CxxVariableScanner::ParseFunctionArguments() { return DoParseFunctionArguments(m_buffer); }

void CxxVariableScanner::UngetToken(const CxxLexerToken& token)
{
    ::LexerUnget(m_scanner);

    // Fix the depth if needed
    if(token.GetType() == '(') {
        --m_parenthesisDepth;
    } else if(token.GetType() == ')') {
        ++m_parenthesisDepth;
    }
}

wxString& CxxVariableScanner::Buffer() { return m_buffers[0]; }

bool CxxVariableScanner::OnForLoop(Scanner_t scanner, wxString& variable_definition)
{
    CxxLexerToken tok;

    // The next token must be '('
    if(!::LexerNext(scanner, tok))
        return false;

    // Parser error
    if(tok.GetType() != '(')
        return false;

    constexpr int STATE_NORMAL = 0;
    constexpr int STATE_CXX11 = 1;
    int state = STATE_NORMAL;

    int depth = 0;
    bool cont = true;
    while(cont && ::LexerNext(scanner, tok)) {
        if(tok.is_keyword() || tok.is_builtin_type()) {
            variable_definition << " " << tok.GetWXString();
            continue;
        }
        switch(tok.GetType()) {
        case '(':
        case '<':
        case '[':
        case '{':
            depth++;
            variable_definition << tok.GetWXString();
            break;
        case '>':
        case ']':
        case '}':
            depth--;
            variable_definition << tok.GetWXString();
            break;
        case ')':
            if(depth == 0) {
                // we are done
                cont = false;
                if(state == STATE_CXX11) {
                    // append ".begin()"
                    variable_definition << ".begin()";
                }
                variable_definition << ";";
            } else {
                variable_definition << ")";
                depth--;
            }
            break;
        case ':':
            // c++11 ranged for loop
            state = STATE_CXX11;
            // we are going to create a variable of this type:
            // TYPENAME name = CONTAINER.begin();
            variable_definition << "=";
            break;
        case T_IDENTIFIER:
            variable_definition << " " << tok.GetWXString();
            break;
        case ';':
            // no need to check for depth, we cant have ';' in non depth 0
            variable_definition << ";";
            cont = false;
            break;
        default:
            variable_definition << tok.GetWXString();
            break;
        }
    }

    // read the remainder (for C++11 ranged loop we already consumed the closing parenthesis)
    if(state == STATE_NORMAL && !SkipToClosingParenthesis(scanner)) {
        return false;
    }

    // we are now expecting a '{'
    ::LexerNext(scanner, tok);
    if(tok.GetType() != '{')
        return false;
    return true;
}

bool CxxVariableScanner::OnCatch(Scanner_t scanner)
{
    CxxLexerToken tok;

    // The next token must be '('
    if(!::LexerNext(scanner, tok))
        return false;

    // Parser error
    if(tok.GetType() != '(')
        return false;
    int depth(1);
    wxString& buffer = Buffer();
    buffer << ";"; // Help the parser
    while(::LexerNext(scanner, tok)) {
        switch(tok.GetType()) {
        case '(':
            ++depth;
            buffer << tok.GetWXString();
            break;
        case ')':
            --depth;
            buffer << tok.GetWXString();
            if(depth == 0) {
                return true;
            }
            break;
        default:
            buffer << tok.GetWXString() << " ";
            break;
        }
    }
    return false;
}

bool CxxVariableScanner::OnWhile(Scanner_t scanner)
{

    CxxLexerToken tok;

    // The next token must be '('
    if(!::LexerNext(scanner, tok))
        return false;

    // Parser error
    if(tok.GetType() != '(')
        return false;
    int depth(1);
    while(::LexerNext(scanner, tok)) {
        switch(tok.GetType()) {
        case '(':
            ++depth;
            break;
        case ')':
            --depth;
            if(depth == 0)
                return true;
            break;
        default:
            break;
        }
    }
    return false;
}

bool CxxVariableScanner::OnDeclType(Scanner_t scanner)
{
    CxxLexerToken tok;
    wxString& buffer = Buffer();

    // The next token must be '('
    if(!::LexerNext(scanner, tok))
        return false;

    // Parser error
    if(tok.GetType() != '(')
        return false;
    int depth(1);
    buffer << "decltype(";
    while(::LexerNext(scanner, tok)) {
        switch(tok.GetType()) {
        case '(':
            ++depth;
            buffer << tok.GetWXString();
            break;
        case ')':
            --depth;
            buffer << ")";
            if(depth == 0) {
                return true;
            }
            break;
        default:
            break;
        }
    }
    return false;
}

wxString& CxxVariableScanner::PushBuffer()
{
    wxString buffer;
    m_buffers.insert(m_buffers.begin(), buffer);
    return m_buffers[0];
}

wxString& CxxVariableScanner::PopBuffer()
{
    if(m_buffers.size() > 1) {
        m_buffers.erase(m_buffers.begin());
    }
    return m_buffers[0];
}

bool CxxVariableScanner::SkipToClosingParenthesis(Scanner_t scanner)
{
    int depth = 0;
    CxxLexerToken tok;
    while(::LexerNext(scanner, tok)) {
        switch(tok.GetType()) {
        case '(':
            depth++;
            break;
        case ')':
            if(depth == 0) {
                return true;
            }
            depth--;
            break;
        default:
            break;
        }
    }
    return false;
}

wxString CxxVariableScanner::ToString(CxxVariable::LexerToken::Vec_t& vartype)
{
    wxString str;
    for(const auto& token : vartype) {
        str << token.text << " ";
    }
    str.Trim();
    return str;
}

bool CxxVariableScanner::skip_parenthesis_block(Scanner_t scanner)
{
    int depth = 0;
    CxxLexerToken token;
    while(::LexerNext(scanner, token)) {
        // Skip prep processing state
        switch(token.GetType()) {
        case '(':
            depth++;
            break;
        case ')':
            if(depth == 0) {
                return true;
            }
            depth--;
            break;
        default:
            break;
        }
    }
    return false;
}

bool CxxVariableScanner::skip_curly_brackets_block(Scanner_t scanner)
{
    int depth = 0;
    CxxLexerToken token;
    while(::LexerNext(scanner, token)) {
        // Skip prep processing state
        switch(token.GetType()) {
        case '{':
            depth++;
            break;
        case '}':
            if(depth == 0) {
                return true;
            }
            depth--;
            break;
        default:
            break;
        }
    }
    return false;
}