flesh out yaml parser.
This commit is contained in:
parent
0485a7242d
commit
71385a1fd5
8 changed files with 250 additions and 6 deletions
144
yaml.h
Normal file
144
yaml.h
Normal file
|
|
@ -0,0 +1,144 @@
|
|||
#ifndef YAML_H
|
||||
#define YAML_H
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef enum {
|
||||
YAML_TYPE_STRING, /**< A value that represents a string. */
|
||||
YAML_TYPE_NUMBER, /**< A value that represents a number. */
|
||||
YAML_TYPE_OBJECT, /**< A value that represents an object. */
|
||||
YAML_TYPE_ARRAY, /**< A value that represents an array. */
|
||||
YAML_TYPE_TRUE, /**< A value that represents true. */
|
||||
YAML_TYPE_FALSE, /**< A value that represents false. */
|
||||
YAML_TYPE_NULL, /**< A value that represents null. */
|
||||
} yaml_type_t;
|
||||
|
||||
typedef struct {
|
||||
void *data;
|
||||
yaml_type_t type;
|
||||
} yaml_value_t;
|
||||
|
||||
typedef struct {
|
||||
const char *data;
|
||||
size_t length;
|
||||
} yaml_string_t;
|
||||
|
||||
typedef struct {
|
||||
const char *data;
|
||||
size_t length;
|
||||
} yaml_number_t;
|
||||
|
||||
typedef struct yaml_object_field_s {
|
||||
yaml_string_t *name;
|
||||
yaml_value_t *value;
|
||||
|
||||
struct yaml_object_field_s *next;
|
||||
} yaml_object_field_t;
|
||||
|
||||
typedef struct {
|
||||
yaml_object_field_t *head;
|
||||
size_t count;
|
||||
} yaml_object_t;
|
||||
|
||||
typedef struct yaml_array_element_s {
|
||||
yaml_value_t *value;
|
||||
|
||||
struct yaml_array_element_s *next;
|
||||
} yaml_array_element_t;
|
||||
|
||||
typedef struct {
|
||||
yaml_array_element_t *head;
|
||||
size_t count;
|
||||
} yaml_array_t;
|
||||
|
||||
/**
|
||||
* Parse a UTF-8 encoded string into a value object.
|
||||
*
|
||||
* Returns `NULL` if parsing fails.
|
||||
*/
|
||||
yaml_value_t *yaml_parse(const void *, size_t);
|
||||
|
||||
/**
|
||||
* Traverse the tree to retrieve a specific sub-value from a root value.
|
||||
*
|
||||
* Returns `NULL` if the requested sub-value does not exist.
|
||||
*/
|
||||
yaml_value_t *yaml_get(const yaml_value_t *, const char *);
|
||||
|
||||
/**
|
||||
* Access a value object as a string.
|
||||
*
|
||||
* Returns `NULL` if the value is not a string.
|
||||
*/
|
||||
yaml_string_t *yaml_value_as_string(const yaml_value_t *);
|
||||
|
||||
/**
|
||||
* Access a value object as a number.
|
||||
*
|
||||
* Returns `NULL` if the value is not a number.
|
||||
*/
|
||||
yaml_number_t *yaml_value_as_number(const yaml_value_t *);
|
||||
|
||||
/**
|
||||
* Access a value object as an object.
|
||||
*
|
||||
* Returns `NULL` if the value is not an object.
|
||||
*/
|
||||
yaml_object_t *yaml_value_as_object(const yaml_value_t *);
|
||||
|
||||
/**
|
||||
* Access a value object as an array.
|
||||
*
|
||||
* Returns `NULL` if the value is not an array.
|
||||
*/
|
||||
yaml_array_t *yaml_value_as_array(const yaml_value_t *);
|
||||
|
||||
/**
|
||||
* Return a string describing the most recently encountered error.
|
||||
*
|
||||
* Returns `NULL` if no error has been encountered.
|
||||
*/
|
||||
const char *yaml_error(void);
|
||||
|
||||
#endif /* YAML_H */
|
||||
|
||||
#ifdef YAML_IMPLEMENTATION
|
||||
|
||||
char error_message[4096];
|
||||
|
||||
yaml_value_t *yaml_parse(const void *content, size_t length) {
|
||||
/* TODO */
|
||||
return NULL;
|
||||
}
|
||||
|
||||
yaml_value_t *yaml_get(const yaml_value_t *root, const char *path) {
|
||||
/* TODO */
|
||||
return NULL;
|
||||
}
|
||||
|
||||
yaml_string_t *yaml_value_as_string(const yaml_value_t *value) {
|
||||
/* TODO */
|
||||
return NULL;
|
||||
}
|
||||
|
||||
yaml_number_t *yaml_value_as_number(const yaml_value_t *value) {
|
||||
/* TODO */
|
||||
return NULL;
|
||||
}
|
||||
|
||||
yaml_object_t *yaml_object_as_object(const yaml_value_t *value) {
|
||||
/* TODO */
|
||||
return NULL;
|
||||
}
|
||||
|
||||
yaml_array_t *yaml_object_as_array(const yaml_value_t *value) {
|
||||
/* TODO */
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const char *yaml_error(void) {
|
||||
if (error_message[0] == '\0') return NULL;
|
||||
return error_message;
|
||||
}
|
||||
|
||||
#endif /* YAML_IMPLEMENTATION */
|
||||
Loading…
Add table
Add a link
Reference in a new issue