1 %option nounput noinput 2 %{ 3 /* $OpenBSD: tokenizer.l,v 1.9 2017/06/15 13:48:42 bcallah 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$ 20 */ 21 #include "parser.h" 22 #include <assert.h> 23 #include <stdlib.h> 24 #include <errno.h> 25 #include <stdint.h> 26 #include <limits.h> 27 28 extern void m4_warnx(const char *, ...); 29 extern int mimic_gnu; 30 extern int32_t yylval; 31 32 int32_t number(void); 33 int32_t parse_radix(void); 34 extern int yylex(void); 35 36 #define YY_DECL int yylex(void) 37 %} 38 39 delim [ \t\n] 40 ws {delim}+ 41 hex 0[xX][0-9a-fA-F]+ 42 oct 0[0-7]* 43 dec [1-9][0-9]* 44 radix 0[rR][0-9]+:[0-9a-zA-Z]+ 45 46 %option noyywrap 47 48 %% 49 {ws} {/* just skip it */} 50 {hex}|{oct}|{dec} { yylval = number(); return(NUMBER); } 51 {radix} { if (mimic_gnu) { 52 yylval = parse_radix(); return(NUMBER); 53 } else { 54 return(ERROR); 55 } 56 } 57 "<=" { return(LE); } 58 ">=" { return(GE); } 59 "<<" { return(LSHIFT); } 60 ">>" { return(RSHIFT); } 61 "==" { return(EQ); } 62 "!=" { return(NE); } 63 "&&" { return(LAND); } 64 "||" { return(LOR); } 65 "**" { if (mimic_gnu) { return (EXPONENT); } } 66 . { return yytext[0]; } 67 %% 68 69 int32_t 70 number(void) 71 { 72 long l; 73 74 errno = 0; 75 l = strtol(yytext, NULL, 0); 76 if (((l == LONG_MAX || l == LONG_MIN) && errno == ERANGE) || 77 l > INT32_MAX || l < INT32_MIN) { 78 m4_warnx("numeric overflow in expr: %s", yytext); 79 } 80 return l; 81 } 82 83 int32_t 84 parse_radix(void) 85 { 86 long base; 87 char *next; 88 long l; 89 int d; 90 91 l = 0; 92 base = strtol(yytext+2, &next, 0); 93 if (base > 36 || next == NULL) { 94 m4_warnx("error in number %s", yytext); 95 } else { 96 next++; 97 while (*next != 0) { 98 if (*next >= '0' && *next <= '9') 99 d = *next - '0'; 100 else if (*next >= 'a' && *next <= 'z') 101 d = *next - 'a' + 10; 102 else { 103 assert(*next >= 'A' && *next <= 'Z'); 104 d = *next - 'A' + 10; 105 } 106 if (d >= base) { 107 m4_warnx("error in number %s", yytext); 108 return 0; 109 } 110 l = base * l + d; 111 next++; 112 } 113 } 114 return l; 115 } 116 117