-
Notifications
You must be signed in to change notification settings - Fork 244
Expand file tree
/
Copy pathlexstate.c
More file actions
212 lines (182 loc) · 5.85 KB
/
lexstate.c
File metadata and controls
212 lines (182 loc) · 5.85 KB
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include "rbs/defines.h"
#include "rbs/lexer.h"
#include "rbs/util/rbs_assert.h"
static const char *RBS_TOKENTYPE_NAMES[] = {
"NullType",
"pEOF",
"ErrorToken",
"pLPAREN", /* ( */
"pRPAREN", /* ) */
"pCOLON", /* : */
"pCOLON2", /* :: */
"pLBRACKET", /* [ */
"pRBRACKET", /* ] */
"pLBRACE", /* { */
"pRBRACE", /* } */
"pHAT", /* ^ */
"pARROW", /* -> */
"pFATARROW", /* => */
"pCOMMA", /* , */
"pBAR", /* | */
"pAMP", /* & */
"pSTAR", /* * */
"pSTAR2", /* ** */
"pDOT", /* . */
"pDOT3", /* ... */
"pBANG", /* ! */
"pQUESTION", /* ? */
"pLT", /* < */
"pGT", /* > */
"pEQ", /* = */
"kALIAS", /* alias */
"kATTRACCESSOR", /* attr_accessor */
"kATTRREADER", /* attr_reader */
"kATTRWRITER", /* attr_writer */
"kBOOL", /* bool */
"kBOT", /* bot */
"kCLASS", /* class */
"kCLASSALIAS", /* class-alias */
"kDEF", /* def */
"kEND", /* end */
"kEXTEND", /* extend */
"kFALSE", /* kFALSE */
"kIN", /* in */
"kINCLUDE", /* include */
"kINSTANCE", /* instance */
"kINTERFACE", /* interface */
"kMODULE", /* module */
"kMODULEALIAS", /* module-alias */
"kNIL", /* nil */
"kOUT", /* out */
"kPREPEND", /* prepend */
"kPRIVATE", /* private */
"kPUBLIC", /* public */
"kSELF", /* self */
"kSINGLETON", /* singleton */
"kTOP", /* top */
"kTRUE", /* true */
"kTYPE", /* type */
"kUNCHECKED", /* unchecked */
"kUNTYPED", /* untyped */
"kVOID", /* void */
"kUSE", /* use */
"kAS", /* as */
"k__TODO__", /* __todo__ */
"kATRBS", /* @rbs */
"kSKIP", /* skip */
"kRETURN", /* return */
"tLIDENT", /* Identifiers starting with lower case */
"tUIDENT", /* Identifiers starting with upper case */
"tULIDENT", /* Identifiers starting with `_` */
"tULLIDENT",
"tGIDENT", /* Identifiers starting with `$` */
"tAIDENT", /* Identifiers starting with `@` */
"tA2IDENT", /* Identifiers starting with `@@` */
"tBANGIDENT",
"tEQIDENT",
"tQIDENT", /* Quoted identifier */
"pAREF_OPR", /* [] */
"tOPERATOR", /* Operator identifier */
"tCOMMENT",
"tLINECOMMENT",
"tINLINECOMMENT",
"tTRIVIA",
"tDQSTRING", /* Double quoted string */
"tSQSTRING", /* Single quoted string */
"tINTEGER", /* Integer */
"tSYMBOL", /* Symbol */
"tDQSYMBOL",
"tSQSYMBOL",
"tANNOTATION", /* Annotation */
};
const rbs_position_t NullPosition = { -1, -1, -1, -1 };
const rbs_range_t NULL_RANGE = { { -1, -1, -1, -1 }, { -1, -1, -1, -1 } };
const rbs_token_t NullToken = { .type = NullType, .range = { { 0 }, { 0 } } };
const char *rbs_token_type_str(enum RBSTokenType type) {
return RBS_TOKENTYPE_NAMES[type];
}
int rbs_token_chars(rbs_token_t tok) {
return tok.range.end.char_pos - tok.range.start.char_pos;
}
int rbs_token_bytes(rbs_token_t tok) {
return RBS_RANGE_BYTES(tok.range);
}
unsigned int rbs_peek(rbs_lexer_t *lexer) {
return lexer->current_code_point;
}
bool rbs_next_char(rbs_lexer_t *lexer, unsigned int *codepoint, size_t *byte_len) {
if (RBS_UNLIKELY(lexer->current.byte_pos == lexer->end_pos)) {
return false;
}
const char *start = lexer->string.start + lexer->current.byte_pos;
// Fast path for ASCII (single-byte) characters
if ((unsigned int) *start < 128) {
*codepoint = (unsigned int) *start;
*byte_len = 1;
return true;
}
*byte_len = lexer->encoding->char_width((const uint8_t *) start, (ptrdiff_t) (lexer->string.end - start));
if (*byte_len == 1) {
*codepoint = (unsigned int) *start;
} else {
*codepoint = 12523; // Dummy data for "ル" from "ルビー" (Ruby) in Unicode
}
return true;
}
void rbs_skip(rbs_lexer_t *lexer) {
RBS_ASSERT(lexer->current_character_bytes > 0, "rbs_skip called with current_character_bytes == 0");
if (RBS_UNLIKELY(lexer->current_code_point == '\0')) {
return;
}
unsigned int codepoint;
size_t byte_len;
lexer->current.byte_pos += lexer->current_character_bytes;
lexer->current.char_pos += 1;
if (lexer->current_code_point == '\n') {
lexer->current.line += 1;
lexer->current.column = 0;
lexer->first_token_of_line = true;
} else {
lexer->current.column += 1;
}
if (rbs_next_char(lexer, &codepoint, &byte_len)) {
lexer->current_code_point = codepoint;
lexer->current_character_bytes = byte_len;
} else {
lexer->current_character_bytes = 1;
lexer->current_code_point = '\0';
}
}
rbs_token_t rbs_next_token(rbs_lexer_t *lexer, enum RBSTokenType type) {
rbs_token_t t;
t.type = type;
t.range.start = lexer->start;
t.range.end = lexer->current;
lexer->start = lexer->current;
if (type != tTRIVIA) {
lexer->first_token_of_line = false;
}
return t;
}
rbs_token_t rbs_next_eof_token(rbs_lexer_t *lexer) {
if ((size_t) lexer->current.byte_pos == rbs_string_len(lexer->string) + 1) {
// End of String
rbs_token_t t;
t.type = pEOF;
t.range.start = lexer->start;
t.range.end = lexer->start;
lexer->start = lexer->current;
return t;
} else {
// NULL byte in the middle of the string
return rbs_next_token(lexer, pEOF);
}
}
void rbs_skipn(rbs_lexer_t *lexer, size_t size) {
for (size_t i = 0; i < size; i++) {
rbs_skip(lexer);
}
}
char *rbs_peek_token(rbs_lexer_t *lexer, rbs_token_t tok) {
return (char *) lexer->string.start + tok.range.start.byte_pos;
}