-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParser.java
More file actions
351 lines (300 loc) · 11.8 KB
/
Copy pathParser.java
File metadata and controls
351 lines (300 loc) · 11.8 KB
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
// Parser.java
// Analisador sintático do TONACO SCRIPT v2.0
// Criado por: Guilherme Lucas Tonaco Carvalho
package com.tonaco.tns;
import java.util.*;
public class Parser {
private final List<Token> tokens;
private int current = 0;
public Parser(List<Token> tokens) {
this.tokens = tokens;
}
public ProgramNode parse() {
List<ASTNode> statements = new ArrayList<>();
while (!isAtEnd()) {
ASTNode stmt = declaration();
if (stmt != null) statements.add(stmt);
}
return new ProgramNode(statements);
}
// ============================================================
// DECLARAÇÕES
// ============================================================
private ASTNode declaration() {
try {
if (match(TokenType.FUNCTION)) return functionDeclaration();
return statement();
} catch (RuntimeException e) {
synchronize();
return null;
}
}
private FunctionNode functionDeclaration() {
Token name = consume(TokenType.IDENTIFIER, "Esperado nome da função");
consume(TokenType.LPAREN, "Esperado '(' após nome da função");
List<String> params = new ArrayList<>();
if (!check(TokenType.RPAREN)) {
do {
params.add(consume(TokenType.IDENTIFIER, "Esperado nome do parâmetro").lexeme);
} while (match(TokenType.COMMA));
}
consume(TokenType.RPAREN, "Esperado ')'");
consume(TokenType.LBRACE, "Esperado '{' antes do corpo da função");
ASTNode body = blockStatement();
return new FunctionNode(name.lexeme, params, body);
}
// ============================================================
// STATEMENTS
// ============================================================
private ASTNode statement() {
if (match(TokenType.PRINT)) return printStatement();
if (match(TokenType.LET)) return letStatement();
if (match(TokenType.IF)) return ifStatement();
if (match(TokenType.WHILE)) return whileStatement();
if (match(TokenType.FOR)) return forStatement();
if (match(TokenType.RETURN)) return returnStatement();
if (match(TokenType.LBRACE)) return blockStatement();
// Comandos TNS
if (match(TokenType.SCAN)) return scanStatement();
if (match(TokenType.DEEPSCAN)) return deepScanStatement();
if (match(TokenType.ANALYZE)) return analyzeStatement();
if (match(TokenType.DOWNLOAD)) return downloadStatement();
if (match(TokenType.GITHUB)) return githubStatement();
if (match(TokenType.REPORT)) return reportStatement();
if (match(TokenType.SYNC)) { consumeOptionalSemicolon(); return new SyncNode(); }
return expressionStatement();
}
private PrintNode printStatement() {
ASTNode expr = expression();
consumeOptionalSemicolon();
return new PrintNode(expr);
}
private LetNode letStatement() {
Token name = consume(TokenType.IDENTIFIER, "Esperado nome da variável");
String typeHint = null;
if (match(TokenType.COLON)) {
typeHint = consume(TokenType.IDENTIFIER, "Esperado tipo após ':'").lexeme;
}
consume(TokenType.ASSIGN, "Esperado '=' após nome da variável");
ASTNode value = expression();
consumeOptionalSemicolon();
return new LetNode(name.lexeme, value, typeHint);
}
private IfNode ifStatement() {
consume(TokenType.LPAREN, "Esperado '(' após 'if'");
ASTNode condition = expression();
consume(TokenType.RPAREN, "Esperado ')'");
ASTNode thenBranch = statement();
ASTNode elseBranch = null;
if (match(TokenType.ELSE)) elseBranch = statement();
return new IfNode(condition, thenBranch, elseBranch);
}
private WhileNode whileStatement() {
consume(TokenType.LPAREN, "Esperado '(' após 'while'");
ASTNode condition = expression();
consume(TokenType.RPAREN, "Esperado ')'");
ASTNode body = statement();
return new WhileNode(condition, body);
}
private ForNode forStatement() {
consume(TokenType.LPAREN, "Esperado '(' após 'for'");
ASTNode init = null;
if (!check(TokenType.SEMICOLON)) {
if (match(TokenType.LET)) init = letStatement();
else init = expressionStatement();
} else consume(TokenType.SEMICOLON, "");
ASTNode condition = check(TokenType.SEMICOLON) ? new BooleanNode(true) : expression();
consume(TokenType.SEMICOLON, "Esperado ';' após condição do for");
ASTNode increment = null;
if (!check(TokenType.RPAREN)) increment = expression();
consume(TokenType.RPAREN, "Esperado ')'");
ASTNode body = statement();
return new ForNode(init, condition, increment, body);
}
private ReturnNode returnStatement() {
ASTNode value = null;
if (!check(TokenType.SEMICOLON) && !isAtEnd()) value = expression();
consumeOptionalSemicolon();
return new ReturnNode(value);
}
private BlockNode blockStatement() {
List<ASTNode> stmts = new ArrayList<>();
while (!check(TokenType.RBRACE) && !isAtEnd()) {
ASTNode d = declaration();
if (d != null) stmts.add(d);
}
consume(TokenType.RBRACE, "Esperado '}'");
return new BlockNode(stmts);
}
// --- Comandos TNS ---
private ScanNode scanStatement() {
ASTNode url = expression();
consumeOptionalSemicolon();
return new ScanNode(url);
}
private DeepScanNode deepScanStatement() {
ASTNode url = expression();
ASTNode depth = null;
if (match(TokenType.COMMA)) depth = expression();
consumeOptionalSemicolon();
return new DeepScanNode(url, depth);
}
private AnalyzeNode analyzeStatement() {
ASTNode url = expression();
consumeOptionalSemicolon();
return new AnalyzeNode(url);
}
private DownloadNode downloadStatement() {
ASTNode url = expression();
ASTNode dest = null;
if (match(TokenType.COMMA)) dest = expression();
consumeOptionalSemicolon();
return new DownloadNode(url, dest);
}
private GithubNode githubStatement() {
ASTNode repo = expression();
consumeOptionalSemicolon();
return new GithubNode(repo);
}
private ReportNode reportStatement() {
Token fmt = consume(TokenType.IDENTIFIER, "Esperado formato: json ou html");
consumeOptionalSemicolon();
return new ReportNode(fmt.lexeme.toLowerCase());
}
private ASTNode expressionStatement() {
ASTNode expr = expression();
consumeOptionalSemicolon();
return expr;
}
// ============================================================
// EXPRESSÕES
// ============================================================
private ASTNode expression() { return assignment(); }
private ASTNode assignment() {
ASTNode expr = logicalOr();
if (match(TokenType.ASSIGN)) {
ASTNode value = assignment();
if (expr instanceof VariableNode var) return new AssignNode(var.name, value);
throw error("Lado esquerdo da atribuição inválido");
}
return expr;
}
private ASTNode logicalOr() {
ASTNode expr = logicalAnd();
while (match(TokenType.OR)) {
TokenType op = previous().type;
expr = new BinaryNode(op, expr, logicalAnd());
}
return expr;
}
private ASTNode logicalAnd() {
ASTNode expr = equality();
while (match(TokenType.AND)) {
TokenType op = previous().type;
expr = new BinaryNode(op, expr, equality());
}
return expr;
}
private ASTNode equality() {
ASTNode expr = comparison();
while (match(TokenType.EQUALS, TokenType.NOT_EQUALS)) {
expr = new BinaryNode(previous().type, expr, comparison());
}
return expr;
}
private ASTNode comparison() {
ASTNode expr = addition();
while (match(TokenType.LESS, TokenType.GREATER, TokenType.LESS_EQUAL, TokenType.GREATER_EQUAL)) {
expr = new BinaryNode(previous().type, expr, addition());
}
return expr;
}
private ASTNode addition() {
ASTNode expr = multiplication();
while (match(TokenType.PLUS, TokenType.MINUS)) {
expr = new BinaryNode(previous().type, expr, multiplication());
}
return expr;
}
private ASTNode multiplication() {
ASTNode expr = unary();
while (match(TokenType.MULTIPLY, TokenType.DIVIDE, TokenType.MODULO)) {
expr = new BinaryNode(previous().type, expr, unary());
}
return expr;
}
private ASTNode unary() {
if (match(TokenType.NOT, TokenType.MINUS)) {
return new UnaryNode(previous().type, unary());
}
return call();
}
private ASTNode call() {
ASTNode expr = primary();
if (expr instanceof VariableNode var && match(TokenType.LPAREN)) {
List<ASTNode> args = new ArrayList<>();
if (!check(TokenType.RPAREN)) {
do { args.add(expression()); } while (match(TokenType.COMMA));
}
consume(TokenType.RPAREN, "Esperado ')' após argumentos");
return new CallNode(var.name, args);
}
return expr;
}
private ASTNode primary() {
if (match(TokenType.NUMBER)) return new NumberNode(previous().literal);
if (match(TokenType.STRING)) return new StringNode((String) previous().literal);
if (match(TokenType.TRUE)) return new BooleanNode(true);
if (match(TokenType.FALSE)) return new BooleanNode(false);
if (match(TokenType.NULL)) return new NullNode();
if (match(TokenType.IDENTIFIER)) return new VariableNode(previous().lexeme);
if (match(TokenType.LPAREN)) {
ASTNode expr = expression();
consume(TokenType.RPAREN, "Esperado ')'");
return expr;
}
throw error("Expressão inválida — token inesperado: " + peek().lexeme);
}
// ============================================================
// HELPERS
// ============================================================
private boolean match(TokenType... types) {
for (TokenType t : types) {
if (check(t)) { advance(); return true; }
}
return false;
}
private Token consume(TokenType type, String message) {
if (check(type)) return advance();
throw error(message);
}
private void consumeOptionalSemicolon() {
match(TokenType.SEMICOLON); // ponto-e-vírgula é opcional em v1
}
private boolean check(TokenType type) {
return !isAtEnd() && peek().type == type;
}
private Token advance() {
if (!isAtEnd()) current++;
return previous();
}
private boolean isAtEnd() { return peek().type == TokenType.EOF; }
private Token peek() { return tokens.get(current); }
private Token previous() { return tokens.get(current - 1); }
private RuntimeException error(String message) {
Token t = peek();
String msg = String.format("[Parser] Linha %d, col %d: %s", t.line, t.column, message);
System.err.println(msg);
return new RuntimeException(msg);
}
private void synchronize() {
advance();
while (!isAtEnd()) {
if (previous().type == TokenType.SEMICOLON) return;
switch (peek().type) {
case FUNCTION, LET, IF, WHILE, FOR, PRINT, RETURN -> { return; }
default -> advance();
}
}
}
}