-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.h
191 lines (175 loc) · 8.47 KB
/
parser.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
190
191
/*
* Project: IFJ16, a programming language interpreter
* FIT VUT Brno
* Authors: xzaryb00 - Zarybnický Jakub
* xtamas01 - Tamaškovič Marek
* xvasko12 - Vaško Martin
* xvasko14 - Vaško Michal
* xzales12 - Záleský Jiří
*/
#ifndef IFJ_PARSER_H
#define IFJ_PARSER_H
#include <stdio.h>
#include <string.h>
#include "error.h"
#include "ir.h"
#include "scanner.h"
#include "ial.h"
#include "interpret.h"
#define STRINGIFY(arg) STRINGIFY1(arg)
#define STRINGIFY1(arg) STRINGIFY2(arg)
#define STRINGIFY2(arg) #arg
typedef struct {
FILE *file;
Token *start;
Token *current;
unsigned stackSize;
unsigned stackPtr;
Token **stack;
Interpret *interpret;
char *lastClassName;
} Lexer;
Lexer *createLexer(FILE *f, Interpret *i);
void freeLexer(Lexer *l);
bool try(Lexer *l);
bool rollback(Lexer *l);
bool commit(Lexer *l);
#define bracket(l, fn, ...) \
try(l); \
if (fn(l, ## __VA_ARGS__)) { \
commit(l); \
return true; \
} \
rollback(l);
#define bracket_(l, fn) \
try(l); \
if (fn(l)) { \
commit(l); \
return true; \
} \
rollback(l);
#define expectMore(l) \
if (peekToken(l) == NULL) { \
MERROR(ERR_SYNTAX, "Unexpected end of input.\n"); \
}
#define expectType(l, x) do { \
expectMore(l); \
Token *t = peekToken(l); \
if (t->type != (x)) { \
FERROR(ERR_SYNTAX, \
"Expected %s on line %d:%d, received '%s'.\n", \
STRINGIFY(x), t->lineNum, t->lineChar, t->original); \
} \
} while (0);
#define expectSymbol_(l, x, f) do { \
expectMore(l); \
Token *t = peekToken(l); \
if (t->type != SYMBOL || t->val.symbol != (x)) { \
f; \
FERROR(ERR_SYNTAX, \
"Expected %s on line %d:%d, received '%s'.\n", \
STRINGIFY(x), t->lineNum, t->lineChar, t->original); \
} \
nextToken(l); \
} while (0);
#define expectSymbol(l, x) expectSymbol_(l, x, NULL)
#define expectReserved_(l, x, f) do { \
expectMore(l); \
Token *t = peekToken(l); \
if (t->type != RESERVED || t->val.reserved != (x)) { \
f; \
FERROR(ERR_SYNTAX, \
"Expected %s on line %d:%d, received '%s'.\n", \
STRINGIFY(x), t->lineNum, t->lineChar, t->original); \
} \
nextToken(l); \
} while (0);
#define expectReserved(l, x) expectReserved_(l, x, NULL)
#define tryType(l, x, ret) do { \
Token *t = peekToken(l); \
if (t == NULL || t->type != (x)) { \
return ret; \
} \
} while (0);
#define trySymbol(l, x, ret) do { \
Token *t = peekToken(l); \
if (t == NULL || t->type != SYMBOL || t->val.symbol != (x)) { \
return ret; \
} \
nextToken(l); \
} while (0);
#define tryReserved(l, x, ret) do { \
Token *t = peekToken(l); \
if (t == NULL || t->type != RESERVED || t->val.reserved != (x)) { \
return ret; \
} \
nextToken(l); \
} while (0);
#define tryValueType(l, ret) do { \
Token *t = peekToken(l); \
if (t == NULL || t->type != RESERVED || \
!(t->val.reserved == RES_BOOLEAN || \
t->val.reserved == RES_DOUBLE || \
t->val.reserved == RES_INT || \
t->val.reserved == RES_STRING)) { \
return ret; \
} \
} while (0);
#define errorExpectedExpression(l) do { \
Token *t = peekToken(l); \
FERROR(ERR_SYNTAX, \
"Expected an expression on line %d:%d, received '%s'.\n", \
t->lineNum, t->lineChar, t->original); \
} while (0);
#define errorExpectedCommand(l) do { \
Token *t = peekToken(l); \
FERROR(ERR_SYNTAX, \
"Expected a command on line %d:%d, received '%s'.\n", \
t->lineNum, t->lineChar, t->original); \
} while (0);
#define PARSE_EXPRESSION(e, l, x) \
Expression *e = parseExpression(l, x); \
if (e == NULL) { \
errorExpectedExpression(l); \
}
#define hasMore(l) \
(peekToken(l) != NULL)
#define isType(l, x) \
(peekToken(l) != NULL && peekToken(l)->type == (x))
#define isSymbol(l, x) \
(peekToken(l) != NULL && peekToken(l)->type == SYMBOL && peekToken(l)->val.symbol == (x))
#define isReserved(l, x) \
(peekToken(l) != NULL && peekToken(l)->type == RESERVED && peekToken(l)->val.reserved == (x))
#define nextToken(l) if (peekToken(l) != NULL) (l)->current = (l)->current->next
Token *peekToken(Lexer *l);
void parseClass(Lexer *l);
bool parseClassBody(Lexer *l);
bool parseStaticDeclaration(Lexer *l);
bool parseStaticDefinition(Lexer *l);
bool parseFunction(Lexer *l);
bool parseFunctionBody(Lexer *l, Block *f);
bool parseLocalDeclaration(Lexer *l, Block *b);
bool parseLocalDefinition(Lexer *l, Block *b);
bool parseIf(Lexer *l, Block *b);
bool parseWhile(Lexer *l, Block *b);
bool parseDoWhile(Lexer *l, Block *b);
bool parseFor(Lexer *l, Block *b);
bool parseCommand(Lexer *l, Block *b);
bool parseAssign(Lexer *l, Block *b);
bool parseAssignExpression(Lexer *l, Block *b, SymbolType end);
bool parseFuncall(Lexer *l, Block *b);
bool parseContinue(Lexer *l, Block *b);
bool parseBreak(Lexer *l, Block *b);
bool parseReturn(Lexer *l, Block *b);
bool parseBlock(Lexer *l, Block *b);
Expression *parseExpression(Lexer *, SymbolType end);
bool parseDeclaration(Lexer *l, Declaration **d);
Declaration *parseArgListDecl(Lexer *l, int *argCount);
Expression *parseArgListCall(Lexer *l, int *argCount);
char *parseSimpleId(Lexer *l);
char *parseAndQualifySimpleId(Lexer *l);
char *parseAndQualifyId(Lexer *l);
char *parseAnyId(Lexer *l);
ValueType parseType(Lexer *l);
ValueType parseReturnType(Lexer *l);
#endif /* IFJ_PARSER_H */