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