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