[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.9

1.1       espie       1: %{
1.9     ! bcallah     2: /* $OpenBSD: tokenizer.l,v 1.8 2012/04/12 17:00:11 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.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.1       espie      39:
                     40: %%
1.2       espie      41: {ws}                   {/* just skip it */}
                     42: {hex}|{oct}|{dec}      { yylval = number(); return(NUMBER); }
1.6       espie      43: {radix}                        { if (mimic_gnu) {
                     44:                                yylval = parse_radix(); return(NUMBER);
                     45:                          } else {
                     46:                                return(ERROR);
                     47:                          }
                     48:                        }
1.2       espie      49: "<="                   { return(LE); }
                     50: ">="                   { return(GE); }
                     51: "<<"                   { return(LSHIFT); }
                     52: ">>"                   { return(RSHIFT); }
                     53: "=="                   { return(EQ); }
                     54: "!="                   { return(NE); }
                     55: "&&"                   { return(LAND); }
                     56: "||"                   { return(LOR); }
1.8       espie      57: "**"                   { if (mimic_gnu) { return (EXPONENT); } }
1.2       espie      58: .                      { return yytext[0]; }
1.1       espie      59: %%
                     60:
                     61: int32_t
                     62: number()
                     63: {
                     64:        long l;
                     65:
                     66:        errno = 0;
                     67:        l = strtol(yytext, NULL, 0);
                     68:        if (((l == LONG_MAX || l == LONG_MIN) && errno == ERANGE) ||
1.9     ! bcallah    69:            l > INT32_MAX || l < INT32_MIN)
        !            70:                m4_warnx("numeric overflow in expr: %s", yytext);
1.1       espie      71:        return l;
1.6       espie      72: }
                     73:
                     74: int32_t
                     75: parse_radix()
                     76: {
                     77:        long base;
                     78:        char *next;
                     79:        long l;
1.7       espie      80:        int d;
1.1       espie      81:
1.6       espie      82:        l = 0;
                     83:        base = strtol(yytext+2, &next, 0);
                     84:        if (base > 36 || next == NULL) {
1.9     ! bcallah    85:                m4_warnx("error in number %s", yytext);
1.6       espie      86:        } else {
                     87:                next++;
                     88:                while (*next != 0) {
                     89:                        if (*next >= '0' && *next <= '9')
1.7       espie      90:                                d = *next - '0';
1.6       espie      91:                        else if (*next >= 'a' && *next <= 'z')
1.7       espie      92:                                d = *next - 'a' + 10;
                     93:                        else {
                     94:                                assert(*next >= 'A' && *next <= 'Z');
                     95:                                d = *next - 'A' + 10;
                     96:                        }
                     97:                        if (d >= base) {
1.9     ! bcallah    98:                                m4_warnx("error in number %s", yytext);
1.7       espie      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: