File: lexppd.l

package info (click to toggle)
a2ps 1%3A4.14-2
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 13,328 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 (272 lines) | stat: -rw-r--r-- 6,880 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
%{ /* -*- c -*- */
/*
 * Lexer for PPD files.
 *
 * Copyright (c) 1988, 89, 90, 91, 92, 93 Miguel Santana
 * Copyright (c) 1995, 96, 97, 98 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.
 */

/*
 * $Id: lexppd.l,v 1.1.1.1.2.1 2007/12/29 01:58:19 mhatta Exp $
 */

#include "a2ps.h"
#include "lexppd.h"
#include "ppd.h"
#include "parseppd.h"
#include "routines.h"
#include "xobstack.h"
#include "message.h"
#include "pathwalk.h"

/* File currently parsed, and the path where to find PPD files. */
char * ppdfilename;
char * const *ppdpath;

int yylex PARAMS ((void));
void yyerror PARAMS ((const char *));

/* Initilizes the obstacks */
void ppdlex_initialize PARAMS ((void));

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

/* Stack to handle included PPD files. */

#define MAX_INCLUDE_DEPTH 10
static YY_BUFFER_STATE include_stack[MAX_INCLUDE_DEPTH];
static char *filename_stack[MAX_INCLUDE_DEPTH];
static int include_stack_ptr = 0;
%}

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


%x STATE_STRING
%x STATE_USTRING
%x STATE_INCLUDE

eol	\n|\r\n
comment	\*%.*{eol}
blank	[ \t\f]*
symbol	[^/#, \t\r\f\n\":()+]*
%%

{comment}	{ ; }
{blank}		{ ; }

\"		{ BEGIN STATE_STRING; }
\/		{ BEGIN STATE_USTRING; }
"*Include:"	{ BEGIN STATE_INCLUDE; }

"*DefaultFont"	{ return tDefaultFont; }
"*Font"		{ return tFont; }
"*ModelName"	{ return tModelName; }
"*NickName"	{ return tNickName; }

{symbol}	{  yylval.string = xstrdup (yytext);
		   return SYMBOL;
		}

.		{ return yytext[0]; }

{eol}		{ return EOL; }

<STATE_STRING>{		/* string of characters */
  \"		{ /* return the string */
    char * string;

    obstack_1grow (&string_stack, '\0');
    string = obstack_finish (&string_stack);

    obstack_free (&string_stack, string);

    BEGIN INITIAL;	/* Return to the regular scanning */
    yylval.string = xstrdup (string);

    return STRING;
  }

  \\[0-7]{1,3}		{
    int value = yytext[1] - '0';
    char *cursor = yytext + 2;

    while (*cursor)
      value = 8 * value + *cursor++ - '0';
    obstack_1grow (&string_stack, value);
  }

  \\x[0-9a-fA-F]{1,2}	{
    int value = 0;
    char *cursor = yytext + 2;

    while (*cursor)
      if (*cursor >= 'a' && *cursor <= 'f')
	value = 16 * value + *cursor++ - 'a' + 10;
      else if (*cursor >= 'A' && *cursor <= 'F')
	value = 16 * value + *cursor++ - 'A' + 10;
      else
	value = 16 * value + *cursor++ - '0';
    obstack_1grow (&string_stack, value);
  }

  \\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 are legal in string */
    obstack_grow (&string_stack, yytext, yyleng);
  }
}

<STATE_USTRING>{		/* string of characters */
  [:\n]		{ /* return the string */
    uchar * string;

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

    obstack_free (&string_stack, string);

    BEGIN INITIAL;	/* Return to the regular scanning */
    yylval.ustring = xustrdup (string);

    return USTRING;
  }

  \\[0-7]{1,3}		{
    int value = yytext[1] - '0';
    char *cursor = yytext + 2;

    while (*cursor)
      value = 8 * value + *cursor++ - '0';
    obstack_1grow (&string_stack, value);
  }

  \\x[0-9a-fA-F]{1,2}	{
    int value = 0;
    char *cursor = yytext + 2;

    while (*cursor)
      if (*cursor >= 'a' && *cursor <= 'f')
	value = 16 * value + *cursor++ - 'a' + 10;
      else if (*cursor >= 'A' && *cursor <= 'F')
	value = 16 * value + *cursor++ - 'A' + 10;
      else
	value = 16 * value + *cursor++ - '0';
    obstack_1grow (&string_stack, value);
  }

  \\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\\:]+		{	/* \n are legal in string */
    obstack_grow (&string_stack, yytext, yyleng);
  }
}

<STATE_INCLUDE>{	/* Including another PPD file. */

  [ \t]*      	{ ; } /* eat the whitespace */
  \"[^ \t\n]+\"   { /* got the include file name */
    if (include_stack_ptr >= MAX_INCLUDE_DEPTH)
      error (1, 0, _("too many includes"));

    include_stack[include_stack_ptr] = YY_CURRENT_BUFFER;
    filename_stack[include_stack_ptr++] = ppdfilename;

    /* Skip the first quote, kill the last one. */
    yytext ++;
    yytext [yyleng - 2] = 0;
    message (msg_file, (stderr, "%s:%d: includes %s\n",
			ppdfilename, ppdlineno, yytext));

    /* Don't put the suffix, Adobe says it's part of the name. */
    ppdfilename = xpw_find_included_file (ppdpath, ppdfilename,
					  yytext, NULL);
    yyin = xrfopen (ppdfilename);

    yy_switch_to_buffer (yy_create_buffer (yyin, YY_BUF_SIZE));

    BEGIN(INITIAL);
  }
}

<<EOF>> {
  message (msg_file, (stderr, "End of PPD file `%s'.\n", ppdfilename));
  if (--include_stack_ptr < 0)
    {
      yyterminate();
    }
  else
    {
      fclose (yyin);
      yy_delete_buffer (YY_CURRENT_BUFFER);
      XFREE (ppdfilename);
      ppdfilename = filename_stack[include_stack_ptr];
      yy_switch_to_buffer (include_stack[include_stack_ptr]);
      message (msg_file, (stderr, "Back to file `%s'.\n", ppdfilename));
    }
}

%%

int
yywrap (void)
{
  return 1;
}

/*
 * Initialize the obstacks
 */
void
ppdlex_initialize (void)
{
  static int first_time = 1;
  if (first_time)
    {
      first_time = 0;
      obstack_init (&string_stack);
    }
  /* Reset the include stack. */
  include_stack_ptr = 0;
}