lexer.h (1665B)
1 #ifndef DC_LEXER_H 2 #define DC_LEXER_H 3 4 #include "dlib.h" 5 6 typedef struct { 7 Sv filename; 8 size_t line, column; 9 } Loc; 10 11 typedef enum { 12 T_ERROR_ = 0, 13 T_EOF = 256, 14 T_INVALID, 15 T_WHITESPACE, 16 T_WHITESPACE_NL, 17 T_IDENT, 18 T_NUMBER, 19 T_SQ_STRING, 20 T_DQ_STRING, 21 T_ARROW, 22 T_CONST_SYMBOL, 23 T_VARIABLE_SYMBOL, 24 T_FUNCTION_SYMBOL, 25 T_LABEL, 26 T_TEMP, 27 28 T_KEYWORDS_, 29 30 T_RETURN = T_KEYWORDS_, 31 T_EXTERN, 32 T_I8, 33 T_I16, 34 T_I32, 35 T_I64, 36 T_PTR, 37 T_VOID, 38 T_LOAD, 39 T_STORE, 40 T_CALL, 41 T_SYSCALL, 42 T_JUMP, 43 T_BRANCH, 44 T_PHI, 45 46 T_COUNT_ 47 } Token_kind; 48 49 typedef struct { 50 Token_kind kind; 51 Loc loc; 52 Sv sv; 53 long int i64; 54 } Token; 55 56 typedef struct { 57 Sv source; 58 Loc current_loc; 59 Token last_tok; 60 Loc last_valid_loc; 61 } Lexer; 62 63 #define CHAR_INVALID (-2) 64 int next_char(Lexer *lexer); 65 int next_char_if(Lexer *lexer, int (*pred)(int)); 66 67 int is_ident_char(int c); 68 int is_symbol_char(int c); 69 bool is_operator_token(Token_kind kind); 70 71 int skip_comment(Lexer *lexer); 72 Token next_token(Lexer *lexer); 73 Token peek_token(Lexer *lexer); 74 75 Token_kind get_keyword_kind(Sv sv); 76 77 Token next_token_if(Lexer *lexer, int (*pred)(Token)); 78 Token next_token_if_kind(Lexer *lexer, Token_kind kind); 79 bool expect_token_kind(Lexer *lexer, Token_kind kind); 80 81 bool expect_end_statement_impl(Lexer *lexer, const char *file, int line); 82 #define expect_end_statement(l) expect_end_statement_impl(l, __FILE__, __LINE__) 83 84 void skip_whitespace(Lexer *lexer); 85 86 void sb_append_token_kind(Sb *sb, Token_kind kind); 87 void sb_append_loc(Sb *sb, Loc loc); 88 89 void print_loc(Loc loc); 90 void print_loc_pad(Loc loc, int min_width); 91 void debug_print_token(Token t); 92 93 #endif /* DC_LEXER_H */