-
Notifications
You must be signed in to change notification settings - Fork 0
/
lex.h
172 lines (132 loc) · 4.55 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
#ifndef LEX_H
#define LEX_H
#include "program.h"
#include "util.h"
#include <stdlib.h>
enum lex_token_ty {
LEX_TOKEN_TY_UNKNOWN,
LEX_TOKEN_TY_EOF,
/* Trivia (whitespace, comments). Handled entirely by lexer */
LEX_TOKEN_TY_WHITESPACE,
LEX_TOKEN_TY_INLINE_COMMENT,
LEX_TOKEN_TY_MULTILINE_COMMENT,
/* Bracket types */
LEX_TOKEN_TY_OPEN_BRACKET, // (
LEX_TOKEN_TY_CLOSE_BRACKET, // )
LEX_TOKEN_TY_OPEN_BRACE, // {
LEX_TOKEN_TY_CLOSE_BRACE, // }
LEX_TOKEN_TY_OPEN_SQUARE_BRACKET, // [
LEX_TOKEN_TY_CLOSE_SQUARE_BRACKET, // ]
LEX_TOKEN_TY_COLON, // :
LEX_TOKEN_TY_SEMICOLON, // ;
LEX_TOKEN_TY_COMMA, // ,
LEX_TOKEN_TY_DOT, // .
LEX_TOKEN_TY_ARROW, // ->
LEX_TOKEN_TY_QMARK, // ?
/* Operators */
LEX_TOKEN_TY_OP_LOGICAL_NOT, // !
LEX_TOKEN_TY_OP_NOT, // ~
LEX_TOKEN_TY_OP_INC, // ++
LEX_TOKEN_TY_OP_DEC, // --
LEX_TOKEN_TY_OP_LOGICAL_AND, // &&
LEX_TOKEN_TY_OP_LOGICAL_OR, // ||
LEX_TOKEN_TY_OP_AND, // &
LEX_TOKEN_TY_OP_AND_ASSG, // &=
LEX_TOKEN_TY_OP_OR, // |
LEX_TOKEN_TY_OP_OR_ASSG, // |=
LEX_TOKEN_TY_OP_XOR, // ^
LEX_TOKEN_TY_OP_XOR_ASSG, // ^=
LEX_TOKEN_TY_OP_RSHIFT, // >>
LEX_TOKEN_TY_OP_RSHIFT_ASSG, // >>=
LEX_TOKEN_TY_OP_LSHIFT, // <<
LEX_TOKEN_TY_OP_LSHIFT_ASSG, // <<=
LEX_TOKEN_TY_OP_ADD, // +
LEX_TOKEN_TY_OP_ADD_ASSG, // +=
LEX_TOKEN_TY_OP_SUB, // -
LEX_TOKEN_TY_OP_SUB_ASSG, // -=
LEX_TOKEN_TY_OP_MUL, // *
LEX_TOKEN_TY_OP_MUL_ASSG, // *=
LEX_TOKEN_TY_OP_DIV, // /
LEX_TOKEN_TY_OP_DIV_ASSG, // /=
LEX_TOKEN_TY_OP_QUOT, // %
LEX_TOKEN_TY_OP_QUOT_ASSG, // %=
LEX_TOKEN_TY_OP_ASSG, // =
LEX_TOKEN_TY_OP_EQ, // ==
LEX_TOKEN_TY_OP_NEQ, // !=
LEX_TOKEN_TY_OP_LT, // <
LEX_TOKEN_TY_OP_LTEQ, // <=
LEX_TOKEN_TY_OP_GT, // >
LEX_TOKEN_TY_OP_GTEQ, // >=
/* Variadic */
LEX_TOKEN_TY_ELLIPSIS,
/* Keywords */
LEX_TOKEN_TY_KW_GOTO,
LEX_TOKEN_TY_KW_BREAK,
LEX_TOKEN_TY_KW_CONTINUE,
LEX_TOKEN_TY_KW_FOR,
LEX_TOKEN_TY_KW_DO,
LEX_TOKEN_TY_KW_WHILE,
LEX_TOKEN_TY_KW_IF,
LEX_TOKEN_TY_KW_ELSE,
LEX_TOKEN_TY_KW_TYPEDEF,
LEX_TOKEN_TY_KW_STATIC,
LEX_TOKEN_TY_KW_EXTERN,
LEX_TOKEN_TY_KW_AUTO,
LEX_TOKEN_TY_KW_REGISTER,
LEX_TOKEN_TY_KW_INLINE,
LEX_TOKEN_TY_KW_CONST,
LEX_TOKEN_TY_KW_VOLATILE,
LEX_TOKEN_TY_KW_VOID,
LEX_TOKEN_TY_KW_FLOAT,
LEX_TOKEN_TY_KW_DOUBLE,
LEX_TOKEN_TY_KW_CHAR,
LEX_TOKEN_TY_KW_SHORT,
LEX_TOKEN_TY_KW_INT,
LEX_TOKEN_TY_KW_LONG,
LEX_TOKEN_TY_KW_SIGNED,
LEX_TOKEN_TY_KW_UNSIGNED,
LEX_TOKEN_TY_KW_RETURN,
LEX_TOKEN_TY_KW_ENUM,
LEX_TOKEN_TY_KW_STRUCT,
LEX_TOKEN_TY_KW_UNION,
LEX_TOKEN_TY_KW_SIZEOF,
LEX_TOKEN_TY_KW_ALIGNOF, // both `_Alignof` (C11) and `alignof` (C23 onwards)
LEX_TOKEN_TY_KW_ALIGNAS, // both `_Alignas` (C11) and `alignas` (C23 onwards)
LEX_TOKEN_TY_IDENTIFIER,
/* Literals (all suffixes are case-insensitive) */
LEX_TOKEN_TY_ASCII_CHAR_LITERAL, // 'a'
LEX_TOKEN_TY_ASCII_STR_LITERAL, // "foobar"
LEX_TOKEN_TY_FLOAT_LITERAL, // 10.0f
LEX_TOKEN_TY_DOUBLE_LITERAL, // 10.0
LEX_TOKEN_TY_LONG_DOUBLE_LITERAL, // 10.0l
// Note: `lex.c` relies on `unsigned` being `signed + 1`
LEX_TOKEN_TY_SIGNED_INT_LITERAL, // 10
LEX_TOKEN_TY_UNSIGNED_INT_LITERAL, // 10u
LEX_TOKEN_TY_SIGNED_LONG_LITERAL, // 10l
LEX_TOKEN_TY_UNSIGNED_LONG_LITERAL, // 10ul or 10lu
LEX_TOKEN_TY_SIGNED_LONG_LONG_LITERAL, // 10ll
LEX_TOKEN_TY_UNSIGNED_LONG_LONG_LITERAL, // 10ull or 10llu
};
enum lex_create_result { LEX_CREATE_RESULT_SUCCESS = 0 };
struct token {
enum lex_token_ty ty;
struct text_span span;
};
struct lexer;
bool lexer_at_eof(struct lexer *lexer);
enum lex_create_result lexer_create(struct preprocessed_program *program,
struct lexer **lexer);
void lexer_free(struct lexer **lexer);
struct text_pos get_position(struct lexer *lexer);
void backtrack(struct lexer *lexer, struct text_pos position);
void peek_token(struct lexer *lexer, struct token *token);
void consume_token(struct lexer *lexer, struct token token);
// returns the associated text for a token, or NULL
// e.g
// * `token.ty == LEX_TOKEN_TY_OPEN_PAREN`, this returns NULL
// * `token.ty == LEX_TOKEN_TY_IDENTIFIER`, this returns the identifier
// TODO: maybe make this so it doesn't copy, and returns string length as well
const char *associated_text(const struct lexer *lexer,
const struct token *token);
const char *token_name(const struct lexer *lexer, const struct token *token);
#endif