From 135cc1ef50930b86189be2303f68ab387ebf7f27 Mon Sep 17 00:00:00 2001 From: Thomas White Date: Sun, 17 Feb 2019 23:04:20 +0100 Subject: WIP on parser --- libstorycode/narrative.c | 20 ++++++++++ libstorycode/narrative.h | 8 ++++ libstorycode/presentation.c | 37 ++++++++++++++++++ libstorycode/presentation.h | 6 +++ libstorycode/scparse_priv.h | 50 ++++++++++++++++++++++++ libstorycode/slide.c | 16 ++++++++ libstorycode/storycode.c | 11 +++++- libstorycode/storycode.l | 10 ++++- libstorycode/storycode.y | 94 ++++++++++++++++++++++++++++++--------------- 9 files changed, 217 insertions(+), 35 deletions(-) create mode 100644 libstorycode/scparse_priv.h diff --git a/libstorycode/narrative.c b/libstorycode/narrative.c index c2e0d27..23b8e34 100644 --- a/libstorycode/narrative.c +++ b/libstorycode/narrative.c @@ -52,3 +52,23 @@ void narrative_free(Narrative *n) free(n->items); free(n); } + + +void narrative_add_prestitle(Narrative *n, const char *text) +{ +} + + +void narrative_add_bp(Narrative *n, const char *text) +{ +} + + +void narrative_add_text(Narrative *n, const char *text) +{ +} + + +void narrative_add_slide(Narrative *n, Slide *slide) +{ +} diff --git a/libstorycode/narrative.h b/libstorycode/narrative.h index b9a9f7c..8f9727f 100644 --- a/libstorycode/narrative.h +++ b/libstorycode/narrative.h @@ -29,8 +29,16 @@ typedef struct _narrative Narrative; +#include "slide.h" + extern Narrative *narrative_new(void); extern void narrative_free(Narrative *n); +extern void narrative_add_prestitle(Narrative *n, const char *text); +extern void narrative_add_bp(Narrative *n, const char *text); +extern void narrative_add_text(Narrative *n, const char *text); +extern void narrative_add_slide(Narrative *n, Slide *slide); + + #endif /* NARRATIVE_H */ diff --git a/libstorycode/presentation.c b/libstorycode/presentation.c index 2ac751f..e393020 100644 --- a/libstorycode/presentation.c +++ b/libstorycode/presentation.c @@ -27,6 +27,8 @@ #include #include +#include +#include #include "presentation.h" #include "stylesheet.h" @@ -39,6 +41,7 @@ struct _presentation Narrative *narrative; int n_slides; Slide **slides; + int max_slides; }; @@ -51,10 +54,44 @@ Presentation *presentation_new() p->narrative = NULL; p->slides = NULL; p->n_slides = 0; + p->max_slides = 0; return p; } + void presentation_free(Presentation *p) { free(p); } + + +void presentation_add_stylesheet(Presentation *p, Stylesheet *ss) +{ + assert(p->stylesheet == NULL); + p->stylesheet = ss; +} + + +void presentation_add_narrative(Presentation *p, Narrative *n) +{ + assert(p->narrative == NULL); + p->narrative = n; +} + + +void presentation_add_slide(Presentation *p, Slide *s) +{ + assert(p->n_slides <= p->max_slides); + if ( p->n_slides == p->max_slides ) { + Slide **nslides = realloc(p->slides, + (p->max_slides+8)*sizeof(Slide *)); + if ( nslides == NULL ) { + fprintf(stderr, "Failed to allocate memory for slide\n"); + return; + } + p->slides = nslides; + p->max_slides += 8; + } + + p->slides[p->n_slides++] = s; +} diff --git a/libstorycode/presentation.h b/libstorycode/presentation.h index 7f4b144..b45f79b 100644 --- a/libstorycode/presentation.h +++ b/libstorycode/presentation.h @@ -29,8 +29,14 @@ typedef struct _presentation Presentation; +#include "stylesheet.h" +#include "narrative.h" + extern Presentation *presentation_new(void); extern void presentation_free(Presentation *p); +extern void presentation_add_stylesheet(Presentation *p, Stylesheet *ss); +extern void presentation_add_narrative(Presentation *p, Narrative *n); +extern void presentation_add_slide(Presentation *p, Slide *s); #endif /* PRESENTATION_H */ diff --git a/libstorycode/scparse_priv.h b/libstorycode/scparse_priv.h new file mode 100644 index 0000000..4a1e5db --- /dev/null +++ b/libstorycode/scparse_priv.h @@ -0,0 +1,50 @@ +/* + * scparse_priv.h + * + * Copyright © 2019 Thomas White + * + * This file is part of Colloquium. + * + * Colloquium is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +#ifndef SCPARSE_PRIV_H +#define SCPARSE_PRIV_H + +#include "presentation.h" +#include "narrative.h" + +struct frame_geom +{ + double x; + double y; + double w; + double h; + /* FIXME: units */ +}; + + +struct scpctx +{ + Presentation *p; + Narrative *n; + Stylesheet *ss; + Slide *s; + + /* Frame options */ + struct frame_geom geom; +}; + +#endif /* SCPARSE_PRIV_H */ diff --git a/libstorycode/slide.c b/libstorycode/slide.c index 28a4ee3..3246713 100644 --- a/libstorycode/slide.c +++ b/libstorycode/slide.c @@ -30,6 +30,22 @@ #include "slide.h" +enum slide_item_type +{ + SLIDE_ITEM_TEXT, +}; + + +struct slide_item +{ + enum slide_item_type type; + + /* For SLIDE_ITEM_TEXT */ + char **paragraphs; + int n_paras; +}; + + struct _slide { int n_items; diff --git a/libstorycode/storycode.c b/libstorycode/storycode.c index e9eb505..edfe580 100644 --- a/libstorycode/storycode.c +++ b/libstorycode/storycode.c @@ -29,16 +29,23 @@ #include #include "presentation.h" +#include "narrative.h" +#include "slide.h" +#include "stylesheet.h" #include "storycode_parse.h" #include "storycode_lex.h" +#include "scparse_priv.h" + Presentation *storycode_parse_presentation(const char *sc) { YY_BUFFER_STATE b; + struct scpctx parse_ctx; b = sc_scan_string(sc); - scparse(); + scparse(&parse_ctx); sc_delete_buffer(b); - return NULL; + + return parse_ctx.p; } diff --git a/libstorycode/storycode.l b/libstorycode/storycode.l index 663a4b1..f68026d 100644 --- a/libstorycode/storycode.l +++ b/libstorycode/storycode.l @@ -22,6 +22,12 @@ %{ #define YYDEBUG 1 + + #include "presentation.h" + #include "narrative.h" + #include "slide.h" + #include "stylesheet.h" + #include "storycode_parse.h" %} @@ -49,11 +55,11 @@ BGCOL { return SC_BGCOL; } (?i:left) { return SC_LEFT; } (?i:center) { return SC_CENTER; } (?i:right) { return SC_RIGHT; } -.*\n { sclval = strdup(yytext); sclval[yyleng-1] = '\0'; BEGIN(0); return SC_STRING; } +.*\n { sclval.str = strdup(yytext); sclval.str[yyleng-1] = '\0'; BEGIN(0); return SC_STRING; } "[" { return SC_SQOPEN; } "]" { return SC_SQCLOSE; } :[ ] { BEGIN(string); } -:\n { sclval = strdup(""); return SC_STRING; } +:\n { sclval.str = strdup(""); return SC_STRING; } [{] { return SC_OPENBRACE; } [}] { return SC_CLOSEBRACE; } [.\n ] {} diff --git a/libstorycode/storycode.y b/libstorycode/storycode.y index 095ed96..e17c56b 100644 --- a/libstorycode/storycode.y +++ b/libstorycode/storycode.y @@ -20,15 +20,19 @@ * */ -%{ - #include - extern int sclex(); - extern int scparse(); - void scerror(const char *s); -%} - %define api.token.prefix {SC_} %define api.prefix {sc} +%locations + +%code requires { + + #include "presentation.h" + #include "narrative.h" + #include "slide.h" + #include "stylesheet.h" + + #include "scparse_priv.h" +} %union { Presentation *p; @@ -39,6 +43,14 @@ char *str; } +%{ + #include + + extern int sclex(); + extern int scparse(); + void scerror(struct scpctx *ctx, const char *s); +%} + %token STYLES SLIDE %token NARRATIVE %token PRESTITLE @@ -66,24 +78,42 @@ %type STRING %type bulletpoint %type textframe +%type imageframe +%type multi_line_string +%type frameopt +%type geometry /* FIXME: Should have its own type */ +%type slidetitle + +%parse-param { struct scpctx *ctx }; +%initial-action +{ + ctx->p = presentation_new(); + + /* These are the objects currently being created. They will be + * added to the presentation when they're complete */ + ctx->n = narrative_new(); + ctx->ss = stylesheet_new(); + ctx->s = slide_new(); +} %% presentation: - stylesheet narrative -| narrative + stylesheet narrative { presentation_add_stylesheet(ctx->p, ctx->ss); + presentation_add_narrative(ctx->p, ctx->n); } +| narrative { presentation_add_narrative(ctx->p, ctx->n); } ; narrative: - narrative_el -| narrative narrative_el + narrative_el { } +| narrative narrative_el { } ; narrative_el: - prestitle { narrative_add_prestitle(n, $1); } -| bulletpoint { narrative_add_bp(n, $1); } -| slide { narrative_add_slide(n, $1); } -| STRING { narrative_add_text(n, $1); } + prestitle { narrative_add_prestitle(ctx->n, $1); } +| bulletpoint { narrative_add_bp(ctx->n, $1); } +| slide { narrative_add_slide(ctx->n, $1); } +| STRING { narrative_add_text(ctx->n, $1); } ; /* Can be in narrative or slide */ @@ -100,9 +130,11 @@ bulletpoint: /* ------ Slide contents ------ */ slide: - SLIDE OPENBRACE { printf("start of slide\n"); } + SLIDE OPENBRACE slide_parts - CLOSEBRACE { printf("end of slide\n"); } + CLOSEBRACE { presentation_add_slide(ctx->p, ctx->s); + narrative_add_slide(ctx->n, ctx->s); + ctx->s = slide_new(); /* New work in progress object */ } ; slide_parts: @@ -111,15 +143,15 @@ slide_parts: ; slide_part: - prestitle -| imageframe -| textframe -| FOOTER -| slidetitle + prestitle { slide_add_prestitle(ctx->s, $1); } +| imageframe { slide_add_image(ctx->s, $1, ctx->geom); frameopts_reset(ctx); } +| textframe { slide_add_text(ctx->s, $1, ctx->geom); frameopts_reset(ctx); } +| FOOTER { slide_add_footer(ctx->s); } +| slidetitle { slide_add_slidetitle(ctx->s, $1); } ; imageframe: - IMAGEFRAME frame_options STRING { printf("image frame '%s'\n", $STRING); } + IMAGEFRAME frame_options STRING { $$ = $STRING; } ; textframe: @@ -145,8 +177,8 @@ frame_option: ; frameopt: - geometry -| alignment + geometry {} +| alignment {} ; geometry: @@ -171,10 +203,10 @@ length: /* ------ Stylesheet ------ */ stylesheet: - STYLES OPENBRACE { printf("Here comes the stylesheet\n"); } - style_narrative { printf("Stylesheet - narrative\n"); } - style_slide { printf("Stylesheet - slide\n"); } - CLOSEBRACE + STYLES OPENBRACE + style_narrative + style_slide + CLOSEBRACE { printf("stylesheet\n"); } ; style_narrative: @@ -217,6 +249,6 @@ styledef: %% -void scerror(const char *s) { - printf("Error\n"); +void scerror(struct scpctx *ctx, const char *s) { + printf("Storycode parse error at %i-%i\n", yylloc.first_line, yylloc.first_column); } -- cgit v1.2.3