aboutsummaryrefslogtreecommitdiff
path: root/src/grecs-gram.y
blob: f31305e10371f5babc54cf6cedfa541fa919d8e8 (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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
%{
/* grecs - Gray's Extensible Configuration System
   Copyright (C) 2007-2011 Sergey Poznyakoff

   Grecs 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.

   Grecs 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 Grecs. If not, see <http://www.gnu.org/licenses/>. */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <grecs.h>
#include <grecs-gram.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
  
static struct grecs_node *parse_tree;
int grecs_error_count;    

int grecs_default_port = 0;

%}

%union {
	char *string;
	grecs_value_t value;
	struct grecs_list *list;
	struct grecs_node *node;
	struct { struct grecs_node *head, *tail; } node_list;
}

%token <string> IDENT STRING QSTRING MSTRING
%type <string> string slist
%type <list> slist0
%type <value> value tag vallist
%type <list> values list vlist
%type <node> stmt simple block
%type <node_list> stmtlist

%%

input   : stmtlist
          {
		  parse_tree = grecs_node_create(grecs_node_root,
						 &grecs_current_locus);
		  grecs_node_bind(parse_tree, $1.head, 1);
	  }
        ;

stmtlist: stmt
          {
		  $$.head = $$.tail = $1;
	  } 
        | stmtlist stmt
	  {
		  grecs_node_bind($1.tail, $2, 0);
	  }
        ;

stmt    : simple
        | block
        ;

simple  : IDENT vallist ';'
          {
		  $$ = grecs_node_create(grecs_node_stmt,
					 &grecs_current_locus);
		  $$->ident = $1;
		  $$->value = $2;
	  }
        ;

block   : IDENT tag '{' stmtlist '}' opt_sc
          {
		  $$ = grecs_node_create(grecs_node_block,
					 &grecs_current_locus);
		  $$->ident = $1;
		  $$->value = $2;
		  grecs_node_bind($$, $4.head, 1);
	  }
        ;

tag     : /* empty */
          {
		  $$.type = GRECS_TYPE_STRING;
		  $$.v.string = NULL;
	  }
        | value
        ;

vallist : vlist
          {
		  size_t n;
		  
		  if ((n = grecs_list_size($1)) == 1) {
			  $$ = *(grecs_value_t *)grecs_list_index($1, 0);
		  } else {
			  size_t i;
			  struct grecs_list_entry *ep;
		
			  $$.type = GRECS_TYPE_ARRAY;
			  $$.v.arg.c = n;
			  $$.v.arg.v = grecs_malloc(n *
						     sizeof($$.v.arg.v[0]));
			  for (i = 0, ep = $1->head; ep; i++, ep = ep->next)
				  $$.v.arg.v[i] = *(grecs_value_t *)ep->data;
		  }
		  grecs_list_free($1);	      
	  }
	;

vlist   : value
          {
		  $$ = _grecs_simple_list_create(0);
		  grecs_list_append($$, grecs_value_dup(&$1));
	  }
        | vlist value
          {
		  grecs_list_append($1, grecs_value_dup(&$2));
	  }
        ;

value   : string
          {
		  $$.type = GRECS_TYPE_STRING;
		  $$.v.string = $1;
	  }
        | list
          {
		  $$.type = GRECS_TYPE_LIST;
		  $$.v.list = $1;
	  }
        | MSTRING
          {
		  $$.type = GRECS_TYPE_STRING;
		  $$.v.string = $1;
	  }	      
        ;        

string  : STRING
        | IDENT
        | slist
        ;

slist   : slist0
          {
		  struct grecs_list_entry *ep;
		  
		  grecs_line_begin();
		  for (ep = $1->head; ep; ep = ep->next)
			  grecs_line_add(ep->data, strlen(ep->data));
		  $$ = grecs_line_finish();
		  grecs_list_free($1);
	  }
        ;

slist0  : QSTRING 
          {
		  $$ = _grecs_simple_list_create(0);
		  grecs_list_append($$, $1);
	  }
        | slist0 QSTRING
          {
		  grecs_list_append($1, $2);
		  $$ = $1;
	  }
        ;

list    : '(' ')'
          {
		  $$ = NULL;
	  }
        | '(' values ')'
          {
		  $$ = $2;
	  }
        | '(' values ',' ')'
          {
		  $$ = $2;
	  }
        ;

values  : value
          {
		  $$ = _grecs_simple_list_create(0);
		  grecs_list_append($$, grecs_value_dup(&$1));
	  }
        | values ',' value
          {
		  grecs_list_append($1, grecs_value_dup(&$3));
		  $$ = $1;
	  }
        ;

opt_sc  : /* empty */
        | ';'
        ;

%%

int
yyerror(char *s)
{
	grecs_error(&grecs_current_locus, 0, "%s", s);
	return 0;
}

static void
listel_dispose(void *el)
{
	free(el);
}
 
struct grecs_list *
_grecs_simple_list_create(int dispose)
{
	struct grecs_list *lp = grecs_list_create();
	if (dispose)
		lp->free_entry = listel_dispose;
	return lp;
}

int
grecs_vasprintf(char **pbuf, size_t *psize, const char *fmt, va_list ap)
{
	char *buf = *pbuf;
	size_t buflen = *psize;
	int rc = 0;
  
	if (!buf) {
		if (buflen == 0)
			buflen = 512; /* Initial allocation */
      
		buf = calloc(1, buflen);
		if (buf == NULL)
			return ENOMEM;
	}
  
	for (;;) {
		ssize_t n = vsnprintf(buf, buflen, fmt, ap);
		if (n < 0 || n >= buflen || !memchr(buf, '\0', n + 1)) {
			char *newbuf;
			size_t newlen = buflen * 2;
			if (newlen < buflen) {
				rc = ENOMEM;
				break;
			}
			newbuf = realloc(buf, newlen);
			if (newbuf == NULL) {
				rc = ENOMEM;
				break;
			}
			buflen = newlen;
			buf = newbuf;
		} else
			break;
	}

	if (rc) {
		if (!*pbuf) {
			/* We made first allocation, now free it */
			free(buf);
			buf = NULL;
			buflen = 0;
		}
	}
	
	*pbuf = buf;
	*psize = buflen;
	return rc;
}  

int
grecs_asprintf(char **pbuf, size_t *psize, const char *fmt, ...)
{
	int rc;
	va_list ap;
	
	va_start(ap, fmt);
	rc = grecs_vasprintf(pbuf, psize, fmt, ap);
	va_end(ap);
	return rc;
}

struct grecs_node *
grecs_parse(const char *name)
{
	int rc;
	if (grecs_lex_begin(name))
		return NULL;
	parse_tree = NULL;
	rc = yyparse();
	grecs_lex_end();
	if (grecs_error_count) {
		grecs_tree_free(parse_tree);
		parse_tree = NULL;
	}
	return parse_tree;
}

void
grecs_gram_trace(int n)
{
	yydebug = n;
}



    

Return to:

Send suggestions and report system problems to the System administrator.