-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexer.h
executable file
·189 lines (146 loc) · 4.11 KB
/
lexer.h
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
#pragma once
#ifndef __LEXER_H
#define __LEXER_H
//
// forward declarations
//
struct SymbolEntry;
class BaseParser;
//======================================================================
//
//======================================================================
#define DEFAULT_NUM_BUF 32
#define DEFAULT_TEXT_BUF 2048
// predefined token values
enum {
TV_ERROR = 256,
TV_DONE,
// numeric values
TV_INTVAL,
TV_FLOATVAL,
TV_CHARVAL,
// string literal
TV_STRING,
// identifier
TV_ID,
TV_USER
};
//
struct TokenTable
{
const char *lexeme;
int token;
};
//
union YYSTYPE
{
// literal values
int ival;
float fval;
char char_val;
SymbolEntry *sym; // ID value
TokenTable *ptt; // keyword
};
//
class LexicalAnalyzer
{
protected:
// File descriptor node
struct FDNode
{
FILE *fdDocument;
char *pTextData;
std::string filename;
int column;
int yylineno;
void *pUserData;
FDNode() : fdDocument(nullptr), pTextData(nullptr), filename(""), column(0), yylineno(1), pUserData(nullptr) {}
// move ctor
FDNode(FDNode &&rhs)
{
fdDocument = rhs.fdDocument;
pTextData = rhs.pTextData;
filename = rhs.filename;
column = rhs.column;
yylineno = rhs.yylineno;
pUserData = rhs.pUserData;
// take ownership of the file ptr
rhs.fdDocument = nullptr;
}
virtual ~FDNode()
{
if (fdDocument)
fclose(fdDocument);
}
};
int m_iTotalLinesParsed;
//char m_szCurrentSourceLineText[256];
//int m_iCurrentSourceLineIndex;
using FDStack = std::vector<FDNode>;
FDStack m_fdStack;
BaseParser *m_pParser;
YYSTYPE *m_yylval;
bool m_bUnixComments;
bool m_bCPPComments;
bool m_bCStyleComments;
bool m_bASMComments;
bool m_bHexNumbers;
bool m_bCharLiterals;
bool m_bCaseSensitive;
int (*compare_function)(const char*, const char*);
struct ltstr
{
bool operator()(const std::string &s1, const std::string &s2) const
{
return strcmp(s1.c_str(), s2.c_str()) < 0;
}
};
using TokenTableMap = std::map<std::string, int, ltstr>;
TokenTableMap m_tokenTable;
// methods to help with lexical processing
// yylex() will use these to find tokens
int skipLeadingWhiteSpace();
int follow(int expect, int ifyes, int ifno);
int backslash(int c);
// comments
void skipToEOL(void);
void cstyle_comment(void);
int getNumber();
int getStringLiteral();
int getCharLiteral();
int getKeyword();
int getChar();
int ungetChar(int c);
public:
LexicalAnalyzer(TokenTable *atokenTable, BaseParser *pParser, YYSTYPE *pyylval);
virtual ~LexicalAnalyzer() = default;
//const char *GetCurrentSourceText() { return m_szCurrentSourceLineText; }
//void ClearCurrentSourceText() { m_szCurrentSourceLineText[0] = 0; }
int pushFile(const char *theFile);
int popFile();
std::string getFile() const { return m_fdStack.back().filename; }
int setData(char *theData, const char *fileName, void* pUserData);
virtual void freeData(void* pUserData);
const char *getLexemeFromToken(int token);
void caseSensitive(bool onoff = true);
int getColumn() { return m_fdStack.back().column; }
int getLineNumber() { return m_fdStack.back().yylineno; }
int getTotalLinesParsed() { return m_iTotalLinesParsed; }
void setUnixComments(bool onoff) { m_bUnixComments = onoff; }
void setCPPComments(bool onoff) { m_bCPPComments = onoff; }
void setCStyleComments(bool onoff) { m_bCStyleComments = onoff; }
void setASMComments(bool onoff) { m_bASMComments = onoff; }
void setHexNumbers(bool onoff) { m_bHexNumbers = onoff; }
void setCharLiterals(bool onoff) { m_bCharLiterals = onoff; }
void copyToEOF(FILE *fout);
void copyUntilChar(int endChar, int nestChar, FILE *fout);
void copyUntilChar(int endChar, int nestChar, char *buf);
// functions that may typically be overridden
virtual int yylex();
virtual void yyerror(const char *s);
virtual void yywarning(const char *s);
virtual bool isidval(int c);
virtual bool iswhitespace(int c);
virtual int specialTokens(int chr);
};
#endif //#ifndef __LEXER_H