[go: up one dir, main page]

File: sheets-map.l

package info (click to toggle)
a2ps 1%3A4.14-1.3
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 13,324 kB
  • sloc: ansic: 26,966; sh: 11,844; lex: 2,286; perl: 1,156; yacc: 757; makefile: 609; lisp: 398; ada: 263; objc: 189; f90: 109; ml: 85; sql: 74; pascal: 57; modula3: 33; haskell: 32; sed: 30; java: 29; python: 24
file content (310 lines) | stat: -rw-r--r-- 7,891 bytes parent folder | download | duplicates (8)
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
%{ /* -*- c -*- */
/* Scanner for sheets.map files.
 * Copyright (c) 1999-2000 Akim Demaille, Miguel Santana
 */

/*
 * This file is part of a2ps.
 *
 * This program 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, or (at your option)
 * any later version.
 *
 * This program 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 this program; see the file COPYING.  If not, write to
 * the Free Software Foundation, 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "main.h"
#include "select.h"
#include "xobstack.h"

extern struct darray * sheets_map;

int yylex PARAMS ((void));
void yyerror PARAMS ((const char *));
static void yy_include_push PARAMS ((char *file));
static void yy_include_pop PARAMS ((void));

/* Obstack for strings reading */
static struct obstack string_stack;

/* Stack to handle included files. */

#define MAX_INCLUDE_DEPTH 10
static YY_BUFFER_STATE include_stack[MAX_INCLUDE_DEPTH];
static int lineno_stack[MAX_INCLUDE_DEPTH];
static char *filename_stack[MAX_INCLUDE_DEPTH];
static int include_stack_ptr;
static char *yyfilename;

typedef enum
{
  tKEY = 10,
  tGLOB = 11,
  tFILE = 12
} token_t;

static char *token_val;
static bool insensitive_p;

static inline void
obstack_hexa_grow (struct obstack *o, const char *str)
{
  int value = 0;

  /* FIXME: Requires ASCII?  */
  while (*str)
    if (*str >= 'a' && *str <= 'f')
      value = 16 * value + *str++ - 'a' + 10;
    else if (*str >= 'A' && *str <= 'F')
      value = 16 * value + *str++ - 'A' + 10;
    else
      value = 16 * value + *str++ - '0';
  obstack_1grow (o, value);
}

static inline void
obstack_octal_grow (struct obstack *o, const char *str)
{
  int value = *str++ - '0';

  while (*str)
    value = 8 * value + *str++ - '0';
  obstack_1grow (o, value);
}


%}

%option yylineno
%option prefix="smap"
%option outfile="lex.yy.c"

%x STATE_GLOB
%x STATE_FILE

white         [[:space:]]+
key           [-a-zA-Z0-9_]+:
comment       #.*
include       include\([^)]+\)
%%

{key}         token_val = xstrndup (yytext, yyleng - 1); return tKEY;
\/            BEGIN STATE_GLOB;
\<            BEGIN STATE_FILE;
{white}       ;
{comment}     ;
{include}     {	/* Kill the closing paren and pass the file name. */
                yytext[yyleng - 1] = '\0';
                yy_include_push (yytext + strlen ("include("));
              }

.             { error_at_line (1, 0, yyfilename, yylineno,
			       _("unexpected character `%c'"), *yytext);
              }
<<EOF>>       {   message (msg_file, (stderr, "End of file `%s'.\n",
				      yyfilename));
               	if (--include_stack_ptr < 0)
               	  yyterminate ();
		else
		  yy_include_pop () ;
               }

<STATE_GLOB>{
  \/i? 			{ /* return the string */
    uchar *string;

    obstack_1grow (&string_stack, '\0');
    string = (uchar *) obstack_finish (&string_stack);
    obstack_free (&string_stack, string);
    token_val = xustrdup (string);

    /* Decode the additional flags. */
    insensitive_p = yytext[1] == 'i';

    BEGIN INITIAL;	/* Return to the regular scanning */
    return tGLOB;
  }

  \\[0-7]{1,3}		{ obstack_octal_grow (&string_stack, yytext + 1); }
  \\x[0-9a-fA-F]{1,2}	{ obstack_hexa_grow (&string_stack, yytext + 2); }
  \\a			{ obstack_1grow (&string_stack, '\007'); }
  \\b			{ obstack_1grow (&string_stack, '\b'); }
  \\d			{ obstack_1grow (&string_stack, 127); }
  \\e			{ obstack_1grow (&string_stack, 27); }
  \\f			{ obstack_1grow (&string_stack, '\f'); }
  \\n			{ obstack_1grow (&string_stack, '\n'); }
  \\r			{ obstack_1grow (&string_stack, '\r'); }
  \\t			{ obstack_1grow (&string_stack, '\t'); }
  \\v			{ obstack_1grow (&string_stack, '\v'); }
  \\.			{ obstack_1grow (&string_stack, yytext[1]); }

  \n 			{
    error_at_line (1, 0, yyfilename, yylineno,
		   /* TRANS: %s is ".." or <..> or /../ etc. */
		   _("end of line inside a %s"), "/../");
  }

  [^\/\n\\]+		{
    obstack_grow (&string_stack, yytext, yyleng);
  }
}

<STATE_FILE>{		/* string of characters */
  \>i? 			{ /* return the string */
    uchar *string;

    obstack_1grow (&string_stack, '\0');
    string = (uchar *) obstack_finish (&string_stack);
    obstack_free (&string_stack, string);
    token_val = xustrdup (string);

    /* Decode the additional flags. */
    insensitive_p = yytext[1] == 'i';

    BEGIN INITIAL;	/* Return to the regular scanning */
    return tFILE;
  }

  \\[0-7]{1,3}		{ obstack_octal_grow (&string_stack, yytext + 1); }
  \\x[0-9a-fA-F]{1,2}	{ obstack_hexa_grow (&string_stack, yytext + 2); }
  \\a			{ obstack_1grow (&string_stack, '\007'); }
  \\b			{ obstack_1grow (&string_stack, '\b'); }
  \\d			{ obstack_1grow (&string_stack, 127); }
  \\e			{ obstack_1grow (&string_stack, 27); }
  \\f			{ obstack_1grow (&string_stack, '\f'); }
  \\n			{ obstack_1grow (&string_stack, '\n'); }
  \\r			{ obstack_1grow (&string_stack, '\r'); }
  \\t			{ obstack_1grow (&string_stack, '\t'); }
  \\v			{ obstack_1grow (&string_stack, '\v'); }
  \\.			{ obstack_1grow (&string_stack, yytext[1]); }

  \n 			{
    error_at_line (1, 0, yyfilename, yylineno,
		   _("end of line inside a %s"), "<..>");
  }

  [^>\n\\]+		{
    obstack_grow (&string_stack, yytext, yyleng);
  }
}
%%

/*-----------------------------------------------------.
| Handle the inclusion of files at the scanner level.  |
`-----------------------------------------------------*/

/* Switch the scanning onto FILE, coming back to YYIN later.  */

static void
yy_include_push (char *file)
{
  if (include_stack_ptr >= MAX_INCLUDE_DEPTH)
    error (1, 0, _("too many includes"));

  include_stack[include_stack_ptr] = YY_CURRENT_BUFFER;
  lineno_stack[include_stack_ptr] = yylineno;
  filename_stack[include_stack_ptr++] = yyfilename;

  message (msg_file, (stderr, "%s:%d: includes %s\n",
		      yyfilename, yylineno, file));

  yyfilename = file;
  yyin = xrfopen (yyfilename);

  yy_switch_to_buffer (yy_create_buffer (yyin, YY_BUF_SIZE));
}

/* Pop the inclusion stack and proceed.  To be called on <<EOF>>.*/

static void
yy_include_pop (void)
{
  fclose (yyin);
  yy_delete_buffer (YY_CURRENT_BUFFER);
  yyfilename = filename_stack[include_stack_ptr];
  yylineno = lineno_stack[include_stack_ptr];
  yy_switch_to_buffer (include_stack[include_stack_ptr]);
  message (msg_file, (stderr, "Back to file `%s'.\n", yyfilename));
}


/*----------------------.
| Prepare the scanner.  |
`----------------------*/

/* Initialize the include stack to FILE. */

static inline void
yy_open (const char *file)
{
  yyfilename = xstrdup (file);
  yyin = xrfopen (yyfilename);
  include_stack_ptr = 0;
}

/* End of the scanning. */

static inline void
yy_close (void)
{
  fclose (yyin);
  free (yyfilename);
}

int
yywrap (void)
{
  return 1;
}


void
sheets_map_load (const char *filename)
{
  static int first_time = 1;
  token_t token;
  char *key = NULL;

  if (first_time)
    {
      first_time = 0;
      obstack_init (&string_stack);
    }

  yy_open (filename);

  while ((token = yylex ()))
    {
      switch (token)
	{
	case tKEY:
	  key = token_val;
	  break;

	case tGLOB:
	  if (!key)
	    error_at_line (1, 0, yyfilename, yylineno,
			   _("no key defined for `%s'"), quotearg (token_val));
	  sheets_map_add (token_val, false, insensitive_p, key);
	  break;

	case tFILE:
	  if (!key)
	    error_at_line (1, 0, yyfilename, yylineno,
			   _("no key defined for `%s'"), quotearg (token_val));
	  sheets_map_add (token_val, true, insensitive_p, key);
	  break;
	}
    }

  yy_close ();
}