13#include <unordered_map>
26 template <
typename Ch =
char,
typename String = std::basic_
string<Ch, std::
char_traits<Ch>>>
45 std::optional<ch_type>
peekch(
int num = 1);
87 template <
typename Ch,
typename String>
89 const std::string &filename)
94 template <
typename Ch,
typename String>
109 template <
typename Ch,
typename String>
116 template <
typename Ch,
typename String>
123 template <
typename Ch,
typename String>
130 template <
typename Ch,
typename String>
139 template <
typename Ch,
typename String>
141 if (
static_cast<int64_t
>(
index) >= num) {
152 template <
typename Ch,
typename String>
156 return std::make_pair(line, col);
159 template <
typename Ch,
typename String>
164 template <
typename Ch,
typename String>
166 const std::string &newFile) {
172 template <
typename Ch,
typename String>
177 template <
typename Ch,
typename String>
194 template <
typename Ch,
int MAX_SIZE = 256>
210 template <
typename Ch,
int MAX_CHARS>
213 for (i = 0; i < MAX_CHARS; ++i) {
216 for (i =
'a'; i <=
'z'; ++i) {
219 for (i =
'A'; i <=
'Z'; ++i) {
222 for (i =
'0'; i <=
'9'; ++i) {
259 template <
typename Ch,
int MAX_CHARS>
261 if (c >= 0 && c < MAX_CHARS) {
274 template <
typename Ch =
char,
typename String = std::basic_
string<Ch, std::
char_traits<Ch>>>
306 const std::pair<uint64_t, uint64_t>
get_pos()
const {
return std::make_pair(
line,
col); }
308 void set_pos(
const std::pair<uint64_t, uint64_t> &p);
325 template <
typename Ch,
typename String>
331 template <
typename Ch,
typename String>
336 template <
typename Ch,
typename String>
342 template <
typename Ch,
typename String>
348 template <
typename Ch,
typename String>
350 out <<
value <<
" -> \t";
354 template <
typename Ch,
typename String>
std::basic_string< char, std::char_traits< char > > string_type
std::optional< ch_type > getch()
Consume and return the next character, advancing the position.
bool eof(uint64_t pos=0)
Check if end-of-buffer has been reached.
std::optional< ch_type > backward_step(int num=1)
Move the position backward by num characters.
void process_line_directive(uint64_t newLine, const std::string &newFile)
Process a line directive to update position tracking.
void reset(uint64_t pos=0)
Reset the buffer position.
std::string cur_file() const
Get the current source filename.
std::optional< ch_type > curch()
Return the current character without advancing.
std::optional< ch_type > forward_step(int num=1)
Advance the position by num characters.
StringBuffer(const string_type &buf, const std::string &filename="unknown")
Construct a string buffer.
std::pair< uint64_t, uint64_t > cur_line()
Get the current (line, column) position.
std::optional< ch_type > peekch(int num=1)
Peek ahead by num positions without advancing.
std::optional< ch_type > prevch()
Return the previously consumed character.
TokenMap()
Construct the map with default ASCII character classifications.
std::unordered_map< char, types::CharType > token_map
std::optional< types::CharType > lookup_int8(int8_t c)
Look up the character type for an 8-bit character.
void set_filename(const std::string &filename)
Set the source filename for this token.
uint64_t getLine() const
Get the source line number.
std::string get_filename() const
Get the source filename for this token.
string_type getTokenValue() const
Get the token's text value.
void print(std::ostream &out)
Print the token value and type to a stream.
std::basic_string< char, std::char_traits< char > > string_type
types::TokenType getTokenType() const
Get the token's type classification.
Token(const types::TokenType &t)
Construct a token with a given type.
Token< Ch, String > & operator=(const Token< Ch, String > &t)
Copy assignment.
Token(const Token< Ch, String > &t)
Copy constructor.
uint64_t getCol() const
Get the source column number.
Token()
Default constructor — creates a null token at line 1, col 1.
Token< Ch, String > & operator=(const types::TokenType &type)
Assign a new token type, keeping the current value.
void set_pos(const std::pair< uint64_t, uint64_t > &p)
Set the source position from a (line, column) pair.
const std::pair< uint64_t, uint64_t > get_pos() const
Get the (line, column) source position.
Token< Ch, String > & operator=(Token< Ch, String > &&t)
Move assignment.
Token(Token< Ch, String > &&t)
Move constructor.
void setToken(const types::TokenType &type, const String &value)
Set the token's type and value.
int64_t pos(const char *substr, const char *s)
TokenType
Classification of scanned tokens.
void print_type_TokenType(std::ostream &out, const TokenType &tt)
Print a TokenType name to a stream.
CharType
Classification of individual characters during lexing.
Token, character, and operator type enumerations for the scanner.