lexer.h (1348B)
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_IDENT, 17 T_NUMBER, 18 T_SQ_STRING, 19 T_DQ_STRING, 20 21 T_KEYWORDS_, 22 23 T_RETURN = T_KEYWORDS_, 24 T_IF, 25 T_ELSE, 26 T_WHILE, 27 T_FOR, 28 T_SWITCH, 29 T_CASE, 30 T_DEFAULT, 31 T_BREAK, 32 T_CONTINUE, 33 T_TYPEDEF, 34 T_FUNC, 35 T_VAR, 36 T_CONST, 37 T_STRUCT, 38 T_UNION, 39 T_ENUM, 40 T_OPERATOR, 41 42 T_COUNT_ 43 } Token_kind; 44 45 typedef struct { 46 Token_kind kind; 47 Loc loc; 48 Sv sv; 49 } Token; 50 51 typedef struct { 52 Sv source; 53 Loc current_loc; 54 } Lexer; 55 56 #define CHAR_INVALID (-2) 57 int next_char(Lexer *lexer); 58 int next_char_if(Lexer *lexer, int (*pred)(int)); 59 60 int is_ident_char(int c); 61 int is_symbol_char(int c); 62 63 int skip_comment(Lexer *lexer); 64 Token next_token(Lexer *lexer); 65 Token peek_token(Lexer *lexer); 66 67 Token_kind get_keyword_kind(Sv sv); 68 69 Token next_token_if(Lexer *lexer, int (*pred)(Token)); 70 Token next_token_if_kind(Lexer *lexer, Token_kind kind); 71 Token expect_token_kind(Lexer *lexer, Token_kind kind); 72 void skip_whitespace(Lexer *lexer); 73 74 void sb_append_token_kind(Sb *sb, Token_kind kind); 75 void sb_append_loc(Sb *sb, Loc loc); 76 77 void print_loc(Loc loc); 78 void print_loc_pad(Loc loc, int min_width); 79 void debug_print_token(Token t); 80 81 #endif /* DC_LEXER_H */