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