lex.c 13 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * Copyright 2011 Jacek Caban for CodeWeavers
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

19 20 21
#include "config.h"
#include "wine/port.h"

22
#include <assert.h>
23
#include <limits.h>
24 25 26 27 28 29 30 31 32

#include "vbscript.h"
#include "parse.h"
#include "parser.tab.h"

#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(vbscript);

33 34 35
static const WCHAR andW[] = {'a','n','d',0};
static const WCHAR byrefW[] = {'b','y','r','e','f',0};
static const WCHAR byvalW[] = {'b','y','v','a','l',0};
36
static const WCHAR callW[] = {'c','a','l','l',0};
37
static const WCHAR caseW[] = {'c','a','s','e',0};
38
static const WCHAR classW[] = {'c','l','a','s','s',0};
39
static const WCHAR constW[] = {'c','o','n','s','t',0};
40 41 42
static const WCHAR defaultW[] = {'d','e','f','a','u','l','t',0};
static const WCHAR dimW[] = {'d','i','m',0};
static const WCHAR doW[] = {'d','o',0};
43
static const WCHAR eachW[] = {'e','a','c','h',0};
44 45 46 47 48 49 50 51 52
static const WCHAR elseW[] = {'e','l','s','e',0};
static const WCHAR elseifW[] = {'e','l','s','e','i','f',0};
static const WCHAR emptyW[] = {'e','m','p','t','y',0};
static const WCHAR endW[] = {'e','n','d',0};
static const WCHAR eqvW[] = {'e','q','v',0};
static const WCHAR errorW[] = {'e','r','r','o','r',0};
static const WCHAR exitW[] = {'e','x','i','t',0};
static const WCHAR explicitW[] = {'e','x','p','l','i','c','i','t',0};
static const WCHAR falseW[] = {'f','a','l','s','e',0};
53
static const WCHAR forW[] = {'f','o','r',0};
54 55 56 57 58
static const WCHAR functionW[] = {'f','u','n','c','t','i','o','n',0};
static const WCHAR getW[] = {'g','e','t',0};
static const WCHAR gotoW[] = {'g','o','t','o',0};
static const WCHAR ifW[] = {'i','f',0};
static const WCHAR impW[] = {'i','m','p',0};
59
static const WCHAR inW[] = {'i','n',0};
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
static const WCHAR isW[] = {'i','s',0};
static const WCHAR letW[] = {'l','e','t',0};
static const WCHAR loopW[] = {'l','o','o','p',0};
static const WCHAR meW[] = {'m','e',0};
static const WCHAR modW[] = {'m','o','d',0};
static const WCHAR newW[] = {'n','e','w',0};
static const WCHAR nextW[] = {'n','e','x','t',0};
static const WCHAR notW[] = {'n','o','t',0};
static const WCHAR nothingW[] = {'n','o','t','h','i','n','g',0};
static const WCHAR nullW[] = {'n','u','l','l',0};
static const WCHAR onW[] = {'o','n',0};
static const WCHAR optionW[] = {'o','p','t','i','o','n',0};
static const WCHAR orW[] = {'o','r',0};
static const WCHAR privateW[] = {'p','r','i','v','a','t','e',0};
static const WCHAR propertyW[] = {'p','r','o','p','e','r','t','y',0};
static const WCHAR publicW[] = {'p','u','b','l','i','c',0};
static const WCHAR remW[] = {'r','e','m',0};
static const WCHAR resumeW[] = {'r','e','s','u','m','e',0};
78
static const WCHAR selectW[] = {'s','e','l','e','c','t',0};
79
static const WCHAR setW[] = {'s','e','t',0};
80
static const WCHAR stepW[] = {'s','t','e','p',0};
81 82 83
static const WCHAR stopW[] = {'s','t','o','p',0};
static const WCHAR subW[] = {'s','u','b',0};
static const WCHAR thenW[] = {'t','h','e','n',0};
84
static const WCHAR toW[] = {'t','o',0};
85 86 87 88 89
static const WCHAR trueW[] = {'t','r','u','e',0};
static const WCHAR untilW[] = {'u','n','t','i','l',0};
static const WCHAR wendW[] = {'w','e','n','d',0};
static const WCHAR whileW[] = {'w','h','i','l','e',0};
static const WCHAR xorW[] = {'x','o','r',0};
90 91 92 93 94

static const struct {
    const WCHAR *word;
    int token;
} keywords[] = {
95 96 97 98
    {andW,       tAND},
    {byrefW,     tBYREF},
    {byvalW,     tBYVAL},
    {callW,      tCALL},
99
    {caseW,      tCASE},
100
    {classW,     tCLASS},
101
    {constW,     tCONST},
102 103 104
    {defaultW,   tDEFAULT},
    {dimW,       tDIM},
    {doW,        tDO},
105
    {eachW,      tEACH},
106 107 108 109 110 111 112 113 114
    {elseW,      tELSE},
    {elseifW,    tELSEIF},
    {emptyW,     tEMPTY},
    {endW,       tEND},
    {eqvW,       tEQV},
    {errorW,     tERROR},
    {exitW,      tEXIT},
    {explicitW,  tEXPLICIT},
    {falseW,     tFALSE},
115
    {forW,       tFOR},
116 117 118 119 120
    {functionW,  tFUNCTION},
    {getW,       tGET},
    {gotoW,      tGOTO},
    {ifW,        tIF},
    {impW,       tIMP},
121
    {inW,        tIN},
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
    {isW,        tIS},
    {letW,       tLET},
    {loopW,      tLOOP},
    {meW,        tME},
    {modW,       tMOD},
    {newW,       tNEW},
    {nextW,      tNEXT},
    {notW,       tNOT},
    {nothingW,   tNOTHING},
    {nullW,      tNULL},
    {onW,        tON},
    {optionW,    tOPTION},
    {orW,        tOR},
    {privateW,   tPRIVATE},
    {propertyW,  tPROPERTY},
    {publicW,    tPUBLIC},
    {remW,       tREM},
    {resumeW,    tRESUME},
140
    {selectW,    tSELECT},
141
    {setW,       tSET},
142
    {stepW,      tSTEP},
143 144 145
    {stopW,      tSTOP},
    {subW,       tSUB},
    {thenW,      tTHEN},
146
    {toW,        tTO},
147 148 149 150 151
    {trueW,      tTRUE},
    {untilW,     tUNTIL},
    {wendW,      tWEND},
    {whileW,     tWHILE},
    {xorW,       tXOR}
152 153
};

154 155 156 157 158
static inline BOOL is_identifier_char(WCHAR c)
{
    return isalnumW(c) || c == '_';
}

159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
static int check_keyword(parser_ctx_t *ctx, const WCHAR *word)
{
    const WCHAR *p1 = ctx->ptr;
    const WCHAR *p2 = word;
    WCHAR c;

    while(p1 < ctx->end && *p2) {
        c = tolowerW(*p1);
        if(c != *p2)
            return c - *p2;
        p1++;
        p2++;
    }

    if(*p2 || (p1 < ctx->end && is_identifier_char(*p1)))
        return 1;

    ctx->ptr = p1;
    return 0;
}

static int check_keywords(parser_ctx_t *ctx)
{
    int min = 0, max = sizeof(keywords)/sizeof(keywords[0])-1, r, i;

    while(min <= max) {
        i = (min+max)/2;

        r = check_keyword(ctx, keywords[i].word);
        if(!r)
            return keywords[i].token;

        if(r > 0)
            min = i+1;
        else
            max = i-1;
    }

    return 0;
}

200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
static int parse_identifier(parser_ctx_t *ctx, const WCHAR **ret)
{
    const WCHAR *ptr = ctx->ptr++;
    WCHAR *str;
    int len;

    while(ctx->ptr < ctx->end && is_identifier_char(*ctx->ptr))
        ctx->ptr++;
    len = ctx->ptr-ptr;

    str = parser_alloc(ctx, (len+1)*sizeof(WCHAR));
    if(!str)
        return 0;

    memcpy(str, ptr, (len+1)*sizeof(WCHAR));
    str[len] = 0;
    *ret = str;
    return tIdentifier;
}

220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
static int parse_string_literal(parser_ctx_t *ctx, const WCHAR **ret)
{
    const WCHAR *ptr = ++ctx->ptr;
    WCHAR *rptr;
    int len = 0;

    while(ctx->ptr < ctx->end) {
        if(*ctx->ptr == '\n') {
            FIXME("newline inside string literal\n");
            return 0;
        }

       if(*ctx->ptr == '"') {
            if(ctx->ptr[1] != '"')
                break;
            len--;
            ctx->ptr++;
        }
        ctx->ptr++;
    }

    if(ctx->ptr == ctx->end) {
        FIXME("unterminated string literal\n");
        return 0;
    }

    len += ctx->ptr-ptr;

    *ret = rptr = parser_alloc(ctx, (len+1)*sizeof(WCHAR));
    if(!rptr)
        return 0;

    while(ptr < ctx->ptr) {
        if(*ptr == '"')
            ptr++;
        *rptr++ = *ptr++;
    }

    *rptr = 0;
    ctx->ptr++;
    return tString;
}

263 264
static int parse_numeric_literal(parser_ctx_t *ctx, void **ret)
{
265
    BOOL use_int = TRUE;
266 267
    LONGLONG d = 0, hlp;
    int exp = 0;
268
    double r;
269 270 271 272

    if(*ctx->ptr == '0' && !('0' <= ctx->ptr[1] && ctx->ptr[1] <= '9') && ctx->ptr[1] != '.')
        return *ctx->ptr++;

273 274 275 276 277 278 279 280 281 282 283 284 285
    while(ctx->ptr < ctx->end && isdigitW(*ctx->ptr)) {
        hlp = d*10 + *(ctx->ptr++) - '0';
        if(d>MAXLONGLONG/10 || hlp<0) {
            exp++;
            break;
        }
        else
            d = hlp;
    }
    while(ctx->ptr < ctx->end && isdigitW(*ctx->ptr)) {
        exp++;
        ctx->ptr++;
    }
286

287 288
    if(*ctx->ptr == '.') {
        use_int = FALSE;
289 290 291 292 293 294 295 296 297 298 299 300
        ctx->ptr++;

        while(ctx->ptr < ctx->end && isdigitW(*ctx->ptr)) {
            hlp = d*10 + *(ctx->ptr++) - '0';
            if(d>MAXLONGLONG/10 || hlp<0)
                break;

            d = hlp;
            exp--;
        }
        while(ctx->ptr < ctx->end && isdigitW(*ctx->ptr))
            ctx->ptr++;
301 302
    }

303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
    if(*ctx->ptr == 'e' || *ctx->ptr == 'E') {
        int e = 0, sign = 1;

        if(*++ctx->ptr == '-') {
            ctx->ptr++;
            sign = -1;
        }

        if(!isdigitW(*ctx->ptr)) {
            FIXME("Invalid numeric literal\n");
            return 0;
        }

        use_int = FALSE;

        do {
            e = e*10 + *(ctx->ptr++) - '0';
            if(sign == -1 && -e+exp < -(INT_MAX/100)) {
                /* The literal will be rounded to 0 anyway. */
                while(isdigitW(*ctx->ptr))
                    ctx->ptr++;
                *(double*)ret = 0;
                return tDouble;
            }

            if(sign*e + exp > INT_MAX/100) {
                FIXME("Invalid numeric literal\n");
                return 0;
            }
        } while(isdigitW(*ctx->ptr));

        exp += sign*e;
    }

    if(use_int && (LONG)d == d) {
        LONG l = d;
        *(LONG*)ret = l;
        return (short)l == l ? tShort : tLong;
    }

    r = exp>=0 ? d*pow(10, exp) : d/pow(10, -exp);
    if(isinf(r)) {
        FIXME("Invalid numeric literal\n");
        return 0;
    }

    *(double*)ret = r;
350 351 352
    return tDouble;
}

353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
static int hex_to_int(WCHAR c)
{
    if('0' <= c && c <= '9')
        return c-'0';
    if('a' <= c && c <= 'f')
        return c+10-'a';
    if('A' <= c && c <= 'F')
        return c+10-'A';
    return -1;
}

static int parse_hex_literal(parser_ctx_t *ctx, LONG *ret)
{
    const WCHAR *begin = ctx->ptr;
    LONG l = 0, d;

    while((d = hex_to_int(*++ctx->ptr)) != -1)
        l = l*16 + d;

372
    if(begin + 9 /* max digits+1 */ < ctx->ptr || (*ctx->ptr != '&' && is_identifier_char(*ctx->ptr))) {
373 374 375 376
        FIXME("invalid literal\n");
        return 0;
    }

377 378 379
    if(*ctx->ptr == '&')
        ctx->ptr++;

380 381 382 383
    *ret = l;
    return (short)l == l ? tShort : tLong;
}

384 385 386 387 388 389
static void skip_spaces(parser_ctx_t *ctx)
{
    while(*ctx->ptr == ' ' || *ctx->ptr == '\t' || *ctx->ptr == '\r')
        ctx->ptr++;
}

390 391 392 393 394 395 396 397 398 399
static int comment_line(parser_ctx_t *ctx)
{
    ctx->ptr = strchrW(ctx->ptr, '\n');
    if(ctx->ptr)
        ctx->ptr++;
    else
        ctx->ptr = ctx->end;
    return tNL;
}

400 401 402 403
static int parse_next_token(void *lval, parser_ctx_t *ctx)
{
    WCHAR c;

404
    skip_spaces(ctx);
405 406 407 408
    if(ctx->ptr == ctx->end)
        return ctx->last_token == tNL ? tEOF : tNL;

    c = *ctx->ptr;
409

410 411 412
    if('0' <= c && c <= '9')
        return parse_numeric_literal(ctx, lval);

413 414 415 416
    if(isalphaW(c)) {
        int ret = check_keywords(ctx);
        if(!ret)
            return parse_identifier(ctx, lval);
417 418 419
        if(ret != tREM)
            return ret;
        c = '\'';
420
    }
421

422 423 424 425 426
    switch(c) {
    case '\n':
        ctx->ptr++;
        return tNL;
    case '\'':
427
        return comment_line(ctx);
428
    case ':':
429 430 431 432 433 434 435 436 437
    case ')':
    case ',':
    case '=':
    case '+':
    case '*':
    case '/':
    case '^':
    case '\\':
    case '.':
438
    case '_':
439
        return *ctx->ptr++;
440 441 442 443 444
    case '-':
        if(ctx->is_html && ctx->ptr[1] == '-' && ctx->ptr[2] == '>')
            return comment_line(ctx);
        ctx->ptr++;
        return '-';
445 446 447 448 449 450 451 452 453 454 455 456
    case '(':
        /* NOTE:
         * We resolve empty brackets in lexer instead of parser to avoid complex conflicts
         * in call statement special case |f()| without 'call' keyword
         */
        ctx->ptr++;
        skip_spaces(ctx);
        if(*ctx->ptr == ')') {
            ctx->ptr++;
            return tEMPTYBRACKETS;
        }
        return '(';
457 458
    case '"':
        return parse_string_literal(ctx, lval);
459 460 461 462
    case '&':
        if(*++ctx->ptr == 'h' || *ctx->ptr == 'H')
            return parse_hex_literal(ctx, lval);
        return '&';
463 464 465 466 467 468 469 470
    case '<':
        switch(*++ctx->ptr) {
        case '>':
            ctx->ptr++;
            return tNEQ;
        case '=':
            ctx->ptr++;
            return tLTEQ;
471 472 473
        case '!':
            if(ctx->is_html && ctx->ptr[1] == '-' && ctx->ptr[2] == '-')
                return comment_line(ctx);
474 475 476 477 478 479 480 481
        }
        return '<';
    case '>':
        if(*++ctx->ptr == '=') {
            ctx->ptr++;
            return tGTEQ;
        }
        return '>';
482 483 484 485 486
    default:
        FIXME("Unhandled char %c in %s\n", *ctx->ptr, debugstr_w(ctx->ptr));
    }

    return 0;
487 488 489 490 491 492 493 494
}

int parser_lex(void *lval, parser_ctx_t *ctx)
{
    int ret;

    while(1) {
        ret = parse_next_token(lval, ctx);
495 496 497 498 499 500 501 502 503
        if(ret == '_') {
            skip_spaces(ctx);
            if(*ctx->ptr != '\n') {
                FIXME("'_' not followed by newline\n");
                return 0;
            }
            ctx->ptr++;
            continue;
        }
504 505 506 507 508 509 510 511
        if(ret != tNL || ctx->last_token != tNL)
            break;

        ctx->last_nl = ctx->ptr-ctx->code;
    }

    return (ctx->last_token = ret);
}