<<
path:
root/public/0x2lib.git/html/0x2_Lexer.jai
blob: 559d1189f78504a86080f06f5f1dbeff2417fef9
[raw]
[clear marker]
2/** TODO: Better test coverage */
5#add_context lib0x2_lexer_error_template := PARSER_ERROR_TEMPLATE;
18is_at_end :: () -> bool #expand {
19 return is_at_end(`index, `source);
22peek :: () -> u8 #expand {
23 return peek(`index, `source);
26peek_back :: () -> u8 #expand {
27 return peek_back(`index, `source);
30peek_at :: (offset: int) -> u8 #expand {
31 return peek_back(`index + offset, `source);
34peek_forward :: (offset: int) -> u8 #expand {
35 return peek_forward(`index, `source, offset);
38advance :: () -> u8 #expand {
39 return advance(*`index, `source);
42advance_by :: inline (offset: int) #expand {
43 advance_by(*`index, offset);
46advance_by :: inline (offset: string) #expand {
47 advance_by(*`index, offset.count);
50advance_by :: inline (offset: ..string) #expand {
52 for offset count += it.count;
53 advance_by(*`index, count);
56match :: (substring: string, offset := 0) -> bool #expand {
57 return match(`source, substring, offset);
61 substrings: []string, offset := 0
63 -> (match: bool, advanced: int) #expand
65 a, b := match_grammar(`source, substrings, offset);
69/** Allocates memory. You have to manage that.
70 If you do not want an allocation, pass `allocate = false`.
72consume_including :: (token: $T/Type.[ string, u8 ], $allocate := true) -> string #expand {
73 return consume_including(`index, `source, token, allocate=allocate);
76/** Allocates memory. You have to manage that.
77 If you do not want an allocation, pass `allocate = false`.
79consume_till :: (token: $T/Type.[ string, u8 ], $allocate := true) -> string #expand {
80 return consume_till(`index, `source, token, allocate=allocate);
84/* ---------------------------- *
85 * Procs, assuming Lexer_State *
86 * ---------------------------- */
88count_newlines_till_now :: (using x2lexer_state: $T/interface Lexer_State) -> int {
93 case "\n"; count += 1;
99count_characters :: (s: string, char: u8) -> int {
101 for s if it == char then count += 1;
105count_any_to_char :: (
106 using x2lexer_state: $T/interface Lexer_State, end_char: u8
108 -> (ok: Result, count: int)
111 for i: index..source.count-1 {
112 current := source[i];
113 if current == end_char return .OK, count;
119count_characters_from_now_to_char :: (
120 using x2lexer_state: $T/interface Lexer_State, char: u8, end_char: u8
122 -> (ok: Result, 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;
133count_backwards_any_till :: (
134 using x2lexer_state: $T/interface Lexer_State, end_char: u8
136 -> (ok: Result, count: int)
140 current := source[i];
141 if current == end_char return .OK, count;
153is_at_end :: inline (idx: int, s: string) -> bool {
154 return idx > s.count-1;
157peek :: inline (idx: int, s: string) -> u8 {
158 if idx > s.count-1 return #char "\0";
162peek_back :: inline (idx: int, s: string) -> u8 {
163 if idx-1 > s.count-1 return #char "\0";
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";
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];
178advance :: inline (idx: *int, s: string) -> u8 {
184advance_by :: inline (idx: *int, offset: int) {
188advance_by :: inline (idx: *int, s: string) {
192match :: (s: string, substring: string, offset := 0) -> bool {
193 if substring.count + offset > s.count return false;
195 for substring if it != s[it_index + offset] return false;
201 s: string, substrings: []string, offset := 0
203 -> (match: bool, advanced: int)
208 if !match(s, it, offset + advanced) return false, advanced;
209 advanced += it.count;
212 return true, advanced;
215/** Allocates memory. You have to manage that.
216 If you do not want an allocation, pass `allocate = false`.
218consume_including :: inline (
221 char_or_string: $T/Type.[ string, u8 ],
226 return consume_till(match_index, data, char_or_string, true, allocate);
229/** Allocates memory. You have to manage that.
230 If you do not want an allocation, pass `allocate = false`.
235 char_or_string: $T/Type.[ string, u8 ],
244 keyword_count := char_or_string.count;
249 offset := ifx include then keyword_count else 0;
251 while count < data.count {
255 window := slice(data, count, keyword_count);
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;
271 x2lexer_state: $T/interface Lexer_State,
276 lines := count_newlines_till_now(x2lexer_state);
277 _, chars := count_backwards_any_till(x2lexer_state, "\n");
279 context.lib0x2_lexer_error_template,
280 tprint(message, ..args),
281 tprint("%:%:%", fp, lines, chars)
285u8_to_str :: (c: *u8, $do_print := false) -> string {
286 s := string.{ 1, c };
287 #if do_print then log(" CHAR: »%«", s);
295PARSER_ERROR_TEMPLATE :: #string STR_END
296-- ERROR ---------------------------------
298------------------------------------------
310using,only(copy_string, log, log_error, tprint) Basic :: #import "Basic";
311using,only(slice) String :: #import "String";
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
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------------------------------------------------------------------------------