-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAstBuilderInt.cpp
302 lines (251 loc) · 7.87 KB
/
AstBuilderInt.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
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
/*
* AstBuilderInt.cpp
*
* Created on: Sep 13, 2020
* Author: ballance
*/
#include "AstBuilderInt.h"
#include "PSSLexer.h"
#include "pssp/ast/IFactory.h"
#include "pssp/ast/IAction.h"
#include "pssp/ast/IComponent.h"
#include "pssp/ast/IExprId.h"
#include "pssp/ast/INamedScope.h"
#include "pssp/ast/IPackageImportStmt.h"
#include "pssp/ast/Location.h"
#include "ScopeUtil.h"
#include "Marker.h"
namespace pssp {
using namespace ast;
AstBuilderInt::AstBuilderInt(
ast::IFactory *factory,
IMarkerListener *marker_l) : m_factory(factory), m_marker_l(marker_l) {
// TODO Auto-generated constructor stub
}
AstBuilderInt::~AstBuilderInt() {
// TODO Auto-generated destructor stub
}
void AstBuilderInt::build(
ast::IGlobalScope *global,
std::istream *in) {
ANTLRInputStream input(*in);
PSSLexer lexer(&input);
m_tokens = std::unique_ptr<CommonTokenStream>(new CommonTokenStream(&lexer));
PSSParser parser(m_tokens.get());
parser.removeErrorListeners();
parser.addErrorListener(this);
PSSParser::Compilation_unitContext *ctx = parser.compilation_unit();
// Only proceed to build out the AST if there are no syntax errors
if (!m_marker_l->hasSeverity(MarkerSeverityE::Error)) {
push_scope(global);
ctx->accept(this);
pop_scope();
}
}
antlrcpp::Any AstBuilderInt::visitPackage_declaration(
PSSParser::Package_declarationContext *ctx) {
IPackageScope *pkg = m_factory->mkPackageScope();
// TODO: populate Id list
for (std::vector<PSSParser::Package_identifierContext *>::const_iterator
it=ctx->package_id_path()->package_identifier().begin();
it!=ctx->package_id_path()->package_identifier().end(); it++) {
pkg->getId().push_back(IExprIdUP(mkId((*it)->identifier())));
}
addChild(pkg);
push_scope(pkg);
for (std::vector<PSSParser::Package_body_itemContext *>::const_iterator
it=ctx->package_body_item().begin();
it!=ctx->package_body_item().end(); it++) {
(*it)->accept(this);
}
pop_scope();
return 0;
}
antlrcpp::Any AstBuilderInt::visitImport_stmt(PSSParser::Import_stmtContext *ctx) {
bool is_wildcard = false;
IExprId *alias = 0;
if (ctx->package_import_pattern()->package_import_qualifier()) {
if (ctx->package_import_pattern()->package_import_qualifier()->package_import_wildcard()) {
is_wildcard = true;
} else {
alias = mkId(ctx->package_import_pattern()->package_import_qualifier()->
package_import_alias()->package_identifier()->identifier());
}
}
IPackageImportStmt *imp = m_factory->mkPackageImportStmt(is_wildcard, alias);
for (std::vector<PSSParser::Type_identifier_elemContext *>::const_iterator
it=ctx->package_import_pattern()->type_identifier()->type_identifier_elem().begin();
it!=ctx->package_import_pattern()->type_identifier()->type_identifier_elem().end(); it++) {
imp->getPath().push_back(IExprIdUP(mkId((*it)->identifier())));
}
addChild(imp);
return 0;
}
void AstBuilderInt::syntaxError(
Recognizer *recognizer,
Token * offendingSymbol,
size_t line,
size_t charPositionInLine,
const std::string &msg,
std::exception_ptr e) {
if (m_marker_l) {
ast::Location loc;
loc.fileid = 0;
loc.lineno = line;
loc.linepos = charPositionInLine;
Marker m(
msg,
MarkerSeverityE::Error,
loc);
m_marker_l->marker(&m);
}
}
void AstBuilderInt::addChild(ast::IScopeChild *c) {
scope()->getChildren().push_back(ast::IScopeChildUP(c));
// ScopeUtil::addChild(scope(), c);
}
void AstBuilderInt::addChild(ast::INamedScopeChild *c) {
scope()->getChildren().push_back(ast::IScopeChildUP(c));
// ScopeUtil::addChild(scope(), c);
}
void AstBuilderInt::addChild(ast::INamedScope *c) {
scope()->getChildren().push_back(ast::IScopeChildUP(c));
}
void AstBuilderInt::addDocstring(ast::IScopeChild *c, Token *t) {
std::vector<Token *> ws_tokens = m_tokens->getHiddenTokensToLeft(
t->getTokenIndex(), 10);
std::vector<Token *> slc_tokens = m_tokens->getHiddenTokensToLeft(
t->getTokenIndex(), 11);
std::vector<Token *> mlc_tokens = m_tokens->getHiddenTokensToLeft(
t->getTokenIndex(), 12);
fprintf(stdout, "ws_tokens=%d slc_tokens=%d mlc_tokens=%d\n",
ws_tokens.size(), slc_tokens.size(), mlc_tokens.size());
if (slc_tokens.size() == 0 && mlc_tokens.size() == 0) {
return;
}
int32_t last_ws_line = -1;
if (ws_tokens.size() > 0) {
last_ws_line = ws_tokens.back()->getLine();
}
std::string docstring;
if (slc_tokens.size() > 0 && mlc_tokens.size() > 0) {
if (slc_tokens.back()->getLine() > mlc_tokens.back()->getLine()) {
// Single-line comment is last
docstring = processDocStringSingleLineComment(
slc_tokens,
ws_tokens);
} else {
// Multi-line comment is last
docstring = processDocStringMultiLineComment(
mlc_tokens,
ws_tokens);
}
} else if (slc_tokens.size() > 0) {
// Single-line comment
docstring = processDocStringSingleLineComment(
slc_tokens,
ws_tokens);
} else {
// Multi-line comment
docstring = processDocStringMultiLineComment(
mlc_tokens,
ws_tokens);
}
if (docstring != "") {
c->setDocstring(docstring);
}
/*
fprintf(stdout, "Token pos: %d\n", comp->getLine());
for (std::vector<Token*>::const_iterator
it=tokens.begin();
it!=tokens.end(); it++) {
fprintf(stdout, "Token %d: %s\n",
(*it)->getLine(),
(*it)->getText().c_str());
}
*/
}
std::string AstBuilderInt::processDocStringMultiLineComment(
const std::vector<Token *> &mlc_tokens,
const std::vector<Token *> &ws_tokens) {
int32_t last_ws_line = -1;
if (ws_tokens.size() > 0) {
last_ws_line = ws_tokens.back()->getLine();
}
fprintf(stdout, "last_ws_line=%d comment_line=%d\n",
last_ws_line,
mlc_tokens.back()->getLine());
std::string comment;
if (last_ws_line < 0 || last_ws_line < mlc_tokens.back()->getLine()) {
fprintf(stdout, "OK: no whitespace between element and comment\n");
} else if (last_ws_line >= 0) {
fprintf(stdout, "TODO: check if whitespace exceeds a limit\n");
// Find the extent of the comment
uint32_t comment_last_line = mlc_tokens.back()->getLine();
comment = mlc_tokens.back()->getText();
std::string ws = ws_tokens.back()->getText();
int32_t i=0;
while (i < comment.size() &&
(i=comment.find('\n', i)) != std::string::npos) {
comment_last_line++;
i++;
}
i=0;
while (i < comment.size() &&
(i=ws.find('\n', i)) != std::string::npos) {
last_ws_line++;
i++;
}
fprintf(stdout, "Comment last line: %d\n", comment_last_line);
fprintf(stdout, "Whitespace last line: %d\n", last_ws_line);
if (last_ws_line <= (comment_last_line+2)) {
fprintf(stdout, "Note: Have a doc comment\n");
// TODO: now we need to clean up the comment
//
// Trim off the beginning and end of the comment
comment = comment.substr(2,comment.size()-4);
fprintf(stdout, "Comment: %s\n", comment.c_str());
// Step through the lines looking for a '*' prefix
i=0;
while (i<comment.size()) {
if (comment.at(i) == '*') {
comment.erase(i, 1);
fprintf(stdout, "Post-remove(1): %s\n", comment.c_str());
} else if ((i+1<comment.size()) &&
(isspace(comment.at(i)) && comment.at(i+1) == '*')) {
comment.erase(i, 2);
fprintf(stdout, "Post-remove(2): %s\n", comment.c_str());
}
if ((i=comment.find('\n',i)) != std::string::npos) {
i++;
} else {
break;
}
}
} else {
fprintf(stdout, "Note: False alarm\n");
comment.clear();
}
}
return comment;
}
std::string AstBuilderInt::processDocStringSingleLineComment(
const std::vector<Token *> &slc_tokens,
const std::vector<Token *> &ws_tokens) {
return "";
}
IExprId *AstBuilderInt::mkId(PSSParser::IdentifierContext *ctx) {
IExprId *id;
if (ctx->ESCAPED_ID()) {
id = m_factory->mkExprId(ctx->ESCAPED_ID()->toString(), true);
} else {
id = m_factory->mkExprId(ctx->ID()->toString(), false);
}
Location loc = id->getLocation();
loc.lineno = ctx->start->getLine();
loc.linepos = ctx->start->getCharPositionInLine();
id->setLocation(loc);
// TODO: Fill in location info
return id;
}
}