-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlex.h
198 lines (161 loc) · 3.16 KB
/
lex.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
192
193
194
195
196
197
198
/**
* @file lex.h
* @author Jindřich Vodák ([email protected])
* @brief Hlavičkový soubor pro Lexical analyzator
* Implementation IFJ22 compiler
*/
#include "dynstring.h"
#include <stdio.h>
#include <stdlib.h>
#ifndef LUAINTERPRET_LEX_H
#define LUAINTERPRET_LEX_H
extern bool php;
extern bool declare;
extern bool endingMark;
// represents the type of lexical item
typedef enum {
// keywords
elseKey,
functionKey,
ifKey,
nullKey,
returnKey,
voidKey,
whileKey,
stringKey,
floatKey,
intKey,
stringNullKey,
floatNullKey,
intNullKey,
// declare header
declareHeader,
// curly braces
curlyBraceRight,
curlyBraceLeft,
// assignment
equals,
colon,
semicolon,
comma,
// operators
multiplicationOp,
divisionOp,
plusOp,
minusOp,
concatenationOp,
lesserThanOp,
lesserEqOp,
greaterThanOp,
greaterEqOp,
eqOp,
notEqOp,
// parentheses
leftPar,
rightPar,
// literals
stringLiteral, // "hello"
integerLiteral,
floatLiteral,
// identifiers
identifierFunc,
identifierVar,
// unknown
unknown,
// EOF type
ending,
lexTypeCount
} lexType;
// FSM states
typedef enum
{
// initial state
init_s,
// literal states
string_lit_s,
string_lit_f_s,
integer_lit_f_s,
float_lit_dot_s,
float_lit_e_s,
float_lit_sign_s,
float_lit_exp_f_s,
float_lit_f_s,
// identifier states
identifier_func_f_s,
identifier_var_dollar_s,
identifier_var_f_s,
// operator states
multiplication_f_s,
division_f_s,
plus_f_s,
minus_f_s,
dot_f_s,
concatenation_f_s,
lesser_than_f_s,
lesser_eq_f_s,
greater_than_f_s,
greater_eq_f_s,
eq_2_s,
eq_f_s,
not_eq_1_s,
not_eq_2_s,
not_eq_f_s,
// escape sequences states
string_lit_backslash_s,
string_lit_backslash_x_s,
string_lit_backslash_x_1_s,
string_lit_backslash_1_s,
string_lit_backslash_2_s,
// parentheses states
left_par_f_s,
right_par_f_s,
// commentary states
com_line_f_s,
com_block_s,
com_block_ast_s,
// curly braces states
left_curly_f_s,
right_curly_f_s,
// assignment states
equals_f_s,
colon_f_s,
semicolon_f_s,
comma_f_s,
// TODO
// nullable type state
null_f_s,
// unknown state
unknown_f_s,
} state;
// represents the value of the token
typedef union
{
dynStr_t *valueString;
int valueInteger;
double valueFloat;
} data_t;
// represents the output
typedef struct
{
lexType type;
data_t data;
int rowNumber;
int rowPosNumber;
} token_t;
// gets the next token and advances the pointer
token_t getToken(FILE *stream);
// prints the content of a token
void printTokenData(token_t input);
// frees the token from memory
void freeToken(token_t *input);
/**
* Resets row and rowPos counters.
*/
void resetCounters();
// sets the file pointer one token back
void ungetToken(FILE *stream);
extern bool teston;
extern token_t *testTokens;
void clearBuffer(dynStr_t *buffer);
void headerCheck(FILE *stream);
#endif