/** TODO: Better test coverage */ #add_context lib0x2_lexer_error_template := PARSER_ERROR_TEMPLATE; Lexer_State :: struct { index: int; source: string; } /* ------------ * * Lexer Macros * * ------------ */ is_at_end :: () -> bool #expand { return is_at_end(`index, `source); } peek :: () -> u8 #expand { return peek(`index, `source); } peek_back :: () -> u8 #expand { return peek_back(`index, `source); } peek_at :: (offset: int) -> u8 #expand { return peek_back(`index + offset, `source); } peek_forward :: (offset: int) -> u8 #expand { return peek_forward(`index, `source, offset); } advance :: () -> u8 #expand { return advance(*`index, `source); } advance_by :: inline (offset: int) #expand { advance_by(*`index, offset); } advance_by :: inline (offset: string) #expand { advance_by(*`index, offset.count); } advance_by :: inline (offset: ..string) #expand { count: int; for offset count += it.count; advance_by(*`index, count); } match :: (substring: string, offset := 0) -> bool #expand { return match(`source, substring, offset); } match_grammar :: ( substrings: []string, offset := 0 ) -> (match: bool, advanced: int) #expand { a, b := match_grammar(`source, substrings, offset); return a, b; } /** Allocates memory. You have to manage that. If you do not want an allocation, pass `allocate = false`. */ consume_including :: (token: $T/Type.[ string, u8 ], $allocate := true) -> string #expand { return consume_including(`index, `source, token, allocate=allocate); } /** Allocates memory. You have to manage that. If you do not want an allocation, pass `allocate = false`. */ consume_till :: (token: $T/Type.[ string, u8 ], $allocate := true) -> string #expand { return consume_till(`index, `source, token, allocate=allocate); } /* ---------------------------- * * Procs, assuming Lexer_State * * ---------------------------- */ count_newlines_till_now :: (using x2lexer_state: $T/interface Lexer_State) -> int { count := 1; for i: 0..index { if source[i] == { case "\r"; #through; case "\n"; count += 1; } } return count; } count_characters :: (s: string, char: u8) -> int { count: int; for s if it == char then count += 1; return count; } count_any_to_char :: ( using x2lexer_state: $T/interface Lexer_State, end_char: u8 ) -> (ok: Result, count: int) { count: int; for i: index..source.count-1 { current := source[i]; if current == end_char return .OK, count; count += 1; } return .EOF, count; } count_characters_from_now_to_char :: ( using x2lexer_state: $T/interface Lexer_State, char: u8, end_char: u8 ) -> (ok: Result, count: int) { count: int; for i: index..source.count-1 { current := source[i]; if current == end_char return .OK, count; if current == char then count += 1; } return .EOF, count; } count_backwards_any_till :: ( using x2lexer_state: $T/interface Lexer_State, end_char: u8 ) -> (ok: Result, count: int) { count: int; for < i: 0..index { current := source[i]; if current == end_char return .OK, count; count += 1; } return .EOF, count; } /* --------------- * * Procs Stateless * * --------------- */ is_at_end :: inline (idx: int, s: string) -> bool { return idx > s.count-1; } peek :: inline (idx: int, s: string) -> u8 { if idx > s.count-1 return #char "\0"; return s[idx]; } peek_back :: inline (idx: int, s: string) -> u8 { if idx-1 > s.count-1 return #char "\0"; return s[idx-1]; } peek_at :: (new_index: int, s: string) -> u8 { if new_index >= 0 && new_index < s.count then return s[new_index]; else return #char "\0"; } peek_forward :: inline (idx: int, s: string, offset: int) -> u8 { if idx+offset > s.count-1 return #char "\0"; return s[idx+offset]; } advance :: inline (idx: *int, s: string) -> u8 { value := s[idx.*]; idx.* += 1; return value; } advance_by :: inline (idx: *int, offset: int) { idx.* += offset; } advance_by :: inline (idx: *int, s: string) { idx.* += s.count; } match :: (s: string, substring: string, offset := 0) -> bool { if substring.count + offset > s.count return false; for substring if it != s[it_index + offset] return false; return true; } match_grammar :: ( s: string, substrings: []string, offset := 0 ) -> (match: bool, advanced: int) { advanced: int; for substrings { if !match(s, it, offset + advanced) return false, advanced; advanced += it.count; } return true, advanced; } /** Allocates memory. You have to manage that. If you do not want an allocation, pass `allocate = false`. */ consume_including :: inline ( match_index: int, data: string, char_or_string: $T/Type.[ string, u8 ], $allocate := true ) -> string { return consume_till(match_index, data, char_or_string, true, allocate); } /** Allocates memory. You have to manage that. If you do not want an allocation, pass `allocate = false`. */ consume_till :: ( pos: int, data: string, char_or_string: $T/Type.[ string, u8 ], include := false, $allocate := true ) -> string { #if T == u8 { keyword_count := 1; } else { keyword_count := char_or_string.count; } count := pos; offset := ifx include then keyword_count else 0; while count < data.count { #if T == u8 { window := data[idx]; } else { window := slice(data, count, keyword_count); } if window == char_or_string { consumed := slice(data, pos, count - pos + offset); #if allocate then return copy_string(consumed); else return consumed; } count += 1; } return ""; } report_error :: ( x2lexer_state: $T/interface Lexer_State, fp: string, message: string, args: ..Any ) { lines := count_newlines_till_now(x2lexer_state); _, chars := count_backwards_any_till(x2lexer_state, "\n"); log_error( context.lib0x2_lexer_error_template, tprint(message, ..args), tprint("%:%:%", fp, lines, chars) ); } u8_to_str :: (c: *u8, $do_print := false) -> string { s := string.{ 1, c }; #if do_print then log(" CHAR: »%«", s); return s; } #scope_file PARSER_ERROR_TEMPLATE :: #string STR_END -- ERROR --------------------------------- %1 ------------------------------------------ Parsing error at: %2 STR_END; Result :: enum { EOF; OK; } using,only(copy_string, log, log_error, tprint) Basic :: #import "Basic"; using,only(slice) String :: #import "String"; /* ------------------------------------------------------------------------------ This software is available under 2 licenses -- choose whichever you prefer. ------------------------------------------------------------------------------ ALTERNATIVE A - MIT License Copyright (c) 2026 Adam Blazeowsky Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ------------------------------------------------------------------------------ ALTERNATIVE B - Public Domain (www.unlicense.org) This is free and unencumbered software released into the public domain. Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means. In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ------------------------------------------------------------------------------ */