Logo

index : 0x2lib

Library extension for Jai

  • summary
  • about
  • tree
  • log
  • branches
<< path: root/public/0x2lib.git/html/0x2_Lexer.jai blob: 559d1189f78504a86080f06f5f1dbeff2417fef9 [raw] [clear marker]

        
0
1
2/** TODO: Better test coverage */
3
4
5#add_context lib0x2_lexer_error_template := PARSER_ERROR_TEMPLATE;
6
7
8Lexer_State :: struct {
9 index: int;
10 source: string;
11}
12
13
14/* ------------ *
15 * Lexer Macros *
16 * ------------ */
17
18is_at_end :: () -> bool #expand {
19 return is_at_end(`index, `source);
20}
21
22peek :: () -> u8 #expand {
23 return peek(`index, `source);
24}
25
26peek_back :: () -> u8 #expand {
27 return peek_back(`index, `source);
28}
29
30peek_at :: (offset: int) -> u8 #expand {
31 return peek_back(`index + offset, `source);
32}
33
34peek_forward :: (offset: int) -> u8 #expand {
35 return peek_forward(`index, `source, offset);
36}
37
38advance :: () -> u8 #expand {
39 return advance(*`index, `source);
40}
41
42advance_by :: inline (offset: int) #expand {
43 advance_by(*`index, offset);
44}
45
46advance_by :: inline (offset: string) #expand {
47 advance_by(*`index, offset.count);
48}
49
50advance_by :: inline (offset: ..string) #expand {
51 count: int;
52 for offset count += it.count;
53 advance_by(*`index, count);
54}
55
56match :: (substring: string, offset := 0) -> bool #expand {
57 return match(`source, substring, offset);
58}
59
60match_grammar :: (
61 substrings: []string, offset := 0
62)
63 -> (match: bool, advanced: int) #expand
64{
65 a, b := match_grammar(`source, substrings, offset);
66 return a, b;
67}
68
69/** Allocates memory. You have to manage that.
70 If you do not want an allocation, pass `allocate = false`.
71*/
72consume_including :: (token: $T/Type.[ string, u8 ], $allocate := true) -> string #expand {
73 return consume_including(`index, `source, token, allocate=allocate);
74}
75
76/** Allocates memory. You have to manage that.
77 If you do not want an allocation, pass `allocate = false`.
78*/
79consume_till :: (token: $T/Type.[ string, u8 ], $allocate := true) -> string #expand {
80 return consume_till(`index, `source, token, allocate=allocate);
81}
82
83
84/* ---------------------------- *
85 * Procs, assuming Lexer_State *
86 * ---------------------------- */
87
88count_newlines_till_now :: (using x2lexer_state: $T/interface Lexer_State) -> int {
89 count := 1;
90 for i: 0..index {
91 if source[i] == {
92 case "\r"; #through;
93 case "\n"; count += 1;
94 }
95 }
96 return count;
97}
98
99count_characters :: (s: string, char: u8) -> int {
100 count: int;
101 for s if it == char then count += 1;
102 return count;
103}
104
105count_any_to_char :: (
106 using x2lexer_state: $T/interface Lexer_State, end_char: u8
107)
108 -> (ok: Result, count: int)
109{
110 count: int;
111 for i: index..source.count-1 {
112 current := source[i];
113 if current == end_char return .OK, count;
114 count += 1;
115 }
116 return .EOF, count;
117}
118
119count_characters_from_now_to_char :: (
120 using x2lexer_state: $T/interface Lexer_State, char: u8, end_char: u8
121)
122 -> (ok: Result, count: int)
123{
124 count: int;
125 for i: index..source.count-1 {
126 current := source[i];
127 if current == end_char return .OK, count;
128 if current == char then count += 1;
129 }
130 return .EOF, count;
131}
132
133count_backwards_any_till :: (
134 using x2lexer_state: $T/interface Lexer_State, end_char: u8
135)
136 -> (ok: Result, count: int)
137{
138 count: int;
139 for < i: 0..index {
140 current := source[i];
141 if current == end_char return .OK, count;
142 count += 1;
143 }
144
145 return .EOF, count;
146}
147
148
149/* --------------- *
150 * Procs Stateless *
151 * --------------- */
152
153is_at_end :: inline (idx: int, s: string) -> bool {
154 return idx > s.count-1;
155}
156
157peek :: inline (idx: int, s: string) -> u8 {
158 if idx > s.count-1 return #char "\0";
159 return s[idx];
160}
161
162peek_back :: inline (idx: int, s: string) -> u8 {
163 if idx-1 > s.count-1 return #char "\0";
164 return s[idx-1];
165}
166
167peek_at :: (new_index: int, s: string) -> u8 {
168 if new_index >= 0 && new_index < s.count
169 then return s[new_index];
170 else return #char "\0";
171}
172
173peek_forward :: inline (idx: int, s: string, offset: int) -> u8 {
174 if idx+offset > s.count-1 return #char "\0";
175 return s[idx+offset];
176}
177
178advance :: inline (idx: *int, s: string) -> u8 {
179 value := s[idx.*];
180 idx.* += 1;
181 return value;
182}
183
184advance_by :: inline (idx: *int, offset: int) {
185 idx.* += offset;
186}
187
188advance_by :: inline (idx: *int, s: string) {
189 idx.* += s.count;
190}
191
192match :: (s: string, substring: string, offset := 0) -> bool {
193 if substring.count + offset > s.count return false;
194
195 for substring if it != s[it_index + offset] return false;
196
197 return true;
198}
199
200match_grammar :: (
201 s: string, substrings: []string, offset := 0
202)
203 -> (match: bool, advanced: int)
204{
205 advanced: int;
206
207 for substrings {
208 if !match(s, it, offset + advanced) return false, advanced;
209 advanced += it.count;
210 }
211
212 return true, advanced;
213}
214
215/** Allocates memory. You have to manage that.
216 If you do not want an allocation, pass `allocate = false`.
217*/
218consume_including :: inline (
219 match_index: int,
220 data: string,
221 char_or_string: $T/Type.[ string, u8 ],
222 $allocate := true
223)
224 -> string
225{
226 return consume_till(match_index, data, char_or_string, true, allocate);
227}
228
229/** Allocates memory. You have to manage that.
230 If you do not want an allocation, pass `allocate = false`.
231*/
232consume_till :: (
233 pos: int,
234 data: string,
235 char_or_string: $T/Type.[ string, u8 ],
236 include := false,
237 $allocate := true
238)
239 -> string
240{
241 #if T == u8 {
242 keyword_count := 1;
243 } else {
244 keyword_count := char_or_string.count;
245 }
246
247 count := pos;
248
249 offset := ifx include then keyword_count else 0;
250
251 while count < data.count {
252 #if T == u8 {
253 window := data[idx];
254 } else {
255 window := slice(data, count, keyword_count);
256 }
257
258 if window == char_or_string {
259 consumed := slice(data, pos, count - pos + offset);
260 #if allocate then return copy_string(consumed);
261 else return consumed;
262 }
263
264 count += 1;
265 }
266
267 return "";
268}
269
270report_error :: (
271 x2lexer_state: $T/interface Lexer_State,
272 fp: string,
273 message: string,
274 args: ..Any
275) {
276 lines := count_newlines_till_now(x2lexer_state);
277 _, chars := count_backwards_any_till(x2lexer_state, "\n");
278 log_error(
279 context.lib0x2_lexer_error_template,
280 tprint(message, ..args),
281 tprint("%:%:%", fp, lines, chars)
282 );
283}
284
285u8_to_str :: (c: *u8, $do_print := false) -> string {
286 s := string.{ 1, c };
287 #if do_print then log(" CHAR: »%«", s);
288 return s;
289}
290
291
292#scope_file
293
294
295PARSER_ERROR_TEMPLATE :: #string STR_END
296-- ERROR ---------------------------------
297%1
298------------------------------------------
299Parsing error at: %2
300
301STR_END;
302
303
304Result :: enum {
305 EOF;
306 OK;
307}
308
309
310using,only(copy_string, log, log_error, tprint) Basic :: #import "Basic";
311using,only(slice) String :: #import "String";
312
313
314/*
315------------------------------------------------------------------------------
316This software is available under 2 licenses -- choose whichever you prefer.
317------------------------------------------------------------------------------
318ALTERNATIVE A - MIT License
319Copyright (c) 2026 Adam Blazeowsky
320Permission is hereby granted, free of charge, to any person obtaining a copy of
321this software and associated documentation files (the "Software"), to deal in
322the Software without restriction, including without limitation the rights to
323use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
324of the Software, and to permit persons to whom the Software is furnished to do
325so, subject to the following conditions:
326The above copyright notice and this permission notice shall be included in all
327copies or substantial portions of the Software.
328THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
329IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
330FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
331AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
332LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
333OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
334SOFTWARE.
335------------------------------------------------------------------------------
336ALTERNATIVE B - Public Domain (www.unlicense.org)
337This is free and unencumbered software released into the public domain.
338Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
339software, either in source code form or as a compiled binary, for any purpose,
340commercial or non-commercial, and by any means.
341In jurisdictions that recognize copyright laws, the author or authors of this
342software dedicate any and all copyright interest in the software to the public
343domain. We make this dedication for the benefit of the public at large and to
344the detriment of our heirs and successors. We intend this dedication to be an
345overt act of relinquishment in perpetuity of all present and future rights to
346this software under copyright law.
347THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
348IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
349FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
350AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
351ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
352WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
353------------------------------------------------------------------------------
354*/
355
356
Copyright 2026  E766CB298A6D1E64 | Git-Thing heavily inspired by cgit