aboutsummaryrefslogtreecommitdiff
path: root/src/bind-lex.l
blob: 12dddf743d709b7a26f3069ff178ef3f12a604b9 (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
/* grecs - Gray's Extensible Configuration System       -*- c -*- */
%top {
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
}
%{
/* 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/>. */

#include <grecs.h>
#include <bind-gram.h>
#include <unistd.h>
#include <fcntl.h>
#include <ctype.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/stat.h>
	
#include <wordsplit.h>

%}
    
%x COMMENT STR

WS [ \t\f][ \t\f]*
ID [a-zA-Z_][a-zA-Z_0-9-]*
P [1-9][0-9]*

%%
         /* C-style comments */
"/*"         BEGIN(COMMENT);
<COMMENT>[^*\n]*        /* eat anything that's not a '*' */
<COMMENT>"*"+[^*/\n]*   /* eat up '*'s not followed by '/'s */
<COMMENT>\n             ++grecs_current_locus.line;
<COMMENT>"*"+"/"        BEGIN(INITIAL);
         /* End-of-line comments */
#.*\n     { grecs_current_locus.line++; }
#.*     /* end-of-file comment */;
"//".*\n  { grecs_current_locus.line++; }
"//".*    /* end-of-file comment */;
        /* Identifiers */
{ID}      { grecs_line_begin();
	    grecs_line_add(yytext, yyleng);
	    yylval.ident.string = grecs_line_finish();
	    yylval.ident.locus = grecs_current_locus;
	    return BIND_IDENT;
          }
         /* Strings */
[a-zA-Z0-9_\.\*/:@-]+    { grecs_line_begin();
	                   grecs_line_add(yytext, yyleng);
                           yylval.string = grecs_line_finish();
                           return BIND_STRING; }
         /* Quoted strings */
\"[^\\"\n]*\"         { grecs_line_begin();
                        grecs_line_add(yytext + 1, yyleng - 2);
                        yylval.string = grecs_line_finish();
                        return BIND_STRING; }
\"[^\\"\n]*\\. |
\"[^\\"\n]*\\\n        { BEGIN(STR);
                         grecs_line_begin();
		         grecs_line_acc_grow_unescape_last(yytext + 1,
                                                           yyleng - 1); }
\"[^\\"\n]*\n          { BEGIN(STR);
                         grecs_line_begin();
		         grecs_line_acc_grow(yytext + 1, yyleng - 1); }
<STR>[^\\"\n]*\\. |
<STR>\"[^\\"\n]*\\\n  { grecs_line_acc_grow_unescape_last(yytext, yyleng); }
<STR>[^\\"\n]*\n |
<STR>\"[^\\"\n]*\n    { grecs_line_acc_grow(yytext, yyleng); }
<STR>[^\\"\n]*\"      { BEGIN(INITIAL);
                        if (yyleng > 1) 
				grecs_line_add(yytext, yyleng - 1); 
                        yylval.string = grecs_line_finish();
		        return BIND_STRING; }
{WS}     ;
         /* Other tokens */
\n       { grecs_current_locus.line++; } 
[,;{}()] return yytext[0];
.        { if (isascii(yytext[0]) && isprint(yytext[0]))
		grecs_error(&grecs_current_locus, 0,
			     _("stray character %c"), yytext[0]);
           else
             grecs_error(&grecs_current_locus, 0, _("stray character \\%03o"),
		   	   (unsigned char) yytext[0]); }
%%

struct bind_input_context {
	ino_t i_node;
	dev_t i_dev;
	grecs_locus_t locus;		/* Current input location */
	YY_BUFFER_STATE state;
	FILE *input;
};

static struct grecs_list *input_stack;
static ino_t i_node;
static dev_t i_dev;

static void
free_context(void *ptr)
{
	free(ptr);
}

static int
cmp_context(const void *a, const void *b)
{
	struct bind_input_context const *ac = a;
	struct bind_input_context const *bc = b;

	return !(ac->i_node == bc->i_node && ac->i_dev == bc->i_dev);
}

static int
_push_context(const char *name, ino_t i_node, dev_t i_dev)
{
	struct bind_input_context ctx, *pctx;
	
	if (!input_stack) {
		input_stack = grecs_list_create();
		input_stack->free_entry = free_context;
		input_stack->cmp = cmp_context;
	} else {
		ctx.i_dev = i_dev;
		ctx.i_node = i_node;
		pctx = grecs_list_locate(input_stack, &ctx);
		if (pctx) {
			grecs_error(&grecs_current_locus, 0,
				    _("%s has already been included"), name);
			grecs_error(&pctx->locus, 0,
			    _("this is where the previous inclusion occurred"));
			return 1;
		}

		pctx = grecs_malloc(sizeof(*pctx));
		pctx->i_node = i_node;
		pctx->i_dev = i_dev;
		pctx->locus = grecs_current_locus;
		pctx->state = YY_CURRENT_BUFFER;
		pctx->input = yyin;
		grecs_list_push(input_stack, pctx);
	}
	return 0;
}

static int
_pop_context()
{
	struct bind_input_context *pctx;

	if (!yyin)
		return 1;
	fclose(yyin);
	pctx = grecs_list_pop(input_stack);
	if (!pctx) {
		yyin = NULL;
		return 1;
	}
	i_node = pctx->i_node;
	i_dev = pctx->i_dev;
	grecs_current_locus = pctx->locus;
	yyin = pctx->input;
	yy_delete_buffer(YY_CURRENT_BUFFER);
	yy_switch_to_buffer(pctx->state);
	grecs_free(pctx);
	return 0;
}

int
yywrap()
{
	return _pop_context();
}

int
grecs_bind_new_source(const char *name)
{
	grecs_locus_t *loc = grecs_current_locus.file ?
                        		&grecs_current_locus : NULL;
	struct stat st;
	FILE *fp = fopen(name, "r");
	if (!fp) {
		grecs_error(loc, errno, _("cannot open `%s'"), name);
		return 1;
	}
	if (fstat(fileno(fp), &st)) {
		grecs_error(loc, errno, _("can't state %s"), name);
		fclose(fp);
		return 1;
	}
	if (_push_context(name, st.st_ino, st.st_dev)) {
		fclose(fp);
		return 1;
	}
	i_node = st.st_ino;
	i_dev = st.st_dev;
	yyin = fp;
	yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
	grecs_current_locus.file = grecs_install_text(name);
	grecs_current_locus.line = 1;
	return 0;
}

void
grecs_bind_close_sources()
{
	while (!_pop_context())
		;
	grecs_list_free(input_stack);
}

Return to:

Send suggestions and report system problems to the System administrator.