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/diag.h"
13#include "utils/hash_map.h"
14#include "utils/sstr.h"
15
16#ifdef __cplusplus
17extern "C" {
18#endif
19
20// field type id
21// !NOTE: MUST SAME AS IN gencode/codes/json_parse.h
22
23#define FIELD_TYPE_INT 0
24#define FIELD_TYPE_LONG 1
25#define FIELD_TYPE_FLOAT 2
26#define FIELD_TYPE_DOUBLE 3
27#define FIELD_TYPE_SSTR 4
28#define FIELD_TYPE_ENUM 5
29#define FIELD_TYPE_STRUCT 6
30#define FIELD_TYPE_BOOL 7
31#define FIELD_TYPE_MAP 8
32#define FIELD_TYPE_INT8 9
33#define FIELD_TYPE_INT16 10
34#define FIELD_TYPE_INT32 11
35#define FIELD_TYPE_INT64 12
36#define FIELD_TYPE_UINT8 13
37#define FIELD_TYPE_UINT16 14
38#define FIELD_TYPE_UINT32 15
39#define FIELD_TYPE_UINT64 16
40#define FIELD_TYPE_ONEOF 17
41
42#define TYPE_NAME_INT "int"
43#define TYPE_NAME_BOOL "bool"
44#define TYPE_NAME_SSTR "sstr_t"
45#define TYPE_NAME_LONG "long"
46#define TYPE_NAME_FLOAT "float"
47#define TYPE_NAME_DOUBLE "double"
48#define TYPE_NAME_INT8 "int8_t"
49#define TYPE_NAME_INT16 "int16_t"
50#define TYPE_NAME_INT32 "int32_t"
51#define TYPE_NAME_INT64 "int64_t"
52#define TYPE_NAME_UINT8 "uint8_t"
53#define TYPE_NAME_UINT16 "uint16_t"
54#define TYPE_NAME_UINT32 "uint32_t"
55#define TYPE_NAME_UINT64 "uint64_t"
56
61#define STRUCT_MAP_BUCKET_SIZE 4096
62
67struct enum_value {
68 sstr_t name;
69 int index;
70 // 1 if value is deprecated, 0 otherwise
71 int is_deprecated;
72 struct enum_value* next;
73};
74
79 sstr_t name;
80 struct enum_value* values;
81 int count;
82 // source position where the enum name was defined
83 int name_line;
84 int name_col;
85 // source filename (not owned)
86 const char *filename;
87};
88
94 sstr_t name; // variant name (used as tag value string)
95 sstr_t struct_type_name; // the struct type this variant maps to
96 int index; // 0-based variant index
97 // 1 if variant is deprecated, 0 otherwise
98 int is_deprecated;
99 struct oneof_variant* next;
100};
101
106 sstr_t name;
107 sstr_t tag_field; // JSON discriminator key (default: "type")
108 struct oneof_variant* variants;
109 int count;
110 // source position where the oneof name was defined
111 int name_line;
112 int name_col;
113 // source filename (not owned)
114 const char *filename;
115};
116
123 // field name
124 sstr_t name;
125 // field type id
126 int type;
127 // 1 if the field is an array, 0 otherwise
128 int is_array;
129 // fixed-size array length (>0), 0 means dynamic array
130 int array_size;
131 // the name of field type
132 sstr_t type_name;
133 // for FIELD_TYPE_MAP: value type id
134 int map_value_type;
135 // for FIELD_TYPE_MAP: value type name
136 sstr_t map_value_type_name;
137 // 1 if field is optional (may be absent from JSON), 0 otherwise
138 int is_optional;
139 // 1 if field is nullable (JSON value may be null), 0 otherwise
140 int is_nullable;
141 // JSON key alias (NULL means use field name as-is)
142 sstr_t json_name;
143 // default value literal (NULL means no default)
144 sstr_t default_value;
145 // 1 if field has a declared default value
146 int has_default;
147 // 1 if field is deprecated, 0 otherwise
148 int is_deprecated;
149 // source position where the field was defined
150 int line;
151 int col;
152 // linked list pointer to next field, NULL if this is the last field
153 struct struct_field* next;
154};
155
161 // struct name
162 sstr_t name;
163 // field list
164 struct struct_field* fields;
165 // source position where the struct name was defined
166 int name_line;
167 int name_col;
168 // source filename (not owned)
169 const char *filename;
170};
171
175struct pos {
176 int line;
177 int col;
178 long offset;
179};
180
181// linked list node for tracking include stack (circular include detection)
183 const char* path;
184 struct include_node* parent;
185};
186
192 // struct name --> struct_field list
193 struct hash_map* struct_map;
194 // enum name --> enum_container
195 struct hash_map* enum_map;
196 // oneof name --> oneof_container
197 struct hash_map* oneof_map;
198 // position of string to be parsed
199 struct pos pos;
200 // name of parser
201 char *name;
202 // diagnostic engine (owned by top-level parser, shared with sub-parsers)
203 struct diag_engine *diag;
204 // include stack for circular include detection (linked list, not owned)
205 struct include_node* include_stack;
206};
207
208// token types of struct definitions, return by next_token()
209
210#define TOKEN_LEFT_BRACE '{'
211#define TOKEN_RIGHT_BRACE '}'
212#define TOKEN_LEFT_BRACKET '['
213#define TOKEN_RIGHT_BRACKET ']'
214#define TOKEN_SEMICOLON ';'
215#define TOKEN_COMMA ','
216#define TOKEN_SHARPE '#'
217#define TOKEN_AT '@'
218#define TOKEN_EQUAL '='
219#define TOKEN_STRING 4
220#define TOKEN_IDENTIFY 1
221#define TOKEN_INTEGER 2
222#define TOKEN_FLOAT 3
223#define TOKEN_EOF 0
224#define TOKEN_ERROR -1
225
231 int type;
232 sstr_t txt;
233};
234
241
247void struct_parser_free(struct struct_parser* parser);
248
257int struct_parser_parse(struct struct_parser* parser, sstr_t content);
258
268int struct_parser_validate(struct struct_parser* parser);
269
277int struct_parser_validate_to(struct struct_parser* parser, FILE *out);
278
279#ifdef __cplusplus
280}
281#endif
282
283#endif // STRUCT_PARSE_H_
Diagnostic engine for clang-style error reporting.
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:75
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:1488
int struct_parser_validate(struct struct_parser *parser)
Validate parsed schema for semantic errors.
Definition struct_parse.c:1740
struct struct_parser * struct_parser_new()
create and init a struct_parser instance.
Definition struct_parse.c:166
void struct_parser_free(struct struct_parser *parser)
free a struct_parser instance.
Definition struct_parse.c:206
int struct_parser_validate_to(struct struct_parser *parser, FILE *out)
Validate parsed schema and optionally print diagnostics.
Definition struct_parse.c:1712
Definition diag.h:44
structure to store a parsed enum definition.
Definition struct_parse.h:78
structure to store a single enum value (name-index pair). Enum values are stored as a linked list.
Definition struct_parse.h:67
Definition hash_map.h:34
Definition struct_parse.h:182
structure to store a parsed oneof (tagged union) definition.
Definition struct_parse.h:105
structure to store a single variant of a oneof (tagged union). Variants are stored as a linked list.
Definition struct_parse.h:93
position of string
Definition struct_parse.h:175
structure to store parsed structs. A struct may have multiple fields, we put fields in a linked list.
Definition struct_parse.h:160
structure to store field list of parsed structs. A struct may have multiple field,...
Definition struct_parse.h:122
parser context
Definition struct_parse.h:191
token
Definition struct_parse.h:230