tokenizer.l revision 1.3 1 1.1 christos %{
2 1.3 christos /* $NetBSD: tokenizer.l,v 1.3 2009/10/28 21:42:47 christos 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.3 christos __RCSID("$NetBSD: tokenizer.l,v 1.3 2009/10/28 21:42:47 christos 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.3 christos #define YY_NO_UNPUT
38 1.1 christos %}
39 1.1 christos
40 1.1 christos delim [ \t\n]
41 1.1 christos ws {delim}+
42 1.1 christos hex 0[xX][0-9a-fA-F]+
43 1.1 christos oct 0[0-7]*
44 1.1 christos dec [1-9][0-9]*
45 1.1 christos radix 0[rR][0-9]+:[0-9a-zA-Z]+
46 1.1 christos
47 1.1 christos %%
48 1.1 christos {ws} {/* just skip it */}
49 1.1 christos {hex}|{oct}|{dec} { yylval = number(); return(NUMBER); }
50 1.1 christos {radix} { if (mimic_gnu) {
51 1.1 christos yylval = parse_radix(); return(NUMBER);
52 1.1 christos } else {
53 1.1 christos return(ERROR);
54 1.1 christos }
55 1.1 christos }
56 1.1 christos "<=" { return(LE); }
57 1.1 christos ">=" { return(GE); }
58 1.1 christos "<<" { return(LSHIFT); }
59 1.1 christos ">>" { return(RSHIFT); }
60 1.1 christos "==" { return(EQ); }
61 1.1 christos "!=" { return(NE); }
62 1.1 christos "&&" { return(LAND); }
63 1.1 christos "||" { return(LOR); }
64 1.1 christos . { return yytext[0]; }
65 1.1 christos %%
66 1.1 christos
67 1.1 christos int32_t
68 1.1 christos number()
69 1.1 christos {
70 1.1 christos long l;
71 1.1 christos
72 1.1 christos errno = 0;
73 1.1 christos l = strtol(yytext, NULL, 0);
74 1.1 christos if (((l == LONG_MAX || l == LONG_MIN) && errno == ERANGE) ||
75 1.1 christos l > INT32_MAX || l < INT32_MIN) {
76 1.1 christos fprintf(stderr, "m4: numeric overflow in expr: %s\n", yytext);
77 1.1 christos }
78 1.1 christos return l;
79 1.1 christos }
80 1.1 christos
81 1.1 christos int32_t
82 1.1 christos parse_radix()
83 1.1 christos {
84 1.1 christos long base;
85 1.1 christos char *next;
86 1.1 christos long l;
87 1.1 christos
88 1.1 christos l = 0;
89 1.1 christos base = strtol(yytext+2, &next, 0);
90 1.1 christos if (base > 36 || next == NULL) {
91 1.1 christos fprintf(stderr, "m4: error in number %s\n", yytext);
92 1.1 christos } else {
93 1.1 christos next++;
94 1.1 christos while (*next != 0) {
95 1.1 christos if (*next >= '0' && *next <= '9')
96 1.1 christos l = base * l + *next - '0';
97 1.1 christos else if (*next >= 'a' && *next <= 'z')
98 1.1 christos l = base * l + *next - 'a' + 10;
99 1.1 christos else if (*next >= 'A' && *next <= 'Z')
100 1.1 christos l = base * l + *next - 'A' + 10;
101 1.1 christos next++;
102 1.1 christos }
103 1.1 christos }
104 1.1 christos return l;
105 1.1 christos }
106 1.1 christos
107