a32cfca73ddbb139b12b82151454178c6d3059c4
[dragonfly.git] / usr.bin / m4 / tokenizer.l
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: src/usr.bin/m4/tokenizer.l,v 1.3 2012/11/17 01:54:24 svnexp Exp $
20  */
21 #pragma GCC diagnostic ignored "-Wsign-compare"
22 #include "parser.h"
23 #include <assert.h>
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
32 int32_t number(void);
33 int32_t parse_radix(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 "**"                    { if (mimic_gnu) { return (EXPONENT); } }
61 .                       { return yytext[0]; }
62 %%
63
64 int32_t
65 number(void)
66 {
67         long l;
68
69         errno = 0;
70         l = strtol(yytext, NULL, 0);
71         if (((l == LONG_MAX || l == LONG_MIN) && errno == ERANGE) ||
72             l > INT32_MAX || l < INT32_MIN) {
73                 fprintf(stderr, "m4: numeric overflow in expr: %s\n", yytext);
74         }
75         return l;
76 }
77
78 int32_t
79 parse_radix(void)
80 {
81         long base;
82         char *next;
83         long l;
84         int d;
85
86         l = 0;
87         base = strtol(yytext + 2, &next, 0);
88         if (base > 36 || next == NULL) {
89                 fprintf(stderr, "m4: error in number %s\n", yytext);
90         } else {
91                 next++;
92                 while (*next != 0) {
93                         if (*next >= '0' && *next <= '9')
94                                 d = *next - '0';
95                         else if (*next >= 'a' && *next <= 'z')
96                                 d = *next - 'a' + 10;
97                         else {
98                                 assert(*next >= 'A' && *next <= 'Z');
99                                 d = *next - 'A' + 10;
100                         }
101                         if (d >= base) {
102                                 fprintf(stderr,
103                                     "m4: error in number %s\n", yytext);
104                                 return 0;
105                         }
106                         l = base * l + d;
107                         next++;
108                 }
109         }
110         return l;
111 }