File: language.cpp

package info (click to toggle)
codelite 10.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 71,364 kB
  • sloc: cpp: 415,397; ansic: 18,277; php: 9,547; lex: 4,181; yacc: 2,820; python: 2,294; sh: 383; makefile: 51; xml: 13
file content (2447 lines) | stat: -rw-r--r-- 82,425 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
// copyright            : (C) 2008 by Eran Ifrah
// file name            : language.cpp
//
// -------------------------------------------------------------------------
// A
//              _____           _      _     _ _
//             /  __ \         | |    | |   (_) |
//             | /  \/ ___   __| | ___| |    _| |_ ___
//             | |    / _ \ / _  |/ _ \ |   | | __/ _ )
//             | \__/\ (_) | (_| |  __/ |___| | ||  __/
//              \____/\___/ \__,_|\___\_____/_|\__\___|
//
//                                                  F i l e
//
//    This program is free software; you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation; either version 2 of the License, or
//    (at your option) any later version.
//
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
#include "precompiled_header.h"
#include "file_logger.h"
#include "crawler_include.h"
#include <wx/regex.h>
#include <wx/tokenzr.h>

#include "language.h"
#include "pptable.h"
#include "variable.h"
#include "function.h"
#include "ctags_manager.h"
#include "y.tab.h"
#include <wx/stopwatch.h>
#include <wx/ffile.h>
#include "map"
#include <algorithm>
#include "CxxPreProcessor.h"
#include "CxxUsingNamespaceCollector.h"
#include "CxxTemplateFunction.h"
#include "CxxScannerTokens.h"

//#define __PERFORMANCE
#include "performance.h"

#include "code_completion_api.h"
#include "scope_optimizer.h"

static wxString PathFromNameAndScope(const wxString& typeName, const wxString& typeScope)
{
    wxString path;
    if(typeScope != wxT("<global>")) path << typeScope << wxT("::");

    path << typeName;
    return path;
}

static wxString NameFromPath(const wxString& path)
{
    wxString name = path.AfterLast(wxT(':'));
    return name;
}

static wxString ScopeFromPath(const wxString& path)
{
    wxString scope = path.BeforeLast(wxT(':'));
    if(scope.IsEmpty()) return wxT("<global>");

    if(scope.EndsWith(wxT(":"))) {
        scope.RemoveLast();
    }

    if(scope.IsEmpty()) return wxT("<global>");

    return scope;
}

Language::Language()
    : m_expression(wxEmptyString)
    , m_scanner(new CppScanner())
    , m_tokenScanner(new CppScanner())
    , m_tm(NULL)
{
    // Initialise the braces map
    m_braces['<'] = '>';
    m_braces['('] = ')';
    m_braces['['] = ']';
    m_braces['{'] = '}';

    // C++ / C auto complete delimiters for tokens
    std::vector<wxString> delimArr;
    delimArr.push_back(_T("::"));
    delimArr.push_back(_T("->"));
    delimArr.push_back(_T("."));
    delimArr.push_back(wxT("@"));
    SetAutoCompDeliemters(delimArr);
}

/// Destructor
Language::~Language() {}

/// Return the visible scope until pchStopWord is encountered
wxString Language::OptimizeScope(const wxString& srcString, int lastFuncLine, wxString& localsScope)
{
    std::string out, locals;
    const wxCharBuffer inp = srcString.mb_str(wxConvUTF8);
    ::OptimizeScope(inp.data(), out, lastFuncLine, locals);

    wxString scope = _U(out.c_str());
    localsScope = wxString(locals.c_str(), wxConvUTF8);
    return scope;
}

ParsedToken* Language::ParseTokens(const wxString& scopeName)
{
    wxString token;
    wxString delim;
    bool subscript;
    ParsedToken* header(NULL);
    ParsedToken* currentToken(header);
    wxString funcArgList;

    while(NextToken(token, delim, subscript, funcArgList)) {

        ParsedToken* pt = new ParsedToken;
        pt->SetSubscriptOperator(subscript);
        pt->SetOperator(delim);
        pt->SetPrev(currentToken);
        pt->SetCurrentScopeName(scopeName);
        pt->SetArgumentList(funcArgList);

        ExpressionResult result = ParseExpression(token);
        if(result.m_name.empty() && result.m_isGlobalScope == false) {
            ParsedToken::DeleteTokens(header);
            return NULL;
        }

        if(result.m_isGlobalScope && pt->GetOperator() != wxT("::")) {
            ParsedToken::DeleteTokens(header);
            return NULL;
        }

        if(result.m_isaType) {
            pt->SetTypeScope(
                result.m_scope.empty() ? wxString(wxT("<global>")) : wxString::From8BitData(result.m_scope.c_str()));
            pt->SetTypeName(wxString::From8BitData(result.m_name.c_str()));

        } else if(result.m_isGlobalScope) {
            pt->SetTypeScope(wxT("<global>"));
            pt->SetTypeName(wxT("<global>"));

        } else if(result.m_isThis) {
            //-----------------------------------------
            // special handle for 'this' keyword
            //-----------------------------------------

            pt->SetTypeScope(
                result.m_scope.empty() ? wxString(wxT("<global>")) : wxString::From8BitData(result.m_scope.c_str()));
            if(scopeName == wxT("<global>")) {
                ParsedToken::DeleteTokens(header);
                return NULL;
            }

            if(pt->GetOperator() == wxT("::")) {
                ParsedToken::DeleteTokens(header);
                return NULL;
            }

            if(result.m_isPtr && pt->GetOperator() == wxT(".")) {
                ParsedToken::DeleteTokens(header);
                return NULL;
            }

            if(!result.m_isPtr && pt->GetOperator() == wxT("->")) {
                ParsedToken::DeleteTokens(header);
                return NULL;
            }
            pt->SetTypeName(scopeName);
            pt->SetName(wxT("this"));
        }

        pt->SetIsTemplate(result.m_isTemplate);

        // If the current token is 'this' then the type is actually the
        // current scope
        pt->SetName(_U(result.m_name.c_str()));

        wxArrayString argsList;
        ParseTemplateInitList(wxString::From8BitData(result.m_templateInitList.c_str()), argsList);
        pt->SetTemplateInitialization(argsList);

        if(currentToken == NULL) {
            header = pt;
            currentToken = pt;
        } else {
            currentToken->SetNext(pt);
            currentToken = pt;
        }
        token.Clear();
        delim.Clear();
        subscript = false;
    }

    if(header && header->GetNext() && header->GetName().IsEmpty() && header->GetOperator() == "::") {
        // a chain with more than one token and the first token is simple "::"
        // Delete the first token from the list
        ParsedToken* newHeader = header->GetNext();
        newHeader->SetPrev(NULL);
        wxDELETE(header);
        header = newHeader;
    }
    return header;
}

bool Language::NextToken(wxString& token, wxString& delim, bool& subscriptOperator, wxString& funcArgList)
{
    int type(0);
    int depth(0);
    bool collectingFuncArgList = true;

    subscriptOperator = false;
    funcArgList.Clear();

    while((type = m_tokenScanner->yylex()) != 0) {
        switch(type) {
        case lexTHIS:
            token << wxT("this");
            break;
        case CLCL:
        case wxT('.'):
        case lexARROW:
            if(depth == 0) {
                delim = _U(m_tokenScanner->YYText());
                return true;
            } else {
                token << wxT(" ") << _U(m_tokenScanner->YYText());
            }
            break;
        case wxT('['):
            subscriptOperator = true;
            depth++;
            token << wxT(" ") << _U(m_tokenScanner->YYText());
            break;
        case wxT('('):
            if(token.IsEmpty()) {
                // casting like expression, (type)->
                // simply ignore the parenthessis
                break;
            }
        // fall through
        case wxT('<'):
        case wxT('{'):
            depth++;
            token << wxT(" ") << _U(m_tokenScanner->YYText());
            break;
        case wxT(')'):
            if(depth == 0) {
                // ignore this closing brace
                // since it might have been here because of an extra open brace at the beginig of token

                // in cases like:
                // ((wxString))::
                // or:
                // wxString str;
                // ((str)).
                break;
            }
        // fall through
        case wxT('>'):
        case wxT(']'):
        case wxT('}'):
            depth--;
            if(depth == 0 && type == wxT(')')) {
                // we have found closing brace, disable siganture collection
                funcArgList << wxT(')');
                collectingFuncArgList = false;
            }

            token << wxT(" ") << _U(m_tokenScanner->YYText());
            break;
        case IDENTIFIER:
        case wxT(','):
        case lexDOUBLE:
        case lexINT:
        case lexSTRUCT:
        case lexLONG:
        case lexENUM:
        case lexCHAR:
        case UNION:
        case lexFLOAT:
        case lexSHORT:
        case UNSIGNED:
        case SIGNED:
        case lexVOID:
        case lexCLASS:
        case TYPEDEFname:
            token << wxT(" ") << _U(m_tokenScanner->YYText());
            break;
        default:
            break;
        }

        if(collectingFuncArgList && depth) {
            funcArgList << wxString::From8BitData(m_tokenScanner->YYText());
        }
    }

    if(token.IsEmpty() == false && depth == 0) {
        if(delim.IsEmpty()) {
            delim = wxT(".");
            return true;
        }
    }
    return false;
}

void Language::SetAutoCompDeliemters(const std::vector<wxString>& delimArr) { m_delimArr = delimArr; }

bool Language::ProcessExpression(const wxString& stmt, const wxString& text, const wxFileName& fn, int lineno,
    wxString& typeName,              // output
    wxString& typeScope,             // output
    wxString& oper,                  // output
    wxString& scopeTemplateInitList) // output
{
    CL_DEBUG(wxT(" >>> Language::ProcessExpression started ..."));

    bool evaluationSucceeded = true;
    m_templateArgs.clear();

    wxString statement(stmt);

    // Trim whitespace from right and left
    static wxString trimString(_T("{};\r\n\t\v "));

    statement.erase(0, statement.find_first_not_of(trimString));
    statement.erase(statement.find_last_not_of(trimString) + 1);

    wxString lastFuncSig;
    wxString visibleScope, scopeName, localsBody;

    CL_DEBUG(wxT("Getting function signature from the database..."));
    TagEntryPtr tag = GetTagsManager()->FunctionFromFileLine(fn, lineno);
    if(tag) {
        lastFuncSig = tag->GetSignature();
    }
    CL_DEBUG(wxT("Getting function signature from the database... done"));

    CL_DEBUG(wxT("Optimizing scope..."));
    int lastFuncLine = tag ? tag->GetLine() : -1;
    wxString textAfterTokensReplacements;
    textAfterTokensReplacements = ApplyCtagsReplacementTokens(text);
    visibleScope = this->OptimizeScope(textAfterTokensReplacements, lastFuncLine, localsBody);
    CL_DEBUG(wxT("Optimizing scope...done"));

    std::vector<wxString> additionalScopes;

    CL_DEBUG(wxT("Obtaining the scope name..."));
    scopeName = GetScopeName(visibleScope, &additionalScopes);
    CL_DEBUG(wxT("Obtaining the scope name...done"));

    // Always use the global namespace as an addition scope
    // but make sure we add it last
    additionalScopes.push_back(wxT("<global>"));

    SetLastFunctionSignature(lastFuncSig);
    SetVisibleScope(localsBody);
    SetAdditionalScopes(additionalScopes, fn.GetFullPath());

    // get next token using the tokenscanner object
    m_tokenScanner->SetText(_C(statement));

    // By default we keep the head of the list to the top
    // of the chain
    TokenContainer container;

    CL_DEBUG(wxT("Parsing tokens of scope: %s..."), scopeName.c_str());
    container.head = ParseTokens(scopeName);
    if(!container.head) {
        return false;
    }
    CL_DEBUG(wxT("Parsing tokens of scope: %s... done"), scopeName.c_str());

    container.current = container.head;

    while(container.current) {

        CL_DEBUG(wxT("PrcocessToken..."));
        bool res = ProcessToken(&container);
        CL_DEBUG(wxT("step 1 completed"));

        if(!res && !container.Rewind()) {
            evaluationSucceeded = false;
            break;

        } else if(!res && container.Rewind()) {
            // ProcessToken() modified the list
            container.SetRewind(false);
            continue;
        }

        container.retries = 0;

        // HACK1: Let the user override the parser decisions
        CL_DEBUG(wxT("Checking ExcuteUserTypes..."));
        ExcuteUserTypes(container.current);
        CL_DEBUG(wxT("Checking ExcuteUserTypes... done"));

        CL_DEBUG(wxT("Checking DoIsTypeAndScopeExist..."));
        // We call here to IsTypeAndScopeExists which will attempt to provide the best scope / type
        DoIsTypeAndScopeExist(container.current);
        CL_DEBUG(wxT("Checking DoIsTypeAndScopeExist... done"));

        CL_DEBUG(wxT("Checking DoExtractTemplateInitListFromInheritance..."));
        DoExtractTemplateInitListFromInheritance(container.current);
        CL_DEBUG(wxT("Checking DoExtractTemplateInitListFromInheritance... done"));

        if(container.current->GetIsTemplate() && container.current->GetTemplateArgList().IsEmpty()) {
            // We got no template declaration...
            container.current->SetTemplateArgList(DoExtractTemplateDeclarationArgs(container.current), m_templateArgs);
        }

        int retryCount(0);
        bool cont(false);
        bool cont2(false);

        do {
            CL_DEBUG(wxT("Checking CheckForTemplateAndTypedef..."));
            CheckForTemplateAndTypedef(container.current);
            CL_DEBUG(wxT("Checking CheckForTemplateAndTypedef... done"));

            // We check subscript operator only once
            cont = (container.current->GetSubscriptOperator() && OnSubscriptOperator(container.current));
            if(cont) {
                ExcuteUserTypes(container.current);
            }
            container.current->SetSubscriptOperator(false);
            cont2 = (container.current->GetOperator() == wxT("->") && OnArrowOperatorOverloading(container.current));
            if(cont2) {
                ExcuteUserTypes(container.current);
            }
            retryCount++;
        } while((cont || cont2) && retryCount < 5);

        // Update the results we got so far
        typeName = container.current->GetTypeName();
        typeScope = container.current->GetTypeScope();

        // Keep the last operator used, it is required by the caller
        oper = container.current->GetOperator();

        container.current = container.current->GetNext();
        CL_DEBUG(wxT("PrcocessToken... done"));
    }

    // release the tokens
    ParsedToken::DeleteTokens(container.head);
    CL_DEBUG(wxT(" <<< Language::ProcessExpression started ... done"));
    return evaluationSucceeded;
}

bool Language::OnTemplates(ParsedToken* token)
{
    token->ResolveTemplateType(GetTagsManager());
    return token->ResovleTemplate(GetTagsManager());
}

void Language::DoSimpleTypedef(ParsedToken* token)
{
    // If the match is typedef, try to replace it with the actual
    // typename
    std::vector<TagEntryPtr> tags;
    std::vector<TagEntryPtr> filteredTags;
    wxString path;

    GetTagsManager()->FindByPath(token->GetPath(), tags);

    // try to remove all tags that are Macros from this list
    for(size_t i = 0; i < tags.size(); i++) {
        if(!tags.at(i)->IsMacro()) {
            filteredTags.push_back(tags.at(i));
        }
    }

    if(filteredTags.size() == 1) {
        // we have a single match, test to see if it a typedef
        TagEntryPtr tag = filteredTags.at(0);
        wxString tmpInitList;

        wxString realName = tag->NameFromTyperef(tmpInitList);
        if(realName.IsEmpty() == false) {
            token->SetTypeName(realName);
            token->SetTypeScope(tag->GetScope());

            // incase the realName already includes the scope, remove it from the typename
            token->RemoveScopeFromType();
        }
    }
}

bool Language::OnTypedef(ParsedToken* token)
{
    // If the match is typedef, try to replace it with the actual
    // typename
    bool res(false);
    std::vector<TagEntryPtr> tags;
    std::vector<TagEntryPtr> filteredTags;
    wxString path;
    TagsManager* tagsManager = GetTagsManager();

    wxString oldName = token->GetTypeName();
    wxString oldScope = token->GetTypeScope();

    tagsManager->FindByPath(token->GetPath(), tags);

    // try to remove all tags that are Macros from this list
    for(size_t i = 0; i < tags.size(); i++) {
        if(!tags.at(i)->IsMacro()) {
            filteredTags.push_back(tags.at(i));
        }
    }

    if(filteredTags.size() == 1) {
        // We have a single match, test to see if it a typedef
        TagEntryPtr tag = filteredTags.at(0);
        wxString tmpInitList;

        wxString realName = tag->NameFromTyperef(tmpInitList);
        if(realName.IsEmpty() == false) {

            wxArrayString scopeTempalteInitList;
            ParseTemplateInitList(tmpInitList, scopeTempalteInitList);

            if(scopeTempalteInitList.IsEmpty() == false) {
                token->SetTemplateInitialization(scopeTempalteInitList);
                token->SetIsTemplate(true);
            }

            token->SetTypeName(realName);
            token->SetTypeScope(tag->GetScope());

            // incase the typeName includes the scope in it, remove it
            token->RemoveScopeFromType();

            // if the resolved type does not exist, try again against the
            // global namespace. IsTypeAndScopeContainer() will check
            // this and will update the typeScope to 'global' if needed
            DoIsTypeAndScopeExist(token);
            res = true;
        }
    }

    if(filteredTags.empty()) {
        // this is yet another attempt to fix a match which we failed to resolve it completly
        // a good example for such case is using a typedef which was defined inside a function
        // body

        // try to locate any typedefs defined locally
        clTypedefList typedefsList;
        const wxCharBuffer buf = _C(GetVisibleScope());
        get_typedefs(buf.data(), typedefsList);

        if(typedefsList.empty() == false) {
            // take the first match
            clTypedefList::iterator iter = typedefsList.begin();
            for(; iter != typedefsList.end(); iter++) {
                clTypedef td = *iter;
                wxString matchName(td.m_name.c_str(), wxConvUTF8);
                if(matchName == token->GetTypeName()) {
                    wxArrayString scopeTempalteInitList;
                    wxString tmpInitList;

                    token->SetTypeName(wxString(td.m_realType.m_type.c_str(), wxConvUTF8));
                    token->SetTypeScope(wxString(td.m_realType.m_typeScope.c_str(), wxConvUTF8));
                    tmpInitList = wxString(td.m_realType.m_templateDecl.c_str(), wxConvUTF8);

                    ParseTemplateInitList(tmpInitList, scopeTempalteInitList);
                    token->SetTemplateInitialization(scopeTempalteInitList);
                    res = true;
                    break;
                }
            }
        }
    }
    return res && (oldName != token->GetTypeName() || oldScope != token->GetTypeScope());
}

void Language::ParseTemplateArgs(const wxString& argListStr, wxArrayString& argsList)
{
    CppScanner scanner;
    scanner.SetText(_C(argListStr));
    int type = scanner.yylex();
    wxString word = _U(scanner.YYText());

    // Eof?
    if(type == 0) {
        return;
    }
    if(type != (int)'<') {
        return;
    }

    bool nextIsArg(false);
    bool cont(true);
    while(cont) {
        type = scanner.yylex();
        if(type == 0) {
            break;
        }

        switch(type) {
        case lexCLASS:
        case IDENTIFIER: {
            wxString word = _U(scanner.YYText());
            if(word == wxT("class") || word == wxT("typename")) {
                nextIsArg = true;

            } else if(nextIsArg) {
                argsList.Add(word);
                nextIsArg = false;
            }
            break;
        }
        case(int)'>':
            cont = false;
            break;
        default:
            break;
        }
    }
}

void Language::ParseTemplateInitList(const wxString& argListStr, wxArrayString& argsList)
{
    CppScanner scanner;
    scanner.SetText(_C(argListStr));
    int type = scanner.yylex();
    wxString word = _U(scanner.YYText());

    // Eof?
    if(type == 0) {
        return;
    }
    if(type != (int)'<') {
        return;
    }

    int depth(1);
    wxString typeName;
    while(depth > 0) {
        type = scanner.yylex();
        if(type == 0) {
            break;
        }

        switch(type) {
        case(int)',': {
            if(depth == 1) {
                argsList.Add(typeName.Trim().Trim(false));
                typeName.Empty();
            }
            break;
        }
        case(int)'>':
            depth--;
            break;
        case(int)'<':
            depth++;
            break;
        case(int)'*':
        case(int)'&':
            // ignore pointers & references
            break;
        default:
            if(depth == 1) {
                typeName << _U(scanner.YYText());
            }
            break;
        }
    }

    if(typeName.Trim().Trim(false).IsEmpty() == false) {
        argsList.Add(typeName.Trim().Trim(false));
    }
    typeName.Empty();
}

void Language::ParseComments(const wxFileName& fileName, std::vector<CommentPtr>* comments)
{
    wxString content;
    try {
        wxFFile f(fileName.GetFullPath().GetData());
        if(!f.IsOpened()) return;

        // read the content of the file and parse it
        f.ReadAll(&content);
        f.Close();
    } catch(...) {
        return;
    }

    m_scanner->Reset();
    m_scanner->SetText(_C(content));
    m_scanner->KeepComment(1);

    int type(0);

    wxString comment(_T(""));
    int line(-1);

    while(true) {
        type = m_scanner->yylex();
        if(type == 0) // eof
            break;

        // we keep only comments
        if(type == CPPComment) {
            // incase the previous comment was one line above this one,
            // concatenate them to a single comment
            if(m_scanner->lineno() - 1 == line) {
                comment << m_scanner->GetComment();
                line = m_scanner->lineno();
                m_scanner->ClearComment();
                continue;
            }

            // save the previous comment buffer
            if(comment.IsEmpty() == false) {
                comments->push_back(new Comment(comment, fileName.GetFullPath(), line - 1));
                comment.Empty();
                line = -1;
            }

            // first time or no comment is buffer
            if(comment.IsEmpty()) {
                comment = m_scanner->GetComment();
                line = m_scanner->lineno();
                m_scanner->ClearComment();
                continue;
            }

            comments->push_back(new Comment(m_scanner->GetComment(), fileName.GetFullPath(), m_scanner->lineno() - 1));
            comment.Empty();
            line = -1;
            m_scanner->ClearComment();

        } else if(type == CComment) {
            comments->push_back(new Comment(m_scanner->GetComment(), fileName.GetFullPath(), m_scanner->lineno()));
            m_scanner->ClearComment();
        }
    }

    if(comment.IsEmpty() == false) {
        comments->push_back(new Comment(comment, fileName.GetFullPath(), line - 1));
    }

    // reset the scanner
    m_scanner->KeepComment(0);
    m_scanner->Reset();
}

wxString Language::GetScopeName(const wxString& in, std::vector<wxString>* additionlNS)
{
    std::vector<std::string> moreNS;

    const wxCharBuffer buf = _C(in);

    TagsManager* mgr = GetTagsManager();
    std::map<std::string, std::string> ignoreTokens = mgr->GetCtagsOptions().GetTokensMap();

    std::string scope_name = get_scope_name(buf.data(), moreNS, ignoreTokens);
    wxString scope = _U(scope_name.c_str());
    if(scope.IsEmpty()) {
        scope = wxT("<global>");
    }

    if(additionlNS) {
        for(size_t i = 0; i < moreNS.size(); i++) {
            additionlNS->push_back(_U(moreNS.at(i).c_str()));
        }

        // In case we are found some 'using namesapce XXX;' statement
        // we should scan the following scopes:
        // XXX
        // and also:
        // XXX::CurrentScope (assuming that CurrentScope != <global>)
        if(scope != wxT("<global>")) {
            std::vector<wxString> tmpScopes;
            for(size_t i = 0; i < additionlNS->size(); i++) {
                tmpScopes.push_back(additionlNS->at(i));
                tmpScopes.push_back(additionlNS->at(i) + wxT("::") + scope);
            }
            additionlNS->clear();
            additionlNS->insert(additionlNS->begin(), tmpScopes.begin(), tmpScopes.end());
        }

        wxArrayString moreScopes = GetTagsManager()->BreakToOuterScopes(scope);
        for(size_t i = 0; i < moreScopes.GetCount(); i++) {
            if(moreScopes.Item(i) != scope &&
                std::find(additionlNS->begin(), additionlNS->end(), moreScopes.Item(i)) == additionlNS->end()) {
                additionlNS->push_back(moreScopes.Item(i));
            }
        }
    }

    return scope;
}

ExpressionResult Language::ParseExpression(const wxString& in)
{
    ExpressionResult result;
    if(in.IsEmpty()) {
        result.m_isGlobalScope = true;

    } else {
        const wxCharBuffer buf = _C(in);
        result = parse_expression(buf.data());
    }
    return result;
}

bool Language::ProcessToken(TokenContainer* tokeContainer)
{
    // try local scope
    VariableList li;
    FunctionList fooList;

    // first we try to match the current scope
    std::vector<TagEntryPtr> tags;

    TagsManager* mgr = GetTagsManager();
    std::map<std::string, std::string> ignoreTokens = mgr->GetCtagsOptions().GetTokensMap();

    wxString type;
    wxString typeScope;
    ParsedToken* token = tokeContainer->current;

    // Handle 'this'
    if(token->IsThis()) {
        token->SetTypeName(token->GetContextScope());
        token->SetTypeScope(wxT("<global>"));
        return true;
    }

    // Since locals should take precedence over globals/memebers
    // we test the local scope first
    if(token->GetPrev() == NULL) {
        // We are the first token in the chain
        // examine the local scope

        CL_DEBUG(wxT("Parsing for local variables..."));
        CL_DEBUG1("Current scrope:\n%s\n", GetVisibleScope());

        const wxCharBuffer buf = _C(GetVisibleScope());
        const wxCharBuffer buf2 = _C(GetLastFunctionSignature() + wxT(";"));
        get_variables(buf.data(), li, ignoreTokens, false);
        get_variables(buf2.data(), li, ignoreTokens, true);
        CL_DEBUG(wxT("Parsing for local variables... done"));

        // Search for a full match in the returned list
        for(VariableList::iterator iter = li.begin(); iter != li.end(); iter++) {
            // Print the locals found
            CL_DEBUG1("%s", iter->m_name.c_str());
            Variable var = (*iter);
            wxString var_name = _U(var.m_name.c_str());
            if(var_name == token->GetName()) {

                if(var.m_isAuto) {
                    tokeContainer->current->SetIsAutoVariable(true);
                    tokeContainer->current->SetAutoExpression(var.m_completeType);
                    DoFixTokensFromVariable(tokeContainer, tokeContainer->current->GetAutoExpression());

                } else {
                    DoFixTokensFromVariable(tokeContainer, wxString::From8BitData(var.m_completeType.c_str()));
                }
                return false;
            }
        }
    }

    // Try the lookup tables
    bool hasMatch = DoSearchByNameAndScope(token->GetName(), token->GetContextScope(), tags, type, typeScope);
    if(!hasMatch && token->GetPrev() == NULL) {
        // failed to find it in the local scope and in the lookup table
        // try the additional scopes
        for(size_t i = 0; i < GetAdditionalScopes().size(); i++) {
            tags.clear();
            hasMatch = DoSearchByNameAndScope(token->GetName(), GetAdditionalScopes().at(i), tags, type, typeScope);
            if(hasMatch) {
                break;
            }
        }

        if(!hasMatch) {
            // Try macros
            PPToken tok = GetTagsManager()->GetDatabase()->GetMacro(token->GetName());
            if(tok.flags & PPToken::IsValid) {
                // we got a match in the macros DB
                if(tok.flags & PPToken::IsFunctionLike) {
                    // Handle function like macros
                    wxString initList = token->GetArgumentList();
                    if(initList.StartsWith(wxT("("))) {
                        initList.Remove(0, 1);
                    }

                    if(initList.EndsWith(wxT(")"))) {
                        initList.RemoveLast();
                    }

                    wxArrayString initListArr = wxStringTokenize(initList, wxT(","), wxTOKEN_STRTOK);
                    tok.expandOnce(initListArr);
                }

                DoFixTokensFromVariable(tokeContainer, tok.replacement);
                return false;
            }
        }
    }

    if(hasMatch && !tags.empty()) {
        if(token->GetPrev() == NULL) {

            // we are first in the chain, but still we exists in the database
            // this means that we are either a scope followed by operator (e.g. Foo::)
            // or global variable or member
            // for the last two cases, we need to handle this as if we did not find a match in the
            // database
            li.clear();
            TagEntryPtr tag = tags.at(0);
            if(tag->GetKind() == wxT("member") || tag->GetKind() == wxT("variable")) {
                const wxCharBuffer buf = _C(tags.at(0)->GetPattern());
                get_variables(buf.data(), li, ignoreTokens, true);

                // Search for a full match in the returned list
                for(VariableList::iterator iter = li.begin(); iter != li.end(); iter++) {
                    Variable var = (*iter);
                    wxString var_name = _U(var.m_name.c_str());
                    if(var_name == tags.at(0)->GetName()) {
                        DoFixTokensFromVariable(tokeContainer, _U(var.m_completeType.c_str()));
                    }
                }
                return false;
            }

        } else {
            li.clear();

            // if we are a "member" or " variable"
            // try to locate the template initialization list
            bool isTyperef = !tags.at(0)->GetTyperef().IsEmpty();

            TagEntryPtr tag = tags.at(0);
            if(!isTyperef && (tags.at(0)->GetKind() == wxT("member") || tags.at(0)->GetKind() == wxT("variable"))) {
                const wxCharBuffer buf = _C(tags.at(0)->GetPattern());
                get_variables(buf.data(), li, ignoreTokens, true);

                // Search for a full match in the returned list
                for(VariableList::iterator iter = li.begin(); iter != li.end(); iter++) {
                    Variable var = (*iter);
                    wxString var_name = _U(var.m_name.c_str());
                    if(var_name == tags.at(0)->GetName()) {
                        ExpressionResult expRes = ParseExpression(_U(var.m_completeType.c_str()));
                        if(expRes.m_isTemplate) {
                            token->SetIsTemplate(expRes.m_isTemplate);

                            wxArrayString argsList;
                            ParseTemplateInitList(wxString::From8BitData(expRes.m_templateInitList.c_str()), argsList);
                            token->SetTemplateInitialization(argsList);
                        }
                    }
                }
            } else if(tag->IsTemplateFunction()) {
                // Parse the definition list
                CxxTemplateFunction ctf(tag);
                ctf.ParseDefinitionList();

                if(!ctf.GetList().IsEmpty()) {
                    token->SetIsTemplate(true);
                    token->SetTemplateArgList(ctf.GetList(), m_templateArgs);
                }
            }
            // fall through...
        }

        // we got a match
        token->SetTypeName(type);
        token->SetTypeScope(typeScope);

        return DoCorrectUsingNamespaces(token, tags);
    }
    return false;
}

bool Language::CorrectUsingNamespace(
    wxString& type, wxString& typeScope, const wxString& parentScope, std::vector<TagEntryPtr>& tags)
{
    wxString strippedScope(typeScope);
    wxArrayString tmplInitList;
    DoRemoveTempalteInitialization(strippedScope, tmplInitList);

    if(typeScope == wxT("<global>") && GetAdditionalScopes().empty() == false) {
        // Incase the typeScope is "global" and we got additional-scopes
        // Use the additional scopes *before* the "global" scope
        for(size_t i = 0; i < GetAdditionalScopes().size(); i++) {
            tags.clear();
            wxString newScope(GetAdditionalScopes().at(i));
            if(typeScope != wxT("<global>")) {
                newScope << wxT("::") << typeScope;
            }

            if(DoSearchByNameAndScope(type, newScope, tags, type, typeScope)) {
                return true;
            }
        }
    }

    // try the passed scope (might be <global> now)
    if(GetTagsManager()->IsTypeAndScopeExists(type, strippedScope)) {
        return true;
    }

    // if we are here, it means that the more scopes did not matched any, try the parent scope
    tags.clear();

    // try all the scopes of the parent:
    // for example:
    // assuming the parent scope is A::B::C
    // try to match:
    // A::B::C
    // A::B
    // A
    wxArrayString scopesToScan = GetTagsManager()->BreakToOuterScopes(parentScope);
    scopesToScan.Add(wxT("<global>"));
    for(size_t i = 0; i < scopesToScan.GetCount(); i++) {
        tags.clear();
        if(DoSearchByNameAndScope(type, scopesToScan.Item(i), tags, type, typeScope, false)) {
            return true;
        }
    }
    return true;
}

bool Language::DoSearchByNameAndScope(const wxString& name, const wxString& scopeName, std::vector<TagEntryPtr>& tags,
    wxString& type, wxString& typeScope, bool testGlobalScope)
{
    PERF_BLOCK("DoSearchByNameAndScope")
    {
        std::vector<TagEntryPtr> tmp_tags;
        GetTagsManager()->FindByNameAndScope(name, scopeName, tmp_tags);
        if(tmp_tags.empty() && testGlobalScope) {
            // try the global scope maybe?
            GetTagsManager()->FindByNameAndScope(name, wxT("<global>"), tmp_tags);
        }

        // filter macros from the result
        for(size_t i = 0; i < tmp_tags.size(); i++) {
            TagEntryPtr t = tmp_tags.at(i);
            if(t->GetKind() != wxT("macro") && !t->IsConstructor()) {
                tags.push_back(t);
            }
        }

        if(tags.size() == 1) {
            TagEntryPtr tag(tags.at(0));
            // we have a single match!
            if(tag->IsMethod()) {

                clFunction foo;
                if(FunctionFromPattern(tag, foo)) {
                    type = _U(foo.m_returnValue.m_type.c_str());

                    // Guess the return value scope:
                    // if we got scope, use it
                    if(foo.m_returnValue.m_typeScope.empty() == false)
                        typeScope = _U(foo.m_returnValue.m_typeScope.c_str());

                    else {

                        // we got no scope to use.
                        // try the wxT("<global>") scope
                        typeScope = wxT("<global>");
                        if(!GetTagsManager()->GetDatabase()->IsTypeAndScopeExistLimitOne(type, typeScope)) {
                            // try the current scope
                            typeScope = scopeName;
                        }
                        // TODO: continue to scan the entire 'using namespaces' stack
                    }
                    return true;
                }

                return false;

            } else if(tag->GetKind() == wxT("member") || tag->GetKind() == wxT("variable")) {

                if(tag->GetKind() == wxT("member") && !tag->GetTyperef().IsEmpty()) {
                    // Incase the tag is of type 'member' AND the name is different than
                    // the typeref, we are actually dealing here with a 'using directive' entry
                    // in this case, the fully qualified name is tag->NameFromTyperef()
                    wxString dummy;
                    wxString typeRef = tag->NameFromTyperef(dummy);

                    type = NameFromPath(typeRef);
                    typeScope = ScopeFromPath(typeRef);
                    return true;
                }

                Variable var;
                if(VariableFromPattern(tag->GetPattern(), tag->GetName(), var)) {
                    type = _U(var.m_type.c_str());
                    typeScope = var.m_typeScope.empty() ? wxT("<global>") : _U(var.m_typeScope.c_str());
                    return true;
                }
                return false;
            } else {
                type = tag->GetName();
                typeScope = tag->GetScopeName();
            }
            return true;
        } else if(tags.size() > 1) {

            // if list contains more than one entry, check if all entries are of type 'function' or 'prototype'
            // (they can be mixed). If all entries are of one of these types, test their return value,
            // if all have the same return value, then we are ok
            clFunction foo;
            int classMatches(0);
            size_t classMatchIdx(0);

            for(size_t i = 0; i < tags.size(); i++) {
                TagEntryPtr tag(tags.at(i));
                if(!FunctionFromPattern(tag, foo)) {
                    break;
                }

                type = _U(foo.m_returnValue.m_type.c_str());
                typeScope =
                    foo.m_returnValue.m_typeScope.empty() ? tag->GetScope() : _U(foo.m_returnValue.m_typeScope.c_str());
                if(type != wxT("void")) {
                    return true;
                }
            }

            // Dont give up yet!
            // If in the list of matches there is a single entry of type class
            // use it as our match
            for(size_t i = 0; i < tags.size(); i++) {
                if(tags.at(i)->IsClass() || tags.at(i)->IsStruct()) {
                    classMatches++;
                    classMatchIdx = i;
                }
            }

            if(classMatches == 1) {
                TagEntryPtr tag = tags.at(classMatchIdx);
                tags.clear();
                tags.push_back(tag);

                type = tag->GetName();
                typeScope = tag->GetScopeName();
                return true;
            }

            return false;
        }
    }
    return false;
}

bool Language::VariableFromPattern(const wxString& in, const wxString& name, Variable& var)
{
    VariableList li;
    wxString pattern(in);
    // we need to extract the return value from the pattern
    pattern = pattern.BeforeLast(wxT('$'));
    pattern = pattern.AfterFirst(wxT('^'));

    const wxCharBuffer patbuf = _C(pattern);
    li.clear();

    TagsManager* mgr = GetTagsManager();
    std::map<std::string, std::string> ignoreTokens = mgr->GetCtagsOptions().GetTokensMap();

    get_variables(patbuf.data(), li, ignoreTokens, false);
    VariableList::iterator iter = li.begin();
    for(; iter != li.end(); iter++) {
        Variable v = *iter;
        if(name == _U(v.m_name.c_str())) {
            var = (*iter);
            return true;
        }
    } // if(li.size() == 1)
    return false;
}

bool Language::FunctionFromPattern(TagEntryPtr tag, clFunction& foo)
{
    FunctionList fooList;
    wxString pattern(tag->GetPattern());
    // we need to extract the return value from the pattern
    pattern = pattern.BeforeLast(wxT('$'));
    pattern = pattern.AfterFirst(wxT('^'));

    pattern = pattern.Trim();
    pattern = pattern.Trim(false);
    if(pattern.EndsWith(wxT(";"))) {
        pattern = pattern.RemoveLast();
    }

    // remove any comments from the pattern
    wxString tmp_pattern(pattern);
    pattern.Empty();
    GetTagsManager()->StripComments(tmp_pattern, pattern);

    // a limitiation of the function parser...
    pattern << wxT(';');

    TagsManager* mgr = GetTagsManager();
    std::map<std::string, std::string> ignoreTokens = mgr->GetCtagsOptions().GetTokensMap();

    // use the replacement table on the pattern before processing it
    DoReplaceTokens(pattern, GetTagsManager()->GetCtagsOptions().GetTokensWxMap());

    const wxCharBuffer patbuf = _C(pattern);
    get_functions(patbuf.data(), fooList, ignoreTokens);
    if(fooList.size() == 1) {
        foo = (*fooList.begin());
        DoFixFunctionUsingCtagsReturnValue(foo, tag);
        return true;

    } else if(fooList.size() == 0) {
        // Fail to parse the statement, assume we got a broken pattern
        // (this can happen because ctags keeps only the first line of a function which was declared
        // over multiple lines)
        // Manually construct the pattern from TagEntry
        wxString pat2;

        pat2 << tag->GetReturnValue() << wxT(" ") << tag->GetName() << tag->GetSignature() << wxT(";");

        // use the replacement table on the pattern before processing it
        DoReplaceTokens(pat2, GetTagsManager()->GetCtagsOptions().GetTokensWxMap());

        const wxCharBuffer patbuf1 = _C(pat2);
        get_functions(patbuf1.data(), fooList, ignoreTokens);
        if(fooList.size() == 1) {
            foo = (*fooList.begin());
            DoFixFunctionUsingCtagsReturnValue(foo, tag);
            return true;

        } else if(fooList.empty()) {
            // try a nasty hack:
            // the yacc cant find ctor declarations
            // so add a 'void ' infront of the function...
            wxString pat_tag(pattern);
            pat_tag = pat_tag.Trim(false).Trim();
            wxString pat3;
            bool dummyReturnValue(true);

            // failed to parse function.
            if(tag->GetReturnValue().IsEmpty() == false && !(tag->IsConstructor() || tag->IsDestructor())) {
                pat3 = pat_tag;
                pat3.Prepend(tag->GetReturnValue() + wxT(" "));
                dummyReturnValue = false;

            } else {
                // consider virtual methods as well
                bool virt(false);
                virt = pat_tag.StartsWith(wxT("virtual"), &pat3);
                if(virt) {
                    pat3.Prepend(wxT("void "));
                    pat3.Prepend(wxT("virtual "));
                } else {
                    pat3 = pat_tag;
                    pat3.Prepend(wxT("void "));
                }
            }
            const wxCharBuffer patbuf2 = _C(pat3);
            get_functions(patbuf2.data(), fooList, ignoreTokens);
            if(fooList.size() == 1) {
                foo = (*fooList.begin());

                if(dummyReturnValue) foo.m_returnValue.Reset(); // clear the dummy return value
                return true;
            }
        }
    }
    return false;
}

void Language::GetLocalVariables(const wxString& in, std::vector<TagEntryPtr>& tags, const wxString& name, size_t flags)
{
    VariableList li;
    Variable var;
    wxString pattern(in);

    pattern = pattern.Trim().Trim(false);

    if(flags & ReplaceTokens) {
        // Apply ctags replcements table on the current input string
        pattern = ApplyCtagsReplacementTokens(in);
    }

    const wxCharBuffer patbuf = _C(pattern);
    li.clear();

    TagsManager* mgr = GetTagsManager();
    std::map<std::string, std::string> ignoreTokens = mgr->GetCtagsOptions().GetTokensMap();

    // incase the 'in' string starts with '(' it is most likely that the input string is the
    // function signature in that case we pass 'true' as the fourth parameter to get_variables(..)
    get_variables(patbuf.data(), li, ignoreTokens, pattern.StartsWith(wxT("(")));

    VariableList::iterator iter = li.begin();
    for(; iter != li.end(); iter++) {
        var = (*iter);
        if(var.m_name.empty()) {
            continue;
        }

        wxString tagName = _U(var.m_name.c_str());

        // if we have name, collect only tags that matches name
        if(name.IsEmpty() == false) {

            // incase CaseSensitive is not required, make both string lower case
            wxString tmpName(name);
            wxString tmpTagName(tagName);
            if(flags & IgnoreCaseSensitive) {
                tmpName.MakeLower();
                tmpTagName.MakeLower();
            }

            if(flags & PartialMatch && !tmpTagName.StartsWith(tmpName)) continue;
            // Don't suggest what we have typed so far
            if(flags & PartialMatch && tmpTagName == tmpName) continue;
            if(flags & ExactMatch && tmpTagName != tmpName) continue;

        } // else no name is specified, collect all tags

        TagEntryPtr tag(new TagEntry());
        tag->SetName(tagName);
        tag->SetKind(wxT("variable"));
        tag->SetParent(wxT("<local>"));

        wxString scope;
        if(var.m_typeScope.empty() == false) {
            scope << wxString(var.m_typeScope.c_str(), wxConvUTF8) << wxT("::");
        }
        if(var.m_type.empty() == false) {
            scope << wxString(var.m_type.c_str(), wxConvUTF8);
        }
        tag->SetScope(scope);
        tag->SetAccess(wxT("public"));
        tag->SetPattern(_U(var.m_pattern.c_str()));
        tags.push_back(tag);
    }
}

bool Language::OnArrowOperatorOverloading(ParsedToken* token)
{
    bool ret(false);
    // collect all functions of typename
    std::vector<TagEntryPtr> tags;
    wxString typeScope(token->GetTypeScope());
    wxString typeName(token->GetTypeName());

    // this function will retrieve the ineherited tags as well
    GetTagsManager()->GetDereferenceOperator(token->GetPath(), tags);

    if(tags.size() == 1) {
#if 0
        wxString pattern = tags.at(0)->GetPattern();
        // strip the pattern from ctags regex chars
        pattern = pattern.BeforeLast(wxT('$'));
        pattern = pattern.AfterFirst(wxT('^'));

        Scanner_t cppScanner = ::LexerNew(pattern);
        bool cont = true;
        int depth = 0;
        CxxLexerToken lexerToken;
        std::vector<wxString> parts;
        while(cont && ::LexerNext(cppScanner, lexerToken)) {
            switch(lexerToken.type) {
            case T_OPERATOR:
                cont = false;
                break;
            case T_IDENTIFIER:
                if(depth == 0) {
                    parts.push_back(lexerToken.text);
                }
                break;
            // Dont collect these keywords
            case T_TYPENAME:
            case T_CLASS:
            case T_STRUCT:
            case T_EXPLICIT:
            case T_UNION:
            case T_NAMESPACE:
                break;
            case '<':
                depth++;
                break;
            case '>':
                depth--;
                break;
            default:
                if(depth == 0) {
                    parts.push_back(lexerToken.text);
                }
                break;
            }
        }
        ::LexerDestroy(&cppScanner);
        if(parts.empty()) {
            return false;
        }

        typeName = parts.back();
        parts.pop_back();
        if(!parts.empty()) {
            parts.pop_back(); // remove the '::'
        }

        typeScope.clear();
        // Convert the vector to string
        for(size_t i = 0; i < parts.size(); ++i) {
            typeScope << parts.at(i);
        }
        if(typeScope.IsEmpty()) {
            typeScope = token->GetPath();
        }

        token->SetTypeName(typeName);
        token->SetTypeScope(typeScope);
        DoIsTypeAndScopeExist(token);
        ret = true;
#else
        // loop over the tags and scan for operator -> overloading
        // we found our overloading operator
        // extract the 'real' type from the pattern
        clFunction f;
        if(FunctionFromPattern(tags.at(0), f)) {

            typeName = _U(f.m_returnValue.m_type.c_str());
            typeScope =
                f.m_returnValue.m_typeScope.empty() ? token->GetPath() : _U(f.m_returnValue.m_typeScope.c_str());

            token->SetTypeName(typeName);
            token->SetTypeScope(typeScope);

            // Call the magic method that fixes typename/typescope
            DoIsTypeAndScopeExist(token);
            ret = true;
        }
#endif
    }
    return ret;
}

void Language::SetTagsManager(TagsManager* tm) { m_tm = tm; }

TagsManager* Language::GetTagsManager()
{
    if(!m_tm) {
        // for backward compatibility allows access to the tags manager using
        // the singleton call
        return TagsManagerST::Get();
    } else {
        return m_tm;
    }
}

void Language::DoRemoveTempalteInitialization(wxString& str, wxArrayString& tmplInitList)
{
    CppScanner sc;
    sc.SetText(_C(str));

    int type(0);
    int depth(0);

    wxString token;
    wxString outputString;
    str.Clear();

    while((type = sc.yylex()) != 0) {
        if(type == 0) return;

        token = _U(sc.YYText());
        switch(type) {
        case wxT('<'):
            if(depth == 0) outputString.Clear();
            outputString << token;
            depth++;
            break;

        case wxT('>'):
            outputString << token;
            depth--;
            break;

        default:
            if(depth > 0)
                outputString << token;
            else
                str << token;
            break;
        }
    }

    if(outputString.IsEmpty() == false) {
        ParseTemplateInitList(outputString, tmplInitList);
    }
}

void Language::DoFixFunctionUsingCtagsReturnValue(clFunction& foo, TagEntryPtr tag)
{
    if(foo.m_returnValue.m_type.empty()) {

        // Use the CTAGS return value
        wxString ctagsRetValue = tag->GetReturnValue();
        DoReplaceTokens(ctagsRetValue, GetTagsManager()->GetCtagsOptions().GetTokensWxMap());

        const wxCharBuffer cbuf = ctagsRetValue.mb_str(wxConvUTF8);
        std::map<std::string, std::string> ignoreTokens = GetTagsManager()->GetCtagsOptions().GetTokensMap();

        VariableList li;
        get_variables(cbuf.data(), li, ignoreTokens, false);
        if(li.size() == 1) {
            foo.m_returnValue = *li.begin();
        }
    }
}

void Language::DoReplaceTokens(wxString& inStr, const std::map<wxString, wxString>& ignoreTokens)
{
    if(inStr.IsEmpty()) return;

    std::map<wxString, wxString>::const_iterator iter = ignoreTokens.begin();
    for(; iter != ignoreTokens.end(); iter++) {
        wxString findWhat = iter->first;
        wxString replaceWith = iter->second;

        if(findWhat.StartsWith(wxT("re:"))) {
            findWhat.Remove(0, 3);
            wxRegEx re(findWhat);
            if(re.IsValid() && re.Matches(inStr)) {
                re.ReplaceAll(&inStr, replaceWith);
            }
        } else {
            // Simple replacement
            int where = inStr.Find(findWhat);
            if(where >= 0) {
                if(inStr.Length() > static_cast<size_t>(where)) {
                    // Make sure that the next char is a non valid char otherwise this is not a complete word
                    if(inStr.Mid(where, 1).find_first_of(
                           wxT("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_1234567890")) != wxString::npos) {
                        // the match is not a full word
                        continue;
                    } else {
                        inStr.Replace(findWhat, replaceWith);
                    }
                } else {
                    inStr.Replace(findWhat, replaceWith);
                }
            }
        }
    }
}

void Language::CheckForTemplateAndTypedef(ParsedToken* token)
{
    bool typedefMatch;
    bool templateMatch;
    int retry(0);

    do {
        typedefMatch = OnTypedef(token);
        if(typedefMatch) {
            ExcuteUserTypes(token);
        }

        // Attempt to fix the result
        DoIsTypeAndScopeExist(token);

        if(typedefMatch) {
            DoExtractTemplateInitListFromInheritance(token);

            // The typeName was a typedef, so make sure we update the template declaration list
            // with the actual type
            std::vector<TagEntryPtr> tags;
            GetTagsManager()->FindByPath(token->GetPath(), tags);
            if(tags.size() == 1 && !tags.at(0)->IsTypedef()) {

                // Not a typedef
                token->SetTemplateArgList(DoExtractTemplateDeclarationArgs(tags.at(0)), m_templateArgs);
                token->SetIsTemplate(token->GetTemplateArgList().IsEmpty() == false);

            } else if(tags.size() == 1) {

                // Typedef
                TagEntryPtr t = tags.at(0);
                wxString pattern(t->GetPattern());
                wxArrayString tmpInitList;
                DoRemoveTempalteInitialization(pattern, tmpInitList);

                // Incase any of the template initialization list is a
                // typedef, resolve it as well
                DoResolveTemplateInitializationList(tmpInitList);
                token->SetTemplateInitialization(tmpInitList);
            }
        }

        templateMatch = OnTemplates(token);
        if(templateMatch) {
            if(!DoIsTypeAndScopeExist(token)) {
                std::vector<TagEntryPtr> dummyTags;
                DoCorrectUsingNamespaces(token, dummyTags);
            }
            token->SetIsTemplate(false);
            DoExtractTemplateInitListFromInheritance(token);
        }

        if(templateMatch) {
            ExcuteUserTypes(token);
        }
        retry++;

    } while((typedefMatch || templateMatch) && retry < 15);
}

void Language::DoResolveTemplateInitializationList(wxArrayString& tmpInitList)
{
    for(size_t i = 0; i < tmpInitList.GetCount(); i++) {
        wxString fixedTemplateArg;
        wxString name = NameFromPath(tmpInitList.Item(i));

        wxString tmpScope = ScopeFromPath(tmpInitList.Item(i));
        wxString scope = tmpScope == wxT("<global>") ? m_templateHelper.GetPath() : tmpScope;

        ParsedToken tok;
        tok.SetTypeName(name);
        tok.SetTypeScope(scope);

        DoSimpleTypedef(&tok);

        name = tok.GetTypeName();
        scope = tok.GetTypeScope();

        if(GetTagsManager()->GetDatabase()->IsTypeAndScopeExistLimitOne(name, scope) == false) {
            // no match, assume template: NAME only
            tmpInitList.Item(i) = name;
        } else
            tmpInitList.Item(i) = PathFromNameAndScope(name, scope);
    }
}

wxArrayString Language::DoExtractTemplateDeclarationArgs(ParsedToken* token)
{
    // Find a tag in the database that matches this find and
    // extract the template declaration for it
    std::vector<TagEntryPtr> tags;
    GetTagsManager()->FindByPath(token->GetPath(), tags);
    if(tags.size() != 1) return wxArrayString();

    return DoExtractTemplateDeclarationArgs(tags.at(0));
}

wxArrayString Language::DoExtractTemplateDeclarationArgsFromScope()
{
    wxString tmpParentScope(m_templateHelper.GetTypeScope());
    wxString cuttedScope(tmpParentScope);

    tmpParentScope.Replace(wxT("::"), wxT("@"));
    std::vector<TagEntryPtr> tags;

    cuttedScope.Trim().Trim(false);
    while(!cuttedScope.IsEmpty()) {

        // try all the scopes of thse parent:
        // for example:
        // assuming the parent scope is A::B::C
        // try to match:
        // A::B::C
        // A::B
        // A
        tags.clear();
        GetTagsManager()->FindByPath(cuttedScope, tags);
        if(tags.size() == 1) {
            if(tags.at(0)->GetPattern().Contains(wxT("template"))) {
                return DoExtractTemplateDeclarationArgs(tags.at(0));
            }
        }

        // get the next scope to search
        cuttedScope = tmpParentScope.BeforeLast(wxT('@'));
        cuttedScope.Replace(wxT("@"), wxT("::"));
        cuttedScope.Trim().Trim(false);

        tmpParentScope = tmpParentScope.BeforeLast(wxT('@'));
    }

    return wxArrayString();
}

wxArrayString Language::DoExtractTemplateDeclarationArgs(TagEntryPtr tag)
{
    wxString pattern = tag->GetPattern();
    wxString templateString;

    // extract the template declartion list
    CppScanner declScanner;
    declScanner.ReturnWhite(1);
    declScanner.SetText(_C(pattern));
    bool foundTemplate(false);
    int type(0);
    while(true) {
        type = declScanner.yylex();
        if(type == 0) // eof
            break;

        wxString word = _U(declScanner.YYText());
        switch(type) {
        case IDENTIFIER:
            if(word == wxT("template")) {
                foundTemplate = true;

            } else if(foundTemplate) {
                templateString << word;
            }
            break;

        default:
            if(foundTemplate) {
                templateString << word;
            }
            break;
        }
    }

    if(foundTemplate) {
        wxArrayString ar;
        ParseTemplateArgs(templateString, ar);
        return ar;
    }
    return wxArrayString();
}

///////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////
//      Scope Class
///////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////

void TemplateHelper::SetTemplateDeclaration(const wxString& templateDeclaration)
{
    LanguageST::Get()->ParseTemplateArgs(templateDeclaration, this->templateDeclaration);
}

void TemplateHelper::SetTemplateInstantiation(const wxString& tempalteInstantiation)
{
    this->templateInstantiationVector.clear();
    wxArrayString l;
    LanguageST::Get()->ParseTemplateInitList(tempalteInstantiation, l);
    this->templateInstantiationVector.push_back(l);
}

void TemplateHelper::SetTemplateInstantiation(const wxArrayString& templInstantiation)
{
    // incase we are using template argument as template instantiation,
    // we should perform the replacement or else we will lose
    // the actual tempalte instantiation list
    // an example for such cases:
    // template <class _Tp> class vector {
    //    typedef Something<_Tp> reference;
    //  reference get();
    // };
    // Now, by attempting to resolve this:
    // vector<wxString> v;
    // v.get()->
    // we should replace Something<_Tp> into Something<wxString> *before* we continue with
    // the resolving

    wxArrayString newInstantiationList = templInstantiation;
    // search for 'name' in the declaration list
    for(size_t i = 0; i < newInstantiationList.GetCount(); i++) {
        int where = this->templateDeclaration.Index(newInstantiationList.Item(i));
        if(where != wxNOT_FOUND) {
            wxString name = Substitute(newInstantiationList.Item(i));
            if(!name.IsEmpty()) newInstantiationList[i] = name;
        }
    }

    templateInstantiationVector.push_back(newInstantiationList);
}

wxString TemplateHelper::Substitute(const wxString& name)
{
    //	for(size_t i=0; i<templateInstantiationVector.size(); i++) {
    int count = static_cast<int>(templateInstantiationVector.size());
    for(int i = count - 1; i >= 0; i--) {
        int where = templateDeclaration.Index(name);
        if(where != wxNOT_FOUND) {
            // it exists, return the name in the templateInstantiation list
            if(templateInstantiationVector.at(i).GetCount() > (size_t)where &&
                templateInstantiationVector.at(i).Item(where) != name)
                return templateInstantiationVector.at(i).Item(where);
        }
    }
    return wxT("");
}

void TemplateHelper::Clear()
{
    typeName.Clear();
    typeScope.Clear();
    templateInstantiationVector.clear();
    templateDeclaration.Clear();
}

wxString TemplateHelper::GetPath() const
{
    wxString path;
    if(typeScope != wxT("<global>")) path << typeScope << wxT("::");

    path << typeName;
    return path;
}

void Language::SetAdditionalScopes(const std::vector<wxString>& additionalScopes, const wxString& filename)
{
    if(!(GetTagsManager()->GetCtagsOptions().GetFlags() & CC_DEEP_SCAN_USING_NAMESPACE_RESOLVING)) {
        this->m_additionalScopes = additionalScopes;

    } else {
        this->m_additionalScopes.clear();
        // Use the cache to get the list of using namespaces.
        // The cache is populated by the CodeCompletionManager worker
        // thread when the file is loaded
        std::map<wxString, std::vector<wxString> >::iterator iter = m_additionalScopesCache.find(filename);
        if(iter != m_additionalScopesCache.end()) {
            this->m_additionalScopes = iter->second;
        }

        // "using namespace" may not contains current namespace, so make sure we add it
        for(size_t i = 0; i < additionalScopes.size(); i++) {
            if(!(std::find(this->m_additionalScopes.begin(), this->m_additionalScopes.end(), additionalScopes.at(i)) !=
                   this->m_additionalScopes.end())) {
                this->m_additionalScopes.push_back(additionalScopes.at(i));
            }
        }
    }
}

const std::vector<wxString>& Language::GetAdditionalScopes() const { return m_additionalScopes; }

bool Language::OnSubscriptOperator(ParsedToken* token)
{
    bool ret(false);
    // collect all functions of typename
    std::vector<TagEntryPtr> tags;
    wxString scope;
    wxString typeName(token->GetTypeName());
    wxString typeScope(token->GetTypeScope());

    if(typeScope == wxT("<global>"))
        scope << token->GetTypeName();
    else
        scope << token->GetTypeScope() << wxT("::") << token->GetTypeName();

    // this function will retrieve the ineherited tags as well
    GetTagsManager()->GetSubscriptOperator(scope, tags);
    if(tags.size() == 1) {
        // we found our overloading operator
        // extract the 'real' type from the pattern
        clFunction f;
        if(FunctionFromPattern(tags.at(0), f)) {
            token->SetTypeName(_U(f.m_returnValue.m_type.c_str()));

            // first assume that the return value has the same scope like the parent (unless the return value has a
            // scope)
            token->SetTypeScope(f.m_returnValue.m_typeScope.empty() ? scope : _U(f.m_returnValue.m_typeScope.c_str()));

            // Call the magic method that fixes typename/typescope
            DoIsTypeAndScopeExist(token);
            ret = true;
        }
    }
    return ret;
}

void Language::ExcuteUserTypes(ParsedToken* token, const wxString& entryPath)
{
    std::map<wxString, wxString> typeMap = GetTagsManager()->GetCtagsOptions().GetTypesMap();
    // HACK1: Let the user override the parser decisions
    wxString path = entryPath.IsEmpty() ? token->GetPath() : entryPath;
    std::map<wxString, wxString>::const_iterator where = typeMap.find(path);
    if(where != typeMap.end()) {
        wxArrayString argList;

        // Split to name and scope
        wxString name, scope;

        scope = where->second.BeforeFirst(wxT('<'));
        name = scope.AfterLast(wxT(':'));
        scope = scope.BeforeLast(wxT(':'));
        if(scope.EndsWith(wxT(":"))) {
            scope.RemoveLast();
        }
        token->SetTypeName(name);

        // Did we got a scope as well?
        if(!scope.IsEmpty()) token->SetTypeScope(scope);

        wxString argsString = where->second.AfterFirst(wxT('<'));
        argsString.Prepend(wxT("<"));

        DoRemoveTempalteInitialization(argsString, argList);
        if(argList.IsEmpty() == false) {
            // If we already got a concrete template initialization list
            // do not override it with the dummy one taken from the user
            // type definition
            if(token->GetTemplateInitialization().IsEmpty()) token->SetTemplateInitialization(argList);
            token->SetIsTemplate(true);
        }
    }
}

bool Language::DoIsTypeAndScopeExist(ParsedToken* token)
{
    // Check to see if this is a primitve type...
    if(is_primitive_type(token->GetTypeName().mb_str(wxConvUTF8).data())) {
        return true;
    }

    // Does the typename is happen to be a template argument?
    if(m_templateArgs.count(token->GetTypeName())) {
        return true;
    }

    wxString type(token->GetTypeName());
    wxString scope(token->GetTypeScope());
    bool res = GetTagsManager()->IsTypeAndScopeExists(type, scope);

    token->SetTypeName(type);
    token->SetTypeScope(scope);
    return res;
}

bool Language::DoCorrectUsingNamespaces(ParsedToken* token, std::vector<TagEntryPtr>& tags)
{
    wxString type(token->GetTypeName());
    wxString scope(token->GetTypeScope());
    bool res = CorrectUsingNamespace(type, scope, token->GetContextScope(), tags);

    token->SetTypeName(type);
    token->SetTypeScope(scope);

    return res;
}

void Language::DoExtractTemplateInitListFromInheritance(TagEntryPtr tag, ParsedToken* token)
{
    wxArrayString initList;
    wxString parent;
    wxString scope;

    if(token->GetIsTemplate()) {
        // if this token is already tagged as 'template' dont
        // change this
        return;
    }

    // Loop over the parents of 'tag' and search for any template parent
    // In case we find one, extract the template initialization list from
    // the parent inheritance line and copy it to the current token.

    // If we do find a match, search for the parent itself in the database
    // and extract its template declaration list
    if(tag->IsClass() || tag->IsStruct()) {
        // returns the inheris string with template initialization list
        wxArrayString inherits = tag->GetInheritsAsArrayWithTemplates();
        wxArrayString inheritsNoTemplate = tag->GetInheritsAsArrayNoTemplates();
        size_t i = 0;
        for(; i < inherits.size(); i++) {
            DoRemoveTempalteInitialization(inherits.Item(i), initList);
            if(initList.IsEmpty() == false) {
                break;
            }
        }

        if(initList.IsEmpty() == false) {
            token->SetIsTemplate(true);
            token->SetTemplateInitialization(initList);

            if(i < inheritsNoTemplate.GetCount()) {
                parent = inheritsNoTemplate.Item(i);
                scope = tag->GetScope();

                // Find this parent
                GetTagsManager()->IsTypeAndScopeExists(parent, scope);
                if(scope.IsEmpty() == false && scope != wxT("<global>")) {
                    parent.Prepend(scope + wxT("::"));
                }

                std::vector<TagEntryPtr> tags;
                GetTagsManager()->FindByPath(parent, tags);
                if(tags.size() == 1) {
                    wxArrayString newArgList = DoExtractTemplateDeclarationArgs(tags.at(0));
                    if(newArgList.IsEmpty() == false) {
                        token->SetTemplateArgList(newArgList, m_templateArgs);
                    }
                }
            }
        }
    }
}

void Language::DoExtractTemplateInitListFromInheritance(ParsedToken* token)
{
    std::vector<TagEntryPtr> tags;
    GetTagsManager()->FindByPath(token->GetPath(), tags);
    if(tags.size() == 1) {
        DoExtractTemplateInitListFromInheritance(tags.at(0), token);
    }
}

void Language::DoFixTokensFromVariable(TokenContainer* tokeContainer, const wxString& variableDecl)
{
    // the current tokan is indeed defined on the local stack.
    // what we do now is creating new chain of tokens based on the
    // variable declaration, removing the token that represents the local variable
    // and link the two chains together
    //
    // In addition, we copy the subscript operator flag
    // from the variable into the token declaration
    ParsedToken* token = tokeContainer->current;
    wxString scopeName = token->GetCurrentScopeName();
    wxString op = token->GetOperator();
    bool subscript = token->GetSubscriptOperator();

    wxString newTextToParse;
    newTextToParse << variableDecl << op;
    m_tokenScanner->SetText(newTextToParse.To8BitData());
    ParsedToken* newToken = ParseTokens(scopeName);
    if(newToken) {
        // copy the subscript operator from the local variable token to the
        // last token in the new parsed list
        ParsedToken* lastToken = newToken;
        while(lastToken && lastToken->GetNext()) {
            lastToken = lastToken->GetNext();
        }
        lastToken->SetSubscriptOperator(subscript);
        // If the local variable token has more tokens down the chain,
        // disconnect it from them while connecting the rest of the
        // tokens to the newly parsed list
        if(token->GetNext()) {
            lastToken->SetNext(token->GetNext());
            token->GetNext()->SetPrev(lastToken);
            token->SetNext(NULL);
        }
        // free the token
        ParsedToken::DeleteTokens(token);
        tokeContainer->head = newToken;
        tokeContainer->current = newToken;
        tokeContainer->SetRewind(true);
    }
}

void Language::DoExtractTemplateArgsFromSelf(ParsedToken* token)
{
    // if it is already marked as template, dont change it
    if(token->GetIsTemplate()) return;

    std::vector<TagEntryPtr> tags;
    GetTagsManager()->FindByPath(token->GetPath(), tags);
    if(tags.size() == 1 && !tags.at(0)->IsTypedef()) {
        // Not a typedef
        token->SetTemplateArgList(DoExtractTemplateDeclarationArgs(tags.at(0)), m_templateArgs);
        token->SetIsTemplate(token->GetTemplateArgList().IsEmpty() == false);
    }
}

// Adaptor to Language
static Language* gs_Language = NULL;
void LanguageST::Free()
{
    if(gs_Language) {
        delete gs_Language;
    }
    gs_Language = NULL;
}

Language* LanguageST::Get()
{
    if(gs_Language == NULL) gs_Language = new Language();
    return gs_Language;
}

wxString Language::ApplyCtagsReplacementTokens(const wxString& in)
{
    // First, get the replacement map
    CLReplacementList replacements;
    const std::map<wxString, wxString>& replacementMap = GetTagsManager()->GetCtagsOptions().GetTokensWxMap();
    std::map<wxString, wxString>::const_iterator iter = replacementMap.begin();
    for(; iter != replacementMap.end(); ++iter) {

        if(iter->second.IsEmpty()) continue;

        wxString pattern = iter->first;
        wxString replace = iter->second;
        pattern.Trim().Trim(false);
        replace.Trim().Trim(false);
        CLReplacement repl;
        repl.construct(pattern.To8BitData().data(), replace.To8BitData().data());
        if(repl.is_ok) {
            replacements.push_back(repl);
        }
    }

    if(replacements.empty()) return in;

    // Now, apply the replacements
    wxString outputStr;
    wxArrayString lines = ::wxStringTokenize(in, wxT("\r\n"), wxTOKEN_STRTOK);
    for(size_t i = 0; i < lines.GetCount(); i++) {
        std::string outStr = lines.Item(i).mb_str(wxConvUTF8).data();
        CLReplacementList::iterator iter = replacements.begin();
        for(; iter != replacements.end(); iter++) {
            ::CLReplacePatternA(outStr, *iter, outStr);
        }

        outputStr << wxString(outStr.c_str(), wxConvUTF8) << wxT("\n");
    }
    return outputStr;
}

int Language::DoReadClassName(CppScanner& scanner, wxString& clsname) const
{
    clsname.clear();
    int type = 0;

    while(true) {
        type = scanner.yylex();
        if(type == 0) return 0;

        if(type == IDENTIFIER) {
            clsname = scanner.YYText();

        } else if(type == '{' || type == ':') {
            return type;

        } else if(type == ';') {
            // we probably encountered a forward declaration or 'friend' statement
            clsname.Clear();
            return (int)';';
        }
    }
    return 0;
}

bool Language::InsertFunctionDecl(
    const wxString& clsname, const wxString& functionDecl, wxString& sourceContent, int visibility)
{
    // detemine the visibility requested
    int typeVisibility = lexPUBLIC;
    wxString strVisibility = wxT("public:\n");
    switch(visibility) {
    default:
    case 0:
        typeVisibility = lexPUBLIC;
        strVisibility = wxT("public:\n");
        break;

    case 1:
        typeVisibility = lexPROTECTED;
        strVisibility = wxT("protected:\n");
        break;

    case 2:
        typeVisibility = lexPRIVATE;
        strVisibility = wxT("private:\n");
        break;
    }

    // step 1: locate the class
    CppScanner scanner;
    scanner.SetText(sourceContent.mb_str(wxConvUTF8).data());

    bool success = false;
    int type = 0;
    while(true) {
        type = scanner.yylex();
        if(type == 0) {
            return false; // EOF
        }

        if(type == lexCLASS) {
            wxString name;
            type = DoReadClassName(scanner, name);
            if(type == 0) {
                return false;
            }

            if(name == clsname) {
                // We found the lex
                success = true;
                break;
            }
        }
    }

    if(!success) return false;

    // scanner is pointing on the class
    // We now need to find the first opening curly brace
    success = false;
    if(type == '{') {
        // DoReadClassName already consumed the '{' character
        // mark this as a success and continue
        success = true;

    } else {
        while(true) {
            type = scanner.yylex();
            if(type == 0) return false; // EOF

            if(type == '{') {
                success = true;
                break;
            }
        }
    }

    if(!success) return false;

    // search for requested visibility, if we could not locate it
    // locate the class ending curly brace
    success = false;
    int depth = 1;
    int visibilityLine = wxNOT_FOUND;
    int closingCurlyBraceLine = wxNOT_FOUND;
    while(true) {
        type = scanner.yylex();
        if(type == 0) break;

        if(type == typeVisibility) {
            visibilityLine = scanner.LineNo();
            break;
        }

        if(type == '{') {
            depth++;

        } else if(type == '}') {
            depth--;

            if(depth == 0) {
                // reached end of class
                closingCurlyBraceLine = scanner.LineNo();
                break;
            }
        }
    }

    wxString strToInsert;
    int insertLine = visibilityLine;
    if(visibilityLine == wxNOT_FOUND) {
        // could not locate the visibility line
        insertLine = closingCurlyBraceLine;
        strToInsert << strVisibility << functionDecl;
        insertLine--; // Place it one line on top of the curly brace

    } else {
        strToInsert << functionDecl;
    }

    if(insertLine == wxNOT_FOUND)
        // could not find any of the two
        return false;

    wxString newContent;
    wxArrayString lines = ::wxStringTokenize(sourceContent, wxT("\n"), wxTOKEN_RET_DELIMS);
    for(size_t i = 0; i < lines.GetCount(); i++) {
        if(insertLine == (int)i) {
            newContent << strToInsert;
        }
        newContent << lines.Item(i);
    }
    sourceContent = newContent;
    return true;
}

void Language::InsertFunctionImpl(const wxString& clsname, const wxString& functionImpl, const wxString& filename,
    wxString& sourceContent, int& insertedLine)
{
    insertedLine = wxNOT_FOUND;
    if(sourceContent.EndsWith(wxT("\n")) == false) sourceContent << wxT("\n");

    // What we want to do is to add our function as the last function in the scope
    ITagsStoragePtr storage = GetTagsManager()->GetDatabase();
    if(!storage) {
        // By default, append the file function to the end of the file
        sourceContent << functionImpl;
        return;
    }

    wxArrayString kinds;
    kinds.Add(wxT("function"));
    storage->SetUseCache(false);
    TagEntryPtrVector_t tags;
    storage->GetTagsByKindAndFile(kinds, filename, wxT("line"), ITagsStorage::OrderDesc, tags);
    storage->SetUseCache(true);

    if(tags.empty()) {
        // By default, append the file function to the end of the file
        sourceContent << functionImpl;
        return;
    }

    // Locate first tag from the requested scope
    TagEntryPtr tag = NULL;
    for(size_t i = 0; i < tags.size(); i++) {
        if(tags.at(i)->GetParent() == clsname) {
            tag = tags.at(i);
            break;
        }
    }

    if(!tag) {
        sourceContent << functionImpl;
        return;
    }

    int line = tag->GetLine();

    // Search for the end of this function
    // and place our function there...
    CppScanner scanner;
    scanner.SetText(sourceContent.mb_str(wxConvUTF8).data());

    int type = 0;

    // Fast forward the scanner to the line number
    while(true) {
        type = scanner.yylex();
        if(type == 0) {
            sourceContent << functionImpl;
            return;
        }

        if(scanner.LineNo() == line) break;
    }

    // search for the opening brace
    int depth = 0;
    while(true) {
        type = scanner.yylex();
        if(type == 0) {
            // EOF?
            sourceContent << functionImpl;
            return;
        }

        if(type == '{') {
            depth++;
            break;
        }
    }

    if(depth != 1) {
        // could locate the open brace
        sourceContent << functionImpl;
        return;
    }

    // now search for the closing one...
    int insertAtLine = wxNOT_FOUND;
    while(true) {
        type = scanner.yylex();
        if(type == 0) {
            // EOF?
            sourceContent << functionImpl;
            return;
        }

        if(type == '{') {
            depth++;

        } else if(type == '}') {
            depth--;

            if(depth == 0) {
                insertAtLine = scanner.lineno();
                break;
            }
        }
    }

    insertedLine = insertAtLine;

    // if we got here, it means we got a match
    wxString newContent;
    bool codeInjected = false;
    wxArrayString lines = ::wxStringTokenize(sourceContent, wxT("\n"), wxTOKEN_RET_DELIMS);
    for(size_t i = 0; i < lines.GetCount(); i++) {
        if(insertAtLine == (int)i) {
            codeInjected = true;
            newContent << functionImpl;
        }
        newContent << lines.Item(i);
    }

    if(!codeInjected) {
        newContent << functionImpl;
    }
    sourceContent = newContent;
}

int Language::GetBestLineForForwardDecl(const wxString& fileContent) const
{
    // Locating the place for adding forward declaration is one line on top of the first non comment/preprocessor
    // code. So basically we constrcut our lexer and call yylex() once (it will skip all whitespaces/comments/pp... )
    CppLexer lexer(fileContent.mb_str(wxConvISO8859_1).data());

    while(true) {
        int type = lexer.lex();
        if(type == 0) {
            // EOF
            return wxNOT_FOUND;
        }
        break;
    }
    // stc is 0 based
    int line = lexer.line_number();
    if(line) --line;
    return line;
}

void Language::UpdateAdditionalScopesCache(const wxString& filename, const std::vector<wxString>& additionalScopes)
{
    if(m_additionalScopesCache.count(filename)) {
        m_additionalScopesCache.erase(filename);
    }
    m_additionalScopesCache.insert(std::make_pair(filename, additionalScopes));
}

void Language::ClearAdditionalScopesCache() { m_additionalScopesCache.clear(); }