tokenizer.l revision 1.2 1 %{
2 /* $NetBSD: tokenizer.l,v 1.2 2009/10/26 21:11:28 christos Exp $ */
3 /* $OpenBSD: tokenizer.l,v 1.6 2008/08/21 21:00:14 espie Exp $ */
4 /*
5 * Copyright (c) 2004 Marc Espie <espie (at) 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 #if HAVE_NBTOOL_CONFIG_H
20 #include "nbtool_config.h"
21 #endif
22 #include "parser.h"
23 __RCSID("$NetBSD: tokenizer.l,v 1.2 2009/10/26 21:11:28 christos Exp $");
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 extern int yylex(void);
32 extern int yywrap(void);
33
34 int32_t number(void);
35 int32_t parse_radix(void);
36 %}
37
38 delim [ \t\n]
39 ws {delim}+
40 hex 0[xX][0-9a-fA-F]+
41 oct 0[0-7]*
42 dec [1-9][0-9]*
43 radix 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 . { return yytext[0]; }
63 %%
64
65 int32_t
66 number()
67 {
68 long l;
69
70 errno = 0;
71 l = strtol(yytext, NULL, 0);
72 if (((l == LONG_MAX || l == LONG_MIN) && errno == ERANGE) ||
73 l > INT32_MAX || l < INT32_MIN) {
74 fprintf(stderr, "m4: numeric overflow in expr: %s\n", yytext);
75 }
76 return l;
77 }
78
79 int32_t
80 parse_radix()
81 {
82 long base;
83 char *next;
84 long l;
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 l = base * l + *next - '0';
95 else if (*next >= 'a' && *next <= 'z')
96 l = base * l + *next - 'a' + 10;
97 else if (*next >= 'A' && *next <= 'Z')
98 l = base * l + *next - 'A' + 10;
99 next++;
100 }
101 }
102 return l;
103 }
104
105