json-gen-c  0.1.5
json-gen-c generate C code for json manipulation
struct_parse.h
Go to the documentation of this file.
1
7#ifndef STRUCT_PARSE_H_
8#define STRUCT_PARSE_H_
9
10#include <stddef.h>
11
12#include "utils/hash_map.h"
13#include "utils/sstr.h"
14
15#ifdef __cplusplus
16extern "C" {
17#endif
18
19// field type id
20// !NOTE: MUST SAME AS IN gencode/codes/json_parse.h
21
22#define FIELD_TYPE_INT 0
23#define FIELD_TYPE_LONG 1
24#define FIELD_TYPE_FLOAT 2
25#define FIELD_TYPE_DOUBLE 3
26#define FIELD_TYPE_SSTR 4
27#define FIELD_TYPE_STRUCT 6
28#define FIELD_TYPE_BOOL 7
29
30#define TYPE_NAME_INT "int"
31#define TYPE_NAME_BOOL "bool"
32#define TYPE_NAME_SSTR "sstr_t"
33#define TYPE_NAME_LONG "long"
34#define TYPE_NAME_FLOAT "float"
35#define TYPE_NAME_DOUBLE "double"
36
41#define STRUCT_MAP_BUCKET_SIZE 4096
42
49 // field name
50 sstr_t name;
51 // field type id
52 int type;
53 // 1 if the field is an array, 0 otherwise
54 int is_array;
55 // the name of field type
56 sstr_t type_name;
57 // linked list pointer to next field, NULL if this is the last field
58 struct struct_field* next;
59};
60
66 // struct name
67 sstr_t name;
68 // field list
69 struct struct_field* fields;
70};
71
75struct pos {
76 int line;
77 int col;
78 long offset;
79};
80
86 // struct name --> struct_field list
87 struct hash_map* struct_map;
88 // position of string to be parsed
89 struct pos pos;
90 // name of parser
91 char *name;
92};
93
94// token types of struct definitions, return by next_token()
95
96#define TOKEN_LEFT_BRACE '{'
97#define TOKEN_RIGHT_BRACE '}'
98#define TOKEN_LEFT_BRACKET '['
99#define TOKEN_RIGHT_BRACKET ']'
100#define TOKEN_SEMICOLON ';'
101#define TOKEN_SHARPE '#'
102#define TOKEN_STRING 4
103#define TOKEN_IDENTIFY 1
104#define TOKEN_INTEGER 2
105#define TOKEN_FLOAT 3
106#define TOKEN_EOF 0
107#define TOKEN_ERROR -1
108
114 int type;
115 sstr_t txt;
116};
117
124
130void struct_parser_free(struct struct_parser* parser);
131
140int struct_parser_parse(struct struct_parser* parser, sstr_t content);
141
142#ifdef __cplusplus
143}
144#endif
145
146#endif // STRUCT_PARSE_H_
A simple hash_map implementation.
sstr_t are objects that represent sequences of characters.
void * sstr_t
sstr_t are objects that represent sequences of characters.
Definition sstr.h:73
int struct_parser_parse(struct struct_parser *parser, sstr_t content)
parse a struct definition file, and store the parsed structs in struct_parser.
Definition struct_parse.c:638
struct struct_parser * struct_parser_new()
create and init a struct_parser instance.
Definition struct_parse.c:104
void struct_parser_free(struct struct_parser *parser)
free a struct_parser instance.
Definition struct_parse.c:125
Definition hash_map.h:34
position of string
Definition struct_parse.h:75
structure to store parsed structs. A struct may have multiple fields, we put fields in a linked list.
Definition struct_parse.h:65
structure to store field list of parsed structs. A struct may have multiple field,...
Definition struct_parse.h:48
parser context
Definition struct_parse.h:85
token
Definition struct_parse.h:113