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;
56 
57  // darray
60 
61 typedef enum kson_property_type {
62  // TODO: Do we want to support undefined/null types. If so, pick one and just use that, no defining both.
71 
72 struct kson_property;
73 
74 typedef enum kson_object_type {
78 
79 // An object which can contain properties. Objects
80 // represent both "object" types as well as "array"
81 // types. These types are identical with one key
82 // difference: An object's properties are required to
83 // be named, whereas array properties are unnamed.
84 typedef struct kson_object {
86  // darray
89 
90 // An alias to represent kson arrays, which are really just
91 // kson_objects that contain properties without names.
93 
94 // Represents a property value for a kson property.
95 typedef union kson_property_value {
96  // Signed 64-bit int value.
97  i64 i;
98  // 32-bit float value.
99  f32 f;
100  // String value.
101  const char* s;
102  // Array or object value.
104  // Boolean value.
105  b8 b;
107 
108 // Represents a singe property for a kson object or array.
109 typedef struct kson_property {
110  // The type of property.
112  // The name of the property. If this belongs to an array, it should be INVALID_KSTRING_ID.
114 #ifdef KOHI_DEBUG
115  // The original named string. Only used in debug builds.
116  const char* name_str;
117 #endif
118  // The property value.
121 
122 // Represents a hierarchy of kson objects.
123 typedef struct kson_tree {
124  // The root object, which always must exist.
127 
129  // Use tabs instead of spaces?
131  // Number of tabs/space characters per indent level.
134 
142 
152 
159 
168 KAPI b8 kson_parser_tokenize(kson_parser* parser, const char* source);
169 
180 
188 KAPI b8 kson_tree_from_string(const char* source, kson_tree* out_tree);
189 
196 KAPI const char* kson_tree_to_string(kson_tree* tree);
197 
206 
213 
220 
229 
238 
247 
255 KAPI b8 kson_array_value_add_string(kson_array* array, const char* value);
256 
265 
274 
283 
292 
301 
310 
319 
328 
336 
345 
353 
354 // Object functions.
355 
364 KAPI b8 kson_object_value_add_int(kson_object* object, const char* name, i64 value);
365 
374 KAPI b8 kson_object_value_add_float(kson_object* object, const char* name, f32 value);
375 
384 KAPI b8 kson_object_value_add_boolean(kson_object* object, const char* name, b8 value);
385 
394 KAPI b8 kson_object_value_add_string(kson_object* object, const char* name, const char* value);
395 
404 KAPI b8 kson_object_value_add_mat4(kson_object* object, const char* name, mat4 value);
405 
414 KAPI b8 kson_object_value_add_rect_2di(kson_object* object, const char* name, rect_2di value);
415 
424 KAPI b8 kson_object_value_add_vec4(kson_object* object, const char* name, vec4 value);
425 
434 KAPI b8 kson_object_value_add_vec3(kson_object* object, const char* name, vec3 value);
435 
444 KAPI b8 kson_object_value_add_vec2(kson_object* object, const char* name, vec2 value);
445 
455 
465 
474 KAPI b8 kson_object_value_add_object(kson_object* object, const char* name, kson_object value);
475 
484 
493 KAPI b8 kson_object_value_add_array(kson_object* object, const char* name, kson_array value);
494 
503 
512 
522 
532 KAPI b8 kson_array_element_value_get_int(const kson_array* array, u32 index, i64* out_value);
533 
543 KAPI b8 kson_array_element_value_get_float(const kson_array* array, u32 index, f32* out_value);
544 
554 KAPI b8 kson_array_element_value_get_bool(const kson_array* array, u32 index, b8* out_value);
555 
565 KAPI b8 kson_array_element_value_get_string(const kson_array* array, u32 index, const char** out_value);
566 
576 KAPI b8 kson_array_element_value_get_mat4(const kson_array* array, u32 index, mat4* out_value);
577 
588 
598 KAPI b8 kson_array_element_value_get_vec4(const kson_array* array, u32 index, vec4* out_value);
599 
609 KAPI b8 kson_array_element_value_get_vec3(const kson_array* array, u32 index, vec3* out_value);
610 
620 KAPI b8 kson_array_element_value_get_vec2(const kson_array* array, u32 index, vec2* out_value);
621 
632 
643 
654 
665 
674 KAPI b8 kson_object_property_type_get(const kson_object* object, const char* name, kson_property_type* out_type);
675 
684 
693 KAPI b8 kson_object_property_value_type_get(const kson_object* object, const char* name, kson_property_type* out_type);
694 
704 KAPI b8 kson_object_property_value_get_int(const kson_object* object, const char* name, i64* out_value);
705 
715 KAPI b8 kson_object_property_value_get_float(const kson_object* object, const char* name, f32* out_value);
716 
726 KAPI b8 kson_object_property_value_get_bool(const kson_object* object, const char* name, b8* out_value);
727 
737 KAPI b8 kson_object_property_value_get_string(const kson_object* object, const char* name, const char** out_value);
738 
748 KAPI b8 kson_object_property_value_get_mat4(const kson_object* object, const char* name, mat4* out_value);
749 
759 KAPI b8 kson_object_property_value_get_rect_2di(const kson_object* object, const char* name, rect_2di* out_value);
760 
770 KAPI b8 kson_object_property_value_get_vec4(const kson_object* object, const char* name, vec4* out_value);
771 
781 KAPI b8 kson_object_property_value_get_vec3(const kson_object* object, const char* name, vec3* out_value);
782 
792 KAPI b8 kson_object_property_value_get_vec2(const kson_object* object, const char* name, vec2* out_value);
793 
803 KAPI b8 kson_object_property_value_get_extents_3d(const kson_object* object, const char* name, extents_3d* out_value);
804 
814 KAPI b8 kson_object_property_value_get_extents_2d(const kson_object* object, const char* name, extents_2d* out_value);
815 
825 KAPI b8 kson_object_property_value_get_string_as_kname(const kson_object* object, const char* name, kname* out_value);
826 
837 
847 KAPI b8 kson_object_property_value_get_object(const kson_object* object, const char* name, kson_object* out_value);
848 
858 KAPI b8 kson_object_property_value_get_array(const kson_object* object, const char* name, kson_array* out_value);
859 
866 
873 
876 
879 
880 #endif
This file contains global type definitions which are used throughout the entire engine and applicatio...
#define KAPI
Import/export qualifier.
Definition: defines.h:209
unsigned int u32
Unsigned 32-bit integer.
Definition: defines.h:27
_Bool b8
8-bit boolean type
Definition: defines.h:60
float f32
32-bit floating point number
Definition: defines.h:49
signed long long i64
Signed 64-bit integer.
Definition: defines.h:44
unsigned char u8
Unsigned 8-bit integer.
Definition: defines.h:21
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_object_value_add_rect_2di(kson_object *object, const char *name, rect_2di value)
Adds a named rect_2di value to the provided object.
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_property_value_get_rect_2di(const kson_object *object, const char *name, rect_2di *out_value)
Attempts to retrieve the given object's property value by name as a rect_2di. Fails if not found or o...
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 b8 kson_array_element_value_get_rect_2di(const kson_array *array, u32 index, rect_2di *out_value)
Attempts to retrieve the array element's value at the provided index as a rect_2di....
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:61
@ KSON_PROPERTY_TYPE_OBJECT
Definition: kson_parser.h:67
@ KSON_PROPERTY_TYPE_FLOAT
Definition: kson_parser.h:65
@ KSON_PROPERTY_TYPE_STRING
Definition: kson_parser.h:66
@ KSON_PROPERTY_TYPE_BOOLEAN
Definition: kson_parser.h:69
@ KSON_PROPERTY_TYPE_UNKNOWN
Definition: kson_parser.h:63
@ KSON_PROPERTY_TYPE_INT
Definition: kson_parser.h:64
@ KSON_PROPERTY_TYPE_ARRAY
Definition: kson_parser.h:68
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:74
@ KSON_OBJECT_TYPE_OBJECT
Definition: kson_parser.h:75
@ KSON_OBJECT_TYPE_ARRAY
Definition: kson_parser.h:76
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 const char * kson_tree_to_string_with_options(kson_tree *tree, kson_tree_to_string_options options)
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.
KAPI b8 kson_object_property_value_get_extents_2d(const kson_object *object, const char *name, extents_2d *out_value)
Attempts to retrieve the given object's property value by name as a extents_2d. Fails if not found or...
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_value_add_rect_2di(kson_array *array, rect_2di value)
Adds an unnamed rect_2di value to the provided array.
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
struct kson_tree_to_string_options kson_tree_to_string_options
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)
KAPI b8 kson_object_property_value_get_extents_3d(const kson_object *object, const char *name, extents_3d *out_value)
Attempts to retrieve the given object's property value by name as a extents_3d. Fails if not found or...
kson_object kson_array
Definition: kson_parser.h:92
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.
Represents the extents of a 2d object.
Definition: math_types.h:391
Represents the extents of a 3d object.
Definition: math_types.h:401
Definition: kson_parser.h:84
kson_object_type type
Definition: kson_parser.h:85
struct kson_property * properties
Definition: kson_parser.h:87
Definition: kson_parser.h:51
const char * file_content
Definition: kson_parser.h:52
u32 position
Definition: kson_parser.h:53
u32 current_line
Definition: kson_parser.h:54
u32 current_col
Definition: kson_parser.h:55
kson_token * tokens
Definition: kson_parser.h:58
Definition: kson_parser.h:109
kson_property_type type
Definition: kson_parser.h:111
kstring_id name
Definition: kson_parser.h:113
kson_property_value value
Definition: kson_parser.h:119
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:128
b8 use_tabs
Definition: kson_parser.h:130
u8 indent_count
Definition: kson_parser.h:132
Definition: kson_parser.h:123
kson_object root
Definition: kson_parser.h:125
Definition: kson_parser.h:95
i64 i
Definition: kson_parser.h:97
b8 b
Definition: kson_parser.h:105
const char * s
Definition: kson_parser.h:101
kson_object o
Definition: kson_parser.h:103
f32 f
Definition: kson_parser.h:99
a 4x4 matrix, typically used to represent object transformations.
Definition: math_types.h:383
A 2-element vector.
Definition: math_types.h:31
A 3-element vector.
Definition: math_types.h:117
A 4-element vector.
Definition: math_types.h:229
A 4-element integer-based vector.
Definition: math_types.h:574