2 * Copyright 2011 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
20 #include <wine/config.h>
21 #include <wine/port.h>
29 #include "parser.tab.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(vbscript
);
35 static const WCHAR andW
[] = {'a','n','d',0};
36 static const WCHAR byrefW
[] = {'b','y','r','e','f',0};
37 static const WCHAR byvalW
[] = {'b','y','v','a','l',0};
38 static const WCHAR callW
[] = {'c','a','l','l',0};
39 static const WCHAR caseW
[] = {'c','a','s','e',0};
40 static const WCHAR classW
[] = {'c','l','a','s','s',0};
41 static const WCHAR constW
[] = {'c','o','n','s','t',0};
42 static const WCHAR defaultW
[] = {'d','e','f','a','u','l','t',0};
43 static const WCHAR dimW
[] = {'d','i','m',0};
44 static const WCHAR doW
[] = {'d','o',0};
45 static const WCHAR eachW
[] = {'e','a','c','h',0};
46 static const WCHAR elseW
[] = {'e','l','s','e',0};
47 static const WCHAR elseifW
[] = {'e','l','s','e','i','f',0};
48 static const WCHAR emptyW
[] = {'e','m','p','t','y',0};
49 static const WCHAR endW
[] = {'e','n','d',0};
50 static const WCHAR eqvW
[] = {'e','q','v',0};
51 static const WCHAR errorW
[] = {'e','r','r','o','r',0};
52 static const WCHAR exitW
[] = {'e','x','i','t',0};
53 static const WCHAR explicitW
[] = {'e','x','p','l','i','c','i','t',0};
54 static const WCHAR falseW
[] = {'f','a','l','s','e',0};
55 static const WCHAR forW
[] = {'f','o','r',0};
56 static const WCHAR functionW
[] = {'f','u','n','c','t','i','o','n',0};
57 static const WCHAR getW
[] = {'g','e','t',0};
58 static const WCHAR gotoW
[] = {'g','o','t','o',0};
59 static const WCHAR ifW
[] = {'i','f',0};
60 static const WCHAR impW
[] = {'i','m','p',0};
61 static const WCHAR inW
[] = {'i','n',0};
62 static const WCHAR isW
[] = {'i','s',0};
63 static const WCHAR letW
[] = {'l','e','t',0};
64 static const WCHAR loopW
[] = {'l','o','o','p',0};
65 static const WCHAR meW
[] = {'m','e',0};
66 static const WCHAR modW
[] = {'m','o','d',0};
67 static const WCHAR newW
[] = {'n','e','w',0};
68 static const WCHAR nextW
[] = {'n','e','x','t',0};
69 static const WCHAR notW
[] = {'n','o','t',0};
70 static const WCHAR nothingW
[] = {'n','o','t','h','i','n','g',0};
71 static const WCHAR nullW
[] = {'n','u','l','l',0};
72 static const WCHAR onW
[] = {'o','n',0};
73 static const WCHAR optionW
[] = {'o','p','t','i','o','n',0};
74 static const WCHAR orW
[] = {'o','r',0};
75 static const WCHAR privateW
[] = {'p','r','i','v','a','t','e',0};
76 static const WCHAR propertyW
[] = {'p','r','o','p','e','r','t','y',0};
77 static const WCHAR publicW
[] = {'p','u','b','l','i','c',0};
78 static const WCHAR remW
[] = {'r','e','m',0};
79 static const WCHAR resumeW
[] = {'r','e','s','u','m','e',0};
80 static const WCHAR selectW
[] = {'s','e','l','e','c','t',0};
81 static const WCHAR setW
[] = {'s','e','t',0};
82 static const WCHAR stepW
[] = {'s','t','e','p',0};
83 static const WCHAR stopW
[] = {'s','t','o','p',0};
84 static const WCHAR subW
[] = {'s','u','b',0};
85 static const WCHAR thenW
[] = {'t','h','e','n',0};
86 static const WCHAR toW
[] = {'t','o',0};
87 static const WCHAR trueW
[] = {'t','r','u','e',0};
88 static const WCHAR untilW
[] = {'u','n','t','i','l',0};
89 static const WCHAR wendW
[] = {'w','e','n','d',0};
90 static const WCHAR whileW
[] = {'w','h','i','l','e',0};
91 static const WCHAR xorW
[] = {'x','o','r',0};
104 {defaultW
, tDEFAULT
},
115 {explicitW
, tEXPLICIT
},
118 {functionW
, tFUNCTION
},
132 {nothingW
, tNOTHING
},
137 {privateW
, tPRIVATE
},
138 {propertyW
, tPROPERTY
},
156 static inline BOOL
is_identifier_char(WCHAR c
)
158 return iswalnum(c
) || c
== '_';
161 static int check_keyword(parser_ctx_t
*ctx
, const WCHAR
*word
, const WCHAR
**lval
)
163 const WCHAR
*p1
= ctx
->ptr
;
164 const WCHAR
*p2
= word
;
167 while(p1
< ctx
->end
&& *p2
) {
175 if(*p2
|| (p1
< ctx
->end
&& is_identifier_char(*p1
)))
183 static int check_keywords(parser_ctx_t
*ctx
, const WCHAR
**lval
)
185 int min
= 0, max
= ARRAY_SIZE(keywords
)-1, r
, i
;
190 r
= check_keyword(ctx
, keywords
[i
].word
, lval
);
192 return keywords
[i
].token
;
203 static int parse_identifier(parser_ctx_t
*ctx
, const WCHAR
**ret
)
205 const WCHAR
*ptr
= ctx
->ptr
++;
209 while(ctx
->ptr
< ctx
->end
&& is_identifier_char(*ctx
->ptr
))
213 str
= parser_alloc(ctx
, (len
+1)*sizeof(WCHAR
));
217 memcpy(str
, ptr
, (len
+1)*sizeof(WCHAR
));
223 static int parse_string_literal(parser_ctx_t
*ctx
, const WCHAR
**ret
)
225 const WCHAR
*ptr
= ++ctx
->ptr
;
229 while(ctx
->ptr
< ctx
->end
) {
230 if(*ctx
->ptr
== '\n' || *ctx
->ptr
== '\r') {
231 FIXME("newline inside string literal\n");
235 if(*ctx
->ptr
== '"') {
236 if(ctx
->ptr
[1] != '"')
244 if(ctx
->ptr
== ctx
->end
) {
245 FIXME("unterminated string literal\n");
251 *ret
= rptr
= parser_alloc(ctx
, (len
+1)*sizeof(WCHAR
));
255 while(ptr
< ctx
->ptr
) {
266 static int parse_numeric_literal(parser_ctx_t
*ctx
, void **ret
)
273 if(*ctx
->ptr
== '0' && !('0' <= ctx
->ptr
[1] && ctx
->ptr
[1] <= '9') && ctx
->ptr
[1] != '.')
276 while(ctx
->ptr
< ctx
->end
&& iswdigit(*ctx
->ptr
)) {
277 hlp
= d
*10 + *(ctx
->ptr
++) - '0';
278 if(d
>MAXLONGLONG
/10 || hlp
<0) {
285 while(ctx
->ptr
< ctx
->end
&& iswdigit(*ctx
->ptr
)) {
290 if(*ctx
->ptr
== '.') {
294 while(ctx
->ptr
< ctx
->end
&& iswdigit(*ctx
->ptr
)) {
295 hlp
= d
*10 + *(ctx
->ptr
++) - '0';
296 if(d
>MAXLONGLONG
/10 || hlp
<0)
302 while(ctx
->ptr
< ctx
->end
&& iswdigit(*ctx
->ptr
))
306 if(*ctx
->ptr
== 'e' || *ctx
->ptr
== 'E') {
310 if(*ctx
->ptr
== '-') {
313 }else if(*ctx
->ptr
== '+') {
317 if(!iswdigit(*ctx
->ptr
)) {
318 FIXME("Invalid numeric literal\n");
325 e
= e
*10 + *(ctx
->ptr
++) - '0';
326 if(sign
== -1 && -e
+exp
< -(INT_MAX
/100)) {
327 /* The literal will be rounded to 0 anyway. */
328 while(iswdigit(*ctx
->ptr
))
334 if(sign
*e
+ exp
> INT_MAX
/100) {
335 FIXME("Invalid numeric literal\n");
338 } while(iswdigit(*ctx
->ptr
));
343 if(use_int
&& (LONG
)d
== d
) {
348 r
= exp
>=0 ? d
*pow(10, exp
) : d
/pow(10, -exp
);
350 FIXME("Invalid numeric literal\n");
358 static int hex_to_int(WCHAR c
)
360 if('0' <= c
&& c
<= '9')
362 if('a' <= c
&& c
<= 'f')
364 if('A' <= c
&& c
<= 'F')
369 static int parse_hex_literal(parser_ctx_t
*ctx
, LONG
*ret
)
371 const WCHAR
*begin
= ctx
->ptr
;
374 while((d
= hex_to_int(*++ctx
->ptr
)) != -1)
377 if(begin
+ 9 /* max digits+1 */ < ctx
->ptr
|| (*ctx
->ptr
!= '&' && is_identifier_char(*ctx
->ptr
))) {
378 FIXME("invalid literal\n");
389 static void skip_spaces(parser_ctx_t
*ctx
)
391 while(*ctx
->ptr
== ' ' || *ctx
->ptr
== '\t')
395 static int comment_line(parser_ctx_t
*ctx
)
397 static const WCHAR newlineW
[] = {'\n','\r',0};
398 ctx
->ptr
= wcspbrk(ctx
->ptr
, newlineW
);
406 static int parse_next_token(void *lval
, parser_ctx_t
*ctx
)
411 if(ctx
->ptr
== ctx
->end
)
412 return ctx
->last_token
== tNL
? tEOF
: tNL
;
416 if('0' <= c
&& c
<= '9')
417 return parse_numeric_literal(ctx
, lval
);
420 int ret
= check_keywords(ctx
, lval
);
422 return parse_identifier(ctx
, lval
);
434 return comment_line(ctx
);
448 if(ctx
->is_html
&& ctx
->ptr
[1] == '-' && ctx
->ptr
[2] == '>')
449 return comment_line(ctx
);
454 * We resolve empty brackets in lexer instead of parser to avoid complex conflicts
455 * in call statement special case |f()| without 'call' keyword
459 if(*ctx
->ptr
== ')') {
461 return tEMPTYBRACKETS
;
465 return parse_string_literal(ctx
, lval
);
467 if(*++ctx
->ptr
== 'h' || *ctx
->ptr
== 'H')
468 return parse_hex_literal(ctx
, lval
);
471 switch(*++ctx
->ptr
) {
479 if(ctx
->is_html
&& ctx
->ptr
[1] == '-' && ctx
->ptr
[2] == '-')
480 return comment_line(ctx
);
484 if(*++ctx
->ptr
== '=') {
490 FIXME("Unhandled char %c in %s\n", *ctx
->ptr
, debugstr_w(ctx
->ptr
));
496 int parser_lex(void *lval
, parser_ctx_t
*ctx
)
500 if (ctx
->last_token
== tEXPRESSION
)
502 ctx
->last_token
= tNL
;
507 ret
= parse_next_token(lval
, ctx
);
510 if(*ctx
->ptr
!= '\n' && *ctx
->ptr
!= '\r') {
511 FIXME("'_' not followed by newline\n");
514 if(*ctx
->ptr
== '\r')
516 if(*ctx
->ptr
== '\n')
520 if(ret
!= tNL
|| ctx
->last_token
!= tNL
)
523 ctx
->last_nl
= ctx
->ptr
-ctx
->code
;
526 return (ctx
->last_token
= ret
);