#ifndef SCANNER_H #define SCANNER_H #include "types.h" /* Token kinds. */ typedef enum { /* End of file token generated by the scanner * when the input is exhausted. */ T_EOF, /* Special "error" token. */ T_INVALID, /* Single-char tokens. */ T_LPAREN, /* ( */ T_RPAREN, /* ) */ T_LBRACE, /* { */ T_RBRACE, /* } */ T_LBRACKET, /* [ */ T_RBRACKET, /* ] */ T_COMMA, /* , */ T_DOT, /* . */ T_DOT_DOT, /* .. */ T_MINUS, /* - */ T_PLUS, /* + */ T_SEMICOLON, /* ; */ T_SLASH, /* / */ T_STAR, /* * */ T_PERCENT, /* % */ T_AMP, /* & */ T_PIPE, /* | */ T_CARET, /* ^ */ T_TILDE, /* ~ */ T_UNDERSCORE, /* _ */ /* One or two char tokens. */ T_QUESTION, /* ? */ T_BANG, /* ! */ T_BANG_EQ, /* != */ T_EQ, /* = */ T_EQ_EQ, /* == */ T_GT, /* > */ T_GT_EQ, /* >= */ T_LT, /* < */ T_LT_EQ, /* <= */ T_LSHIFT, /* << */ T_RSHIFT, /* >> */ /* Literals. */ T_IDENT, /* fnord */ T_AT_IDENT, /* @sizeOf */ T_STRING, /* "fnord" */ T_CHAR, /* 'f' */ T_NUMBER, /* 42 */ T_TRUE, /* true */ T_FALSE, /* false */ T_NIL, /* nil */ T_UNDEF, /* undefined */ /* Keywords. */ T_IF, T_ELSE, T_RETURN, T_BREAK, T_CONTINUE, T_THROW, T_PANIC, T_WHILE, T_FOR, T_LOOP, T_TRY, T_CATCH, T_IN, T_FN, T_UNION, T_RECORD, T_DEFAULT, T_PUB, T_MUT, T_CONST, T_STATIC, T_LET, T_AND, T_OR, T_NOT, T_MATCH, T_CASE, T_USE, T_SUPER, /* super */ T_EXTERN, /* extern */ T_MOD, /* mod */ T_AS, /* as */ T_ALIGN, /* align */ T_THROWS, /* throws */ /* Type-related tokens. */ T_COLON, /* : */ T_COLON_COLON, /* :: */ T_ARROW, /* -> */ T_FAT_ARROW, /* => */ /* Builtin type names. */ T_I8, T_I16, T_I32, T_I64, T_U8, T_U16, T_U32, T_U64, T_F32, T_BOOL, T_VOID, T_OPAQUE } tokenclass_t; /* Code location. */ typedef struct { const char *src; /* Pointer to source code location. */ const char *file; /* File path. */ u32 line; /* line number. */ u32 col; /* Column number. */ } location_t; /* Token structure. */ typedef struct { tokenclass_t cls; const char *start; /* Start of the token in the source code. */ u32 length; /* Byte length of token in source code. */ u32 position; /* Byte offset in source. */ } token_t; /* Scanner state. */ typedef struct { const char *file; /* File path. */ const char *source; /* Start of source buffer. */ const char *token; /* Start of current token. */ const char *cursor; /* Current position. */ } scanner_t; /* Initialize scanner with source text. */ void scanner_init(scanner_t *s, const char *file, const char *source); /* Get next token from scanner. */ token_t scanner_next(scanner_t *s); /* Get line and column information for a token. */ location_t scanner_get_location(scanner_t *s, u32 position); #endif