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