xref: /minix/usr.bin/m4/tokenizer.l (revision 71c7dcb9)
1 %{
2 /* $NetBSD: tokenizer.l,v 1.6 2012/03/20 20:34:58 matt Exp $ */
3 /* $OpenBSD: tokenizer.l,v 1.6 2008/08/21 21:00:14 espie Exp $ */
4 /*
5  * Copyright (c) 2004 Marc Espie <espie@cvs.openbsd.org>
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 #if HAVE_NBTOOL_CONFIG_H
20 #include "nbtool_config.h"
21 #endif
22 #include "parser.h"
23 __RCSID("$NetBSD: tokenizer.l,v 1.6 2012/03/20 20:34:58 matt Exp $");
24 #include <stdlib.h>
25 #include <errno.h>
26 #include <stdint.h>
27 #include <limits.h>
28 
29 extern int mimic_gnu;
30 extern int32_t yylval;
31 extern int yylex(void);
32 extern int yywrap(void);
33 
34 int32_t number(void);
35 int32_t parse_radix(void);
36 
37 %}
38 
39 %option nounput
40 %option noinput
41 
42 delim 	[ \t\n]
43 ws	{delim}+
44 hex	0[xX][0-9a-fA-F]+
45 oct	0[0-7]*
46 dec	[1-9][0-9]*
47 radix	0[rR][0-9]+:[0-9a-zA-Z]+
48 
49 %%
50 {ws}			{/* just skip it */}
51 {hex}|{oct}|{dec}	{ yylval = number(); return(NUMBER); }
52 {radix}			{ if (mimic_gnu) {
53 				yylval = parse_radix(); return(NUMBER);
54 			  } else {
55 			  	return(ERROR);
56 			  }
57 			}
58 "<="			{ return(LE); }
59 ">="			{ return(GE); }
60 "<<"			{ return(LSHIFT); }
61 ">>"			{ return(RSHIFT); }
62 "=="			{ return(EQ); }
63 "!="			{ return(NE); }
64 "&&"			{ return(LAND); }
65 "||"			{ return(LOR); }
66 .			{ return yytext[0]; }
67 %%
68 
69 int32_t
70 number(void)
71 {
72 	long l;
73 
74 	errno = 0;
75 	l = strtol(yytext, NULL, 0);
76 	if (((l == LONG_MAX || l == LONG_MIN) && errno == ERANGE) ||
77 	    l > INT32_MAX || l < INT32_MIN) {
78 		fprintf(stderr, "m4: numeric overflow in expr: %s\n", yytext);
79 	}
80 	return l;
81 }
82 
83 int32_t
parse_radix(void)84 parse_radix(void)
85 {
86 	long base;
87 	char *next;
88 	long l;
89 
90 	l = 0;
91 	base = strtol(yytext+2, &next, 0);
92 	if (base > 36 || next == NULL) {
93 		fprintf(stderr, "m4: error in number %s\n", yytext);
94 	} else {
95 		next++;
96 		while (*next != 0) {
97 			if (*next >= '0' && *next <= '9')
98 				l = base * l + *next - '0';
99 			else if (*next >= 'a' && *next <= 'z')
100 				l = base * l + *next - 'a' + 10;
101 			else if (*next >= 'A' && *next <= 'Z')
102 				l = base * l + *next - 'A' + 10;
103 			next++;
104 		}
105 	}
106 	return l;
107 }
108 
109