summaryrefslogtreecommitdiffstats
path: root/grammar.y
blob: e484530aa75d1776569336de0f111895a2361b6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/* -*- mode: C -*- */
/* ----------------------------------------------------------------------------
   libconfig - A library for processing structured configuration files
   Copyright (C) 2005-2008  Mark A Lindner
 
   This file is part of libconfig.
    
   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public License
   as published by the Free Software Foundation; either version 2.1 of
   the License, or (at your option) any later version.
    
   This library 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
   Lesser General Public License for more details.
    
   You should have received a copy of the GNU Library General Public
   License along with this library; if not, see
   <http://www.gnu.org/licenses/>.
   ----------------------------------------------------------------------------
*/

%defines
%output="y.tab.c"
%pure-parser
%lex-param{void *scanner}
%parse-param{void *scanner}
%parse-param{struct parse_context *ctx}

%{
#include <string.h>
#include <stdlib.h>
#include "libconfig.h"
#ifdef WIN32
#include "wincompat.h"

/* prevent warnings about redefined malloc/free in generated code: */
#ifndef _STDLIB_H
#define _STDLIB_H
#endif
  
#include <malloc.h>
#endif
#include "private.h"

/* these delcarations are provided to suppress compiler warnings */
extern int libconfig_yylex();
extern int libconfig_yyget_lineno();
  
static const char *err_array_elem_type = "mismatched element type in array";
static const char *err_duplicate_setting = "duplicate setting name";

#define IN_ARRAY() \
  (ctx->parent && (ctx->parent->type == CONFIG_TYPE_ARRAY))

#define IN_LIST() \
  (ctx->parent && (ctx->parent->type == CONFIG_TYPE_LIST))

#define CAPTURE_PARSE_POS(S)                                    \
  (S)->line = (unsigned int)libconfig_yyget_lineno(scanner)

void libconfig_yyerror(void *scanner, struct parse_context *ctx,
                      char const *s)
{
  ctx->config->error_line = libconfig_yyget_lineno(scanner);
  ctx->config->error_text = s;
}

%}

%union
{
  long ival;
  long long llval;
  double fval;
  char *sval;
}

%token <ival> TOK_BOOLEAN TOK_INTEGER TOK_HEX
%token <llval> TOK_INTEGER64 TOK_HEX64
%token <fval> TOK_FLOAT
%token <sval> TOK_STRING TOK_NAME
%token TOK_EQUALS TOK_NEWLINE TOK_ARRAY_START TOK_ARRAY_END TOK_LIST_START TOK_LIST_END TOK_COMMA TOK_GROUP_START TOK_GROUP_END TOK_END TOK_GARBAGE

%%

configuration:
    /* empty */
  | setting_list
  ;

setting_list:
    setting
  | setting_list setting
  ;

setting_list_optional:
    /* empty */
  | setting_list
  ;

setting:
  TOK_NAME
  {
    ctx->setting = config_setting_add(ctx->parent, $1, CONFIG_TYPE_NONE);
    free($1);
  
    if(ctx->setting == NULL)
    {
      libconfig_yyerror(scanner, ctx, err_duplicate_setting);
      YYABORT;
    }
    else
    {
      CAPTURE_PARSE_POS(ctx->setting);
    }
  }

  TOK_EQUALS value TOK_END
  ;
  
array:
  TOK_ARRAY_START
  {
    if(IN_LIST())
    {
      ctx->parent = config_setting_add(ctx->parent, NULL, CONFIG_TYPE_ARRAY);
      CAPTURE_PARSE_POS(ctx->parent);
    }
    else
    {
      ctx->setting->type = CONFIG_TYPE_ARRAY;
      ctx->parent = ctx->setting;
      ctx->setting = NULL;
    }
  }
  simple_value_list_optional
  TOK_ARRAY_END
  {
    if(ctx->parent)
      ctx->parent = ctx->parent->parent;    
  }
  ;

list:
  TOK_LIST_START
  {
    if(IN_LIST())
    {
      ctx->parent = config_setting_add(ctx->parent, NULL, CONFIG_TYPE_LIST);
      CAPTURE_PARSE_POS(ctx->parent);
    }
    else
    {
      ctx->setting->type = CONFIG_TYPE_LIST;
      ctx->parent = ctx->setting;
      ctx->setting = NULL;
    }
  }
  value_list_optional
  TOK_LIST_END
  {
    if(ctx->parent)
      ctx->parent = ctx->parent->parent;    
  }
  ;

value:
    simple_value
  | array
  | list
  | group
  ;

simple_value:
    TOK_BOOLEAN
  {
    if(IN_ARRAY() || IN_LIST())
    {
      config_setting_t *e = config_setting_set_bool_elem(ctx->parent, -1,
                                                         (int)$1);
      
      if(! e)
      {
        libconfig_yyerror(scanner, ctx, err_array_elem_type);
        YYABORT;
      }
      else
      {
        CAPTURE_PARSE_POS(e);
      }
    }
    else
      config_setting_set_bool(ctx->setting, (int)$1);
  }
  | TOK_INTEGER
  {
    if(IN_ARRAY() || IN_LIST())
    {
      config_setting_t *e = config_setting_set_int_elem(ctx->parent, -1, $1);
      if(! e)
      {
        libconfig_yyerror(scanner, ctx, err_array_elem_type);
        YYABORT;
      }
      else
      {
        config_setting_set_format(e, CONFIG_FORMAT_DEFAULT);
        CAPTURE_PARSE_POS(e);
      }
    }
    else
    {
      config_setting_set_int(ctx->setting, $1);
      config_setting_set_format(ctx->setting, CONFIG_FORMAT_DEFAULT);
    }
  }
  | TOK_INTEGER64
  {
    if(IN_ARRAY() || IN_LIST())
    {
      config_setting_t *e = config_setting_set_int64_elem(ctx->parent, -1, $1);
      if(! e)
      {
        libconfig_yyerror(scanner, ctx, err_array_elem_type);
        YYABORT;
      }
      else
      {
        config_setting_set_format(e, CONFIG_FORMAT_DEFAULT);
        CAPTURE_PARSE_POS(e);
      }
    }
    else
    {
      config_setting_set_int64(ctx->setting, $1);
      config_setting_set_format(ctx->setting, CONFIG_FORMAT_DEFAULT);
    }
  }
  | TOK_HEX
  {
    if(IN_ARRAY() || IN_LIST())
    {
      config_setting_t *e = config_setting_set_int_elem(ctx->parent, -1, $1);
      if(! e)
      {
        libconfig_yyerror(scanner, ctx, err_array_elem_type);
        YYABORT;
      }
      else
      {
        config_setting_set_format(e, CONFIG_FORMAT_HEX);
        CAPTURE_PARSE_POS(e);
      }
    }
    else
    {
      config_setting_set_int(ctx->setting, $1);
      config_setting_set_format(ctx->setting, CONFIG_FORMAT_HEX);
    }
  }
  | TOK_HEX64
  {
    if(IN_ARRAY() || IN_LIST())
    {
      config_setting_t *e = config_setting_set_int64_elem(ctx->parent, -1, $1);
      if(! e)
      {
        libconfig_yyerror(scanner, ctx, err_array_elem_type);
        YYABORT;
      }
      else
      {
        config_setting_set_format(e, CONFIG_FORMAT_HEX);
        CAPTURE_PARSE_POS(e);
      }
    }
    else
    {
      config_setting_set_int64(ctx->setting, $1);
      config_setting_set_format(ctx->setting, CONFIG_FORMAT_HEX);
    }
  }
  | TOK_FLOAT
  {
    if(IN_ARRAY() || IN_LIST())
    {
      config_setting_t *e = config_setting_set_float_elem(ctx->parent, -1, $1);
      if(! e)
      {
        libconfig_yyerror(scanner, ctx, err_array_elem_type);
        YYABORT;
      }
      else
      {
        CAPTURE_PARSE_POS(e);
      }
    }
    else
      config_setting_set_float(ctx->setting, $1);
  }
  | TOK_STRING
  {
    if(IN_ARRAY() || IN_LIST())
    {
      config_setting_t *e = config_setting_set_string_elem(ctx->parent, -1,
                                                           $1);
      free($1);
      
      if(! e)
      {
        libconfig_yyerror(scanner, ctx, err_array_elem_type);
        YYABORT;
      }
      else
      {
        CAPTURE_PARSE_POS(e);
      }
    }
    else
    {
      config_setting_set_string(ctx->setting, $1);
      free($1);
    }
  }
  ;

value_list:
    value
  | value_list TOK_COMMA value
  ;

value_list_optional:
    /* empty */
  | value_list
  ;

simple_value_list:
    simple_value
  | simple_value_list TOK_COMMA simple_value
  ;

simple_value_list_optional:
    /* empty */
  | simple_value_list
  ;

group:
  TOK_GROUP_START
  {
    if(IN_LIST())
    {
      ctx->parent = config_setting_add(ctx->parent, NULL, CONFIG_TYPE_GROUP);
      CAPTURE_PARSE_POS(ctx->parent);
    }
    else
    {
      ctx->setting->type = CONFIG_TYPE_GROUP;
      ctx->parent = ctx->setting;
      ctx->setting = NULL;
    }
  }
  setting_list_optional
  TOK_GROUP_END
  {
    if(ctx->parent)
      ctx->parent = ctx->parent->parent;
  }
  ;

%%
xme: We are at image_update"); /* finally, draw it! */ if (W->class->draw) W->class->draw(W); /* add a new one-shot timer */ timer_add(widget_image_update, Self, Image->update, 1); } int widget_image_init(WIDGET * Self) { char *section; WIDGET_IMAGE *Image; /* re-use the parent if one exists */ if (Self->parent == NULL) { /* prepare config section */ /* strlen("Widget:")=7 */ section = malloc(strlen(Self->name) + 8); strcpy(section, "Widget:"); strcat(section, Self->name); Image = malloc(sizeof(WIDGET_IMAGE)); memset(Image, 0, sizeof(WIDGET_IMAGE)); /* initial size */ Image->width = 0; Image->height = 0; Image->bitmap = NULL; Image->file = NULL; /* get raw expressions (we evaluate them ourselves) */ Image->file_expr = cfg_get_raw(section, "file", NULL); Image->update_expr = cfg_get_raw(section, "update", NULL); Image->visible_expr = cfg_get_raw(section, "visible", NULL); /* sanity checks */ if (Image->file_expr == NULL || *Image->file_expr == '\0') { error("Warning: Image %s has no file", Self->name); } if (Image->update_expr == NULL || *Image->update_expr == '\0') { error("Image %s has no update, using '100'", Self->name); Image->update_expr = "100"; } /* compile'em */ Compile(Image->file_expr, &Image->file_tree); Compile(Image->update_expr, &Image->update_tree); Compile(Image->visible_expr, &Image->visible_tree); free(section); Self->data = Image; } else { /* re-use the parent */ Self->data = Self->parent->data; } /* just do it! */ widget_image_update(Self); return 0; } int widget_image_quit(WIDGET * Self) { if (Self) { /* do not deallocate child widget! */ if (Self->parent == NULL) { if (Self->data) { WIDGET_IMAGE *Image = Self->data; if (Image->bitmap) free(Image->bitmap); if (Image->file) free(Image->file); DelTree(Image->file_tree); DelTree(Image->update_tree); DelTree(Image->visible_tree); free(Self->data); Self->data = NULL; } } } return 0; } WIDGET_CLASS Widget_Image = { name:"image", type:WIDGET_TYPE_XY, init:widget_image_init, draw:NULL, quit:widget_image_quit, };