File: scanner.l

package info (click to toggle)
mgp 1.13a+upstream20090219-11
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 4,128 kB
  • sloc: ansic: 32,387; sh: 4,295; lisp: 1,405; yacc: 945; lex: 264; makefile: 158; perl: 117; awk: 9
file content (264 lines) | stat: -rw-r--r-- 7,666 bytes parent folder | download | duplicates (4)
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
%{
/*
 * Copyright (C) 1997 and 1998 WIDE Project.  All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the project nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */
/*
 * $Id: scanner.l,v 1.28 2007/11/24 17:47:28 nishida Exp $
 */
/*
 * partly derived from lbl libpcap source code, which has the following
 * copyright notice:
 */
/*
 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996
 *	The Regents of the University of California.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that: (1) source code distributions
 * retain the above copyright notice and this paragraph in its entirety, (2)
 * distributions including binary code include the above copyright notice and
 * this paragraph in its entirety in the documentation or other materials
 * provided with the distribution, and (3) all advertising materials mentioning
 * features or use of this software display the following acknowledgement:
 * ``This product includes software developed by the University of California,
 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
 * the University nor the names of its contributors may be used to endorse
 * or promote products derived from this software without specific prior
 * written permission.
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 */

#include "mgp.h"
#include "tokdefs.h"

static int stoi __P((char *));
static inline int xdtoi __P((int));

#ifdef FLEX_SCANNER
#define YY_NO_UNPUT
#undef YY_INPUT
#define YY_INPUT(buf, result, max)\
 {\
	char *src = in_buffer;\
	int i;\
\
	if (*src == 0)\
		result = YY_NULL;\
	else {\
		for (i = 0; *src && i < max; ++i)\
			buf[i] = *src++;\
		in_buffer += i;\
		result = i;\
	}\
 }
#else
#undef getc
#define getc(fp)  (*in_buffer == 0 ? EOF : *in_buffer++)
#endif

/*#define yylval mgp_lval*/
extern YYSTYPE yylval;
extern int n_errors;
#ifdef HAVE_STDARG_H
/* GCC complains if we declare this function in traditional style */
extern void yyerror(char *, ...);
#else
extern void yyerror(char *);
#endif

static char *in_buffer;
%}

N	(-[0-9]+|[0-9]+|(0x|0X)[0-9A-Fa-f]+)
JISIN	(\033$B|\033$@)
ASCIIIN	(\033\(B|\033\(J)
ASTR	[\040-\176]*
ASTRN	[\040\041\043-\176]*
JSTR	{JISIN}([\041-\176][\041-\176])*{ASCIIIN}
XSTR	[\240-\377]*

%a 6000
%p 4000
%o 8000

%%
(NOOP|noop)		return KW_NOOP;
(DEFAULT|default)	return KW_DEFAULT;
(TAB|tab)		return KW_TAB;
(SIZE|size)		return KW_SIZE;
(FORE|fore)		return KW_FORE;
(BACK|back)		return KW_BACK;
(LEFT|left)		return KW_LEFT;
(LEFTFILL|leftfill)	return KW_LEFTFILL;
(CENTER|center)		return KW_CENTER;
(RIGHT|right)		return KW_RIGHT;
(SHRINK|shrink)		return KW_SHRINK;
(LCUTIN|lcutin)		return KW_LCUTIN;
(RCUTIN|rcutin)		return KW_RCUTIN;
(CONT|cont)		return KW_CONT;
(NODEFAULT|nodefault)	return KW_NODEF;
(XFONT|xfont)		return KW_XFONT;
(VFONT|vfont)		return KW_VFONT;
(TFONT|tfont)		return KW_TFONT;
(TMFONT|tmfont)		return KW_TMFONT;
(IMAGE|image)		return KW_IMAGE;
(NEWIMAGE|newimage)	return KW_NEWIMAGE;
(BIMAGE|bimage)		return KW_BIMAGE;
(PAGE|page)		return KW_PAGE;
(HGAP|hgap)		return KW_HGAP;
(VGAP|vgap)		return KW_VGAP;
(GAP|gap)		return KW_GAP;
(PAUSE|pause)		return KW_PAUSE;
(PREFIX|prefix)		return KW_PREFIX;
(AGAIN|again)		return KW_AGAIN;
(CCOLOR|ccolor)		return KW_CCOLOR;
(BAR|bar)		return KW_BAR;
(INCLUDE|include)	return KW_INCLUDE;
(BGRAD|bgrad)		return KW_BGRAD;
(TEXT|text)		return KW_TEXT;
(LINESTART|linestart)	return KW_LINESTART;
(LINEEND|lineend)	return KW_LINEEND;
(MARK|mark)		return KW_MARK;
(SYSTEM|system)		return KW_SYSTEM;
(XSYSTEM|xsystem)	return KW_XSYSTEM;
(TSYSTEM|tsystem)	return KW_TSYSTEM;
(FILTER|filter)		return KW_FILTER;
(ENDFILTER|endfilter)	return KW_ENDFILTER;
(BQUALITY|bquality)	return KW_QUALITY;
(ICON|icon)		return KW_ICON;
(VFCAP|vfcap)		return KW_VFCAP;
(TFDIR|tfdir)		return KW_TFDIR;
(DEFFONT|deffont)	return KW_DEFFONT;
(TFONT0|tfont0)		return KW_TFONT0;
(TMFONT0|tmfont0)	return KW_TMFONT0;
(FONT|font)		return KW_FONT;
(EMBED|embed)		return KW_EMBED;
(ENDEMBED|endembed)	return KW_ENDEMBED;
(CHARSET|charset)	return KW_CHARSET;
(PCACHE|pcache)	return KW_PCACHE;
(ANIM|anim)	return KW_ANIM;
(VALIGN|valign)	return KW_VALIGN;
(AREA|area)	return KW_AREA;
(OPAQUE|opaque)	return KW_OPAQUE;
(SUP|sup)	return KW_SUP;
(SUB|sub)	return KW_SUB;
(SETSUP|setsup)	return KW_SETSUP;
(M17N|m17n)	return KW_M17N;
(PSFONT|psfont) return KW_PSFONT;

[ \n\t]		;
","		return COMMA;
[0-9][0-9]*x[0-9][0-9]* { yylval.s = strdup((char *)yytext); return WINSIZ; }
{N}		{ yylval.i = stoi((char *)yytext); return NUM; }
[0-9][0-9]*\.[0-9][0-9]*	{ yylval.d = atof((char *)yytext); return DOUBLE; }
[-_.A-Za-z][-_.<>A-Za-z0-9]*	{ yylval.s = strdup((char *)yytext); return ID; }
\"({XSTR}|{ASTRN})({JSTR}{ASTRN}|{XSTR})*\" {
			yylval.s = strdup((char *)yytext) + 1;
			if (strlen(yylval.s))
				yylval.s[strlen(yylval.s) - 1] = '\0';
			return STR;
		}
\"({XSTR}|{ASTRN})*\" {
			if (mgp_flag & FL_NOFORK) {
		  		yyerror("invalid character <%c>", *(char *)yytext);
			} else {
				yylval.s = strdup((char *)yytext) + 1;
				if (strlen(yylval.s))
					yylval.s[strlen(yylval.s) - 1] = '\0';
				return STR;
			}	
		}
.		{
		  yyerror("invalid character <%c>", *(char *)yytext);
		}
%%

void
lex_init(buf)
	char *buf;
{
	n_errors = 0;
	in_buffer = buf;
}

/*
 * Also define a yywrap.  Note that if we're using flex, it will
 * define a macro to map this identifier to pcap_wrap.
 */
int
yywrap()
{
	return 1;
}

/* Hex digit to integer. */
static inline int
xdtoi(c)
	register int c;
{
	if (isdigit(c))
		return c - '0';
	else if (islower(c))
		return c - 'a' + 10;
	else
		return c - 'A' + 10;
}

/*
 * Convert string to integer.  Just like atoi(), but checks for
 * preceding 0x or 0 and uses hex or octal instead of decimal.
 */
static int
stoi(s)
	char *s;
{
	int base = 10;
	int n = 0;
	int sign;

	sign = 1;
	if (*s == '0') {
		if (s[1] == 'x' || s[1] == 'X') {
			s += 2;
			base = 16;
		}
		else {
			base = 8;
			s += 1;
		}
	} else if (*s == '-') {
		sign = -1;
		s++;
	}
	while (*s)
		n = n * base + xdtoi(*s++);

	return n * sign;
}