#pragma once #include #include "commons/commons_byteparser.h" #include "commons/commons_hex.h" #include "commons/commons_macros.h" #include "commons/commons_span_def.h" #include "commons/commons_stringparser.h" INLINE auto IsHexDigit(uint8_t ch) -> bool { if (ch >= '0' && ch <= '9') return true; if (ch >= 'a' && ch <= 'f') return true; if (ch >= 'A' && ch <= 'F') return true; return false; } INLINE auto IsIdentifierStart(uint8_t ch) -> bool { return IsAlpha(ch) || ch == '_'; } INLINE auto IsIdentifierCont(uint8_t ch) -> bool { return IsAlpha(ch) || IsNumber(ch) || ch == '_'; } INLINE void TokenParser_SkipLineWhitespace(byte_parser& self) { while (ByteParser_HasNext(self)) { const uint8_t ch = ByteParser_Peek(self); if (ch != ' ' && ch != '\t') break; ByteParser_Advance(self); } } INLINE auto TokenParser_SkipLineComment(byte_parser& self, uint8_t commentChar = '#') -> bool { if (!ByteParser_HasNext(self) || ByteParser_Peek(self) != commentChar) return false; ByteParser_NextLine(self); return true; } [[nodiscard]] INLINE auto TokenParser_ParseIdentifier(byte_parser& self) -> byteview { ASSERT(ByteParser_HasNext(self) && IsIdentifierStart(ByteParser_Peek(self))); const uint8_t* start = self.at; ByteParser_Advance(self); while (ByteParser_HasNext(self) && IsIdentifierCont(ByteParser_Peek(self))) ByteParser_Advance(self); return byteview { start, (uint64_t)(self.at - start) }; } [[nodiscard]] INLINE auto TokenParser_ParseHexU64(byte_parser& self) -> uint64_t { (void)(ByteParser_StartsWithAdvance(self, "0x"_s) || ByteParser_StartsWithAdvance(self, "0X"_s)); ASSERT(ByteParser_HasNext(self) && IsHexDigit(ByteParser_Peek(self))); uint64_t value = 0; uint32_t digits = 0; while (ByteParser_HasNext(self) && IsHexDigit(ByteParser_Peek(self))) { ASSERT(digits < 16); value = (value << 4) | ParseHexChar(ByteParser_Read(self)); ++digits; } return value; }