diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.c | 12 | ||||
| -rw-r--r-- | src/source-grammar.h | 22 | ||||
| -rw-r--r-- | src/source-grammar.leg | 116 | 
3 files changed, 145 insertions, 5 deletions
| @@ -12,16 +12,18 @@  #include <stdio.h> -#include "template-grammar.h" +#include "source-grammar.h"  int  main(int argc, char **argv)  { -    b_slist_t *t = blogc_template_parse( -        "<html>{{ BOLA }}</html>\n" -        "{% block single_source %}\n"); -    printf("%s\n", (char*) ((blogc_template_stmt_t*)t->next->next->next->data)->value); +    blogc_source_t *t = blogc_source_parse( +        "\n  \nBOLA: guda\n\t\n\n\n\n" +        "CHUNDA: asd\n" +        "----\n" +        "{% block single_source %}\nbola\n\nzas\n"); +    printf("%s\n", t->content);      printf("Hello, World!\n");      return 0;  } diff --git a/src/source-grammar.h b/src/source-grammar.h new file mode 100644 index 0000000..540c21b --- /dev/null +++ b/src/source-grammar.h @@ -0,0 +1,22 @@ +/* + * blogc: A blog compiler. + * Copyright (C) 2015 Rafael G. Martins <rafael@rafaelmartins.eng.br> + * + * This program can be distributed under the terms of the LGPL-2 License. + * See the file COPYING. + */ + +#ifndef _SOURCE_GRAMAR_H +#define _SOURCE_GRAMAR_H + +#include "utils/utils.h" + +typedef struct { +    b_trie_t *config; +    char *content; +} blogc_source_t; + +blogc_source_t* blogc_source_parse(const char *tmpl); +void blogc_source_free(blogc_source_t *source); + +#endif /* _SOURCE_GRAMAR_H */ diff --git a/src/source-grammar.leg b/src/source-grammar.leg new file mode 100644 index 0000000..faab9a4 --- /dev/null +++ b/src/source-grammar.leg @@ -0,0 +1,116 @@ +# +# blogc: A balde compiler. +# Copyright (C) 2015 Rafael G. Martins <rafael@rafaelmartins.eng.br> +# +# This program can be distributed under the terms of the BSD License. +# See the file COPYING. +# + +%{ + +#include <stdio.h> +#include "utils/utils.h" +#include "source-grammar.h" + +#define YY_INPUT(buf, result, max_size)                          \ +{                                                                \ +    int yyc = (charbuf && *charbuf != '\0') ? *charbuf++ : EOF;  \ +    result = (EOF == yyc) ? 0 : (*buf = yyc, 1);                 \ +} + + +static b_trie_t *config = NULL; +static const char *charbuf = NULL; +static char *key = NULL; +static char *content = NULL; + + +static void +blogc_source_config_key(const char *value) +{ +    if (key != NULL) { +        fprintf(stderr, "Syntax error: configuration key already set: %s\n", key); +        exit(1); +    } +    key = b_strdup(value); +} + + +static void +blogc_source_config_value(const char *value) +{ +    if (key == NULL) { +        fprintf(stderr, "Syntax error: configuration value without a key: %s\n", value); +        exit(1); +    } +    b_trie_insert(config, key, b_str_strip(b_strdup(value))); +    free(key); +    key = NULL; +} + + +static void +blogc_source_content(const char *value) +{ +    if (content != NULL) { +        fprintf(stderr, "Syntax error: content set twice\n"); +        exit(1); +    } +    content = b_strdup(value); +} + +%} + +page = ( ( - eol )* ( config ( - eol )* )+ '----' '-'* eol content eof ) | anything +    { fprintf(stderr, "Syntax error near: %s\n", yytext); exit(1); } + +# Useful rules +eol = '\n' | '\r\n' | '\r' +eof = !. +- = [\t ]* +id = [A-Z][A-Z0-9_]* +anything = < ( !eol . )* > eol + +# Configuration +config_key = < id >                     { blogc_source_config_key(yytext); } +config_value = < anything >             { blogc_source_config_value(yytext); } +config = config_key ':' ' '+ config_value + +# Generic content +content = < ( !eof . )+ >               { blogc_source_content(yytext); } + +%% + + +void +blogc_source_free(blogc_source_t *source) +{ +    if (source == NULL) +        return; +    free(source->content); +    b_trie_free(source->config); +    free(source); +} + + +static void +blogc_source_config_free(void *ptr) +{ +    free(ptr); +} + + +blogc_source_t* +blogc_source_parse(const char *tmpl) +{ +    charbuf = tmpl; +    config = b_trie_new(blogc_source_config_free); +    key = NULL; +    content = NULL; +    while(yyparse()); +    blogc_source_t *rv = malloc(sizeof(blogc_source_t)); +    rv->content = content; +    rv->config = config; +    charbuf = NULL; +    return rv; +} | 
