diff options
author | Bad Diode <bd@badd10de.dev> | 2021-10-29 15:37:28 +0200 |
---|---|---|
committer | Bad Diode <bd@badd10de.dev> | 2021-10-29 15:37:28 +0200 |
commit | e73a4c16a2269cdb2f5e7d66fb9839e4c44e14de (patch) | |
tree | c44721b005b7a0623e7acc7103ca8e21a25ff422 /src/lexer.h | |
parent | fcc131afdd029c606ea39f3557bc3d33a075b1de (diff) | |
download | bdl-e73a4c16a2269cdb2f5e7d66fb9839e4c44e14de.tar.gz bdl-e73a4c16a2269cdb2f5e7d66fb9839e4c44e14de.zip |
Prepare third compiler implementation
Diffstat (limited to 'src/lexer.h')
-rwxr-xr-x | src/lexer.h | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/src/lexer.h b/src/lexer.h new file mode 100755 index 0000000..e56f5f2 --- /dev/null +++ b/src/lexer.h | |||
@@ -0,0 +1,67 @@ | |||
1 | #ifndef BDL_LEXER_H | ||
2 | #define BDL_LEXER_H | ||
3 | |||
4 | #include "string_view.h" | ||
5 | |||
6 | typedef enum TokenType { | ||
7 | TOKEN_UNKNOWN = 0, | ||
8 | |||
9 | // Parentheses. | ||
10 | TOKEN_LPAREN, | ||
11 | TOKEN_RPAREN, | ||
12 | |||
13 | // Primitive types. | ||
14 | TOKEN_FIXNUM, | ||
15 | TOKEN_SYMBOL, | ||
16 | TOKEN_STRING, | ||
17 | TOKEN_NIL, | ||
18 | TOKEN_TRUE, | ||
19 | TOKEN_FALSE, | ||
20 | |||
21 | // End of file. | ||
22 | TOKEN_EOF, | ||
23 | } TokenType; | ||
24 | |||
25 | typedef struct Token { | ||
26 | TokenType type; | ||
27 | StringView value; | ||
28 | size_t line; | ||
29 | size_t column; | ||
30 | } Token; | ||
31 | |||
32 | typedef struct Tokens { | ||
33 | Token *tokens; | ||
34 | Errors errors; | ||
35 | } Tokens; | ||
36 | |||
37 | typedef struct Scanner { | ||
38 | StringView current; | ||
39 | size_t line_number; | ||
40 | size_t col_number; | ||
41 | size_t offset; | ||
42 | } Scanner; | ||
43 | |||
44 | // Print a token to standard output for debugging purposes. | ||
45 | void print_token(Token tok); | ||
46 | |||
47 | // Same functionality as the ScanView pairs, but keeping track of line and | ||
48 | // column numbers. | ||
49 | char scan_next(Scanner *scanner); | ||
50 | char scan_peek(const Scanner *scanner); | ||
51 | |||
52 | // Check if the current scanner still have characters left. | ||
53 | bool scan_has_next(const Scanner *scanner); | ||
54 | |||
55 | // Advance the scanner until we ran out of whitespace. | ||
56 | void skip_whitespace(Scanner *scanner); | ||
57 | |||
58 | // Check if a given character is a delimiter. | ||
59 | bool is_delimiter(char c); | ||
60 | |||
61 | // Extract the token type from the current string. | ||
62 | TokenType find_primitive_type(const StringView value); | ||
63 | |||
64 | // Generate a list of tokens from the given string. | ||
65 | Tokens tokenize(const StringView *sv); | ||
66 | |||
67 | #endif // BDL_LEXER_H | ||