json-gen-c  0.1.5
json-gen-c generate C code for json manipulation
struct_parse.h File Reference

Parse struct definitions. Support scalar types(int, long, float, double, sstr_t), arrays, structs. More...

#include <stddef.h>
#include "utils/diag.h"
#include "utils/hash_map.h"
#include "utils/sstr.h"
Include dependency graph for struct_parse.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  enum_value
 structure to store a single enum value (name-index pair). Enum values are stored as a linked list. More...
 
struct  enum_container
 structure to store a parsed enum definition. More...
 
struct  oneof_variant
 structure to store a single variant of a oneof (tagged union). Variants are stored as a linked list. More...
 
struct  oneof_container
 structure to store a parsed oneof (tagged union) definition. More...
 
struct  struct_field
 structure to store field list of parsed structs. A struct may have multiple field, and each field may be an array, or a struct. We use a single linked list to store the fields of a struct. More...
 
struct  struct_container
 structure to store parsed structs. A struct may have multiple fields, we put fields in a linked list. More...
 
struct  pos
 position of string More...
 
struct  include_node
 
struct  struct_parser
 parser context More...
 
struct  struct_token
 token More...
 

Macros

#define FIELD_TYPE_INT   0
 
#define FIELD_TYPE_LONG   1
 
#define FIELD_TYPE_FLOAT   2
 
#define FIELD_TYPE_DOUBLE   3
 
#define FIELD_TYPE_SSTR   4
 
#define FIELD_TYPE_ENUM   5
 
#define FIELD_TYPE_STRUCT   6
 
#define FIELD_TYPE_BOOL   7
 
#define FIELD_TYPE_MAP   8
 
#define FIELD_TYPE_INT8   9
 
#define FIELD_TYPE_INT16   10
 
#define FIELD_TYPE_INT32   11
 
#define FIELD_TYPE_INT64   12
 
#define FIELD_TYPE_UINT8   13
 
#define FIELD_TYPE_UINT16   14
 
#define FIELD_TYPE_UINT32   15
 
#define FIELD_TYPE_UINT64   16
 
#define FIELD_TYPE_ONEOF   17
 
#define TYPE_NAME_INT   "int"
 
#define TYPE_NAME_BOOL   "bool"
 
#define TYPE_NAME_SSTR   "sstr_t"
 
#define TYPE_NAME_LONG   "long"
 
#define TYPE_NAME_FLOAT   "float"
 
#define TYPE_NAME_DOUBLE   "double"
 
#define TYPE_NAME_INT8   "int8_t"
 
#define TYPE_NAME_INT16   "int16_t"
 
#define TYPE_NAME_INT32   "int32_t"
 
#define TYPE_NAME_INT64   "int64_t"
 
#define TYPE_NAME_UINT8   "uint8_t"
 
#define TYPE_NAME_UINT16   "uint16_t"
 
#define TYPE_NAME_UINT32   "uint32_t"
 
#define TYPE_NAME_UINT64   "uint64_t"
 
#define STRUCT_MAP_BUCKET_SIZE   4096
 We use a hash map to store parsed structs, and use the struct name as the key. This is the size of the hash map's bucket.
 
#define TOKEN_LEFT_BRACE   '{'
 
#define TOKEN_RIGHT_BRACE   '}'
 
#define TOKEN_LEFT_BRACKET   '['
 
#define TOKEN_RIGHT_BRACKET   ']'
 
#define TOKEN_SEMICOLON   ';'
 
#define TOKEN_COMMA   ','
 
#define TOKEN_SHARPE   '#'
 
#define TOKEN_AT   '@'
 
#define TOKEN_EQUAL   '='
 
#define TOKEN_STRING   4
 
#define TOKEN_IDENTIFY   1
 
#define TOKEN_INTEGER   2
 
#define TOKEN_FLOAT   3
 
#define TOKEN_EOF   0
 
#define TOKEN_ERROR   -1
 

Functions

struct struct_parserstruct_parser_new ()
 create and init a struct_parser instance.
 
void struct_parser_free (struct struct_parser *parser)
 free a struct_parser instance.
 
int struct_parser_parse (struct struct_parser *parser, sstr_t content)
 parse a struct definition file, and store the parsed structs in struct_parser.
 
int struct_parser_validate (struct struct_parser *parser)
 Validate parsed schema for semantic errors.
 
int struct_parser_validate_to (struct struct_parser *parser, FILE *out)
 Validate parsed schema and optionally print diagnostics.
 

Detailed Description

Parse struct definitions. Support scalar types(int, long, float, double, sstr_t), arrays, structs.

Function Documentation

◆ struct_parser_free()

void struct_parser_free ( struct struct_parser parser)

free a struct_parser instance.

Parameters
parserstruct struct_parser*

◆ struct_parser_new()

struct struct_parser * struct_parser_new ( )

create and init a struct_parser instance.

Returns
struct struct_parser* if success, NULL otherwise.

◆ struct_parser_parse()

int struct_parser_parse ( struct struct_parser parser,
sstr_t  content 
)

parse a struct definition file, and store the parsed structs in struct_parser.

Parameters
parsercontext of parser.
contentcontent of the file.
Returns
int 0 if success, -1 otherwise.

◆ struct_parser_validate()

int struct_parser_validate ( struct struct_parser parser)

Validate parsed schema for semantic errors.

Checks for: undefined type references, duplicate field names, duplicate enum values, and C keyword usage.

Parameters
parserParser with populated struct_map and enum_map.
Returns
0 if no errors, -1 if validation errors found.

◆ struct_parser_validate_to()

int struct_parser_validate_to ( struct struct_parser parser,
FILE *  out 
)

Validate parsed schema and optionally print diagnostics.

Parameters
parserParser with populated struct_map, enum_map, and oneof_map.
outDestination stream for diagnostics, or NULL to suppress printing.
Returns
0 if no errors, -1 if validation errors found.