mirror of
https://github.com/sigmasternchen/libparcival
synced 2025-03-15 03:48:55 +00:00
grammar is now complete
This commit is contained in:
parent
e01841c1eb
commit
0eb49b5161
4 changed files with 78 additions and 2 deletions
|
@ -2,6 +2,7 @@
|
|||
#include <stdarg.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "common.h"
|
||||
|
||||
|
@ -21,4 +22,6 @@ void _panic(const char* function, const char* format, ...) {
|
|||
}
|
||||
|
||||
fprintf(stderr, "\n");
|
||||
|
||||
exit(4);
|
||||
}
|
||||
|
|
36
src/parser.y
36
src/parser.y
|
@ -15,10 +15,14 @@ extern void yyerror(char*);
|
|||
%union {
|
||||
struct tree tree;
|
||||
struct params params;
|
||||
struct template template;
|
||||
|
||||
char* text;
|
||||
}
|
||||
|
||||
%type <template> template
|
||||
%type <template> metaSection
|
||||
|
||||
%type <tree> mainSection
|
||||
|
||||
%type <params> parameter
|
||||
|
@ -27,6 +31,7 @@ extern void yyerror(char*);
|
|||
|
||||
%type <text> statementHeader
|
||||
%type <text> blockStatement
|
||||
%type <text> metaStatement
|
||||
%type <text> statement
|
||||
%type <text> output
|
||||
%type <text> texts
|
||||
|
@ -37,16 +42,40 @@ extern void yyerror(char*);
|
|||
%token STATEMENT_BEGIN STATEMENT_END
|
||||
%token OUTPUT_BEGIN OUTPUT_END
|
||||
|
||||
%start file
|
||||
%start template
|
||||
|
||||
%%
|
||||
|
||||
file: metaSection SECTION mainSection
|
||||
template: metaSection SECTION mainSection
|
||||
{
|
||||
$$ = $1;
|
||||
$$.tree = $3;
|
||||
}
|
||||
| mainSection
|
||||
{
|
||||
$$ = newTemplate();
|
||||
$$.tree = $1;
|
||||
}
|
||||
;
|
||||
|
||||
metaSection: /* empty */
|
||||
{
|
||||
$$ = newTemplate();
|
||||
}
|
||||
| PARAMS_BEGIN parameters PARAMS_END metaSection
|
||||
{
|
||||
$$ = $4;
|
||||
if ($$.params.no != 0) {
|
||||
yyerror("only one parameter block allowed in meta section");
|
||||
YYERROR;
|
||||
}
|
||||
$$.params = $2;
|
||||
}
|
||||
| STATEMENT_BEGIN metaStatement STATEMENT_END metaSection
|
||||
{
|
||||
$$ = $4;
|
||||
addStat(&$$.stats, $2);
|
||||
}
|
||||
;
|
||||
|
||||
parameters: /* empty */
|
||||
|
@ -77,6 +106,9 @@ moreParameters: /* empty */
|
|||
;
|
||||
|
||||
metaStatement: statement
|
||||
{
|
||||
$$ = $1;
|
||||
}
|
||||
;
|
||||
|
||||
mainSection: /* empty */
|
||||
|
|
26
src/tree.c
26
src/tree.c
|
@ -135,3 +135,29 @@ struct params combineParams(struct params p1, struct params p2) {
|
|||
|
||||
return p;
|
||||
}
|
||||
|
||||
struct stats newStats() {
|
||||
return (struct stats) {
|
||||
.texts = NULL,
|
||||
.no = 0
|
||||
};
|
||||
}
|
||||
|
||||
void addStat(struct stats* stats, char* text) {
|
||||
void* tmp = realloc(stats->texts, sizeof(char*) * (stats->no + 1));
|
||||
if (tmp == NULL) {
|
||||
panic("realloc");
|
||||
}
|
||||
|
||||
stats->texts = tmp;
|
||||
|
||||
stats->texts[stats->no++] = text;
|
||||
}
|
||||
|
||||
struct template newTemplate() {
|
||||
return (struct template) {
|
||||
.params = newParams(),
|
||||
.stats = newStats(),
|
||||
.tree = newTree()
|
||||
};
|
||||
}
|
||||
|
|
15
src/tree.h
15
src/tree.h
|
@ -40,5 +40,20 @@ struct params newParams();
|
|||
void addParam(struct params*, char*, char*);
|
||||
struct params combineParams(struct params, struct params);
|
||||
|
||||
struct stats {
|
||||
char** texts;
|
||||
size_t no;
|
||||
};
|
||||
|
||||
struct stats newStats();
|
||||
void addStat(struct stats*, char*);
|
||||
|
||||
struct template {
|
||||
struct params params;
|
||||
struct tree tree;
|
||||
struct stats stats;
|
||||
};
|
||||
|
||||
struct template newTemplate();
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Reference in a new issue