diff options
author | sanine <sanine.not@pm.me> | 2023-02-01 00:16:08 -0600 |
---|---|---|
committer | sanine <sanine.not@pm.me> | 2023-02-01 00:16:08 -0600 |
commit | 0710a40e50d12cd731b0372d79d0ccdfbaee10d4 (patch) | |
tree | 271f9626d3eea71165bcfa6f515940e90a85f56c /yy | |
parent | eacf29f8342a4d759fa70b4d7217e86aa2fb4a15 (diff) |
add kalmia_parse_file()
Diffstat (limited to 'yy')
-rw-r--r-- | yy/kalmia.y | 51 | ||||
-rw-r--r-- | yy/main.c | 19 |
2 files changed, 53 insertions, 17 deletions
diff --git a/yy/kalmia.y b/yy/kalmia.y index ec806f7..3d28248 100644 --- a/yy/kalmia.y +++ b/yy/kalmia.y @@ -29,12 +29,19 @@ struct kai_tag_t *next; }; + struct kai_tag_t * kai_parse_file(const char *filename); void kai_tag_destroy(struct kai_tag_t *tag); } %parse-param { struct kai_tag_t **document } %code { + /* flex definitions */ int yylex(YYSTYPE *yylvalp, YYLTYPE *yyllocp, yyscan_t scanner); - int yyerror(YYLTYPE *yyllocp, yyscan_t unused, struct kai_tag_t **unused2, const char *msg); + int kalmialex_init(yyscan_t *); + void kalmiaset_in(FILE *, yyscan_t); + int kalmialex_destroy(yyscan_t); + + int yyerror(YYLTYPE *yyllocp, yyscan_t unused, + struct kai_tag_t **unused2, const char *msg); #define TAG_MISMATCH(a, b) \ do { \ @@ -44,10 +51,12 @@ YYABORT; \ } while (0) + /* create/destroy kai_attr_t */ 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); + /* create/destroy kai_tag_t */ 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); @@ -59,6 +68,7 @@ struct kai_tag_t *tag; } +/* tokens */ %token PROLOG %token S_TAG_OPEN E_TAG_OPEN TAG_CLOSE EMPTY_TAG_CLOSE %token <string> NAME @@ -66,12 +76,14 @@ %token <string> TEXT %token <string> CONTENT +/* tag targets */ %type <tag> start_tag %type <string> end_tag %type <tag> empty_tag %type <tag> tag %type <tag> tags +/* attribute targets */ %type <attr> attribute %type <attr> attributes @@ -162,6 +174,7 @@ attribute: %% +/* print an error message */ int yyerror( YYLTYPE *yyllocp, yyscan_t unused, @@ -176,6 +189,8 @@ int yyerror( } + +/* create a new kai_attr_t object */ struct kai_attr_t * kai_attr_new(char *key, char *value) { struct kai_attr_t *attr = malloc(sizeof(struct kai_attr_t)); @@ -188,6 +203,7 @@ struct kai_attr_t * kai_attr_new(char *key, char *value) return attr; } +/* get the end of a kai_attr_t list */ struct kai_attr_t * kai_attr_last(struct kai_attr_t *head) { struct kai_attr_t *ptr = head; @@ -197,6 +213,7 @@ struct kai_attr_t * kai_attr_last(struct kai_attr_t *head) return ptr; } +/* destroy a kai_attr_t list */ void kai_attr_destroy(struct kai_attr_t *attr) { if (attr == NULL) { @@ -210,6 +227,8 @@ void kai_attr_destroy(struct kai_attr_t *attr) } + +/* create a kai_tag_t object */ 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)); @@ -225,6 +244,7 @@ struct kai_tag_t * kai_tag_new(char *type, struct kai_attr_t *attrs) return tag; } +/* get the end of a kai_tag_t list */ struct kai_tag_t * kai_tag_last(struct kai_tag_t *head) { struct kai_tag_t *ptr = head; @@ -234,6 +254,7 @@ struct kai_tag_t * kai_tag_last(struct kai_tag_t *head) return ptr; } +/* destroy a kai_tag_t list */ void kai_tag_destroy(struct kai_tag_t *tag) { if (tag == NULL) { @@ -247,3 +268,31 @@ void kai_tag_destroy(struct kai_tag_t *tag) kai_tag_destroy(tag->next); free(tag); } + + + +/* parse a file into a document */ +struct kai_tag_t * kai_parse_file(const char *filename) +{ + FILE *file = fopen(filename, "r"); + if (file == NULL) { + fprintf(stderr, "Could not open file \"%s\"\n", filename); + return NULL; + } + + yyscan_t scanner; + struct kai_tag_t *document; + + kalmialex_init(&scanner); + kalmiaset_in(file, scanner); + int result = kalmiaparse(scanner, &document); + kalmialex_destroy(scanner); + fclose(file); + + if (result != 0) { + /* parse function will print error message */ + return NULL; + } + + return document; +} @@ -50,23 +50,10 @@ int main(int argc, char **argv) fprintf(stderr, "You must specify a file to parse!\n"); return -1; } - FILE *in = fopen(argv[1], "r"); - if (in == NULL) { - fprintf(stderr, "Could not open file \"%s\"\n", argv[1]); - return -1; - } - - struct kai_tag_t *document; - yyscan_t scanner; - kalmialex_init(&scanner); - kalmiaset_in(in, scanner); - int result = kalmiaparse(scanner, &document); - kalmialex_destroy(scanner); - fclose(in); - - if (result != 0) { - return result; + struct kai_tag_t *document = kai_parse_file(argv[1]); + if (document == NULL) { + return -1; } print_tag("", document); |