[BACK]Return to tokenizer.l CVS log [TXT][DIR] Up to [local] / src / usr.bin / m4

Annotation of src/usr.bin/m4/tokenizer.l, Revision 1.10

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