Line data Source code
1 : #ifndef HEADER_fd_src_discof_rpcserver_json_lex_h 2 : #define HEADER_fd_src_discof_rpcserver_json_lex_h 3 : 4 : /***** 5 : Header file for a json lexical scanner 6 : *****/ 7 : 8 : #include "../../util/fd_util.h" 9 : 10 : // Lexical token value 11 0 : #define JSON_TOKEN_LBRACKET 1 /* [ */ 12 0 : #define JSON_TOKEN_RBRACKET 2 /* ] */ 13 0 : #define JSON_TOKEN_LBRACE 3 /* { */ 14 0 : #define JSON_TOKEN_RBRACE 4 /* } */ 15 0 : #define JSON_TOKEN_COLON 5 /* : */ 16 0 : #define JSON_TOKEN_COMMA 6 /* , */ 17 0 : #define JSON_TOKEN_NULL 7 /* null */ 18 0 : #define JSON_TOKEN_BOOL 8 /* true or false */ 19 0 : #define JSON_TOKEN_INTEGER 9 20 0 : #define JSON_TOKEN_FLOAT 10 21 0 : #define JSON_TOKEN_STRING 11 22 : 23 0 : #define JSON_TOKEN_END 0 /* end of input */ 24 0 : #define JSON_TOKEN_ERROR -1 25 : 26 : // Lexical state 27 : struct json_lex_state { 28 : // Input json text 29 : const char* json; 30 : ulong json_sz; 31 : 32 : // Current position in text 33 : ulong pos; 34 : // Last token parsed 35 : long last_tok; 36 : 37 : // Value of last boolean 38 : int last_bool; 39 : // Value of last string, number (as text), or error message. UTF-8 encoded. 40 : char* last_str; 41 : ulong last_str_sz; 42 : ulong last_str_alloc; 43 : char last_str_firstbuf[512]; 44 : 45 : fd_spad_t * spad; 46 : }; 47 : typedef struct json_lex_state json_lex_state_t; 48 : 49 : // Initialize a lexical state given some json text 50 : void json_lex_state_new(json_lex_state_t* state, 51 : const char* json, 52 : ulong json_sz, 53 : fd_spad_t * spad); 54 : 55 : void json_lex_state_delete(json_lex_state_t* state); 56 : 57 : // Retrieve the next token 58 : long json_lex_next_token(json_lex_state_t* state); 59 : 60 : // Get the last lexical text result. This can be a string, number (as 61 : // text), or error message. 62 : const char* json_lex_get_text(json_lex_state_t* state, ulong* sz); 63 : 64 : // Convert the string to an integer (assuming decimal representation) 65 : long json_lex_as_int(json_lex_state_t* lex); 66 : 67 : // Convert the string to a float 68 : double json_lex_as_float(json_lex_state_t* lex); 69 : 70 : // Replaces the string with the result of a formatted printf. 71 : void json_lex_sprintf(json_lex_state_t* lex, const char* format, ...) 72 : __attribute__ ((format (printf, 2, 3))); 73 : 74 : #endif /* HEADER_fd_src_discof_rpcserver_json_lex_h */