From 7b9d04f56c0e22abaeec8dc779bd0800b0d93f79 Mon Sep 17 00:00:00 2001 From: Thomas White Date: Thu, 14 Feb 2019 00:11:37 +0100 Subject: Move parser to separate library --- libstorycode/narrative.c | 30 +++++++ libstorycode/narrative.h | 31 ++++++++ libstorycode/storycode.l | 65 +++++++++++++++ libstorycode/storycode.y | 202 +++++++++++++++++++++++++++++++++++++++++++++++ meson.build | 21 ++++- src/storycode.l | 65 --------------- src/storycode.y | 198 ---------------------------------------------- 7 files changed, 345 insertions(+), 267 deletions(-) create mode 100644 libstorycode/narrative.c create mode 100644 libstorycode/narrative.h create mode 100644 libstorycode/storycode.l create mode 100644 libstorycode/storycode.y delete mode 100644 src/storycode.l delete mode 100644 src/storycode.y diff --git a/libstorycode/narrative.c b/libstorycode/narrative.c new file mode 100644 index 0000000..971af82 --- /dev/null +++ b/libstorycode/narrative.c @@ -0,0 +1,30 @@ +/* + * narrative.c + * + * 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 . + * + */ + + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#include + diff --git a/libstorycode/narrative.h b/libstorycode/narrative.h new file mode 100644 index 0000000..096ace5 --- /dev/null +++ b/libstorycode/narrative.h @@ -0,0 +1,31 @@ +/* + * narrative.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 NARRATIVE_H +#define NARRATIVE_H + +#ifdef HAVE_CONFIG_H +#include +#endif + + +#endif /* NARRATIVE_H */ diff --git a/libstorycode/storycode.l b/libstorycode/storycode.l new file mode 100644 index 0000000..a64fbec --- /dev/null +++ b/libstorycode/storycode.l @@ -0,0 +1,65 @@ +/* + * storycode.l + * + * 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 . + * + */ + +%{ + #define YYDEBUG 1 + #include "storycode.tab.h" +%} + +%option noyywrap nounput noinput +%s string + +%% + +STYLES { return SC_STYLES; } +PRESTITLE { return SC_PRESTITLE; } +SLIDETITLE { return SC_SLIDETITLE; } +NARRATIVE { return SC_NARRATIVE; } +SLIDE { return SC_SLIDE; } +BP { return SC_BP; } +TYPE { return SC_TYPE; } +TEXT { return SC_TEXTFRAME; } +IMAGE { return SC_IMAGEFRAME; } +FOOTER { return SC_FOOTER; } +FONT { return SC_FONT; } +PAD { return SC_PAD; } +ALIGN { return SC_ALIGN; } +FGCOL { return SC_FGCOL; } +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; } +"[" { return SC_SQOPEN; } +"]" { return SC_SQCLOSE; } +:[ ] { BEGIN(string); } +:\n { sclval = strdup(""); return SC_STRING; } +[{] { return SC_OPENBRACE; } +[}] { return SC_CLOSEBRACE; } +[.\n ] {} +[0-9\.]+ { /* FIXME: lval */ return SC_VALUE; } +[uf] { return SC_UNIT; } +[+] { return SC_PLUS; } +[x] { return SC_TIMES; } + + +%% diff --git a/libstorycode/storycode.y b/libstorycode/storycode.y new file mode 100644 index 0000000..625f080 --- /dev/null +++ b/libstorycode/storycode.y @@ -0,0 +1,202 @@ +/* + * storycode.y + * + * 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 . + * + */ + +%{ + extern int sclex(); + extern int scparse(); + void scerror(const char *s); +%} + +%define api.value.type {char *} +%define api.token.prefix {SC_} +%token STYLES SLIDE +%token NARRATIVE +%token PRESTITLE +%token SLIDETITLE +%token FOOTER +%token TEXTFRAME +%token IMAGEFRAME +%token BP + +%token FONT TYPE PAD ALIGN FGCOL BGCOL + +%token LEFT CENTER RIGHT + +%token STRING +%token OPENBRACE CLOSEBRACE +%token SQOPEN SQCLOSE +%token PLUS TIMES +%token UNIT VALUE + +%% + +presentation: + stylesheet narrative +| narrative +; + +narrative: + narrative_el +| narrative narrative_el +; + +narrative_el: + prestitle { printf("prestitle: '%s'\n", $1); } +| bulletpoint { printf("* '%s'\n", $1); } +| slide +| STRING { printf("Text line '%s'\n", $1); } +; + +/* Can be in narrative or slide */ + +prestitle: + PRESTITLE STRING { $$ = $2; } +; + +bulletpoint: + BP STRING { $$ = $2; } +; + + +/* ------ Slide contents ------ */ + +slide: + SLIDE OPENBRACE { printf("start of slide\n"); } + slide_parts + CLOSEBRACE { printf("end of slide\n"); } +; + +slide_parts: + %empty +| slide_parts slide_part +; + +slide_part: + prestitle +| imageframe +| textframe +| FOOTER +| slidetitle +; + +imageframe: + IMAGEFRAME frame_options STRING { printf("image frame '%s'\n", $STRING); } +; + +textframe: + TEXTFRAME frame_options multi_line_string { printf("text frame '%s'\n", $3); } +| TEXTFRAME frame_options OPENBRACE multi_line_string CLOSEBRACE { printf("text frame m\n"); } + +multi_line_string: + STRING { printf("string '%s'\n", $1); } +| multi_line_string STRING { printf("more string '%s'\n", $2); } +| bulletpoint { printf("string *%s\n", $1); } +| multi_line_string bulletpoint { printf("more string *%s\n", $1); } +; + +/* There can be any number of options */ +frame_options: + %empty +| frame_options frame_option +; + +/* Each option is enclosed in square brackets */ +frame_option: + SQOPEN frameopt SQCLOSE { printf("got an option: '%s'\n", $2); } +; + +frameopt: + geometry +| alignment +; + +geometry: + length TIMES length PLUS length PLUS length { $$ = "geom"; printf("Geometry\n"); } +; + +alignment: + LEFT +| CENTER +| RIGHT +; + +slidetitle: + SLIDETITLE STRING { $$ = $2; } +; + +length: + VALUE UNIT +; + + +/* ------ Stylesheet ------ */ + +stylesheet: + STYLES OPENBRACE { printf("Here comes the stylesheet\n"); } + style_narrative { printf("Stylesheet - narrative\n"); } + style_slide { printf("Stylesheet - slide\n"); } + CLOSEBRACE +; + +style_narrative: + NARRATIVE OPENBRACE style_narrative_def CLOSEBRACE { printf("narrative style\n"); } +; + +style_narrative_def: + %empty +| style_narrative_def style_prestitle +| style_narrative_def styledef +; + +style_slide: + SLIDE OPENBRACE style_slide_def CLOSEBRACE { printf("slide style\n"); } +; + +style_slide_def: + %empty +| style_slide_def style_prestitle +| style_slide_def styledef +; + +style_prestitle: + PRESTITLE OPENBRACE styledefs CLOSEBRACE { printf("prestitle style\n"); } +; + +styledefs: + %empty +| styledefs styledef +; + +styledef: + FONT STRING { printf("font def: '%s'\n", $2); } +| TYPE STRING { printf("type def: '%s'\n", $2); } +| PAD STRING { printf("pad def: '%s'\n", $2); } +| FGCOL STRING { printf("fgcol def: '%s'\n", $2); } +| BGCOL STRING { printf("bgcol def: '%s'\n", $2); } +| ALIGN STRING { printf("align def: '%s'\n", $2); } +; + +%% + +void scerror(const char *s) { + printf("Error\n"); +} diff --git a/meson.build b/meson.build index 759fa0b..7e6ac83 100644 --- a/meson.build +++ b/meson.build @@ -27,13 +27,17 @@ gresources = gnome.compile_resources('colloquium-resources', 'data/colloquium.gresource.xml', source_dir: 'data', c_name: 'colloquium') + +# libstorycode +libstorycode_includes = include_directories('libstorycode') + flex = find_program('flex') bison = find_program('bison') storycode_tab_ch = custom_target('storycode.tab.c', output : ['storycode.tab.c', 'storycode.tab.h'], - input : 'src/storycode.y', + input : 'libstorycode/storycode.y', command : [bison, '--defines=@OUTPUT1@', '-p', 'sc', '--report=all', @@ -42,18 +46,27 @@ storycode_tab_ch = custom_target('storycode.tab.c', storycode_c = custom_target('storycode.c', output : ['storycode.c', 'storycode.h'], - input : ['src/storycode.l', storycode_tab_ch], + input : ['libstorycode/storycode.l', storycode_tab_ch], command : [flex, '--outfile=@OUTPUT0@', '--header-file=@OUTPUT1@', '-P', 'sc', '@INPUT@']) +libstorycode = library('storycode', + ['libstorycode/narrative.c', + storycode_c, + ], + include_directories : libstorycode_includes, + install : true) + +libstorycode_dep = declare_dependency(include_directories : libstorycode_includes, + link_with : libstorycode) + executable('sc2_test', ['src/sc2_test.c', - storycode_c, ], gresources, - dependencies : [gtkdep]) + dependencies : [gtkdep, libstorycode_dep]) # Main program executable('colloquium', diff --git a/src/storycode.l b/src/storycode.l deleted file mode 100644 index a64fbec..0000000 --- a/src/storycode.l +++ /dev/null @@ -1,65 +0,0 @@ -/* - * storycode.l - * - * 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 . - * - */ - -%{ - #define YYDEBUG 1 - #include "storycode.tab.h" -%} - -%option noyywrap nounput noinput -%s string - -%% - -STYLES { return SC_STYLES; } -PRESTITLE { return SC_PRESTITLE; } -SLIDETITLE { return SC_SLIDETITLE; } -NARRATIVE { return SC_NARRATIVE; } -SLIDE { return SC_SLIDE; } -BP { return SC_BP; } -TYPE { return SC_TYPE; } -TEXT { return SC_TEXTFRAME; } -IMAGE { return SC_IMAGEFRAME; } -FOOTER { return SC_FOOTER; } -FONT { return SC_FONT; } -PAD { return SC_PAD; } -ALIGN { return SC_ALIGN; } -FGCOL { return SC_FGCOL; } -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; } -"[" { return SC_SQOPEN; } -"]" { return SC_SQCLOSE; } -:[ ] { BEGIN(string); } -:\n { sclval = strdup(""); return SC_STRING; } -[{] { return SC_OPENBRACE; } -[}] { return SC_CLOSEBRACE; } -[.\n ] {} -[0-9\.]+ { /* FIXME: lval */ return SC_VALUE; } -[uf] { return SC_UNIT; } -[+] { return SC_PLUS; } -[x] { return SC_TIMES; } - - -%% diff --git a/src/storycode.y b/src/storycode.y deleted file mode 100644 index cbd952a..0000000 --- a/src/storycode.y +++ /dev/null @@ -1,198 +0,0 @@ -/* - * storycode.y - * - * 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 . - * - */ - -%{ - extern int sclex(); - extern int scparse(); - void scerror(const char *s); -%} - -%define api.value.type {char *} -%define api.token.prefix {SC_} -%token STYLES SLIDE -%token NARRATIVE -%token PRESTITLE -%token SLIDETITLE -%token FOOTER -%token TEXTFRAME -%token IMAGEFRAME -%token BP - -%token FONT TYPE PAD ALIGN FGCOL BGCOL - -%token LEFT CENTER RIGHT - -%token STRING -%token OPENBRACE CLOSEBRACE -%token SQOPEN SQCLOSE -%token PLUS TIMES -%token UNIT VALUE - -%% - -storycode: - %empty -| storycode scblock -; - -scblock: - stylesheet { printf("That was the stylesheet\n"); } -| prestitle { printf("prestitle: '%s'\n", $1); } -| bulletpoint { printf("* '%s'\n", $1); } -| slide -| STRING { printf("Text line '%s'\n", $1); } -; - -stylesheet: - STYLES OPENBRACE { printf("Here comes the stylesheet\n"); } - style_narrative { printf("Stylesheet - narrative\n"); } - style_slide { printf("Stylesheet - slide\n"); } - CLOSEBRACE -; - - -/* Can be in narrative or slide */ - -prestitle: - PRESTITLE STRING { $$ = $2; } -; - -bulletpoint: - BP STRING { $$ = $2; } -; - -/* ------ Slide contents ------ */ - -slide: - SLIDE OPENBRACE { printf("start of slide\n"); } - slide_parts - CLOSEBRACE { printf("end of slide\n"); } -; - -slide_parts: - %empty -| slide_parts slide_part -; - -slide_part: - prestitle -| imageframe -| textframe -| FOOTER -| slidetitle -; - -imageframe: - IMAGEFRAME frame_options STRING { printf("image frame '%s'\n", $STRING); } -; - -textframe: - TEXTFRAME frame_options multi_line_string { printf("text frame '%s'\n", $3); } -| TEXTFRAME frame_options OPENBRACE multi_line_string CLOSEBRACE { printf("text frame m\n"); } - -multi_line_string: - STRING { printf("string '%s'\n", $1); } -| multi_line_string STRING { printf("more string '%s'\n", $2); } -| bulletpoint { printf("string *%s\n", $1); } -| multi_line_string bulletpoint { printf("more string *%s\n", $1); } -; - -/* There can be any number of options */ -frame_options: - %empty -| frame_options frame_option -; - -/* Each option is enclosed in square brackets */ -frame_option: - SQOPEN frameopt SQCLOSE { printf("got an option: '%s'\n", $2); } -; - -frameopt: - geometry -| alignment -; - -geometry: - length TIMES length PLUS length PLUS length { $$ = "geom"; printf("Geometry\n"); } -; - -alignment: - LEFT -| CENTER -| RIGHT -; - -slidetitle: - SLIDETITLE STRING { $$ = $2; } -; - -length: - VALUE UNIT -; - - -/* ------ Stylesheet ------ */ - -style_narrative: - NARRATIVE OPENBRACE style_narrative_def CLOSEBRACE { printf("narrative style\n"); } -; - -style_narrative_def: - %empty -| style_narrative_def style_prestitle -| style_narrative_def styledef -; - -style_slide: - SLIDE OPENBRACE style_slide_def CLOSEBRACE { printf("slide style\n"); } -; - -style_slide_def: - %empty -| style_slide_def style_prestitle -| style_slide_def styledef -; - -style_prestitle: - PRESTITLE OPENBRACE styledefs CLOSEBRACE { printf("prestitle style\n"); } -; - -styledefs: - %empty -| styledefs styledef -; - -styledef: - FONT STRING { printf("font def: '%s'\n", $2); } -| TYPE STRING { printf("type def: '%s'\n", $2); } -| PAD STRING { printf("pad def: '%s'\n", $2); } -| FGCOL STRING { printf("fgcol def: '%s'\n", $2); } -| BGCOL STRING { printf("bgcol def: '%s'\n", $2); } -| ALIGN STRING { printf("align def: '%s'\n", $2); } -; - -%% - -void scerror(const char *s) { - printf("Error\n"); -} -- cgit v1.2.3