tokenizer.c (13485B)
1 #include "tokenizer.h" 2 3 #include <ctype.h> 4 #include <string.h> 5 6 typedef enum { 7 StateInitial, 8 StateAlpha, 9 StateDigit, 10 StateOpenParen, 11 StateComment, 12 StateClosingComment, 13 StatePeriod, 14 StateColon, 15 StateGt, 16 StateLt, 17 } State; 18 19 typedef struct { 20 const char* keyword; 21 TokenKind kind; 22 } Keyword; 23 24 static const Keyword keywords[] = { 25 { .keyword = "program", .kind = TokenProgram}, 26 { .keyword = "var", .kind = TokenVar}, 27 { .keyword = "array", .kind = TokenArray}, 28 { .keyword = "of", .kind = TokenOf}, 29 { .keyword = "then", .kind = TokenThen}, 30 { .keyword = "begin", .kind = TokenBegin}, 31 { .keyword = "do", .kind = TokenDo}, 32 { .keyword = "while", .kind = TokenWhile}, 33 { .keyword = "end", .kind = TokenEnd}, 34 { .keyword = "if", .kind = TokenIf}, 35 { .keyword = "else", .kind = TokenElse}, 36 { .keyword = "function", .kind = TokenFunction}, 37 {.keyword = "procedure", .kind = TokenProcedure}, 38 { .keyword = "real", .kind = TokenReal}, 39 { .keyword = "integer", .kind = TokenInteger}, 40 { .keyword = "div", .kind = TokenDiv}, 41 { .keyword = "mod", .kind = TokenMod}, 42 { .keyword = "and", .kind = TokenAnd}, 43 { .keyword = "or", .kind = TokenOr}, 44 { .keyword = "not", .kind = TokenNot}, 45 }; 46 47 constexpr unsigned long keywordsLen = sizeof(keywords) / sizeof(keywords[0]); 48 49 #include <stdio.h> 50 51 Error MakeTokenError( 52 const char* program, 53 Token* this, 54 String message, 55 String dynamic 56 ) 57 { 58 int i = 0; 59 int line = 1; 60 int column = 1; 61 while (&program[i] < this->lex.ptr) { 62 if (program[i] == '\n') { 63 column = 0; 64 line += 1; 65 } 66 i += 1; 67 column += 1; 68 } 69 70 Error error = { 71 .location = 72 { 73 .line = line, 74 .column = column, 75 }, 76 .message = message, 77 .dynamic = dynamic, 78 }; 79 80 return error; 81 } 82 83 void MakeKeyword(Token* this) 84 { 85 for (auto i = 0; i < (int) keywordsLen; i += 1) { 86 if (StringEqCstr(this->lex, keywords[i].keyword)) { 87 this->kind = keywords[i].kind; 88 return; 89 } 90 } 91 92 this->kind = TokenIdent; 93 } 94 95 Tokens* Tokenize(Arena* arena, char* program, Error* error) 96 { 97 auto list = (Tokens*) Calloc(arena, sizeof(Tokens)); 98 auto token = list; 99 100 for (int i = 0; program[i] != '\0'; i += 1) 101 program[i] = tolower(program[i]); 102 103 auto idx = 0; 104 auto state = StateInitial; 105 for (;;) { 106 const auto cchar = program[idx]; 107 const auto cint = (int) (unsigned char) cchar; 108 109 switch (state) { 110 case StateInitial: 111 if (cchar == '\0') { 112 token->this.lex.ptr = &program[idx]; 113 token->this.lex.size = 1; 114 token->this.kind = TokenEOF; 115 token->next = nullptr; 116 return list; 117 } else if (isalpha(cint)) { 118 token->this.lex.ptr = &program[idx]; 119 token->this.lex.size = 1; 120 state = StateAlpha; 121 idx += 1; 122 } else if (isdigit(cint)) { 123 token->this.lex.ptr = &program[idx]; 124 token->this.lex.size = 1; 125 state = StateDigit; 126 idx += 1; 127 } else if (cchar == ';') { 128 token->this.lex.ptr = &program[idx]; 129 token->this.lex.size = 1; 130 token->this.kind = TokenSemicolon; 131 token->next = (Tokens*) Calloc(arena, sizeof(*token->next)); 132 token = token->next; 133 idx += 1; 134 } else if (cchar == ',') { 135 token->this.lex.ptr = &program[idx]; 136 token->this.lex.size = 1; 137 token->this.kind = TokenComma; 138 token->next = (Tokens*) Calloc(arena, sizeof(*token->next)); 139 token = token->next; 140 idx += 1; 141 } else if (cchar == ':') { 142 token->this.lex.ptr = &program[idx]; 143 token->this.lex.size = 1; 144 state = StateColon; 145 idx += 1; 146 } else if (cchar == '(') { 147 token->this.lex.ptr = &program[idx]; 148 token->this.lex.size = 1; 149 state = StateOpenParen; 150 idx += 1; 151 } else if (cchar == ')') { 152 token->this.lex.ptr = &program[idx]; 153 token->this.lex.size = 1; 154 token->this.kind = TokenCloseParen; 155 token->next = (Tokens*) Calloc(arena, sizeof(*token->next)); 156 token = token->next; 157 idx += 1; 158 } else if (cchar == '[') { 159 token->this.lex.ptr = &program[idx]; 160 token->this.lex.size = 1; 161 token->this.kind = TokenOpenSq; 162 token->next = (Tokens*) Calloc(arena, sizeof(*token->next)); 163 token = token->next; 164 idx += 1; 165 } else if (cchar == ']') { 166 token->this.lex.ptr = &program[idx]; 167 token->this.lex.size = 1; 168 token->this.kind = TokenCloseSq; 169 token->next = (Tokens*) Calloc(arena, sizeof(*token->next)); 170 token = token->next; 171 idx += 1; 172 } else if (cchar == '.') { 173 token->this.lex.ptr = &program[idx]; 174 token->this.lex.size = 1; 175 state = StatePeriod; 176 idx += 1; 177 } else if (cchar == '+') { 178 token->this.lex.ptr = &program[idx]; 179 token->this.lex.size = 1; 180 token->this.kind = TokenPlus; 181 token->next = (Tokens*) Calloc(arena, sizeof(*token->next)); 182 token = token->next; 183 idx += 1; 184 } else if (cchar == '-') { 185 token->this.lex.ptr = &program[idx]; 186 token->this.lex.size = 1; 187 token->this.kind = TokenMinus; 188 token->next = (Tokens*) Calloc(arena, sizeof(*token->next)); 189 token = token->next; 190 idx += 1; 191 } else if (cchar == '*') { 192 token->this.lex.ptr = &program[idx]; 193 token->this.lex.size = 1; 194 token->this.kind = TokenMul; 195 token->next = (Tokens*) Calloc(arena, sizeof(*token->next)); 196 token = token->next; 197 idx += 1; 198 } else if (cchar == '=') { 199 token->this.lex.ptr = &program[idx]; 200 token->this.lex.size = 1; 201 token->this.kind = TokenEq; 202 token->next = (Tokens*) Calloc(arena, sizeof(*token->next)); 203 token = token->next; 204 idx += 1; 205 } else if (cchar == '>') { 206 token->this.lex.ptr = &program[idx]; 207 token->this.lex.size = 1; 208 state = StateGt; 209 idx += 1; 210 } else if (cchar == '<') { 211 token->this.lex.ptr = &program[idx]; 212 token->this.lex.size = 1; 213 state = StateLt; 214 idx += 1; 215 } else if (cchar == '/') { 216 token->this.lex.ptr = &program[idx]; 217 token->this.lex.size = 1; 218 token->this.kind = TokenSlash; 219 token->next = (Tokens*) Calloc(arena, sizeof(*token->next)); 220 token = token->next; 221 idx += 1; 222 } else { 223 *error = MakeTokenError( 224 program, 225 &token->this, 226 STR("unexpected symbol"), 227 (String){ 228 .ptr = &program[idx], 229 .size = 1, 230 } 231 ); 232 token->next = nullptr; 233 token->this.kind = TokenEOF; 234 return list; 235 } 236 break; 237 case StateAlpha: 238 if (isalnum(cint)) { 239 token->this.lex.size += 1; 240 idx += 1; 241 } else { 242 MakeKeyword(&token->this); 243 token->next = (Tokens*) Calloc(arena, sizeof(*token->next)); 244 token = token->next; 245 state = StateInitial; 246 } 247 break; 248 case StateDigit: 249 if (isalnum(cint)) { 250 token->this.lex.size += 1; 251 idx += 1; 252 } else { 253 token->next = (Tokens*) Calloc(arena, sizeof(*token->next)); 254 token->this.kind = TokenDigit; 255 token = token->next; 256 state = StateInitial; 257 } 258 break; 259 case StateOpenParen: 260 if (cchar == '*') { 261 state = StateComment; 262 token->this.lex.size += 1; 263 idx += 1; 264 } else { 265 token->next = (Tokens*) Calloc(arena, sizeof(*token->next)); 266 token->this.kind = TokenOpenParen; 267 token = token->next; 268 state = StateInitial; 269 } 270 break; 271 case StateComment: 272 if (cchar == '*') { 273 state = StateClosingComment; 274 token->this.lex.size += 1; 275 idx += 1; 276 277 } else if (cchar == '\0') { 278 *error = MakeTokenError( 279 program, &token->this, STR("unclosed comment"), STR("") 280 ); 281 token->next = nullptr; 282 token->this.kind = TokenComment; 283 return list; 284 } else { 285 idx += 1; 286 token->this.lex.size += 1; 287 } 288 break; 289 case StateClosingComment: 290 if (cchar == ')') { 291 token->next = (Tokens*) Calloc(arena, sizeof(*token->next)); 292 token->this.lex.size += 1; 293 token->this.kind = TokenComment; 294 token = token->next; 295 state = StateInitial; 296 idx += 1; 297 } else { 298 state = StateComment; 299 } 300 break; 301 case StatePeriod: 302 if (cchar == '.') { 303 token->next = (Tokens*) Calloc(arena, sizeof(*token->next)); 304 token->this.lex.size += 1; 305 token->this.kind = TokenTwoPeriods; 306 token = token->next; 307 state = StateInitial; 308 idx += 1; 309 } else { 310 token->next = (Tokens*) Calloc(arena, sizeof(*token->next)); 311 token->this.kind = TokenPeriod; 312 token = token->next; 313 state = StateInitial; 314 } 315 break; 316 case StateColon: 317 if (cchar == '=') { 318 token->next = (Tokens*) Calloc(arena, sizeof(*token->next)); 319 token->this.lex.size += 1; 320 token->this.kind = TokenAssign; 321 token = token->next; 322 state = StateInitial; 323 idx += 1; 324 } else { 325 token->this.kind = TokenColon; 326 token->next = (Tokens*) Calloc(arena, sizeof(*token->next)); 327 token = token->next; 328 state = StateInitial; 329 } 330 break; 331 case StateGt: 332 if (cchar == '=') { 333 token->next = (Tokens*) Calloc(arena, sizeof(*token->next)); 334 token->this.lex.size += 1; 335 token->this.kind = TokenGe; 336 token = token->next; 337 state = StateInitial; 338 idx += 1; 339 } else { 340 token->this.kind = TokenGt; 341 token->next = (Tokens*) Calloc(arena, sizeof(*token->next)); 342 token = token->next; 343 state = StateInitial; 344 } 345 break; 346 case StateLt: 347 if (cchar == '=') { 348 token->next = (Tokens*) Calloc(arena, sizeof(*token->next)); 349 token->this.lex.size += 1; 350 token->this.kind = TokenLe; 351 token = token->next; 352 state = StateInitial; 353 idx += 1; 354 } else if (cchar == '>') { 355 token->next = (Tokens*) Calloc(arena, sizeof(*token->next)); 356 token->this.lex.size += 1; 357 token->this.kind = TokenNeq; 358 token = token->next; 359 state = StateInitial; 360 idx += 1; 361 } else { 362 token->this.kind = TokenLt; 363 token->next = (Tokens*) Calloc(arena, sizeof(*token->next)); 364 token = token->next; 365 state = StateInitial; 366 } 367 break; 368 } 369 } 370 }