-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbaseparser.cpp
215 lines (169 loc) · 4.31 KB
/
baseparser.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
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
#define _CRT_SECURE_NO_WARNINGS
#include "baseparser.h"
#include <assert.h>
#include <stdarg.h>
#ifdef _WIN32
# include <direct.h>
#else
# include <libgen.h>
# include <unistd.h>
# define _chdir chdir
#endif
//======================================================================
//
//======================================================================
BaseParser::BaseParser(std::unique_ptr<SymbolTable> symbolTable)
{
m_lexer = nullptr;
m_errorCount = 0;
m_warningCount = 0;
m_pSymbolTable = std::move(symbolTable);
}
//
BaseParser::~BaseParser()
{
//m_pSymbolTable->dumpUnreferencedSymbolsAtCurrentLevel();
}
// the parser calls this method to report errors
void BaseParser::yyerror(const Position &pos, const char *fmt, ...)
{
char buf[SMALL_BUFFER], s[SMALL_BUFFER];
va_list argptr;
va_start(argptr, fmt);
vsprintf(buf, fmt, argptr);
va_end(argptr);
sprintf(s, "%s(%d) : error near column %d: %s\r\n", pos.srcFile.c_str(), pos.srcLine, pos.srcColumn, buf);
m_errorCount++;
// delegate error messages to the lexical analyzer
m_lexer->yyerror(s);
}
// the parser calls this method to report errors
void BaseParser::yyerror(const char *fmt, ...)
{
char buf[SMALL_BUFFER], s[SMALL_BUFFER];
va_list argptr;
va_start(argptr, fmt);
vsprintf(buf, fmt, argptr);
va_end(argptr);
sprintf(s, "%s(%d) : error near column %d: %s\r\n", m_lexer->getFile().c_str(), m_lexer->getLineNumber(), m_lexer->getColumn(), buf);
m_errorCount++;
// delegate error messages to the lexical analyzer
m_lexer->yyerror(s);
}
// print a warning message
void BaseParser::yywarning(const Position &pos, const char *fmt, ...)
{
char buf[SMALL_BUFFER], s[SMALL_BUFFER];
va_list argptr;
va_start(argptr, fmt);
vsprintf(buf, fmt, argptr);
va_end(argptr);
sprintf(s, "%s(%d) : warning near column %d: %s\r\n", pos.srcFile.c_str(), pos.srcLine, pos.srcColumn, buf);
m_warningCount++;
// delegate error messages to the lexical analyzer
m_lexer->yywarning(s);
}
// print a warning message
void BaseParser::yywarning(const char *fmt, ...)
{
char buf[SMALL_BUFFER], s[SMALL_BUFFER];
va_list argptr;
va_start(argptr, fmt);
vsprintf(buf, fmt, argptr);
va_end(argptr);
sprintf(s, "%s(%d) : warning near column %d: %s\r\n", m_lexer->getFile().c_str(), m_lexer->getLineNumber(), m_lexer->getColumn(), buf);
m_warningCount++;
// delegate error messages to the lexical analyzer
m_lexer->yywarning(s);
}
//
void BaseParser::yylog(const char *fmt, ...)
{
if (!yydebug)
return;
char buf[SMALL_BUFFER];
va_list argptr;
va_start(argptr, fmt);
vsprintf(buf, fmt, argptr);
va_end(argptr);
puts(buf);
}
//
// show an error on unexpected token
//
void BaseParser::expected(int token)
{
if (token < 256)
yyerror("expected to see '%c'", token);
else
yyerror("expected to see '%s'", m_lexer->getLexemeFromToken(token));
}
//
// attempt to match the given token
//
int BaseParser::match(int token)
{
if (lookahead == token)
{
lookahead = m_lexer->yylex();
}
else
{
expected(token);
}
return lookahead;
}
//
// this method does nothing and is meant to be overridden in sub-classes
//
int BaseParser::yyparse()
{
lookahead = m_lexer->yylex();
return 0;
}
//
int BaseParser::parseFile (const char *filename)
{
int rv;
assert(filename);
#ifdef _WIN32
char szFullPath[_MAX_PATH];
char workingDir[_MAX_PATH], drive[_MAX_DRIVE], dir[_MAX_DIR], file[_MAX_FNAME], ext[_MAX_EXT];
char oldWorkingDir[_MAX_PATH];
_fullpath(szFullPath, filename, sizeof(szFullPath));
_splitpath_s(szFullPath, drive, dir, file, ext);
sprintf_s(workingDir, "%s%s", drive, dir);
_getdcwd(_getdrive(), oldWorkingDir, sizeof(oldWorkingDir));
_chdir(workingDir);
#else
char workingDir[PATH_MAX], oldWorkingDir[PATH_MAX];
strcpy(workingDir, dirname((char*)filename));
getcwd(oldWorkingDir, PATH_MAX);
chdir(workingDir);
#endif
rv = m_lexer->pushFile(filename);
if (rv != 0)
{
yyerror("Couldn't open file: %s", filename);
_chdir(oldWorkingDir);
return rv;
}
yyparse();
_chdir(oldWorkingDir);
return 0;
}
//
int BaseParser::parseData(char *textToParse, const char *fileName, void *pUserData)
{
int rv;
assert(textToParse);
rv = m_lexer->setData(textToParse, fileName, pUserData);
if (rv != 0)
{
yyerror("Couldn't parse text"); //open file: %s", filename);
return rv;
}
// TODO - should return the value from yyparse()?
yyparse();
return 0;
}