blob: e125a9913fe05baa7fd49447d481e3d0e7287182 (
plain)
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
|
#include <stdio.h>
#include <string.h>
#include "common.h"
#include "scanner.h"
typedef struct {
const char *start;
const char *current;
int line;
} Scanner;
Scanner scanner;
void initScanner(const char *source) {
scanner.start = source;
scanner.current = source;
scanner.line = 1;
}
static bool isAtEnd() { return *scanner.current == '\0'; }
static Token makeToken(TokenType type) {
Token token;
token.type = type;
token.start = scanner.start;
token.length = (int)(scanner.current - scanner.start);
token.line = scanner.line;
return token;
}
static Token errorToken(const char *message) {
Token token;
token.type = TOKEN_ERROR;
token.start = message;
token.length = (int)strlen(message);
token.line = scanner.line;
return token;
}
Token scanToken() {
scanner.start = scanner.current;
if (isAtEnd())
return makeToken(TOKEN_EOF);
return errorToken("Unexpected character.");
}
|