diff options
author | sanine <sanine.not@pm.me> | 2023-01-31 13:45:50 -0600 |
---|---|---|
committer | sanine <sanine.not@pm.me> | 2023-01-31 13:45:50 -0600 |
commit | 8e21b8f52a5c566767df9b3fe105706b6888a89b (patch) | |
tree | 8926ea0523b57135664f3840117e4d10694c4daf | |
parent | c4933ae58c84edf5a8e8f741dbb49324b1d12787 (diff) |
parse xml into memory
-rw-r--r-- | yy/bad.xml | 126 | ||||
-rw-r--r-- | yy/kalmia.l | 1 | ||||
-rw-r--r-- | yy/kalmia.y | 101 | ||||
-rw-r--r-- | yy/main.c | 42 | ||||
-rw-r--r-- | yy/simple.xml | 8 |
5 files changed, 270 insertions, 8 deletions
diff --git a/yy/bad.xml b/yy/bad.xml new file mode 100644 index 0000000..873797b --- /dev/null +++ b/yy/bad.xml @@ -0,0 +1,126 @@ +<?xml version="1.0" encoding="utf-8"?> +<COLLADA xmlns="http://www.collada.org/2008/03/COLLADASchema" version="1.5.0"> + <asset> + <created>2005-11-14T02:16:38Z</created> + <modified>2005-11-15T11:36:38Z</modified> + <revision>1.0</revision> + </asset> + <library_effects> + <effect id="whitePhong"> + <profile_COMMON> + <technique sid="phong1"> + <phong> + <emission> + <color>1.0 1.0 1.0 1.0</color> + </emission> + <ambient> + <color>1.0 1.0 1.0 1.0</color> + </ambient> + <diffuse> + <color>1.0 1.0 1.0 1.0</color> + </diffuse> + <specular> + <color>1.0 1.0 1.0 1.0</color> + </specular> + <shininess> + <float>20.0</float> + </shininess> + <reflective> + <color>1.0 1.0 1.0 1.0</color> + </reflective> + <reflectivity> + <float>0.5</float> + </reflectivity> + <transparent> + <color>1.0 1.0 1.0 1.0</color> + </transparent> + <transparency> + <float>1.0</float> + </transparency> + </phong> + </technique> + </profile_COMMON> + </effect> + </library_effects> + <library_materials> + <material id="whiteMaterial"> + <instance_effect url="#whitePhong"/> + </material> + </library_materials> + <library_geometries> + <geometry id="box" name="box"> + <mesh> + <source id="box-Pos"> + <float_array id="box-Pos-array" count="24"> + -0.5 0.5 0.5 + 0.5 0.5 0.5 + -0.5 -0.5 0.5 + 0.5 -0.5 0.5 + -0.5 0.5 -0.5 + 0.5 0.5 -0.5 + -0.5 -0.5 -0.5 + 0.5 -0.5 -0.5 + </float_array> + <technique_common> + <accessor source="#box-Pos-array" count="8" stride="3"> + <param name="X" type="float" /> + <param name="Y" type="float" /> + <param name="Z" type="float" /> + </accessor> + </technique_common> + </source> + <source id="box-0-Normal"> + <float_array id="box-0-Normal-array" count="18"> + 1.0 0.0 0.0 + -1.0 0.0 0.0 + 0.0 1.0 0.0 + 0.0 -1.0 0.0 + 0.0 0.0 1.0 + 0.0 0.0 -1.0 + </float_array> + <technique_common> + <accessor source="#box-0-Normal-array" count="6" stride="3"> + <param name="X" sid="hello" type="float"/> + <param name="Y" type="float"/> + <param name="Z" type="float"/> + </accessor> + </technique_common> + </source> + <vertices id="box-Vtx"> + <input semantic="POSITION" source="#box-Pos"/> + </vertices> + <polygons count="6" material="WHITE"> + <input semantic="VERTEX" source="#box-Vtx" offset="0"/> + <input semantic="NORMAL" source="#box-0-Normal" offset="1"/> + <p>0 4 2 4 3 4 1 4</p> + <p>0 2 1 2 5 2 4 2</p> + <p>6 3 7 3 3 3 2 3</p> + <p>0 1 4 1 6 1 2 1</p> + <p>3 0 7 0 5 0 1 0</p> + <p>5 5 7 5 6 5 4 5</p> + </polygons> + </mesh> + </geometry> + </library_geometries> + <library_visual_scenes> + <visual_scene id="DefaultScene"> + <node id="Box" name="Box"> + <translate> 0 0 0</translate> + <rotate> 0 0 1 0</rotate> + <rotate> 0 1 0 0</rotate> + <rotate> 1 0 0 0</rotate> + <scale> 1 1 1</scale> + <instance_geometry url="#box"> + <bind_material> + <technique_common> + <instance_material symbol="WHITE" target="#whiteMaterial"/> + </technique_common> + </bind_material> + </instance_geometry> + </node> + </visual_scene> + </library_visual_scenes> + <scene> + <instance_visual_scene url="#DefaultScene"/> + </scene> +</COLLADA> diff --git a/yy/kalmia.l b/yy/kalmia.l index ef7c200..85f68e3 100644 --- a/yy/kalmia.l +++ b/yy/kalmia.l @@ -21,7 +21,6 @@ %} S \x20\x0a\x0d\x09 -DATE [0-9]{4}\-[0-9]{2}\-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}[A-Z]* ATTR ([\x20\x0a\x0d\x09]*)?"=" diff --git a/yy/kalmia.y b/yy/kalmia.y index 98650f8..56b15ac 100644 --- a/yy/kalmia.y +++ b/yy/kalmia.y @@ -13,8 +13,7 @@ typedef void* yyscan_t; struct kalmia_t { - void *current; - int index; + struct kai_tag_t *tag; }; struct kai_attr_t { @@ -39,9 +38,11 @@ int yyerror(YYLTYPE *yyllocp, yyscan_t unused, struct kalmia_t *unused2, const char *msg); struct kai_attr_t * kai_attr_new(char *key, char *value); + struct kai_attr_t * kai_attr_last(struct kai_attr_t *head); void kai_attr_destroy(struct kai_attr_t *attr); - struct kai_tag_t * kai_tag_new(char *type, struct kai_attr_t *attrs, struct kai_tag_t *children, char *content); + struct kai_tag_t * kai_tag_new(char *type, struct kai_attr_t *attrs); + struct kai_tag_t * kai_tag_last(struct kai_tag_t *head); void kai_tag_destroy(struct kai_tag_t *tag); } @@ -58,11 +59,79 @@ %token <string> TEXT %token <string> CONTENT +%type <tag> start_tag +%type <tag> empty_tag +%type <tag> tag +%type <tag> tags + +%type <attr> attribute +%type <attr> attributes + %% -document: PROLOG +document: PROLOG tag { result->tag = $2; } + + +tags: + tag { $$ = $1; } + | tags tag { $$ = $1; kai_tag_last($$)->next = $2; } + ; + + +tag: + start_tag end_tag { $$ = $1; } + | start_tag CONTENT end_tag { $$ = $1; $$->content = $2; } + | start_tag tags end_tag { $$ = $1; $$->children = $2; } + | empty_tag { $$ = $1; } + ; + +start_tag: + S_TAG_OPEN NAME TAG_CLOSE + { + $$ = kai_tag_new($2, NULL); + if ($$ == NULL) YYNOMEM; + } + | S_TAG_OPEN NAME attributes TAG_CLOSE + { + $$ = kai_tag_new($2, $3); + if ($$ == NULL) YYNOMEM; + } + ; + + +empty_tag: + S_TAG_OPEN NAME EMPTY_TAG_CLOSE + { + $$ = kai_tag_new($2, NULL); + if ($$ == NULL) YYNOMEM; + } + | S_TAG_OPEN NAME attributes EMPTY_TAG_CLOSE + { + $$ = kai_tag_new($2, $3); + if ($$ == NULL) YYNOMEM; + } + ; + + +end_tag: + E_TAG_OPEN NAME TAG_CLOSE + ; + + +attributes: + attribute { $$ = $1; } + | attributes attribute { $$ = $1; kai_attr_last($$)->next = $2; } + ; + +attribute: + ATTR '=' '"' TEXT '"' + { + $$ = kai_attr_new($1, $4); + if ($$ == NULL) YYNOMEM; + } + ; %% @@ -94,6 +163,15 @@ struct kai_attr_t * kai_attr_new(char *key, char *value) return attr; } +struct kai_attr_t * kai_attr_last(struct kai_attr_t *head) +{ + struct kai_attr_t *ptr = head; + while (ptr->next != NULL) { + ptr = ptr->next; + } + return ptr; +} + void kai_attr_destroy(struct kai_attr_t *attr) { if (attr == NULL) { @@ -107,7 +185,7 @@ void kai_attr_destroy(struct kai_attr_t *attr) } -struct kai_tag_t * kai_tag_new(char *type, struct kai_attr_t *attrs, struct kai_tag_t *children, char *content) +struct kai_tag_t * kai_tag_new(char *type, struct kai_attr_t *attrs) { struct kai_tag_t *tag = malloc(sizeof(struct kai_tag_t)); if (tag == NULL) { @@ -116,12 +194,21 @@ struct kai_tag_t * kai_tag_new(char *type, struct kai_attr_t *attrs, struct kai_ tag->type = type; tag->attrs = attrs; - tag->children = children; - tag->content = content; + tag->children = NULL; + tag->content = NULL; tag->next = NULL; return tag; } +struct kai_tag_t * kai_tag_last(struct kai_tag_t *head) +{ + struct kai_tag_t *ptr = head; + while (ptr->next != NULL) { + ptr = ptr->next; + } + return ptr; +} + void kai_tag_destroy(struct kai_tag_t *tag) { if (tag == NULL) { @@ -6,6 +6,44 @@ #include "kalmia.lex.h" +void print_attrs(struct kai_attr_t *attr) +{ + while(attr != NULL) { + printf("%s=\"%s\" ", attr->key, attr->value); + attr = attr->next; + } +} + + +void print_tag(char *indent, struct kai_tag_t *tag) +{ + printf("%s%s[ ", indent, tag->type); + print_attrs(tag->attrs); + printf("]\n"); +} + + +void print_level(int indent_level, struct kai_tag_t *tag) +{ + char indent[64]; + for (int i=0; i<indent_level; i++) { + indent[i] = '\t'; + } + indent[indent_level] = 0; + + while (tag != NULL) { + print_tag(indent, tag); + if (tag->children != NULL) { + print_level(indent_level+1, tag->children); + } + if (tag->content != NULL) { + printf("%s\t%s\n", indent, tag->content); + } + tag = tag->next; + } +} + + int main(int argc, char **argv) { if (argc < 2) { @@ -25,5 +63,9 @@ int main(int argc, char **argv) kalmiaset_in(in, scanner); kalmiaparse(scanner, &result); kalmialex_destroy(scanner); + + print_tag("", result.tag); + print_level(1, result.tag->children); + return 0; } diff --git a/yy/simple.xml b/yy/simple.xml new file mode 100644 index 0000000..3791d0e --- /dev/null +++ b/yy/simple.xml @@ -0,0 +1,8 @@ +<?xml version="1.0" encoding="utf-8"?> +<COLLADA xmlns="http://www.collada.org/2008/03/COLLADASchema" version="1.5.0"> + <asset> + <created>2005-11-14T02:16:38Z</created> + <modified>2005-11-15T11:36:38Z</modified> + <revision>1.0</revision> + </asset> +</COLLADA> |