File: verilog.c

package info (click to toggle)
codelite 17.0.0%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 136,384 kB
  • sloc: cpp: 491,550; 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: 805; 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 (2111 lines) | stat: -rw-r--r-- 60,533 bytes parent folder | download | duplicates (3)
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
/*
 *   Copyright (c) 2003, Darren Hiebert
 *   Copyright (c) 2017, Vitor Antunes
 *   Copyright (c) 2020, Hiroo Hayashi
 *
 *   This source code is released for free distribution under the terms of the
 *   GNU General Public License version 2 or (at your option) any later version.
 *
 *   This module contains functions for generating tags for the Verilog or
 *   SystemVerilog HDL (Hardware Description Language).
 *
 *   References:
 *       IEEE Std 1800-2017, SystemVerilog Language Reference Manual
 *          https://ieeexplore.ieee.org/document/8299595
 *       SystemVerilog IEEE Std 1800-2012 Grammer
 *          https://insights.sigasi.com/tech/systemverilog.ebnf/
 *       Verilog Formal Syntax Specification
 *          http://www.verilog.com/VerilogBNF.html
 */

/*
 *   INCLUDE FILES
 */
#include "general.h"  /* must always come first */

#include <string.h>

#include "debug.h"
#include "entry.h"
#include "keyword.h"
#include "options.h"
#include "parse.h"
#include "read.h"
#include "routines.h"
#include "xtag.h"
#include "ptrarray.h"

/*
 *   MACROS
 */
#define NUMBER_LANGUAGES    2   /* Indicates number of defined indexes */
#define IDX_SYSTEMVERILOG   0
#define IDX_VERILOG         1

#ifndef DEBUG
#define VERBOSE(...) do { \
		verbose("%s:%ld:%s:%d:Internal Error:", getInputFileName(), getInputLineNumber(), __FILE__, __LINE__); \
		verbose(__VA_ARGS__); \
	} while (0)
#else
#define VERBOSE(...) do { \
		fprintf(stderr, "%s:%ld:%s:%d:Internal Error:", getInputFileName(), getInputLineNumber(), __FILE__, __LINE__); \
		fprintf(stderr, __VA_ARGS__); \
	} while (0)
#endif

/*
 *   DATA DECLARATIONS
 */

/* A callback function searching a symbol from the cork symbol table assumes
 * this kind definitions are shared in Verilog and SystemVerilog parsers.
 * If you will separate the definitions for the parsers, you must revise the
 * code related to the symbol table. */
typedef enum {
	/* parser private items */
	K_IGNORE = -16,	/* Verilog/SystemVerilog keywords to be ignored */
	K_DEFINE,
	K_DIRECTIVE,
	K_END,
	K_END_DE,	/* End of Design Elements */
	K_IDENTIFIER,
	K_LOCALPARAM,
	K_PARAMETER,
	K_IMPORT,
	K_WITH,

	K_UNDEFINED = KEYWORD_NONE,
	/* the followings items are also used as indices for VerilogKinds[] and SystemVerilogKinds[] */
	K_CONSTANT= 0,
	K_EVENT,
	K_FUNCTION,
	K_MODULE,
	K_NET,
	K_PORT,
	K_REGISTER,
	K_TASK,
	K_BLOCK,
	K_INSTANCE,
	K_ASSERTION,
	K_CLASS,
	K_COVERGROUP,
	K_ENUM,
	K_INTERFACE,
	K_MODPORT,
	K_PACKAGE,
	K_PROGRAM,
	K_PROTOTYPE,
	K_PROPERTY,
	K_STRUCT,
	K_TYPEDEF,
	K_CHECKER,
	K_CLOCKING,
	K_SEQUENCE,
	K_MEMBER,
	K_IFCLASS,	/* interface class */
	K_CONSTRAINT,
	K_NETTYPE,
} verilogKind;

typedef enum {
       R_MODULE_DECL,
} verilogModuleRole;

typedef struct {
	const char *keyword;
	verilogKind kind;
	short isValid [NUMBER_LANGUAGES];
} keywordAssoc;

typedef struct sTokenInfo {
	verilogKind         kind;
	vString*            name;          /* the name of the token */
	unsigned long       lineNumber;    /* line number where token was found */
	MIOPos              filePosition;  /* file position where token was found */
	struct sTokenInfo*  scope;         /* context of keyword */
	int                 nestLevel;     /* Current nest level */
	verilogKind         lastKind;      /* Kind of last found tag */
	vString*            blockName;     /* Current block name */
	vString*            inheritance;   /* Class inheritance */
	bool                prototype;     /* Is only a prototype */
	bool                classScope;    /* Context is local to the current sub-context */
	bool				parameter;	   /* parameter which can be overridden */
	bool				hasParamList;  /* module definition has a parameter port list */
} tokenInfo;

typedef enum {
	F_PARAMETER,
} verilogField;

/*
 *   DATA DEFINITIONS
 */
static int Ungetc;
static int Lang_verilog;
static int Lang_systemverilog;

static roleDefinition VerilogModuleRoles [] = {
 { true, "decl", "declaring instances" },
};

static roleDefinition SystemVerilogModuleRoles [] = {
 { true, "decl", "declaring instances" },
};

static kindDefinition VerilogKinds [] = {
 { true, 'c', "constant",  "constants (define, parameter, specparam)" },
 { true, 'e', "event",     "events" },
 { true, 'f', "function",  "functions" },
 { true, 'm', "module",    "modules",
   .referenceOnly = false, ATTACH_ROLES(VerilogModuleRoles) },
 { true, 'n', "net",       "net data types" },
 { true, 'p', "port",      "ports" },
 { true, 'r', "register",  "variable data types" },
 { true, 't', "task",      "tasks" },
 { true, 'b', "block",     "blocks (begin, fork)" },
 { true, 'i', "instance",  "instances of module" },
};

static kindDefinition SystemVerilogKinds [] = {
 { true, 'c', "constant",  "constants (define, parameter, specparam, enum values)" },
 { true, 'e', "event",     "events" },
 { true, 'f', "function",  "functions" },
 { true, 'm', "module",    "modules",
   .referenceOnly = false, ATTACH_ROLES(SystemVerilogModuleRoles) },
 { true, 'n', "net",       "net data types" },
 { true, 'p', "port",      "ports" },
 { true, 'r', "register",  "variable data types" },
 { true, 't', "task",      "tasks" },
 { true, 'b', "block",     "blocks (begin, fork)" },
 { true, 'i', "instance",  "instances of module or interface" },
 { true, 'A', "assert",    "assertions (assert, assume, cover, restrict)" },
 { true, 'C', "class",     "classes" },
 { true, 'V', "covergroup","covergroups" },
 { true, 'E', "enum",      "enumerators" },
 { true, 'I', "interface", "interfaces" },
 { true, 'M', "modport",   "modports" },
 { true, 'K', "package",   "packages" },
 { true, 'P', "program",   "programs" },
 { false,'Q', "prototype", "prototypes (extern, pure)" },
 { true, 'R', "property",  "properties" },
 { true, 'S', "struct",    "structs and unions" },
 { true, 'T', "typedef",   "type declarations" },
 { true, 'H', "checker",   "checkers" },
 { true, 'L', "clocking",  "clocking" },
 { true, 'q', "sequence",  "sequences" },
 { true, 'w', "member",    "struct and union members" },
 { true, 'l', "ifclass",   "interface class" },
 { true, 'O', "constraint","constraints" },
 { true, 'N', "nettype",   "nettype declarations" },
};

static const keywordAssoc KeywordTable [] = {
	/*                 	             	  SystemVerilog */
	/*                 	             	  |  Verilog    */
	/* keyword         	keyword ID   	  |  |          */
	{ "`define",       	K_DEFINE,   	{ 1, 1 } },
	{ "begin",         	K_BLOCK,     	{ 1, 1 } },
	{ "end",           	K_END,       	{ 1, 1 } },
	{ "endfunction",   	K_END_DE,    	{ 1, 1 } },
	{ "endmodule",     	K_END_DE,    	{ 1, 1 } },
	{ "endtask",       	K_END_DE,    	{ 1, 1 } },
	{ "event",         	K_EVENT,     	{ 1, 1 } },
	{ "fork",          	K_BLOCK,     	{ 1, 1 } },
	{ "function",      	K_FUNCTION,  	{ 1, 1 } },
	{ "genvar",        	K_REGISTER,  	{ 1, 1 } },
	{ "inout",         	K_PORT,      	{ 1, 1 } },
	{ "input",         	K_PORT,      	{ 1, 1 } },
	{ "integer",       	K_REGISTER,  	{ 1, 1 } },
	{ "join",          	K_END,       	{ 1, 1 } },
	{ "localparam",    	K_LOCALPARAM,  	{ 1, 1 } },
	{ "module",        	K_MODULE,    	{ 1, 1 } },
	{ "output",        	K_PORT,      	{ 1, 1 } },
	{ "parameter",     	K_PARAMETER,  	{ 1, 1 } },
	{ "real",          	K_REGISTER,  	{ 1, 1 } },
	{ "realtime",      	K_REGISTER,  	{ 1, 1 } },
	{ "reg",           	K_REGISTER,  	{ 1, 1 } },
	{ "signed",        	K_IGNORE,    	{ 1, 1 } },
	{ "specparam",     	K_CONSTANT,  	{ 1, 1 } },
	{ "supply0",       	K_NET,       	{ 1, 1 } },
	{ "supply1",       	K_NET,       	{ 1, 1 } },
	{ "task",          	K_TASK,      	{ 1, 1 } },
	{ "time",          	K_REGISTER,  	{ 1, 1 } },
	{ "tri",           	K_NET,       	{ 1, 1 } },
	{ "triand",        	K_NET,       	{ 1, 1 } },
	{ "trior",         	K_NET,       	{ 1, 1 } },
	{ "trireg",        	K_NET,       	{ 1, 1 } },
	{ "tri0",          	K_NET,       	{ 1, 1 } },
	{ "tri1",          	K_NET,       	{ 1, 1 } },
	{ "uwire",         	K_NET,       	{ 1, 1 } },
	{ "wand",          	K_NET,       	{ 1, 1 } },
	{ "wire",          	K_NET,       	{ 1, 1 } },
	{ "wor",           	K_NET,       	{ 1, 1 } },
	{ "assert",        	K_ASSERTION, 	{ 1, 0 } },
	{ "assume",        	K_ASSERTION, 	{ 1, 0 } },
	{ "bit",           	K_REGISTER,  	{ 1, 0 } },
	{ "byte",          	K_REGISTER,  	{ 1, 0 } },
	{ "chandle",       	K_REGISTER,  	{ 1, 0 } },
	{ "checker",       	K_CHECKER,  	{ 1, 0 } },
	{ "class",         	K_CLASS,     	{ 1, 0 } },
	{ "constraint",   	K_CONSTRAINT, 	{ 1, 0 } },
	{ "cover",         	K_ASSERTION, 	{ 1, 0 } },
	{ "clocking",       K_CLOCKING,     { 1, 0 } },
	{ "covergroup",    	K_COVERGROUP,	{ 1, 0 } },
	{ "endchecker",    	K_END_DE,    	{ 1, 0 } },
	{ "endclass",      	K_END_DE,    	{ 1, 0 } },
	{ "endclocking",    K_END_DE,     	{ 1, 0 } },
	{ "endgroup",      	K_END_DE,    	{ 1, 0 } },
	{ "endinterface",  	K_END_DE,    	{ 1, 0 } },
	{ "endpackage",    	K_END_DE,    	{ 1, 0 } },
	{ "endprogram",    	K_END_DE,    	{ 1, 0 } },
	{ "endproperty",   	K_END_DE,    	{ 1, 0 } },
	{ "endsequence",   	K_END_DE,    	{ 1, 0 } },
	{ "enum",          	K_ENUM,      	{ 1, 0 } },
	{ "extern",        	K_PROTOTYPE, 	{ 1, 0 } },
	{ "import",        	K_IMPORT,	  	{ 1, 0 } },
	{ "int",           	K_REGISTER,  	{ 1, 0 } },
	{ "interconnect",  	K_NET,       	{ 1, 0 } },
	{ "interface",     	K_INTERFACE, 	{ 1, 0 } },
	{ "join_any",      	K_END,       	{ 1, 0 } },
	{ "join_none",     	K_END,       	{ 1, 0 } },
	{ "logic",         	K_REGISTER,  	{ 1, 0 } },
	{ "longint",       	K_REGISTER,  	{ 1, 0 } },
	{ "modport",       	K_MODPORT,   	{ 1, 0 } },
	{ "package",       	K_PACKAGE,   	{ 1, 0 } },
	{ "program",       	K_PROGRAM,   	{ 1, 0 } },
	{ "property",      	K_PROPERTY,  	{ 1, 0 } },
	{ "pure",          	K_PROTOTYPE, 	{ 1, 0 } },
	{ "ref",           	K_PORT,      	{ 1, 0 } },
	{ "restrict",      	K_ASSERTION, 	{ 1, 0 } },
	{ "sequence",      	K_SEQUENCE,  	{ 1, 0 } },
	{ "shortint",      	K_REGISTER,  	{ 1, 0 } },
	{ "shortreal",     	K_REGISTER,  	{ 1, 0 } },
	{ "string",        	K_REGISTER,  	{ 1, 0 } },
	{ "struct",        	K_STRUCT,    	{ 1, 0 } },
	{ "type",          	K_REGISTER,  	{ 1, 0 } },
	{ "typedef",       	K_TYPEDEF,   	{ 1, 0 } },
	{ "union",         	K_STRUCT,    	{ 1, 0 } },
	{ "var",           	K_REGISTER,  	{ 1, 0 } },
	{ "void",          	K_REGISTER,  	{ 1, 0 } },
	{ "with",          	K_WITH,			{ 1, 0 } },
	{ "nettype",       	K_NETTYPE,		{ 1, 0 } },
//	{ "virtual",       	K_PROTOTYPE,	{ 1, 0 } },		// do not add for now
};

static tokenInfo *currentContext = NULL;
static ptrArray *tagContents;
static fieldDefinition *fieldTable = NULL;

// IEEE Std 1364-2005 LRM, Appendix B "List of Keywords"
const static struct keywordGroup verilogKeywords = {
	.value = K_IGNORE,
	.addingUnlessExisting = true,
	.keywords = {
		"always", "and", "assign", "automatic", "begin", "buf", "bufif0",
		"bufif1", "case", "casex", "casez", "cell", "cmos", "config",
		"deassign", "default", "defparam", "design", "disable", "edge",
		"else", "end", "endcase", "endconfig", "endfunction", "endgenerate",
		"endmodule", "endprimitive", "endspecify", "endtable", "endtask",
		"event", "for", "force", "forever", "fork", "function", "generate",
		"genvar", "highz0", "highz1", "if", "ifnone", "incdir", "include",
		"initial", "inout", "input", "instance", "integer", "join", "large",
		"liblist", "library", "localparam", "macromodule", "medium", "module",
		"nand", "negedge", "nmos", "nor", "noshowcancelled", "not", "notif0",
		"notif1", "or", "output", "parameter", "pmos", "posedge", "primitive",
		"pull0", "pull1", "pulldown", "pullup", "pulsestyle_onevent",
		"pulsestyle_ondetect", "rcmos", "real", "realtime", "reg", "release",
		"repeat", "rnmos", "rpmos", "rtran", "rtranif0", "rtranif1",
		"scalared", "showcancelled", "signed", "small", "specify",
		"specparam", "strong0", "strong1", "supply0", "supply1", "table",
		"task", "time", "tran", "tranif0", "tranif1", "tri", "tri0", "tri1",
		"triand", "trior", "trireg", "unsigned1", "use", "uwire", "vectored",
		"wait", "wand", "weak0", "weak1", "while", "wire", "wor", "xnor", "xor",
		NULL
	},
};
// IEEE Std 1800-2017 LRM, Annex B "Keywords"
const static struct keywordGroup systemVerilogKeywords = {
	.value = K_IGNORE,
	.addingUnlessExisting = true,
	.keywords = {
		"accept_on", "alias", "always", "always_comb", "always_ff",
		"always_latch", "and", "assert", "assign", "assume", "automatic",
		"before", "begin", "bind", "bins", "binsof", "bit", "break", "buf",
		"bufif0", "bufif1", "byte", "case", "casex", "casez", "cell",
		"chandle", "checker", "class", "clocking", "cmos", "config", "const",
		"constraint", "context", "continue", "cover", "covergroup",
		"coverpoint", "cross", "deassign", "default", "defparam", "design",
		"disable", "dist", "do", "edge", "else", "end", "endcase",
		"endchecker", "endclass", "endclocking", "endconfig", "endfunction",
		"endgenerate", "endgroup", "endinterface", "endmodule", "endpackage",
		"endprimitive", "endprogram", "endproperty", "endspecify",
		"endsequence", "endtable", "endtask", "enum", "event", "eventually",
		"expect", "export", "extends", "extern", "final", "first_match",
		"for", "force", "foreach", "forever", "fork", "forkjoin", "function",
		"generate", "genvar", "global", "highz0", "highz1", "if", "iff",
		"ifnone", "ignore_bins", "illegal_bins", "implements", "implies",
		"import", "incdir", "include", "initial", "inout", "input", "inside",
		"instance", "int", "integer", "interconnect", "interface",
		"intersect", "join", "join_any", "join_none", "large", "let",
		"liblist", "library", "local", "localparam", "logic", "longint",
		"macromodule", "matches", "medium", "modport", "module", "nand",
		"negedge", "nettype", "new", "nexttime", "nmos", "nor",
		"noshowcancelled", "not", "notif0", "notif1", "null", "or", "output",
		"package", "packed", "parameter", "pmos", "posedge", "primitive",
		"priority", "program", "property", "protected", "pull0", "pull1",
		"pulldown", "pullup", "pulsestyle_ondetect", "pulsestyle_onevent",
		"pure", "rand", "randc", "randcase", "randsequence", "rcmos", "real",
		"realtime", "ref", "reg", "reject_on", "release", "repeat",
		"restrict", "return", "rnmos", "rpmos", "rtran", "rtranif0",
		"rtranif1", "s_always", "s_eventually", "s_nexttime", "s_until",
		"s_until_with", "scalared", "sequence", "shortint", "shortreal",
		"showcancelled", "signed", "small", "soft", "solve", "specify",
		"specparam", "static", "string", "strong", "strong0", "strong1",
		"struct", "super", "supply0", "supply1", "sync_accept_on",
		"sync_reject_on", "table", "tagged", "task", "this", "throughout",
		"time", "timeprecision", "timeunit", "tran", "tranif0", "tranif1",
		"tri", "tri0", "tri1", "triand", "trior", "trireg", "type", "typedef",
		"union", "unique", "unique0", "unsigned", "until", "until_with",
		"untyped", "use", "uwire", "var", "vectored", "virtual", "void",
		"wait", "wait_order", "wand", "weak", "weak0", "weak1", "while",
		"wildcard", "wire", "with", "within", "wor", "xnor", "xor",
		NULL
	},
};

// IEEE Std 1364-2005 LRM, "19. Compiler directives"
const static struct keywordGroup verilogDirectives = {
	.value = K_DIRECTIVE,
	.addingUnlessExisting = true,
	.keywords = {
		"`begin_keywords", "`celldefine", "`default_nettype", "`define",
		"`else", "`elsif", "`end_keywords", "`endcelldefine", "`endif",
		"`ifdef", "`ifndef", "`include", "`line", "`nounconnected_drive",
		"`pragma", "`resetall", "`timescale", "`unconnected_drive", "`undef",
		NULL
	},
};

// IEEE Std 1800-2017 LRM, "22. Compiler directives"
const static struct keywordGroup systemVerilogDirectives = {
	.value = K_DIRECTIVE,
	.addingUnlessExisting = true,
	.keywords = {
		"`__LINE__", "`begin_keywords", "`celldefine", "`default_nettype",
		"`define", "`else", "`elsif", "`end_keywords", "`endcelldefine",
		"`endif", "`ifdef", "`ifndef", "`include", "`line",
		"`nounconnected_drive", "`pragma", "`resetall", "`timescale",
		"`unconnected_drive", "`undef", "`undefineall",
		NULL
	},
};

// .enabled field cannot be shared by two languages
static fieldDefinition VerilogFields[] = {
	{ .name = "parameter",
	  .description = "parameter whose value can be overridden.",
	  .enabled = false,
	  .dataType = FIELDTYPE_BOOL },
};

static fieldDefinition SystemVerilogFields[] = {
	{ .name = "parameter",
	  .description = "parameter whose value can be overridden.",
	  .enabled = false,
	  .dataType = FIELDTYPE_BOOL },
};

/*
 *   PROTOTYPE DEFINITIONS
 */

static bool isIdentifier (tokenInfo* token);
static int processDefine (tokenInfo *const token, int c);
static int processType (tokenInfo* token, int c, verilogKind* kind, bool* with);
static int pushEnumNames (tokenInfo* token, int c);
static int pushMembers (tokenInfo* token, int c);
static int readWordToken (tokenInfo *const token, int c);
static int readWordTokenNoSkip (tokenInfo *const token, int c);
static int skipBlockName (tokenInfo *const token, int c);
static int skipClockEvent (tokenInfo* token, int c);
static int skipDelay (tokenInfo* token, int c);
static int tagIdsInPort (tokenInfo *const token, int c, verilogKind kind, bool mayPortDecl);
static int tagIdsInDataDecl (tokenInfo* token, int c, verilogKind kind);

/*
 *   FUNCTION DEFINITIONS
 */

static short isContainer (verilogKind kind)
{
	switch (kind)
	{
		case K_MODULE:
		case K_TASK:
		case K_FUNCTION:
		case K_BLOCK:
		case K_CHECKER:
		case K_CLASS:
		case K_CLOCKING:
		case K_COVERGROUP:
		case K_IFCLASS:
		case K_INTERFACE:
		case K_PACKAGE:
		case K_PROGRAM:
		case K_PROPERTY:
		case K_SEQUENCE:
		case K_TYPEDEF:
		case K_NETTYPE:
		case K_ENUM:
		case K_STRUCT:
			return true;
		default:
			return false;
	}
}

static short isTempContext (tokenInfo const* token)
{
	switch (token->kind)
	{
		case K_TYPEDEF:
		case K_NETTYPE:
		case K_ENUM:
		case K_STRUCT:
			return true;
		default:
			return false;
	}
}

static void clearToken (tokenInfo *token)
{
	token->kind = K_UNDEFINED;	// to be set by updateKind()
	vStringClear (token->name);
	token->lineNumber = getInputLineNumber ();
	token->filePosition = getInputFilePosition ();
	token->scope = NULL;
	token->nestLevel = 0;
	token->lastKind = K_UNDEFINED;
	vStringClear (token->blockName);
	vStringClear (token->inheritance);
	token->prototype = false;
	token->classScope = false;
	token->parameter = false;
	token->hasParamList = false;
}

static tokenInfo *newToken (void)
{
	tokenInfo *const token = xMalloc (1, tokenInfo);
	token->name = vStringNew ();
	token->blockName = vStringNew ();
	token->inheritance = vStringNew ();
	clearToken (token);
	return token;
}

static tokenInfo *dupToken (tokenInfo *token)
{
	tokenInfo *dup = newToken ();
	tokenInfo tmp = *dup;	// save vStrings, name, blockName, and inheritance
	*dup = *token;
	// revert vStrings allocated for dup
	dup->name = tmp.name;
	dup->blockName = tmp.blockName;
	dup->inheritance = tmp.inheritance;
	// copy contents of vStrings
	vStringCopy (dup->name, token->name);
	vStringCopy (dup->blockName, token->blockName);
	vStringCopy (dup->inheritance, token->inheritance);
	return dup;
}

static void deleteToken (tokenInfo * const token)
{
	if (token != NULL)
	{
		vStringDelete (token->name);
		vStringDelete (token->blockName);
		vStringDelete (token->inheritance);
		eFree (token);
	}
}

static tokenInfo *pushToken (tokenInfo * const token, tokenInfo * const tokenPush)
{
	tokenPush->scope = token;
	return tokenPush;
}

static tokenInfo *popToken (tokenInfo * const token)
{
	tokenInfo *localToken;
	if (token != NULL)
	{
		localToken = token->scope;
		deleteToken (token);
		return localToken;
	}
	return NULL;
}

static void pruneTokens (tokenInfo * token)
{
	while ((token = popToken (token)))
		;
}

static void swapToken (tokenInfo *t0, tokenInfo *t1)
{
	tokenInfo tmp = *t0;
	*t0 = *t1;
	*t1 = tmp;
}

static const char *getNameForKind (const verilogKind kind)
{
	if (isInputLanguage (Lang_systemverilog))
		return (SystemVerilogKinds[kind]).name;
	else /* isInputLanguage (Lang_verilog) */
		return (VerilogKinds[kind]).name;
}

static char kindEnabled (const verilogKind kind)
{
	if (isInputLanguage (Lang_systemverilog))
		return SystemVerilogKinds[kind].enabled;
	else /* isInputLanguage (Lang_verilog) */
		return VerilogKinds[kind].enabled;
}

static void buildKeywordHash (const langType language, unsigned int idx)
{
	size_t i;
	const size_t count = ARRAY_SIZE (KeywordTable);
	for (i = 0  ;  i < count  ;  ++i)
	{
		const keywordAssoc *p = &KeywordTable [i];
		if (p->isValid [idx])
			addKeyword (p->keyword, language, (int) p->kind);
	}
}

static void initializeVerilog (const langType language)
{
	Lang_verilog = language;
	buildKeywordHash (language, IDX_VERILOG);
	addKeywordGroup (&verilogKeywords, language);
	addKeywordGroup (&verilogDirectives, language);
	if (tagContents == NULL)
		tagContents = ptrArrayNew ((ptrArrayDeleteFunc)deleteToken);

}

static void initializeSystemVerilog (const langType language)
{
	Lang_systemverilog = language;
	buildKeywordHash (language, IDX_SYSTEMVERILOG);
	addKeywordGroup (&systemVerilogKeywords, language);
	addKeywordGroup (&systemVerilogDirectives, language);
	if (tagContents == NULL)
		tagContents = ptrArrayNew ((ptrArrayDeleteFunc)deleteToken);
}

static void vUngetc (int c)
{
	Assert (Ungetc == '\0');
	Ungetc = c;
}

/* Mostly copied from cppSkipOverCComment() in cpreprocessor.c.
 *
 * cppSkipOverCComment() uses the internal ungetc buffer of
 * CPreProcessor.  On the other hand, the Verilog parser uses
 * getcFromInputFile() directly. getcFromInputFile() uses just
 * another internal ungetc buffer. Using them mixed way will
 * cause a trouble. */
static int verilogSkipOverCComment (void)
{
	int c =  getcFromInputFile ();

	while (c != EOF)
	{
		if (c != '*')
			c = getcFromInputFile ();
		else
		{
			const int next = getcFromInputFile ();

			if (next != '/')
				c = next;
			else
			{
				c = SPACE;  /* replace comment with space */
				break;
			}
		}
	}
	return c;
}

static int _vGetc (bool inSkipPastMatch)
{
	int c;
	if (Ungetc == '\0')
		c = getcFromInputFile ();
	else
	{
		c = Ungetc;
		Ungetc = '\0';
	}
	if (c == '/')
	{
		int c2 = getcFromInputFile ();
		if (c2 == EOF)
			return EOF;
		else if (c2 == '/')  /* strip comment until end-of-line */
		{
			do
				c = getcFromInputFile ();
			while (c != '\n'  &&  c != EOF);
		}
		else if (c2 == '*')  /* strip block comment */
			c = verilogSkipOverCComment ();
		else
			ungetcToInputFile (c2);
	}
	// replace a string with "@" only in skipPastMatch()
	// because the string may contain parens, etc.
	else if (inSkipPastMatch && c == '"')
	{
		int c2;
		do
			c2 = getcFromInputFile ();
		while (c2 != '"'  &&  c2 != EOF);
		c = '@';
	}
	return c;
}

static int vGetc (void)
{
	return _vGetc (false);
}

// Is the first charactor in an identifier? [a-zA-Z_`]
static bool isWordToken (const int c)
{
	return (isalpha (c) || c == '_' || c == '`');
}

// Is a charactor in an identifier? [a-zA-Z0-9_`$]
static bool isIdentifierCharacter (const int c)
{
	return (isalnum (c) || c == '_' || c == '`' || c == '$');
}

// check if double colon.
static bool isDoubleColon (int c)
{
	if (c != ':')
		return false;
	c = vGetc ();
	if (c == ':') {
		return true;
	} else {
		vUngetc (c);
		return false;
	}
}

static int skipWhite (int c)
{
	while (isspace (c))
		c = vGetc ();
	return c;
}

static int skipPastMatch (const char *const pair)
{
	const int begin = pair [0], end = pair [1];
	int matchLevel = 1;
	int c;
	do
	{
		c = _vGetc (true);
		if (c == begin)
			++matchLevel;
		else if (c == end)
			--matchLevel;
	}
	while (matchLevel > 0 && c != EOF);
	return skipWhite (vGetc ());
}

static int skipDimension (int c)
{
	while (c == '[' && c != EOF)
		c = skipPastMatch ("[]");
	return c;
}

static int skipToSemiColon (int c)
{
	while (c != ';' && c != EOF)
		c = vGetc ();
	return c;	// ';' or EOF
}

static int skipString (int c)
{
	if (c == '"')
	{
		do
			c = vGetc ();
		while (c != '"' && c != EOF);
	}
	c = skipWhite (vGetc ());
	return c;
}

static int skipExpression (int c)
{
	while (c != ','  &&  c != ';' && c != ')' && c != '}' && c != ']' && c != EOF)
	{
		if (c == '(')
			c = skipPastMatch ("()");
		else if (c == '{')
			c = skipPastMatch ("{}");
		else if (c == '[')
			c = skipPastMatch ("[]");
		else if (c == '"')
			c = skipString (c);
		else
			c = skipWhite (vGetc ());
	}
	return c;
}

// Skip to newline. The newline preceded by a backslash ( \ ) is ignored.
// Should be used after readWordTokenNoSkip() for compiler directives
static int skipToNewLine (int c)
{
	int prev = EOF; // The previous char of 'c' never be a EOF.
	while (!((prev != '\\') && (c == '\n')) && (c != EOF)) {
		prev = c;
		// Getc() does not work for a comment in multi-line macro
		c = getcFromInputFile ();
	}
	Assert(c == '\n' || c == EOF);
	return c;
}

static int skipMacro (int c, tokenInfo *token)
{
	tokenInfo *localToken = newToken ();	// don't update token outside
	while (c == '`')	// to support back-to-back compiler directives
	{
		c = readWordTokenNoSkip (localToken, c);
		/* Skip compiler directive other than `define */
		if (localToken->kind == K_DIRECTIVE)
		{
			c = skipToNewLine (c);
			c = skipWhite (c);
		}
		/* Skip `define */
		else if (localToken->kind == K_DEFINE)
		{
			c = skipWhite (c);
			c = processDefine (localToken, c);
		}
		/* return macro expansion */
		else
		{
			swapToken (token, localToken);
			c = skipWhite (c);
			if (c == '(')
				c = skipPastMatch ("()");
			break;
		}
	}
	deleteToken (localToken);
	return c;
}

static void _updateKind (tokenInfo *const token)
{
	verilogKind kind = (verilogKind) lookupKeyword (vStringValue (token->name), getInputLanguage () );
	token->kind = ((kind == K_UNDEFINED) && isIdentifier (token)) ? K_IDENTIFIER : kind;
}

/* read an identifier, keyword, number, compiler directive, or macro identifier */
static int _readWordToken (tokenInfo *const token, int c, bool skip)
{
	Assert (isWordToken (c));

	clearToken (token);
	do
	{
		vStringPut (token->name, c);
		c = vGetc ();
	} while (isIdentifierCharacter (c));
	_updateKind (token);

	if (skip)
		return skipWhite (c);
	else
		return c;
}

// read a word token starting with "c".
// returns the first charactor of the next token.
static int readWordToken (tokenInfo *const token, int c)
{
	return _readWordToken (token, c, true);
}

// read a word token starting with "c".
// returns the next charactor of the token read.
// for compiler directives.  Since they are line-based, skipWhite() cannot be used.
static int readWordTokenNoSkip (tokenInfo *const token, int c)
{
	return _readWordToken (token, c, false);
}

/* check if an identifier:
 *   simple_identifier ::= [ a-zA-Z_ ] { [ a-zA-Z0-9_$ ] } */
static bool isIdentifier (tokenInfo* token)
{
	if (token->kind == K_UNDEFINED)
	{
		for (int i = 0; i < vStringLength (token->name); i++)
		{
			int c = vStringChar (token->name, i);
			if (i == 0)
			{
				if (c == '`' || !isWordToken (c))
					return false;
			}
			else
			{
				if (!isIdentifierCharacter (c))
					return false;
			}
		}
		return true;
	}
	else
		return false;
}

static void createContext (verilogKind kind, vString* const name)
{
	tokenInfo *const scope = newToken ();
	vStringCopy (scope->name, name);
	scope->kind = kind;

	if (scope)
	{
		vString *contextName = vStringNew ();

		/* Determine full context name */
		if (currentContext->kind != K_UNDEFINED)
		{
			vStringCopy (contextName, currentContext->name);
			vStringPut (contextName, '.');
		}
		vStringCat (contextName, scope->name);
		/* Create context */
		currentContext = pushToken (currentContext, scope);
		vStringCopy (currentContext->name, contextName);
		vStringDelete (contextName);
		verbose ("Created new context %s (kind %d)\n", vStringValue (currentContext->name), currentContext->kind);
	}
}

static void dropContext ()
{
	verbose ("Dropping context %s\n", vStringValue (currentContext->name));
	currentContext = popToken (currentContext);
}

/* Drop context, but only if an end token is found */
static int dropEndContext (tokenInfo *const token, int c)
{
	verbose ("current context %s; context kind %0d; nest level %0d\n", vStringValue (currentContext->name), currentContext->kind, currentContext->nestLevel);
	if ((currentContext->kind == K_COVERGROUP && strcmp (vStringValue (token->name), "endgroup") == 0)
	    || (currentContext->kind == K_IFCLASS && strcmp (vStringValue (token->name), "endclass") == 0))
	{
		dropContext ();
		c = skipBlockName (token ,c);
	}
	else if (currentContext->kind != K_UNDEFINED)
	{
		vString *endTokenName = vStringNewInit ("end");
		vStringCatS (endTokenName, getNameForKind (currentContext->kind));
		if (strcmp (vStringValue (token->name), vStringValue (endTokenName)) == 0)
		{
			dropContext ();
			c = skipBlockName (token ,c);
			if (currentContext->classScope)
			{
				verbose ("Dropping local context %s\n", vStringValue (currentContext->name));
				currentContext = popToken (currentContext);
			}
		}
		vStringDelete (endTokenName);
	}
	else
		VERBOSE ("Unexpected current context %s\n", vStringValue (currentContext->name));
	return c;
}


static void createTagFull (tokenInfo *const token, verilogKind kind, int role, tokenInfo *const typeref)
{
	tagEntryInfo tag;

	if (kind == K_LOCALPARAM)
		kind = K_CONSTANT;
	else if (kind == K_PARAMETER)
	{
		kind = K_CONSTANT;
		// See LRM 2017 6.20.1 Parameter declaration syntax
		if (currentContext->kind != K_CLASS && currentContext->kind != K_PACKAGE && !currentContext->hasParamList)
			token->parameter = true;
	}
	Assert (kind >= 0 && kind != K_UNDEFINED && kind != K_IDENTIFIER);
	Assert (vStringLength (token->name) > 0);

	/* check if a container before kind is modified by prototype */
	/* BTW should we create a context for a prototype? */
	bool container = isContainer (kind);

	/* Determine if kind is prototype */
	if (currentContext->prototype)
		kind = K_PROTOTYPE;

	/* Do nothing if tag kind is disabled */
	if (! kindEnabled (kind))
	{
		verbose ("kind disabled\n");
		return;
	}

	/* Create tag */
	if (role == ROLE_DEFINITION_INDEX)
		initTagEntry (&tag, vStringValue (token->name), kind);
	else
		initRefTagEntry (&tag, vStringValue (token->name), kind, role);
	tag.lineNumber = token->lineNumber;
	tag.filePosition = token->filePosition;

	verbose ("Adding tag %s (kind %d)", vStringValue (token->name), kind);
	if (currentContext->kind != K_UNDEFINED)
	{
		verbose (" to context %s\n", vStringValue (currentContext->name));
		currentContext->lastKind = kind;
		tag.extensionFields.scopeKindIndex = currentContext->kind;
		tag.extensionFields.scopeName = vStringValue (currentContext->name);
	}
	verbose ("\n");
	if (vStringLength (token->inheritance) > 0)
	{
		tag.extensionFields.inheritance = vStringValue (token->inheritance);
		verbose ("Class %s extends %s\n", vStringValue (token->name), tag.extensionFields.inheritance);
	}

	if (typeref)
	{
		tag.extensionFields.typeRef [0] = getNameForKind (typeref->kind);
		tag.extensionFields.typeRef [1] = vStringValue (typeref->name);
	}

	if (token->parameter)
		attachParserField (&tag, false, fieldTable [F_PARAMETER].ftype, "");

	makeTagEntry (&tag);

	if (isXtagEnabled (XTAG_QUALIFIED_TAGS) && currentContext->kind != K_UNDEFINED
		&& role == ROLE_DEFINITION_INDEX)
	{
		vString *const scopedName = vStringNew ();

		vStringCopy (scopedName, currentContext->name);
		vStringPut (scopedName, '.');
		vStringCat (scopedName, token->name);
		tag.name = vStringValue (scopedName);

		markTagExtraBit (&tag, XTAG_QUALIFIED_TAGS);
		makeTagEntry (&tag);

		vStringDelete (scopedName);
	}

	/* Push token as context if it is a container */
	if (container && role == ROLE_DEFINITION_INDEX)
	{
		createContext (kind, token->name);

		/* Put found contents in context */
		verbose ("Putting tagContents: %d element(s)\n",
				 ptrArrayCount (tagContents));
		for (unsigned int i = 0; i < ptrArrayCount (tagContents); i++)
		{
			tokenInfo *content = ptrArrayItem (tagContents, i);
			createTagFull (content, content->kind, ROLE_DEFINITION_INDEX, NULL);
		}

		/* Drop temporary contexts */
		if (isTempContext (currentContext))
			dropContext ();
	}

	/* Clear no longer required inheritance information */
	vStringClear (token->inheritance);
}

static void createTagWithTypeRef (tokenInfo *const token, verilogKind kind, tokenInfo *const typeref)
{
	createTagFull (token, kind, ROLE_DEFINITION_INDEX, typeref);
}

static void createTag (tokenInfo *const token, verilogKind kind)
{
	createTagWithTypeRef (token, kind, NULL);
}

static void createRefTag (tokenInfo *const token, verilogKind kind, int role)
{
	createTagFull (token, kind, role, NULL);
}

static int skipBlockName (tokenInfo *const token, int c)
{
	if (c == ':')
	{
		c = skipWhite (vGetc ());
		if (isWordToken (c))
			c = readWordToken (token, c);
	}
	return c;
}

// begin, fork
static int processBlock (tokenInfo *const token, int c)
{
	if (c == ':')	// tag an optional block identifier
	{
		c = skipWhite (vGetc ());
		if (isWordToken (c))
		{
			c = readWordToken (token, c);
			verbose ("Found block: %s\n", vStringValue (token->name));
			createTag (token, K_BLOCK);
			verbose ("Current context %s\n", vStringValue (currentContext->name));
		}
	}
	currentContext->nestLevel++;	// increment after creating a context
	return c;
}

// end, join, join_any, join_none
static int processEnd (tokenInfo *const token, int c)
{
	if (currentContext->nestLevel > 0)	// for sanity check
		currentContext->nestLevel--;
	if (currentContext->kind == K_BLOCK && currentContext->nestLevel == 0)
		dropContext ();

	c = skipBlockName (token, c);
	return c;
}

static int processPortList (tokenInfo *token, int c, bool mayPortDecl)
{
	if (c == '(')
	{
		c = skipWhite (vGetc ());	// skip '('
		c = tagIdsInPort (token, c, K_PORT, mayPortDecl);
		if (c == ')')	// sanity check
			c = skipWhite (vGetc ());
		else
			VERBOSE ("Unexpected input: %c\n", c);
	}
	return c;
}

static int skipParameterAssignment (int c)
{
	if (c == '#')
	{
		c = skipWhite (vGetc ());
		if (c == '(')
			c = skipPastMatch ("()");
	}
	return c;
}

// Functions are treated differently because they may also include the type of the return value.
// Tasks are treated in the same way, although not having a return value.
//
// function [ lifetime ] function_data_type_or_implicit [ interface_identifier . | class_scope ] function_identifier [ ( [ tf_port_list ] ) ] ;
// task     [ lifetime ] task_body_declaration          [ interface_identifier . | class_scope ] task_identifier     [ ( [ tf_port_list ] ) ] ;
static int processFunction (tokenInfo *const token, int c)
{
	verilogKind kind = token->kind;	// K_FUNCTION or K_TASK

	/* Search for function name
	 * Last identifier found before a '(' or a ';' is the function name */
	while (c != '(' && c != ';' && c != EOF)
	{
		if (isWordToken (c))
			c = readWordToken (token, c);
		else
			c = skipWhite (vGetc ());
		/* skip parameter assignment of a class type
		 *    ex. function uvm_port_base #(IF) get_if(int index=0); */
		c = skipParameterAssignment (c);

		/* Identify class type prefixes and create respective context*/
		if (isInputLanguage (Lang_systemverilog) && isDoubleColon(c))
		{
			verbose ("Found function declaration with class type %s\n", vStringValue (token->name));
			createContext (K_CLASS, token->name);
			currentContext->classScope = true;
		}
	}
	verbose ("Found function: %s\n", vStringValue (token->name));
	createTag (token, kind);

	/* Get port list from function */
	c = skipWhite (c);
	c = processPortList (token, c, false);
	return c;
}

// ( enum | union ) [ enum_base_type ] { < enum_name_declaration > }  { [ ... ] }
static int processEnum (tokenInfo *const token, int c)
{
	tokenInfo* enumToken = dupToken (token);	// save enum token

	/* skip enum_base_type */
	while (isWordToken (c))
		c = readWordToken (token, c);
	c = skipDimension (c);

	/* Search enum elements */
	c = pushEnumNames (token, c);

	/* Skip bus width definition */
	c = skipDimension (c);

	/* Following identifiers are tag names */
	verbose ("Find enum tags. Token %s kind %d\n", vStringValue (enumToken->name), enumToken->kind);
	c = tagIdsInDataDecl (enumToken, c, enumToken->kind);
	deleteToken (enumToken);

	// Clean up the tag content list at the end of the declaration to support multiple variables
	//   enum { ... } foo, bar;
	ptrArrayClear (tagContents);
	return c;
}

// [ struct | union [ tagged ] ] [ packed [ signed | unsigned ] ] { struct_union_member { struct_union_member } } { [ ... ] }
static int processStruct (tokenInfo *const token, int c)
{
	verilogKind kind = token->kind;	// K_STRUCT, K_TYPEDEF, or K_NETTYPE

	/* Skip packed, signed, and unsigned */
	while (isWordToken (c))
		c = readWordToken (token, c);

	/* create a list of members */
	c = pushMembers (token, c);

	/* Skip packed_dimension */
	c = skipDimension (c);

	/* Following identifiers are tag names */
	verbose ("Find struct|union tags. Token %s kind %d\n", vStringValue (token->name), token->kind);
	c = tagIdsInDataDecl (token, c, kind);
	ptrArrayClear (tagContents);
	return c;
}

// data_declaration ::=
//       [ const ] [ var ] [ static | automatic ] data_type_or_implicit list_of_variable_decl_assignments ;
//     | typedef data_type type_identifier { [ ... ] } ;
//     | typedef interface_instance_identifier [ ... ] . type_identifier type_identifier ; // interface based typedef
//     | typedef [ enum | struct | union | class | interface class ] type_identifier ;
//     | import < package_import_item > ;
//     | nettype data_type net_type_identifier [ with [ class_type :: | package_identifier :: | $unit :: ] tf_identifier ] ;
//     | nettype [ class_type :: | package_identifier :: | $unit :: ] net_type_identifier net_type_identifier ;
static int processTypedef (tokenInfo *const token, int c)
{
	verilogKind kindSave = token->kind;	// K_TYPEDEF or K_NETTYPE
	verilogKind kind = K_UNDEFINED;
	bool not_used;
	if (isWordToken (c))
	{
		c = readWordToken (token, c);
		kind = token->kind;
	}
	// forward typedef (LRM 6.18) is tagged as prototype
	//   (I don't know why...)
	switch (kind)
	{
		case K_CLASS:
		case K_INTERFACE:
			currentContext->prototype = true;
			break;
		case K_ENUM:
		case K_STRUCT:
			if (isWordToken (c))
			{
				c = readWordToken (token, c);
				if (token->kind == K_IDENTIFIER && c == ';')
					currentContext->prototype = true;
			}
			break;
		case K_IDENTIFIER:
			// interface based typedef
			c = skipDimension (c);
			if (c == '.')
			{
				c = skipWhite (vGetc ());
				if (isWordToken (c))
					c = readWordToken (token, c);
			}
			if (c == ';')
				currentContext->prototype = true;
			break;
		default:
			; // do nothing
	}
	c = processType (token, c, &kind, &not_used);

	createTag (token, kindSave);

	ptrArrayClear (tagContents);
	return c;
}

static int processParameterList (tokenInfo *token, int c)
{
	bool parameter = true;	// default "parameter"

	if (c != '#')
		return c;
	c = skipWhite (vGetc ());

	if (c != '(')
		return c;
	c = skipWhite (vGetc ());

	while (c != ')' && c != EOF)
	{
		if (isWordToken (c))
		{
			c = readWordToken (token, c);
			verbose ("Found parameter %s\n", vStringValue (token->name));
			if (token->kind == K_IDENTIFIER)
			{
				if (c == ',' || c == ')' || c == '=')	// ignore user defined type
				{
					tokenInfo *param = dupToken (token);
					param->kind = K_CONSTANT;
					param->parameter = parameter;
					ptrArrayAdd (tagContents, param);
					if (c == '=')
						c = skipExpression (vGetc ());
					else if (c == ',')
						c = skipWhite (vGetc ());
					else	// ')'
						break;
				}
			}
			else if (token->kind == K_PARAMETER)
				parameter = true;
			else if (token->kind == K_LOCALPARAM)
				parameter = false;
		}
		else
			c = skipWhite (vGetc ());
		// unpacked array is not allowed for a parameter
	}
	c = skipWhite (vGetc ());	// skip ')'
	return c;
}

// [ virtual ] class [ static | automatic ] class_identifier [ parameter_port_list ]
//     [ extends class_type [ ( list_of_arguments ) ] ] [ implements < interface_class_type > ] ;
// interface class class_identifier [ parameter_port_list ] [ extends < interface_class_type > ] ;
static int processClass (tokenInfo *const token, int c, verilogKind kind)
{
	tokenInfo *classToken;

	/* Get identifiers */
	while (isWordToken (c))
	{
		c = readWordToken (token, c);
		// skip static or automatic
		if (token->kind != K_IGNORE)
			break;
	}

	if (token->kind != K_IDENTIFIER)
	{
		VERBOSE ("Unexpected input: class name is expected.\n");
		return c;
	}

	/* save token */
	classToken = dupToken (token);

	/* Find class parameters list */
	c = processParameterList (token, c);

	/* Search for inheritance information */
	if (isWordToken (c))
	{
		c = readWordToken (token, c);
		if (strcmp (vStringValue (token->name), "extends") == 0)
		{
			if (isWordToken (c))
				c = readWordToken (token, c);
			vStringCopy (classToken->inheritance, token->name);
			verbose ("Inheritance %s\n", vStringValue (classToken->inheritance));
		}
	}
	// process implements: FIXME

	createTag (classToken, kind);
	deleteToken (classToken);
	ptrArrayClear (tagContents);
	return c;
}

// constraint_declaration ::= [ static ] constraint constraint_identifier '{' { constraint_block_item } '}'
// constraint_prototype ::= [ extern | pure ] [ static ] constraint constraint_identifier ;
static int processConstraint (tokenInfo *const token, int c)
{
	verilogKind kind;
	if (isWordToken (c))
		c = readWordToken (token, c);
	if (c == '{')
	{
		c = skipPastMatch ("{}");
		kind = K_CONSTRAINT;
	}
	else
		kind = K_PROTOTYPE;
	createTag (token, kind);
	return c;
}

static int processDefine (tokenInfo *const token, int c)
{
	/* Bug #961001: Verilog compiler directives are line-based. */
	if (isWordToken (c))
	{
		c = readWordTokenNoSkip (token, c);
		createTag (token, K_CONSTANT);
	}
	c = skipToNewLine (c);
	c = skipWhite (c);
	return c;
}

// immediate_assertion_statement ::=
//     ( assert | asume | cover ) [ #0 | final ] '(' expression ')' block
// concurrent_assertion_statement ::=
//     ( assert | assume ) property ( property_spec ) action_block
//   | expect ( property_spec ) action_block  # ignore : processed as same as "if"
//   | cover property ( property_spec ) statement_or_null
//   | cover sequence ( [clocking_event ] [ disable iff ( expression_or_dist ) ] sequence_expr ) statement_or_null
//   | restrict property ( property_spec ) ;
static int processAssertion (tokenInfo *const token, int c)
{
	if (vStringLength (currentContext->blockName) > 0)
	{
		vStringCopy (token->name, currentContext->blockName);
		vStringClear (currentContext->blockName);	// clear block name not to be reused
		createTag (token, K_ASSERTION);
	}
	// skip final | property | sequence
	if (isWordToken (c))
		c = readWordToken (token, c);
	// skip #0
	c = skipDelay (token, c);
	// skip ( ... )
	if (c == '(')
		c = skipPastMatch ("()");
	return c;
}

// data_declaration ::=
//   ...
//   import < package_identifier :: identifier | package_identifier :: * > ;
// dpi_import_export ::=
//   import ( "DPI-C" | "DPI" ) [ context | pure ] [ c_identifier = ] function data_type_or_void function_identifier [ ( [ tf_port_list ] ) ] ;
// | import ( "DPI-C" | "DPI" ) [ context ]        [ c_identifier = ] task task_identifier [ ( [ tf_port_list ] ) ] ;
// | export ( "DPI-C" | "DPI" ) [ c_identifier = ] function function_identifier ;
// | export ( "DPI-C" | "DPI" ) [ c_identifier = ] task     task_identifier ;
static int processImport (tokenInfo *const token, int c)
{
	if (c == '"') {	// dpi_import: we don't care about export.
		currentContext->prototype = true;
	} else {
		c = skipToSemiColon (c);
		c = skipWhite (vGetc ());	// skip semicolon
	}
	return c;
}

// non-ANSI type
// ( module | interface | program ) [ static | automatic ] identifier { package_import_declaration } [ parameter_port_list ] ( port { , port } ) ;
// ANSI type
// ( module | interface | program ) [ static | automatic ] identifier { package_import_declaration } [ parameter_port_list ] [ ( [ < { (* ... *) } ansi_port_declaration > ] ) ] ;
//
// interface class class_identifier [ parameter_port_list ] [ extends < interface_class_type > ] ;
static int processDesignElementL (tokenInfo *const token, int c)
{
	verilogKind kind = token->kind;

	while (isWordToken (c))
	{
		c = readWordToken (token, c);
		// interface class
		if (token->kind == K_CLASS)
			return processClass (token, c, K_IFCLASS);
		// skip static or automatic
		else if (token->kind != K_IGNORE)
			break;
	}
	if (token->kind == K_IDENTIFIER)
		createTag (token, kind);	// identifier

	// skip package_import_declaration
	while (isWordToken (c))
	{
		c = readWordToken (token, c);
		if (token->kind == K_IMPORT)
		{
			c = skipToSemiColon (c);
			c = skipWhite (vGetc ());	// skip semicolon
		}
		else
		{
			VERBOSE ("Unexpected input\n");
			return c;
		}
	}
	if (c == '#')	// parameter_port_list
	{
		c = processParameterList (token, c);

		/* Put found parameters in context */
		verbose ("Putting parameters: %d element(s)\n",
				ptrArrayCount (tagContents));
		for (unsigned int i = 0; i < ptrArrayCount (tagContents); i++)
		{
			tokenInfo *content = ptrArrayItem (tagContents, i);
			createTag (content, K_CONSTANT);
		}
		ptrArrayClear (tagContents);
		// disable parameter property on parameter declaration statement
		currentContext->hasParamList = true;
	}
	// Process ANSI/non-ANSI port list in main loop
	c = processPortList (token, c, true);
	return c;
}

// ( checker | property | sequence ) identifier [ ( [ port_list ] ) ] ;
// covergroup identifier [ ( [ port_list ] ) ] [ coverage_event ] ;
//   coverage_event ::= clocking_event | with function sample ( ... ) | @@( ... )
// package identifier ;
// modport < identifier ( < ports_declaration > ) > ;
// [ default | global ] clocking [ identifier ] ( @ identifier | @ ( event_expression ) )
static int processDesignElementS (tokenInfo *const token, int c)
{
	verilogKind kind = token->kind;

	if (isWordToken (c))
		c = readWordToken (token, c);
	else
		return c;

	createTag (token, kind);	// identifier

	/* Get port list if required */
	if (c == '(')	// port_list
	{
		if (kind == K_MODPORT)
			c = skipPastMatch ("()");	// ignore port list
		else
			c = processPortList (token, c, false);
	}
	// skip clocking_event for clocking block or coverage_event for covergroup
	// "with function sample ()" is processed in the main loop
	if (c == '@')
		c = skipClockEvent (token, c);
	return c;
}

static int skipDelay (tokenInfo* token, int c)
{
	if (c == '#')
	{
		c = skipWhite (vGetc ());
		if (c == '(')
			c = skipPastMatch ("()");
		else if (c == '#')
			// a dirty hack for "x ##delay1 y[*min:max];"
			c = skipToSemiColon (vGetc ());
		else	// time literals
		{
			while (isIdentifierCharacter (c) || c == '.')
				c = vGetc ();
			c = skipWhite (c);
		}
	}
	return c;
}

static int skipClockEvent (tokenInfo* token, int c)
{
	if (c == '@')
	{
		c = skipWhite (vGetc ());
		// for @@ ( ... ) : coverage_event
		if (c == '@')
			c = skipWhite (vGetc ());
		if (c == '(')
			c = skipPastMatch ("()");
		else if (isWordToken (c))
			c = readWordToken (token, c);
	}
	return c;
}

static int pushEnumNames (tokenInfo* token, int c)
{
	if (c == '{')
	{
		c = skipWhite (vGetc ());
		while (c != '}' && c != EOF)
		{
			if (!isWordToken (c))
			{
				VERBOSE ("Unexpected input: %c\n", c);
				return c;
			}
			c = readWordToken (token, c);

			token->kind = K_CONSTANT;
			ptrArrayAdd (tagContents, dupToken (token));
			verbose ("Pushed enum element \"%s\"\n", vStringValue (token->name));

			/* Skip element ranges */
			/* TODO Implement element ranges */
			c = skipDimension (c);

			/* Skip value assignments */
			if (c == '=')
				c = skipExpression (vGetc ());

			/* Skip comma */
			if (c == ',')
				c = skipWhite (vGetc ());
		}
		c = skipWhite (vGetc ());	// skip '}'
	}
	return c;
}

// create a list of struct/union members
static int pushMembers (tokenInfo* token, int c)
{
	if (c == '{')
	{
		c = skipWhite (vGetc ());
		while (c != '}' && c != EOF)
		{
			verilogKind kind = K_UNDEFINED;	// set kind of context for processType()
			bool not_used;
			if (!isWordToken (c))
			{
				VERBOSE ("Unexpected input: %c\n", c);
				return c;
			}
			c = readWordToken (token, c);

			c = processType (token, c, &kind, &not_used);
			while (true)
			{
				token->kind = K_MEMBER;
				ptrArrayAdd (tagContents, dupToken (token));
				verbose ("Pushed struct/union member \"%s\"\n", vStringValue (token->name));

				/* Skip unpacked dimensions */
				c = skipDimension (c);

				/* Skip value assignments */
				if (c == '=')
					c = skipExpression (vGetc ());

				if (c != ',' || c == EOF)
					break;		// should be ';'

				c = skipWhite (vGetc ());	// skip ','
				if (isWordToken (c))
					c = readWordToken (token, c);
				else
				{
					VERBOSE ("Unexpected input.\n");
					break;
				}
			}
			/* Skip semicolon */
			if (c == ';')
				c = skipWhite (vGetc ());
			/* End of enum elements list */
		}
		c = skipWhite (vGetc ());	// skip '}'
	}
	return c;
}

// input
//   kind: kind of context
// output
//   kind: kind of type
//   token: identifier token (unless K_IDENTIFIER nor K_UNDEFINED)
static int processType (tokenInfo* token, int c, verilogKind* kind, bool* with)
{
	verilogKind actualKind = K_UNDEFINED;
	tokenInfo *tokenSaved;
	*with = false;
	do
	{
		c = skipDimension (c);
		c = skipDelay (token, c);	// class parameter #(...)
		if (c == '{')	// skip enum, struct, or union member
		{
			if (*kind == K_ENUM)
				c = pushEnumNames (token, c);
			else if (*kind == K_STRUCT)
				c = pushMembers (token, c);
			else	// for a nested structure
				c = skipPastMatch ("{}");
		}
		c = skipDimension (c);
		c = skipMacro (c, token);

		// break on ',', ';', ')', '}', or other unexpected charactors
		if (!isWordToken (c))
			break;

		tokenSaved = dupToken (token);
		c = readWordToken (token, c);
		// break on "with"
		if (token->kind == K_WITH)
		{
 			swapToken (token, tokenSaved);
			deleteToken (tokenSaved);
			*with = true;	// inform to caller
			break;
		}
		deleteToken (tokenSaved);

		// fix kind of user defined type
		if (*kind == K_IDENTIFIER)
		{
			if (token->kind == K_NET)
				actualKind = K_NET;
			else if (token->kind == K_REGISTER)
				actualKind = K_REGISTER;
			else if (token->kind == K_PORT)
				actualKind = K_PORT;
			else if (token->kind == K_IDENTIFIER)
			{	// identifier of a user defined type
				*kind = K_REGISTER;	// FIXME: consider kind of the user defined type
				break;
			}
			else
			{
				VERBOSE ("Unexpected input\n");	// FIXME: x dist {}, with
				break;
			}
		}
	} while (c != '`' && c != EOF);	// break on compiler directive

	// skip unpacked dimension (or packed dimension after type-words)
	c = skipDimension (skipWhite (c));

	if (*kind == K_UNDEFINED && *kind != K_PORT)
		*kind = actualKind;
	return c;
}

// class_type ::=
//       ps_class_identifier [ # ( ... ) ] { :: class_identifier [ # ( ... ) ] }
// "interface_identifier ." is also handled
static int skipClassType (tokenInfo* token, int c)
{
	while (c == '#' || c == ':' || c == '.')
	{
		if (c == '#')
		{
			c = skipWhite (vGetc ());
			// a dirty hack for "x ##delay1 y[*min:max];"
			if (c == '#')
				return skipToSemiColon (vGetc ());
			c = skipPastMatch ("()");
		}
		else if (c == ':')
		{
			if (!isDoubleColon(c))
			{
				VERBOSE ("Unexpected input.\n");
				return c;
			}
			c = skipWhite (vGetc ());
			if (isWordToken (c))
				c = readWordToken (token, c);
		}
		else	// c == '.' : interface_identifier .
		{
			c = skipWhite (vGetc ());
			if (isWordToken (c))
				c = readWordToken (token, c);
		}
	}
	return c;
}

// Tag a list of identifiers in a port list
// data_type :: =
//   ...
//   | virtual [ interface ] identifier [ # ( [ ... ] ) ]  [ . identifier ]
//   | [ class_type :: | identifier :: | $unit :: ] identifier { [ ... ] }
//   | [ identifier :: | $unit :: ] identifier [ # ( ... ) ] { :: identifier [ # ( ... ) ] }
//   | ...
//
//   mayPortDecl: may be a ANSI port declaration.  true for module, interface, or program.
static int tagIdsInPort (tokenInfo *const token, int c, verilogKind kind, bool mayPortDecl)
{
	bool first_port = true;
	bool enableTag = true;
	verilogKind localKind;
	bool not_used;

	while (c != ')' && c != EOF)	// skip empty port, "()"
	{
		// skip attribute_instance: (* ... *)
		if (c == '(')
			c = skipPastMatch ("()");

		// skip port direction, "virtual", or "interface"
		while (isWordToken (c))
		{
			c = readWordToken (token, c);
			if (token->kind == K_PORT || token->kind == K_IGNORE || token->kind == K_INTERFACE)
				mayPortDecl = false;	// now never be a non-ANSI port
			else
				break;
		}
		if (token->kind == K_IDENTIFIER)
			c = skipClassType (token, c);
		c = skipMacro (c, token);	// `ifdef, `else, `endif, etc. (between identifiers)

		if (isWordToken (c))
		{
			c = readWordToken (token, c);
			if (token->kind == K_IDENTIFIER)
			{
				mayPortDecl = false;
				c = skipClassType (token, c);
			}
		}
		// aoid tagging enum and struct items
		localKind = token->kind == K_ENUM || token->kind == K_STRUCT ? K_PORT : token->kind;
		c = processType (token, c, &localKind, &not_used);

		// LRM 23.2.2.3 Rules for determining port kind, data type, and direction
		// If the direction, port kind, and data type are all omitted for
		// the first port in the port list, ... non-ANSI style, ...
		if (mayPortDecl && first_port)
		{
			first_port = false;
			if (localKind == K_IDENTIFIER)
				enableTag = false;	// don't tag for non-ANSI port
		}
		if (enableTag && token->kind == K_IDENTIFIER)
			createTag (token, kind);

		if (c == '=')
			c = skipExpression (vGetc ());

		c = skipMacro (c, token);	// `ifdef, `else, `endif, etc. (before comma)
		if (c != ',' || c == EOF)
			break;
		c = skipWhite (vGetc ());	// skip ','
		c = skipMacro (c, token);	// `ifdef, `else, `endif, etc. (after comma)
	}
	return c;
}

// Tag a list of identifiers in a data declaration
static int tagIdsInDataDecl (tokenInfo* token, int c, verilogKind kind)
{
	c = skipClassType (token, c);
	if (c == ';')
		return c;

	// skip drive|charge strength or type_reference, dimensions, and delay for net
	if (c == '(')
		c = skipPastMatch ("()");
	c = skipDimension (c);
	if (c == '.')
		return c;	// foo[...].bar = ..;
	c = skipDelay (token, c);	// ## (cycle delay)

	tokenInfo *tokenSaved = dupToken(token); // maybe a module_identifier
	while (c != EOF)
	{
		bool with = false;
		c = processType (token, c, &kind, &with);	// update token and kind

		if (c == '=' || c == ',' || c == ';' || c == ')' || c == '`' || with)
		{
			// ignore an empty token or procedual assignment: foo = bar;
			if (kind != K_UNDEFINED && kind != K_IDENTIFIER && token->kind != K_UNDEFINED)
				createTag (token, kind);
			if (c == '=')
				c = skipExpression (c);
		}
		else if (c == '(' || c == '[')	// should be an instance
		{
			c = skipDimension (c); // name_of_instance {unpacked_dimension}
			c = skipPastMatch ("()"); // list_of_port_connections

			// if without the next "if" clause, get a instance named: `add_t from the following example
			// var `add_t(foo) = '0;
			if (c == ';' || c == ',')
			{
				tokenSaved->kind = K_MODULE;   // for typeRef field
				verbose ("find instance: %s with kind %s\n", vStringValue (token->name), getNameForKind (K_INSTANCE));
				createTagWithTypeRef (token, K_INSTANCE, tokenSaved);
				createRefTag (tokenSaved, K_MODULE, R_MODULE_DECL);
			}
		}
		c = skipMacro (c, token);	// `ifdef, `else, `endif, etc. (before comma)
		if (c != ',' || c == EOF)
			break;
		c = skipWhite (vGetc ());	// skip ','
		c = skipMacro (c, token);	// `ifdef, `else, `endif, etc. (after comma)
	}
	deleteToken (tokenSaved);
	return c;
}

static int findTag (tokenInfo *const token, int c)
{
	verbose ("Checking token %s of kind %d\n", vStringValue (token->name), token->kind);

	switch (token->kind)
	{
		case K_CONSTANT:
		case K_EVENT:
		case K_LOCALPARAM:
		case K_NET:
		case K_PARAMETER:
		case K_PORT:
		case K_REGISTER:
			if (token->kind == K_PORT && currentContext->kind == K_CLOCKING)
				c = skipToSemiColon (c); // clocking items are not port definitions
			else
				c = tagIdsInDataDecl (token, c, token->kind);
			break;
		case K_IDENTIFIER:
			{
				if (c == '[')	// for a case label foo[x]:
					c = skipPastMatch ("[]");

				if (c == ':')
					; /* label */
				else if (c == ',' || c == '.' || c == '{')	// "foo, ...", "foo.bar,...", or "coverpoint foo { ... }"
					c = skipWhite (vGetc ());
				else if (c == '(')	// task, function, or method call
					c = skipPastMatch ("()");
				else if (c == '=')	// assignment
					c = skipExpression (skipWhite (vGetc ()));
				else
					c = tagIdsInDataDecl (token, c, token->kind); /* user defined type */
			}
			break;
		case K_CLASS:
			c = processClass (token, c, K_CLASS);
			break;
		case K_TYPEDEF:
		case K_NETTYPE:
			c = processTypedef (token, c);
			break;
		case K_ENUM:
			c = processEnum (token, c);
			break;
		case K_STRUCT:
			c = processStruct (token, c);
			break;
		case K_IMPORT:
			c = processImport (token, c);
			break;
		case K_PROTOTYPE:
		case K_WITH:
			currentContext->prototype = true;
			break;

		case K_INTERFACE:
		case K_MODULE:
		case K_PROGRAM:
			c = processDesignElementL (token, c);
			break;
		case K_CHECKER:
		case K_CLOCKING:
		case K_COVERGROUP:
		case K_MODPORT:
		case K_PACKAGE:
		case K_PROPERTY:
		case K_SEQUENCE:
			c = processDesignElementS (token, c);
			break;
		case K_END_DE:
			c = dropEndContext (token, c);
			break;
		case K_BLOCK:
			c = processBlock (token, c);
			break;
		case K_END:
			c = processEnd (token, c);
			break;
		case K_FUNCTION:
		case K_TASK:
			c = processFunction (token, c);
			break;
		case K_ASSERTION:
			c = processAssertion (token, c);
			break;
		case K_CONSTRAINT:
			c = processConstraint (token, c);
			break;

		case K_DEFINE:
			c = processDefine (token, c);
			break;

		case K_IGNORE:
			break;
		default:
			VERBOSE ("Unexpected kind->token %d\n", token->kind);
	}
	return c;
}

static void findVerilogTags (void)
{
	tokenInfo *const token = newToken ();
	int c = skipWhite (vGetc ());
	currentContext = newToken ();
	fieldTable = isInputLanguage (Lang_verilog) ? VerilogFields : SystemVerilogFields;
	ptrArrayClear (tagContents);

	while (c != EOF)
	{
		switch (c)
		{
			case ':':
				/* Store current block name whenever a : is found
				 * This is used later by any tag type that requires this information */
				if (!isDoubleColon(c))
					vStringCopy (currentContext->blockName, token->name);
				c = skipWhite (vGetc ());
				break;
			case ';':
				/* Drop context on prototypes because they don't have an
				 * end statement */
				if (currentContext->scope && currentContext->scope->prototype)
					dropContext ();

				/* Prototypes end at the end of statement */
				currentContext->prototype = false;
				c = skipWhite (vGetc ());
				break;
			case '(':	// ignore locally declared variables in a for-loop (LRM 12.7.1)
				c = skipPastMatch ("()");
				break;
			case '{':
				c = skipPastMatch ("{}");
				break;
			case '#':
				c = skipDelay (token, c);
				break;
			case '@':
				c = skipClockEvent (token, c);
				break;
			case '"':
				c = skipString (c);
				break;
			default :
				if (isWordToken (c))
				{
					c = readWordTokenNoSkip (token, c);
					if (token->kind == K_DIRECTIVE)
					{
						// Skip compiler directives which are line-based.
						c = skipToNewLine (c);
						c = skipWhite (c);
					}
					else if (token->kind != K_UNDEFINED)
						c = findTag (token, skipWhite (c));
				}
				else
					c = skipWhite (vGetc ());
		}
	}
	deleteToken (token);
	pruneTokens (currentContext);
	currentContext = NULL;
}

extern parserDefinition* VerilogParser (void)
{
	static const char *const extensions [] = { "v", NULL };
	parserDefinition* def = parserNew ("Verilog");
	def->kindTable  = VerilogKinds;
	def->kindCount  = ARRAY_SIZE (VerilogKinds);
	def->fieldTable = VerilogFields;
	def->fieldCount = ARRAY_SIZE (VerilogFields);
	def->extensions = extensions;
	def->parser     = findVerilogTags;
	def->initialize = initializeVerilog;
	return def;
}

extern parserDefinition* SystemVerilogParser (void)
{
	static const char *const extensions [] = { "sv", "svh", "svi", NULL };
	parserDefinition* def = parserNew ("SystemVerilog");
	def->kindTable  = SystemVerilogKinds;
	def->kindCount  = ARRAY_SIZE (SystemVerilogKinds);
	def->fieldTable = SystemVerilogFields;
	def->fieldCount = ARRAY_SIZE (SystemVerilogFields);
	def->extensions = extensions;
	def->parser     = findVerilogTags;
	def->initialize = initializeSystemVerilog;
	return def;
}