Kohi Game Engine
kson_parser.h
Go to the documentation of this file.
1 
12 #ifndef _KSON_H_
13 #define _KSON_H_
14 
15 #include "defines.h"
16 #include "math/math_types.h"
17 #include "strings/kname.h"
18 #include "strings/kstring_id.h"
19 
20 typedef enum kson_token_type {
41 
42 typedef struct kson_token {
46 #ifdef KOHI_DEBUG
47  const char* content;
48 #endif
50 
51 typedef struct kson_parser {
52  const char* file_content;
54 
55  // darray
58 
59 typedef enum kson_property_type {
60  // TODO: Do we want to support undefined/null types. If so, pick one and just use that, no defining both.
69 
70 struct kson_property;
71 
72 typedef enum kson_object_type {
76 
77 // An object which can contain properties. Objects
78 // represent both "object" types as well as "array"
79 // types. These types are identical with one key
80 // difference: An object's properties are required to
81 // be named, whereas array properties are unnamed.
82 typedef struct kson_object {
84  // darray
87 
88 // An alias to represent kson arrays, which are really just
89 // kson_objects that contain properties without names.
91 
92 // Represents a property value for a kson property.
93 typedef union kson_property_value {
94  // Signed 64-bit int value.
95  i64 i;
96  // 32-bit float value.
97  f32 f;
98  // String value.
99  const char* s;
100  // Array or object value.
102  // Boolean value.
103  b8 b;
105 
106 // Represents a singe property for a kson object or array.
107 typedef struct kson_property {
108  // The type of property.
110  // The name of the property. If this belongs to an array, it should be INVALID_KSTRING_ID.
112 #ifdef KOHI_DEBUG
113  // The original named string. Only used in debug builds.
114  const char* name_str;
115 #endif
116  // The property value.
119 
120 // Represents a hierarchy of kson objects.
121 typedef struct kson_tree {
122  // The root object, which always must exist.
125 
133 
143 
150 
159 KAPI b8 kson_parser_tokenize(kson_parser* parser, const char* source);
160 
171 
179 KAPI b8 kson_tree_from_string(const char* source, kson_tree* out_tree);
180 
187 KAPI const char* kson_tree_to_string(kson_tree* tree);
188 
195 
202 
211 
220 
229 
237 KAPI b8 kson_array_value_add_string(kson_array* array, const char* value);
238 
247 
256 
265 
274 
283 
292 
301 
309 
318 
326 
327 // Object functions.
328 
337 KAPI b8 kson_object_value_add_int(kson_object* object, const char* name, i64 value);
338 
347 KAPI b8 kson_object_value_add_float(kson_object* object, const char* name, f32 value);
348 
357 KAPI b8 kson_object_value_add_boolean(kson_object* object, const char* name, b8 value);
358 
367 KAPI b8 kson_object_value_add_string(kson_object* object, const char* name, const char* value);
368 
377 KAPI b8 kson_object_value_add_mat4(kson_object* object, const char* name, mat4 value);
378 
387 KAPI b8 kson_object_value_add_vec4(kson_object* object, const char* name, vec4 value);
388 
397 KAPI b8 kson_object_value_add_vec3(kson_object* object, const char* name, vec3 value);
398 
407 KAPI b8 kson_object_value_add_vec2(kson_object* object, const char* name, vec2 value);
408 
418 
428 
437 KAPI b8 kson_object_value_add_object(kson_object* object, const char* name, kson_object value);
438 
447 
456 KAPI b8 kson_object_value_add_array(kson_object* object, const char* name, kson_array value);
457 
466 
475 
485 
495 KAPI b8 kson_array_element_value_get_int(const kson_array* array, u32 index, i64* out_value);
496 
506 KAPI b8 kson_array_element_value_get_float(const kson_array* array, u32 index, f32* out_value);
507 
517 KAPI b8 kson_array_element_value_get_bool(const kson_array* array, u32 index, b8* out_value);
518 
528 KAPI b8 kson_array_element_value_get_string(const kson_array* array, u32 index, const char** out_value);
529 
539 KAPI b8 kson_array_element_value_get_mat4(const kson_array* array, u32 index, mat4* out_value);
540 
550 KAPI b8 kson_array_element_value_get_vec4(const kson_array* array, u32 index, vec4* out_value);
551 
561 KAPI b8 kson_array_element_value_get_vec3(const kson_array* array, u32 index, vec3* out_value);
562 
572 KAPI b8 kson_array_element_value_get_vec2(const kson_array* array, u32 index, vec2* out_value);
573 
584 
595 
606 
617 
626 KAPI b8 kson_object_property_type_get(const kson_object* object, const char* name, kson_property_type* out_type);
627 
636 
645 KAPI b8 kson_object_property_value_type_get(const kson_object* object, const char* name, kson_property_type* out_type);
646 
656 KAPI b8 kson_object_property_value_get_int(const kson_object* object, const char* name, i64* out_value);
657 
667 KAPI b8 kson_object_property_value_get_float(const kson_object* object, const char* name, f32* out_value);
668 
678 KAPI b8 kson_object_property_value_get_bool(const kson_object* object, const char* name, b8* out_value);
679 
689 KAPI b8 kson_object_property_value_get_string(const kson_object* object, const char* name, const char** out_value);
690 
700 KAPI b8 kson_object_property_value_get_mat4(const kson_object* object, const char* name, mat4* out_value);
701 
711 KAPI b8 kson_object_property_value_get_vec4(const kson_object* object, const char* name, vec4* out_value);
712 
722 KAPI b8 kson_object_property_value_get_vec3(const kson_object* object, const char* name, vec3* out_value);
723 
733 KAPI b8 kson_object_property_value_get_vec2(const kson_object* object, const char* name, vec2* out_value);
734 
744 KAPI b8 kson_object_property_value_get_string_as_kname(const kson_object* object, const char* name, kname* out_value);
745 
756 
766 KAPI b8 kson_object_property_value_get_object(const kson_object* object, const char* name, kson_object* out_value);
767 
777 KAPI b8 kson_object_property_value_get_array(const kson_object* object, const char* name, kson_array* out_value);
778 
785 
792 
795 
798 
799 #endif
This file contains global type definitions which are used throughout the entire engine and applicatio...
#define KAPI
Import/export qualifier.
Definition: defines.h:205
unsigned int u32
Unsigned 32-bit integer.
Definition: defines.h:25
_Bool b8
8-bit boolean type
Definition: defines.h:58
float f32
32-bit floating point number
Definition: defines.h:47
signed long long i64
Signed 64-bit integer.
Definition: defines.h:42
This files contains an implementation of knames.
u64 kname
A kname is a string hash made for quick comparisons versus traditional string comparisons.
Definition: kname.h:36
KAPI b8 kson_parser_parse(kson_parser *parser, kson_tree *out_tree)
Uses the given parser to build a kson_tree using the tokens previously parsed. This means that kson_p...
KAPI b8 kson_object_value_add_object_empty(kson_object *object, const char *name)
Adds a named empty object value to the provided object.
KAPI b8 kson_object_property_value_get_string_as_kstring_id(const kson_object *object, const char *name, kstring_id *out_value)
Attempts to retrieve the given object's property value by name as a kstring_id. Fails if not found or...
KAPI b8 kson_parser_tokenize(kson_parser *parser, const char *source)
Uses the given parser to tokenize the provided source string. Note that it is recommended to use the ...
KAPI b8 kson_object_property_value_get_array(const kson_object *object, const char *name, kson_array *out_value)
Attempts to retrieve the a copy given object's property value by name as an array....
struct kson_tree kson_tree
KAPI b8 kson_object_property_value_get_int(const kson_object *object, const char *name, i64 *out_value)
Attempts to retrieve the given object's property value by name as a signed integer....
KAPI b8 kson_parser_create(kson_parser *out_parser)
Creates a kson parser. Note that it is generally recommended to use the kson_tree_from_string() and k...
KAPI b8 kson_object_value_add_array(kson_object *object, const char *name, kson_array value)
Adds a named array value to the provided object.
KAPI b8 kson_array_element_value_get_vec4(const kson_array *array, u32 index, vec4 *out_value)
Attempts to retrieve the array element's value at the provided index as a vec4. Fails if out of range...
KAPI b8 kson_array_element_value_get_array(const kson_array *array, u32 index, kson_array *out_value)
Attempts to retrieve the array element's value at the provided index as an array. Fails if out of ran...
KAPI b8 kson_object_property_count_get(const kson_object *object, u32 *out_count)
KAPI b8 kson_object_value_add_boolean(kson_object *object, const char *name, b8 value)
Adds a named boolean value to the provided object.
KAPI const char * kson_property_type_to_string(kson_property_type type)
Gets the given property type as a constant string. NOTE: Caller should NOT attempt to free this strin...
struct kson_object kson_object
KAPI b8 kson_object_property_value_get_vec4(const kson_object *object, const char *name, vec4 *out_value)
Attempts to retrieve the given object's property value by name as a vec4. Fails if not found or on ty...
KAPI b8 kson_object_value_add_kstring_id_as_string(kson_object *object, const char *name, kstring_id value)
Adds a named kstring_id value as a string to the provided object.
union kson_property_value kson_property_value
KAPI b8 kson_object_value_add_mat4(kson_object *object, const char *name, mat4 value)
Adds a named mat4 value to the provided object.
KAPI b8 kson_object_property_value_get_string_as_kname(const kson_object *object, const char *name, kname *out_value)
Attempts to retrieve the given object's property value by name as a kname. Fails if not found or on t...
kson_token_type
Definition: kson_parser.h:20
@ KSON_TOKEN_TYPE_IDENTIFIER
Definition: kson_parser.h:24
@ KSON_TOKEN_TYPE_CURLY_BRACE_CLOSE
Definition: kson_parser.h:35
@ KSON_TOKEN_TYPE_BRACKET_OPEN
Definition: kson_parser.h:36
@ KSON_TOKEN_TYPE_COMMENT
Definition: kson_parser.h:23
@ KSON_TOKEN_TYPE_NUMERIC_LITERAL
Definition: kson_parser.h:32
@ KSON_TOKEN_TYPE_EOF
Definition: kson_parser.h:39
@ KSON_TOKEN_TYPE_OPERATOR_PLUS
Definition: kson_parser.h:27
@ KSON_TOKEN_TYPE_OPERATOR_ASTERISK
Definition: kson_parser.h:29
@ KSON_TOKEN_TYPE_NEWLINE
Definition: kson_parser.h:38
@ KSON_TOKEN_TYPE_OPERATOR_EQUAL
Definition: kson_parser.h:25
@ KSON_TOKEN_TYPE_STRING_LITERAL
Definition: kson_parser.h:31
@ KSON_TOKEN_TYPE_OPERATOR_DOT
Definition: kson_parser.h:30
@ KSON_TOKEN_TYPE_UNKNOWN
Definition: kson_parser.h:21
@ KSON_TOKEN_TYPE_WHITESPACE
Definition: kson_parser.h:22
@ KSON_TOKEN_TYPE_BRACKET_CLOSE
Definition: kson_parser.h:37
@ KSON_TOKEN_TYPE_CURLY_BRACE_OPEN
Definition: kson_parser.h:34
@ KSON_TOKEN_TYPE_OPERATOR_MINUS
Definition: kson_parser.h:26
@ KSON_TOKEN_TYPE_OPERATOR_SLASH
Definition: kson_parser.h:28
@ KSON_TOKEN_TYPE_BOOLEAN
Definition: kson_parser.h:33
KAPI b8 kson_object_value_add_vec2(kson_object *object, const char *name, vec2 value)
Adds a named vec2 value to the provided object.
KAPI b8 kson_array_value_add_kstring_id_as_string(kson_array *array, kstring_id value)
Adds an unnamed kstring_id value as a string to the provided array.
KAPI kson_property kson_object_property_create(const char *name)
KAPI b8 kson_array_value_add_int(kson_array *array, i64 value)
Adds an unnamed signed 64-bit integer value to the provided array.
KAPI b8 kson_object_value_add_vec4(kson_object *object, const char *name, vec4 value)
Adds a named vec4 value to the provided object.
kson_property_type
Definition: kson_parser.h:59
@ KSON_PROPERTY_TYPE_OBJECT
Definition: kson_parser.h:65
@ KSON_PROPERTY_TYPE_FLOAT
Definition: kson_parser.h:63
@ KSON_PROPERTY_TYPE_STRING
Definition: kson_parser.h:64
@ KSON_PROPERTY_TYPE_BOOLEAN
Definition: kson_parser.h:67
@ KSON_PROPERTY_TYPE_UNKNOWN
Definition: kson_parser.h:61
@ KSON_PROPERTY_TYPE_INT
Definition: kson_parser.h:62
@ KSON_PROPERTY_TYPE_ARRAY
Definition: kson_parser.h:66
KAPI b8 kson_object_property_value_get_string(const kson_object *object, const char *name, const char **out_value)
Attempts to retrieve the given object's property value by name as a string. Fails if not found or on ...
KAPI void kson_object_cleanup(kson_object *obj)
Cleans up the given kson object and its properties recursively.
kson_object_type
Definition: kson_parser.h:72
@ KSON_OBJECT_TYPE_OBJECT
Definition: kson_parser.h:73
@ KSON_OBJECT_TYPE_ARRAY
Definition: kson_parser.h:74
KAPI b8 kson_tree_from_string(const char *source, kson_tree *out_tree)
Takes the provided source and tokenizes, then parses it in order to create a tree of kson_objects.
KAPI void kson_parser_destroy(kson_parser *parser)
Destroys the provided parser.
KAPI kson_property kson_array_property_create(const char *name)
KAPI b8 kson_array_element_value_get_string_as_kname(const kson_array *array, u32 index, kname *out_value)
Attempts to retrieve the array element's value at the provided index as a kname. Fails if out of rang...
KAPI b8 kson_array_value_add_array_empty(kson_array *array)
Adds an unnamed empty array value to the provided array.
KAPI b8 kson_array_element_value_get_bool(const kson_array *array, u32 index, b8 *out_value)
Attempts to retrieve the array element's value at the provided index as a boolean....
KAPI b8 kson_array_value_add_object(kson_array *array, kson_object value)
Adds an unnamed object value to the provided array.
KAPI b8 kson_array_element_count_get(kson_array *array, u32 *out_count)
Obtains the length of the given array.
KAPI b8 kson_object_property_value_get_float(const kson_object *object, const char *name, f32 *out_value)
Attempts to retrieve the given object's property value by name as a floating-point number....
KAPI b8 kson_array_value_add_kname_as_string(kson_array *array, kname value)
Adds an unnamed kname value as a string to the provided array.
KAPI b8 kson_object_value_add_int(kson_object *object, const char *name, i64 value)
Adds a named signed 64-bit integer value to the provided object.
KAPI b8 kson_object_value_add_float(kson_object *object, const char *name, f32 value)
Adds a named floating-point value to the provided object.
KAPI b8 kson_array_element_value_get_vec2(const kson_array *array, u32 index, vec2 *out_value)
Attempts to retrieve the array element's value at the provided index as a vec2. Fails if out of range...
KAPI b8 kson_array_value_add_object_empty(kson_array *array)
Adds an unnamed empty object value to the provided array.
struct kson_parser kson_parser
KAPI b8 kson_array_element_value_get_string(const kson_array *array, u32 index, const char **out_value)
Attempts to retrieve the array element's value at the provided index as a string. Fails if out of ran...
KAPI b8 kson_array_value_add_array(kson_array *array, kson_array value)
Adds an unnamed array value to the provided array.
KAPI b8 kson_object_value_add_array_empty(kson_object *object, const char *name)
Adds a named empty array value to the provided object.
KAPI b8 kson_array_element_type_at(kson_array *array, u32 index, kson_property_type *out_type)
Obtains the element type at the provided index of the given array. Fails if out of range.
KAPI b8 kson_object_value_add_vec3(kson_object *object, const char *name, vec3 value)
Adds a named vec3 value to the provided object.
KAPI b8 kson_object_property_value_get_mat4(const kson_object *object, const char *name, mat4 *out_value)
Attempts to retrieve the given object's property value by name as a mat4. Fails if not found or on ty...
struct kson_property kson_property
KAPI b8 kson_object_value_add_string(kson_object *object, const char *name, const char *value)
Adds a named string value to the provided object.
KAPI b8 kson_array_value_add_vec2(kson_array *array, vec2 value)
Adds an unnamed vec2 value to the provided array.
KAPI void kson_tree_cleanup(kson_tree *tree)
Performs cleanup operations on the given tree, freeing memory and resources held by it.
KAPI b8 kson_object_property_value_get_vec2(const kson_object *object, const char *name, vec2 *out_value)
Attempts to retrieve the given object's property value by name as a vec2. Fails if not found or on ty...
KAPI b8 kson_object_property_type_get(const kson_object *object, const char *name, kson_property_type *out_type)
KAPI b8 kson_object_property_value_get_object(const kson_object *object, const char *name, kson_object *out_value)
Attempts to retrieve the a copy given object's property value by name as an object....
KAPI b8 kson_object_value_add_kname_as_string(kson_object *object, const char *name, kname value)
Adds a named kname value as a string to the provided object.
KAPI b8 kson_array_element_value_get_string_as_kstring_id(const kson_array *array, u32 index, kstring_id *out_value)
Attempts to retrieve the array element's value at the provided index as a kstring_id....
KAPI b8 kson_array_element_value_get_float(const kson_array *array, u32 index, f32 *out_value)
Attempts to retrieve the array element's value at the provided index as a floating-point number....
KAPI b8 kson_array_value_add_vec3(kson_array *array, vec3 value)
Adds an unnamed vec3 value to the provided array.
KAPI b8 kson_object_property_value_get_bool(const kson_object *object, const char *name, b8 *out_value)
Attempts to retrieve the given object's property value by name as a boolean. Fails if not found or on...
struct kson_token kson_token
KAPI b8 kson_array_value_add_mat4(kson_array *array, mat4 value)
Adds an unnamed mat4 value to the provided array.
KAPI b8 kson_object_property_value_type_get(const kson_object *object, const char *name, kson_property_type *out_type)
Attempts to retrieve the given object's property value type by name. Fails if not found.
KAPI const char * kson_tree_to_string(kson_tree *tree)
kson_object kson_array
Definition: kson_parser.h:90
KAPI b8 kson_object_property_value_get_vec3(const kson_object *object, const char *name, vec3 *out_value)
Attempts to retrieve the given object's property value by name as a vec3. Fails if not found or on ty...
KAPI b8 kson_array_element_value_get_object(const kson_array *array, u32 index, kson_object *out_value)
Attempts to retrieve the array element's value at the provided index as an object....
KAPI b8 kson_array_element_value_get_int(const kson_array *array, u32 index, i64 *out_value)
Attempts to retrieve the array element's value at the provided index as a signed integer....
KAPI kson_array kson_array_create(void)
Creates and returns a new kson array.
KAPI b8 kson_array_value_add_float(kson_array *array, f32 value)
Adds an unnamed floating-point value to the provided array.
KAPI b8 kson_array_element_value_get_vec3(const kson_array *array, u32 index, vec3 *out_value)
Attempts to retrieve the array element's value at the provided index as a vec3. Fails if out of range...
KAPI b8 kson_array_value_add_string(kson_array *array, const char *value)
Adds an unnamed string value to the provided array.
KAPI b8 kson_array_value_add_vec4(kson_array *array, vec4 value)
Adds an unnamed vec4 value to the provided array.
KAPI b8 kson_object_value_add_object(kson_object *object, const char *name, kson_object value)
Adds a named object value to the provided object.
KAPI b8 kson_array_element_value_get_mat4(const kson_array *array, u32 index, mat4 *out_value)
Attempts to retrieve the array element's value at the provided index as a mat4. Fails if out of range...
KAPI b8 kson_array_value_add_boolean(kson_array *array, b8 value)
Adds an unnamed boolean value to the provided array.
KAPI kson_object kson_object_create(void)
Creates and returns a new kson object.
This files contains an implementation of kstring_ids.
u64 kstring_id
A kstring_id is a string hash made for quick comparisons versus traditional string comparisons.
Definition: kstring_id.h:32
Contains various math types required for the engine.
Definition: kson_parser.h:82
kson_object_type type
Definition: kson_parser.h:83
struct kson_property * properties
Definition: kson_parser.h:85
Definition: kson_parser.h:51
const char * file_content
Definition: kson_parser.h:52
u32 position
Definition: kson_parser.h:53
kson_token * tokens
Definition: kson_parser.h:56
Definition: kson_parser.h:107
kson_property_type type
Definition: kson_parser.h:109
kstring_id name
Definition: kson_parser.h:111
kson_property_value value
Definition: kson_parser.h:117
Definition: kson_parser.h:42
u32 start
Definition: kson_parser.h:44
kson_token_type type
Definition: kson_parser.h:43
u32 end
Definition: kson_parser.h:45
Definition: kson_parser.h:121
kson_object root
Definition: kson_parser.h:123
Definition: kson_parser.h:93
i64 i
Definition: kson_parser.h:95
b8 b
Definition: kson_parser.h:103
const char * s
Definition: kson_parser.h:99
kson_object o
Definition: kson_parser.h:101
f32 f
Definition: kson_parser.h:97
a 4x4 matrix, typically used to represent object transformations.
Definition: math_types.h:195
A 2-element vector.
Definition: math_types.h:19
A 3-element vector.
Definition: math_types.h:49
A 4-element vector.
Definition: math_types.h:89