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