msvcrt/tests: Remove a space before a '\n'.
[wine/gsoc-2012-control.git] / dlls / jscript / lex.c
bloba937804da7a7fd7b490cb14fd7d763bb0e79676e
1 /*
2 * Copyright 2008 Jacek Caban for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include <math.h>
21 #include "jscript.h"
22 #include "activscp.h"
23 #include "objsafe.h"
24 #include "engine.h"
26 #include "parser.tab.h"
28 #include "wine/debug.h"
29 #include "wine/unicode.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
33 static const WCHAR breakW[] = {'b','r','e','a','k',0};
34 static const WCHAR caseW[] = {'c','a','s','e',0};
35 static const WCHAR catchW[] = {'c','a','t','c','h',0};
36 static const WCHAR continueW[] = {'c','o','n','t','i','n','u','e',0};
37 static const WCHAR defaultW[] = {'d','e','f','a','u','l','t',0};
38 static const WCHAR deleteW[] = {'d','e','l','e','t','e',0};
39 static const WCHAR doW[] = {'d','o',0};
40 static const WCHAR elseW[] = {'e','l','s','e',0};
41 static const WCHAR falseW[] = {'f','a','l','s','e',0};
42 static const WCHAR finallyW[] = {'f','i','n','a','l','l','y',0};
43 static const WCHAR forW[] = {'f','o','r',0};
44 static const WCHAR functionW[] = {'f','u','n','c','t','i','o','n',0};
45 static const WCHAR ifW[] = {'i','f',0};
46 static const WCHAR inW[] = {'i','n',0};
47 static const WCHAR instanceofW[] = {'i','n','s','t','a','n','c','e','o','f',0};
48 static const WCHAR newW[] = {'n','e','w',0};
49 static const WCHAR nullW[] = {'n','u','l','l',0};
50 static const WCHAR returnW[] = {'r','e','t','u','r','n',0};
51 static const WCHAR switchW[] = {'s','w','i','t','c','h',0};
52 static const WCHAR thisW[] = {'t','h','i','s',0};
53 static const WCHAR throwW[] = {'t','h','r','o','w',0};
54 static const WCHAR trueW[] = {'t','r','u','e',0};
55 static const WCHAR tryW[] = {'t','r','y',0};
56 static const WCHAR typeofW[] = {'t','y','p','e','o','f',0};
57 static const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d',0};
58 static const WCHAR varW[] = {'v','a','r',0};
59 static const WCHAR voidW[] = {'v','o','i','d',0};
60 static const WCHAR whileW[] = {'w','h','i','l','e',0};
61 static const WCHAR withW[] = {'w','i','t','h',0};
63 static const struct {
64 const WCHAR *word;
65 int token;
66 } keywords[] = {
67 {breakW, kBREAK},
68 {caseW, kCASE},
69 {catchW, kCATCH},
70 {continueW, kCONTINUE},
71 {defaultW, kDEFAULT},
72 {deleteW, kDELETE},
73 {doW, kDO},
74 {elseW, kELSE},
75 {falseW, kFALSE},
76 {finallyW, kFINALLY},
77 {forW, kFOR},
78 {functionW, kFUNCTION},
79 {ifW, kIF},
80 {inW, kIN},
81 {instanceofW, kINSTANCEOF},
82 {newW, kNEW},
83 {nullW, kNULL},
84 {returnW, kRETURN},
85 {switchW, kSWITCH},
86 {thisW, kTHIS},
87 {throwW, kTHROW},
88 {trueW, kTRUE},
89 {tryW, kTRY},
90 {typeofW, kTYPEOF},
91 {undefinedW, kUNDEFINED},
92 {varW, kVAR},
93 {voidW, kVOID},
94 {whileW, kWHILE},
95 {withW, kWITH}
98 static int lex_error(parser_ctx_t *ctx, HRESULT hres)
100 ctx->hres = hres;
101 return -1;
104 static int check_keyword(parser_ctx_t *ctx, const WCHAR *word, const WCHAR **lval)
106 const WCHAR *p1 = ctx->ptr;
107 const WCHAR *p2 = word;
109 while(p1 < ctx->end && *p2) {
110 if(*p1 != *p2)
111 return *p1 - *p2;
112 p1++;
113 p2++;
116 if(*p2 || (p1 < ctx->end && isalnumW(*p1)))
117 return 1;
119 *lval = ctx->ptr;
120 ctx->ptr = p1;
121 return 0;
124 /* ECMA-262 3rd Edition 7.3 */
125 static BOOL is_endline(WCHAR c)
127 return c == '\n' || c == '\r' || c == 0x2028 || c == 0x2029;
130 static BOOL is_identifier_char(WCHAR c)
132 return isalnumW(c) || c == '$' || c == '_' || c == '\\';
135 static int hex_to_int(WCHAR c)
137 if('0' <= c && c <= '9')
138 return c-'0';
140 if('a' <= c && c <= 'f')
141 return c-'a'+10;
143 if('A' <= c && c <= 'F')
144 return c-'A'+10;
146 return -1;
149 static int check_keywords(parser_ctx_t *ctx, const WCHAR **lval)
151 int min = 0, max = sizeof(keywords)/sizeof(keywords[0])-1, r, i;
153 while(min <= max) {
154 i = (min+max)/2;
156 r = check_keyword(ctx, keywords[i].word, lval);
157 if(!r)
158 return keywords[i].token;
160 if(r > 0)
161 min = i+1;
162 else
163 max = i-1;
166 return 0;
169 static void skip_spaces(parser_ctx_t *ctx)
171 while(ctx->ptr < ctx->end && isspaceW(*ctx->ptr)) {
172 if(is_endline(*ctx->ptr++))
173 ctx->nl = TRUE;
177 static BOOL skip_comment(parser_ctx_t *ctx)
179 if(ctx->ptr+1 >= ctx->end || *ctx->ptr != '/')
180 return FALSE;
182 switch(ctx->ptr[1]) {
183 case '*':
184 ctx->ptr += 2;
185 while(ctx->ptr+1 < ctx->end && (ctx->ptr[0] != '*' || ctx->ptr[1] != '/'))
186 ctx->ptr++;
188 if(ctx->ptr[0] == '*' && ctx->ptr[1] == '/') {
189 ctx->ptr += 2;
190 }else {
191 WARN("unexpected end of file (missing end of comment)\n");
192 ctx->ptr = ctx->end;
194 break;
195 case '/':
196 ctx->ptr += 2;
197 while(ctx->ptr < ctx->end && !is_endline(*ctx->ptr))
198 ctx->ptr++;
199 break;
200 default:
201 return FALSE;
204 return TRUE;
207 static BOOL unescape(WCHAR *str)
209 WCHAR *pd, *p, c;
210 int i;
212 pd = p = str;
213 while(*p) {
214 if(*p != '\\') {
215 *pd++ = *p++;
216 continue;
219 p++;
220 c = 0;
222 switch(*p) {
223 case '\'':
224 case '\"':
225 case '\\':
226 c = *p;
227 break;
228 case 'b':
229 c = '\b';
230 break;
231 case 't':
232 c = '\t';
233 break;
234 case 'n':
235 c = '\n';
236 break;
237 case 'v':
238 c = '\v';
239 break;
240 case 'f':
241 c = '\f';
242 break;
243 case 'r':
244 c = '\r';
245 break;
246 case 'x':
247 i = hex_to_int(*++p);
248 if(i == -1)
249 return FALSE;
250 c = i << 4;
252 i = hex_to_int(*++p);
253 if(i == -1)
254 return FALSE;
255 c += i;
256 break;
257 case 'u':
258 i = hex_to_int(*++p);
259 if(i == -1)
260 return FALSE;
261 c = i << 12;
263 i = hex_to_int(*++p);
264 if(i == -1)
265 return FALSE;
266 c += i << 8;
268 i = hex_to_int(*++p);
269 if(i == -1)
270 return FALSE;
271 c += 1 << 4;
273 i = hex_to_int(*++p);
274 if(i == -1)
275 return FALSE;
276 c += i;
277 break;
278 default:
279 if(isdigitW(*p)) {
280 c = *p++ - '0';
281 while(isdigitW(*p))
282 c = c*10 + (*p++ - '0');
283 *pd++ = c;
284 continue;
287 c = *p;
290 *pd++ = c;
291 p++;
294 *pd = 0;
295 return TRUE;
298 static int parse_identifier(parser_ctx_t *ctx, const WCHAR **ret)
300 const WCHAR *ptr = ctx->ptr++;
301 WCHAR *wstr;
302 int len;
304 while(ctx->ptr < ctx->end && is_identifier_char(*ctx->ptr))
305 ctx->ptr++;
307 len = ctx->ptr-ptr;
309 *ret = wstr = parser_alloc(ctx, (len+1)*sizeof(WCHAR));
310 memcpy(wstr, ptr, (len+1)*sizeof(WCHAR));
311 wstr[len] = 0;
313 /* FIXME: unescape */
314 return tIdentifier;
317 static int parse_string_literal(parser_ctx_t *ctx, const WCHAR **ret, WCHAR endch)
319 const WCHAR *ptr = ++ctx->ptr;
320 WCHAR *wstr;
321 int len;
323 while(ctx->ptr < ctx->end && *ctx->ptr != endch) {
324 if(*ctx->ptr++ == '\\')
325 ctx->ptr++;
328 if(ctx->ptr == ctx->end) {
329 WARN("unexpected end of file\n");
330 return lex_error(ctx, E_FAIL);
333 len = ctx->ptr-ptr;
335 *ret = wstr = parser_alloc(ctx, (len+1)*sizeof(WCHAR));
336 memcpy(wstr, ptr, (len+1)*sizeof(WCHAR));
337 wstr[len] = 0;
339 ctx->ptr++;
341 if(!unescape(wstr)) {
342 WARN("unescape failed\n");
343 return lex_error(ctx, E_FAIL);
346 return tStringLiteral;
349 static literal_t *alloc_int_literal(parser_ctx_t *ctx, LONG l)
351 literal_t *ret = parser_alloc(ctx, sizeof(literal_t));
353 ret->vt = VT_I4;
354 ret->u.lval = l;
356 return ret;
359 static int parse_double_literal(parser_ctx_t *ctx, LONG int_part, literal_t **literal)
361 double d, tmp = 1.0;
363 if(ctx->ptr == ctx->end || !isdigitW(*ctx->ptr)) {
364 ERR("No digit after point\n");
365 return 0;
368 d = int_part;
369 while(ctx->ptr < ctx->end && isdigitW(*ctx->ptr))
370 d += (tmp /= 10.0)*(*ctx->ptr++ - '0');
372 if(ctx->ptr < ctx->end && (*ctx->ptr == 'e' || *ctx->ptr == 'E')) {
373 int sign = 1, e = 0;
375 ctx->ptr++;
376 if(ctx->ptr < ctx->end) {
377 if(*ctx->ptr == '+') {
378 ctx->ptr++;
379 }else if(*ctx->ptr == '-') {
380 sign = -1;
381 ctx->ptr++;
382 }else if(!isdigitW(*ctx->ptr)) {
383 WARN("Expected exponent part\n");
384 return lex_error(ctx, E_FAIL);
388 if(ctx->ptr == ctx->end) {
389 WARN("unexpected end of file\n");
390 return lex_error(ctx, E_FAIL);
393 while(ctx->ptr < ctx->end && isdigitW(*ctx->ptr))
394 e = e*10 + *ctx->ptr++ - '0';
395 e *= sign;
397 d *= pow(10, e);
400 *literal = parser_alloc(ctx, sizeof(literal_t));
401 (*literal)->vt = VT_R8;
402 (*literal)->u.dval = d;
404 return tNumericLiteral;
407 static int parse_numeric_literal(parser_ctx_t *ctx, literal_t **literal)
409 LONG l, d;
411 l = *ctx->ptr++ - '0';
412 if(ctx->ptr == ctx->end) {
413 *literal = alloc_int_literal(ctx, l);
414 return tNumericLiteral;
417 if(!l) {
418 if(*ctx->ptr == 'x' || *ctx->ptr == 'X') {
419 if(++ctx->ptr == ctx->end) {
420 ERR("unexpexted end of file\n");
421 return 0;
424 while(ctx->ptr < ctx->end && (d = hex_to_int(*ctx->ptr)) != -1) {
425 l = l*16 + d;
426 ctx->ptr++;
429 if(ctx->ptr < ctx->end && is_identifier_char(*ctx->ptr)) {
430 WARN("unexpected identifier char\n");
431 return lex_error(ctx, E_FAIL);
434 *literal = alloc_int_literal(ctx, l);
435 return tNumericLiteral;
438 if(isdigitW(*ctx->ptr) || is_identifier_char(*ctx->ptr)) {
439 WARN("wrong char after zero\n");
440 return lex_error(ctx, E_FAIL);
443 *literal = alloc_int_literal(ctx, 0);
446 while(ctx->ptr < ctx->end && isdigitW(*ctx->ptr))
447 l = l*10 + *(ctx->ptr++)-'0';
449 if(ctx->ptr < ctx->end) {
450 if(*ctx->ptr == '.') {
451 ctx->ptr++;
452 return parse_double_literal(ctx, l, literal);
455 if(is_identifier_char(*ctx->ptr)) {
456 WARN("unexpected identifier char\n");
457 return lex_error(ctx, E_FAIL);
461 *literal = alloc_int_literal(ctx, l);
462 return tNumericLiteral;
465 int parser_lex(void *lval, parser_ctx_t *ctx)
467 int ret;
469 ctx->nl = FALSE;
471 do {
472 skip_spaces(ctx);
473 if(ctx->ptr == ctx->end)
474 return 0;
475 }while(skip_comment(ctx));
477 if(isalphaW(*ctx->ptr)) {
478 ret = check_keywords(ctx, lval);
479 if(ret)
480 return ret;
482 return parse_identifier(ctx, (const WCHAR**)lval);
485 if(isdigitW(*ctx->ptr))
486 return parse_numeric_literal(ctx, lval);
488 switch(*ctx->ptr) {
489 case '{':
490 case '(':
491 case ')':
492 case '[':
493 case ']':
494 case ';':
495 case ',':
496 case '~':
497 case '?':
498 case ':':
499 return *ctx->ptr++;
501 case '}':
502 *(const WCHAR**)lval = ctx->ptr++;
503 return '}';
505 case '.':
506 if(++ctx->ptr < ctx->end && isdigitW(*ctx->ptr))
507 return parse_double_literal(ctx, 0, lval);
508 return '.';
510 case '<':
511 if(++ctx->ptr == ctx->end) {
512 *(int*)lval = EXPR_LESS;
513 return tRelOper;
516 switch(*ctx->ptr) {
517 case '=': /* <= */
518 ctx->ptr++;
519 *(int*)lval = EXPR_LESSEQ;
520 return tRelOper;
521 case '<': /* << */
522 if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* <<= */
523 ctx->ptr++;
524 *(int*)lval = EXPR_ASSIGNLSHIFT;
525 return tAssignOper;
527 *(int*)lval = EXPR_LSHIFT;
528 return tShiftOper;
529 default: /* < */
530 *(int*)lval = EXPR_LESS;
531 return tRelOper;
534 case '>':
535 if(++ctx->ptr == ctx->end) { /* > */
536 *(int*)lval = EXPR_GREATER;
537 return tRelOper;
540 switch(*ctx->ptr) {
541 case '=': /* >= */
542 ctx->ptr++;
543 *(int*)lval = EXPR_GREATEREQ;
544 return tRelOper;
545 case '>': /* >> */
546 if(++ctx->ptr < ctx->end) {
547 if(*ctx->ptr == '=') { /* >>= */
548 ctx->ptr++;
549 *(int*)lval = EXPR_ASSIGNRSHIFT;
550 return tAssignOper;
552 if(*ctx->ptr == '>') { /* >>> */
553 if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* >>>= */
554 ctx->ptr++;
555 *(int*)lval = EXPR_ASSIGNRRSHIFT;
556 return tAssignOper;
558 *(int*)lval = EXPR_RRSHIFT;
559 return tRelOper;
562 *(int*)lval = EXPR_RSHIFT;
563 return tShiftOper;
564 default:
565 *(int*)lval = EXPR_GREATER;
566 return tRelOper;
569 case '+':
570 ctx->ptr++;
571 if(ctx->ptr < ctx->end) {
572 switch(*ctx->ptr) {
573 case '+': /* ++ */
574 ctx->ptr++;
575 return tINC;
576 case '=': /* += */
577 ctx->ptr++;
578 *(int*)lval = EXPR_ASSIGNADD;
579 return tAssignOper;
582 return '+';
584 case '-':
585 ctx->ptr++;
586 if(ctx->ptr < ctx->end) {
587 switch(*ctx->ptr) {
588 case '-': /* -- */
589 ctx->ptr++;
590 return tDEC;
591 case '=': /* -= */
592 ctx->ptr++;
593 *(int*)lval = EXPR_ASSIGNSUB;
594 return tAssignOper;
597 return '-';
599 case '*':
600 if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* *= */
601 ctx->ptr++;
602 *(int*)lval = EXPR_ASSIGNMUL;
603 return tAssignOper;
605 return '*';
607 case '%':
608 if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* %= */
609 ctx->ptr++;
610 *(int*)lval = EXPR_ASSIGNMOD;
611 return tAssignOper;
613 return '%';
615 case '&':
616 if(++ctx->ptr < ctx->end) {
617 switch(*ctx->ptr) {
618 case '=': /* &= */
619 ctx->ptr++;
620 *(int*)lval = EXPR_ASSIGNAND;
621 return tAssignOper;
622 case '&': /* && */
623 ctx->ptr++;
624 return tANDAND;
627 return '&';
629 case '|':
630 if(++ctx->ptr < ctx->end) {
631 switch(*ctx->ptr) {
632 case '=': /* |= */
633 ctx->ptr++;
634 *(int*)lval = EXPR_ASSIGNOR;
635 return tAssignOper;
636 case '|': /* || */
637 ctx->ptr++;
638 return tOROR;
641 return '|';
643 case '^':
644 if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* ^= */
645 ctx->ptr++;
646 *(int*)lval = EXPR_ASSIGNXOR;
647 return tAssignOper;
649 return '^';
651 case '!':
652 if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* != */
653 if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* !== */
654 ctx->ptr++;
655 *(int*)lval = EXPR_NOTEQEQ;
656 return tEqOper;
658 *(int*)lval = EXPR_NOTEQ;
659 return tEqOper;
661 return '!';
663 case '=':
664 if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* == */
665 if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* === */
666 ctx->ptr++;
667 *(int*)lval = EXPR_EQEQ;
668 return tEqOper;
670 *(int*)lval = EXPR_EQ;
671 return tEqOper;
673 return '=';
675 case '/':
676 if(++ctx->ptr < ctx->end) {
677 if(*ctx->ptr == '=') { /* /= */
678 ctx->ptr++;
679 *(int*)lval = EXPR_ASSIGNDIV;
680 return tAssignOper;
683 return '/';
685 case '\"':
686 case '\'':
687 return parse_string_literal(ctx, (const WCHAR**)lval, *ctx->ptr);
689 case '_':
690 case '$':
691 return parse_identifier(ctx, lval);
694 WARN("unexpected char '%c' %d\n", *ctx->ptr, *ctx->ptr);
695 return 0;
698 static void add_object_literal(parser_ctx_t *ctx, DispatchEx *obj)
700 obj_literal_t *literal = parser_alloc(ctx, sizeof(obj_literal_t));
702 literal->obj = obj;
703 literal->next = ctx->obj_literals;
704 ctx->obj_literals = literal;
707 literal_t *parse_regexp(parser_ctx_t *ctx)
709 const WCHAR *re, *flags;
710 DispatchEx *regexp;
711 literal_t *ret;
712 DWORD re_len;
713 HRESULT hres;
715 TRACE("\n");
717 re = ctx->ptr;
718 while(ctx->ptr < ctx->end && *ctx->ptr != '/') {
719 if(*ctx->ptr++ == '\\' && ctx->ptr < ctx->end)
720 ctx->ptr++;
723 if(ctx->ptr == ctx->end) {
724 WARN("unexpected end of file\n");
725 return NULL;
728 re_len = ctx->ptr-re;
730 flags = ++ctx->ptr;
731 while(ctx->ptr < ctx->end && isalnumW(*ctx->ptr))
732 ctx->ptr++;
734 hres = create_regexp_str(ctx->script, re, re_len, flags, ctx->ptr-flags, &regexp);
735 if(FAILED(hres))
736 return NULL;
738 add_object_literal(ctx, regexp);
740 ret = parser_alloc(ctx, sizeof(literal_t));
741 ret->vt = VT_DISPATCH;
742 ret->u.disp = (IDispatch*)_IDispatchEx_(regexp);
743 return ret;