-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLexer.cpp
99 lines (96 loc) · 2 KB
/
Lexer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//
// Created by unknown on 2024/11/16.
//
#include "Lexer.h"
#include <string>
#include <sstream>
extern "C" {
int yylex();
extern char *yytext;
extern FILE *yyin;
}
const char *tokenToNameArray[] = {
"module",
"endmodule",
"output",
"input",
"assign",
"wire",
"always",
"posedge",
"negedge",
"senslist_or",
"begin",
"end",
"case",
"casez",
"casex",
"endcase",
"if",
"else",
"reg",
"comma",
"semicolon",
"colon",
"question",
"identifier",
"const_number",
"sized_number",
"lparen",
"rparen",
"lbrace",
"rbrace",
"lbracket",
"rbracket",
"op_add",
"op_sub",
"op_mul",
"op_div",
"op_mod",
"arith_lshift",
"logical_lshift",
"arith_rshift",
"logical_rshift",
"cond_eq",
"cond_ne",
"cond_ge",
"cond_gt",
"cond_le",
"cond_lt",
"single_eq",
"bitwise_and",
"bitwise_or",
"bitwise_not",
"bitwise_xor",
"logical_and",
"logical_or",
"logical_not",
"at",
"single_line_comment",
"multi_line_comment_start",
"multi_line_comment_end",
};
std::string getAllTokens(const std::string &filename) {
std::stringstream out;
yyin = fopen(filename.c_str(), "r");
out << "[";
while (true) {
int ret = yylex();
if (ret == 0) {
break;
}
std::string data{yylval.str};
if (ret != TOKEN_single_line_comment_end) {
free(yylval.str);
}
out << "{\n"
<< R"( "token": ")" << tokenToNameArray[ret + 1024] << "\",\n"
<< R"( "data": ")" << data << "\"\n"
<< "},\n";
}
auto ret = out.str();
ret.pop_back();
ret.pop_back();
ret += "\n]";
return ret;
}