token.h (1279B)
1 #ifndef TOKEN_H 2 #define TOKEN_H 3 4 #include <stdio.h> 5 6 typedef struct { 7 FILE* file; 8 unsigned int line_idx; 9 unsigned int char_idx; 10 } Cursor; 11 12 void cursor_init(Cursor* cursor, FILE* file); 13 char cursor_getc(Cursor* cursor); 14 void cursor_ungetc(Cursor* cursor, char c); 15 16 static const char* TOKEN_ERROR_STRINGS[] = { 17 "field value exceeded max length.", 18 "invalid character. unable to parse integer.", 19 "no digits found. unable to parse integer.", 20 "invalid character. unable to parse float.", 21 "no digits found. unable to parse float.", 22 "field type does not support value parsing.", 23 "field type unrecognized.", 24 }; 25 26 typedef enum { 27 TOKEN_COMMENT = ';', 28 TOKEN_G_CODE = 'G', 29 TOKEN_M_CODE = 'M', 30 TOKEN_S_PARAM = 'S', 31 TOKEN_X_PARAM = 'X', 32 TOKEN_Y_PARAM = 'Y', 33 TOKEN_Z_PARAM = 'Z', 34 TOKEN_F_PARAM = 'F', 35 TOKEN_E_PARAM = 'E', 36 TOKEN_EOF = EOF, 37 TOKEN_ERROR = '!', 38 } TokenType; 39 40 typedef union { 41 float param_value; 42 unsigned int code_value; 43 unsigned int error_value; 44 } TokenValue; 45 46 typedef struct { 47 TokenType type; 48 TokenValue value; 49 unsigned int line_idx; 50 unsigned int char_idx; 51 } Token; 52 53 Token parse_token(Cursor* cursor); 54 void recover_from_error(Cursor* cursor, const Token* token); 55 56 #endif