protoverse

A metaverse protocol
git clone git://jb55.com/protoverse
Log | Files | Refs | README | LICENSE

parse.h (3015B)


      1 
      2 #ifndef PROTOVERSE_PARSE_H
      3 #define PROTOVERSE_PARSE_H
      4 
      5 #include "typedefs.h"
      6 #include "cursor.h"
      7 
      8 #define MAX_ATTRIBUTES 24
      9 #define MAX_CHILDREN 24
     10 #define ARRAY_SIZE(x) ((int)(sizeof(x) / sizeof((x)[0])))
     11 
     12 enum token_error {
     13 	TE_OK,
     14 	TE_STR_START_CHAR,
     15 	TE_NUM_START_CHAR,
     16 	TE_SYM_START_CHAR,
     17 	TE_SYM_CHAR,
     18 	TE_NUM_CHAR,
     19 	TE_UNEXPECTED_TOKEN,
     20 	TE_UNEXPECTED_SYMBOL,
     21 	TE_SYM_OVERFLOW,
     22 	TE_NAN_START_CHAR,
     23 };
     24 
     25 enum cell_type {
     26 	C_GROUP,
     27 	C_SPACE,
     28 	C_ROOM,
     29 	C_OBJECT,
     30 };
     31 
     32 enum object_type {
     33 	O_OBJECT,
     34 	O_TABLE,
     35 	O_CHAIR,
     36 	O_DOOR,
     37 	O_LIGHT,
     38 };
     39 
     40 enum attribute_type {
     41 	A_ID,
     42 	A_TYPE,
     43 	A_NAME,
     44 	A_MATERIAL,
     45 	A_CONDITION,
     46 	A_COLOR,
     47 	A_WIDTH,
     48 	A_DEPTH,
     49 	A_HEIGHT,
     50 	A_LOCATION,
     51 	A_SHAPE,
     52 	A_STATE,
     53 	A_DATA,
     54 };
     55 
     56 enum cell_state {
     57 	STATE_ON,
     58 	STATE_OFF,
     59 	STATE_SLEEPING,
     60 };
     61 
     62 enum shape {
     63 	SHAPE_RECTANGLE,
     64 	SHAPE_CIRCLE,
     65 	SHAPE_SQUARE,
     66 };
     67 
     68 
     69 enum token_type {
     70 	T_OPEN,
     71 	T_CLOSE,
     72 	T_STRING,
     73 	T_SYMBOL,
     74 	T_NUMBER,
     75 };
     76 
     77 struct tok_str {
     78 	u8 *data;
     79 	int len;
     80 };
     81 
     82 enum numtype {
     83 	NUM_INTEGER,
     84 	NUM_FLOAT
     85 };
     86 
     87 enum nantype {
     88 	NAN_ARITHMETIC,
     89 	NAN_CANONICAL,
     90 	NAN_LITERAL
     91 };
     92 
     93 struct number {
     94 	enum numtype type;
     95 	union {
     96 		int integer;
     97 		double fdouble;
     98 	} value;
     99 };
    100 
    101 struct nan {
    102 	enum nantype type;
    103 	struct number number;
    104 };
    105 
    106 struct bufstr {
    107 	const char *ptr;
    108 	int len;
    109 };
    110 
    111 struct data_attr {
    112 	struct bufstr str;
    113 	struct bufstr sym;
    114 };
    115 
    116 union attr_data {
    117 	struct bufstr str;
    118 	struct data_attr data_attr;
    119 	enum shape shape;
    120 	struct number number;
    121 };
    122 
    123 struct attribute {
    124 	union attr_data data;
    125 	enum attribute_type type;
    126 };
    127 
    128 struct cell {
    129 	u16 attributes[MAX_ATTRIBUTES];
    130 	u16 children[MAX_CHILDREN];
    131 	int n_attributes;
    132 	int n_children;
    133 
    134 	enum cell_type type;
    135 	enum object_type obj_type;
    136 };
    137 
    138 struct cursor_err {
    139 	union {
    140 		struct {
    141 			struct tok_str expected;
    142 			struct tok_str got;
    143 		} symbol;
    144 		struct {
    145 			enum token_type expected;
    146 			enum token_type got;
    147 		} lex;
    148 		char c;
    149 	};
    150 	int pos;
    151 };
    152 
    153 struct token_cursor {
    154 	struct cursor c;
    155 	enum token_error err;
    156 	struct cursor_err err_data;
    157 };
    158 
    159 struct parser {
    160 	struct token_cursor tokens;
    161 	struct cursor attributes;
    162 	struct cursor cells;
    163 };
    164 
    165 int parse_buffer(struct parser *parser, u8 *file_buf, int len, int *root);
    166 int parse_file(struct parser *parser, const char *filename, int *root, u8 *buf, u32 bufsize);
    167 int init_parser(struct parser *parser);
    168 int free_parser(struct parser *parser);
    169 void print_cell(struct cursor *attributes, struct cell *cell);
    170 int tokenize_cells(unsigned char *buf, int buf_size, struct token_cursor *tokens);
    171 int parse_cell(struct parser *parser, int *index);
    172 void print_token_error(struct token_cursor *cursor);
    173 const char *cell_type_str(enum cell_type);
    174 const char *object_type_str(enum object_type);
    175 struct cell *get_cell(struct cursor *cells, int index);
    176 struct attribute *get_attr(struct cursor *attributes, int index);
    177 int cell_attr_str(struct cursor *attributes, struct cell *cell, const char** name, int *len, enum attribute_type type);
    178 const char *cell_name(struct cursor *attributes, struct cell *cell, int *len);
    179 
    180 #endif /* PROTOVERSE_PARSE_H */