xref: /freebsd/usr.bin/m4/tokenizer.l (revision bb15ca603fa442c72dde3f3cb8b46db6970e3950)
1 %option nounput noinput
2 %{
3 /* $OpenBSD: tokenizer.l,v 1.7 2010/03/22 20:40:44 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  * $FreeBSD$
20  */
21 #include "parser.h"
22 #include <assert.h>
23 #include <stdlib.h>
24 #include <errno.h>
25 #include <stdint.h>
26 #include <limits.h>
27 
28 extern int mimic_gnu;
29 extern int32_t yylval;
30 
31 int32_t number(void);
32 int32_t parse_radix(void);
33 extern int yylex(void);
34 %}
35 
36 delim 	[ \t\n]
37 ws	{delim}+
38 hex	0[xX][0-9a-fA-F]+
39 oct	0[0-7]*
40 dec	[1-9][0-9]*
41 radix	0[rR][0-9]+:[0-9a-zA-Z]+
42 
43 %%
44 {ws}			{/* just skip it */}
45 {hex}|{oct}|{dec}	{ yylval = number(); return(NUMBER); }
46 {radix}			{ if (mimic_gnu) {
47 				yylval = parse_radix(); return(NUMBER);
48 			  } else {
49 			  	return(ERROR);
50 			  }
51 			}
52 "<="			{ return(LE); }
53 ">="			{ return(GE); }
54 "<<"			{ return(LSHIFT); }
55 ">>"			{ return(RSHIFT); }
56 "=="			{ return(EQ); }
57 "!="			{ return(NE); }
58 "&&"			{ return(LAND); }
59 "||"			{ return(LOR); }
60 .			{ return yytext[0]; }
61 %%
62 
63 int32_t
64 number(void)
65 {
66 	long l;
67 
68 	errno = 0;
69 	l = strtol(yytext, NULL, 0);
70 	if (((l == LONG_MAX || l == LONG_MIN) && errno == ERANGE) ||
71 	    l > INT32_MAX || l < INT32_MIN) {
72 		fprintf(stderr, "m4: numeric overflow in expr: %s\n", yytext);
73 	}
74 	return l;
75 }
76 
77 int32_t
78 parse_radix(void)
79 {
80 	long base;
81 	char *next;
82 	long l;
83 	int d;
84 
85 	l = 0;
86 	base = strtol(yytext+2, &next, 0);
87 	if (base > 36 || next == NULL) {
88 		fprintf(stderr, "m4: error in number %s\n", yytext);
89 	} else {
90 		next++;
91 		while (*next != 0) {
92 			if (*next >= '0' && *next <= '9')
93 				d = *next - '0';
94 			else if (*next >= 'a' && *next <= 'z')
95 				d = *next - 'a' + 10;
96 			else {
97 				assert(*next >= 'A' && *next <= 'Z');
98 				d = *next - 'A' + 10;
99 			}
100 			if (d >= base) {
101 				fprintf(stderr,
102 				    "m4: error in number %s\n", yytext);
103 				return 0;
104 			}
105 			l = base * l + d;
106 			next++;
107 		}
108 	}
109 	return l;
110 }
111 
112