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