cc_parser.y 7.89 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/*
 * Copyright 2014 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
 */

%{

#include "jscript.h"
#include "engine.h"
#include "parser.h"

#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(jscript);

%}

%lex-param { parser_ctx_t *ctx }
%parse-param { parser_ctx_t *ctx }
33
%define api.prefix {cc_parser_}
34
%define api.pure
35 36 37 38 39 40
%start CCExpr

%union {
    ccval_t ccval;
}

41
%token tEQ tEQEQ tNEQ tNEQEQ tLSHIFT tRSHIFT tRRSHIFT tOR tAND tLEQ tGEQ
42 43
%token <ccval> tCCValue

44 45 46
%type <ccval> CCUnaryExpression CCLogicalORExpression CCLogicalANDExpression
%type <ccval> CCBitwiseORExpression CCBitwiseXORExpression CCBitwiseANDExpression
%type <ccval> CCEqualityExpression CCRelationalExpression CCShiftExpression CCAdditiveExpression CCMultiplicativeExpression
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71

%{

static int cc_parser_error(parser_ctx_t *ctx, const char *str)
{
    if(SUCCEEDED(ctx->hres)) {
        WARN("%s\n", str);
        ctx->hres = JS_E_SYNTAX;
    }

    return 0;
}

static int cc_parser_lex(void *lval, parser_ctx_t *ctx)
{
    int r;

    r = try_parse_ccval(ctx, lval);
    if(r)
        return r > 0 ? tCCValue : -1;

    switch(*ctx->ptr) {
    case '(':
    case ')':
    case '+':
72
    case '-':
73 74
    case '*':
    case '/':
75 76 77
    case '~':
    case '%':
    case '^':
78
        return *ctx->ptr++;
79 80 81 82 83 84 85 86 87
    case '=':
         if(*++ctx->ptr == '=') {
             if(*++ctx->ptr == '=') {
                 ctx->ptr++;
                 return tEQEQ;
             }
             return tEQ;
         }
         break;
88 89
    case '!':
        if(*++ctx->ptr == '=') {
90 91 92 93
            if(*++ctx->ptr == '=') {
                ctx->ptr++;
                return tNEQEQ;
            }
94 95 96
            return tNEQ;
        }
        return '!';
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
    case '<':
        switch(*++ctx->ptr) {
        case '<':
            ctx->ptr++;
            return tLSHIFT;
        case '=':
            ctx->ptr++;
            return tLEQ;
        default:
            return '<';
        }
    case '>':
        switch(*++ctx->ptr) {
        case '>':
            if(*++ctx->ptr == '>') {
                ctx->ptr++;
                return tRRSHIFT;
            }
            return tRSHIFT;
        case '=':
            ctx->ptr++;
            return tGEQ;
        default:
            return '>';
        }
    case '|':
        if(*++ctx->ptr == '|') {
            ctx->ptr++;
            return tOR;
        }
        return '|';
    case '&':
        if(*++ctx->ptr == '&') {
            ctx->ptr++;
            return tAND;
        }
        return '&';
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
    }

    WARN("Failed to interpret %s\n", debugstr_w(ctx->ptr));
    return -1;
}

%}

%%

/* FIXME: Implement missing expressions. */

CCExpr
    : CCUnaryExpression { ctx->ccval = $1; YYACCEPT; }

CCUnaryExpression
    : tCCValue                      { $$ = $1; }
151
    | '(' CCLogicalORExpression ')' { $$ = $2; }
152
    | '!' CCUnaryExpression         { $$ = ccval_bool(!get_ccbool($2)); };
153 154 155 156 157 158 159 160 161 162 163 164
    | '~' CCUnaryExpression         { FIXME("'~' expression not implemented\n"); ctx->hres = E_NOTIMPL; YYABORT; }
    | '+' CCUnaryExpression         { FIXME("'+' expression not implemented\n"); ctx->hres = E_NOTIMPL; YYABORT; }
    | '-' CCUnaryExpression         { FIXME("'-' expression not implemented\n"); ctx->hres = E_NOTIMPL; YYABORT; }

CCLogicalORExpression
    : CCLogicalANDExpression        { $$ = $1; }
    | CCLogicalORExpression tOR CCLogicalANDExpression
                                    { FIXME("'||' expression not implemented\n"); ctx->hres = E_NOTIMPL; YYABORT; }

CCLogicalANDExpression
    : CCBitwiseORExpression         { $$ = $1; }
    | CCBitwiseANDExpression tAND CCBitwiseORExpression
165
                                    { $$ = ccval_bool(get_ccbool($1) && get_ccbool($3)); }
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180

CCBitwiseORExpression
    : CCBitwiseXORExpression        { $$ = $1; }
    | CCBitwiseORExpression '|' CCBitwiseXORExpression
                                    { FIXME("'|' expression not implemented\n"); ctx->hres = E_NOTIMPL; YYABORT; }

CCBitwiseXORExpression
    : CCBitwiseANDExpression        { $$ = $1; }
    | CCBitwiseXORExpression '^' CCBitwiseANDExpression
                                    { FIXME("'^' expression not implemented\n"); ctx->hres = E_NOTIMPL; YYABORT; }

CCBitwiseANDExpression
    : CCEqualityExpression          { $$ = $1; }
    | CCBitwiseANDExpression '&' CCEqualityExpression
                                    { FIXME("'&' expression not implemented\n"); ctx->hres = E_NOTIMPL; YYABORT; }
181 182

CCEqualityExpression
183 184
    : CCRelationalExpression        { $$ = $1; }
    | CCEqualityExpression tEQ CCRelationalExpression
185
                                    { $$ = ccval_bool(get_ccnum($1) == get_ccnum($3)); }
186
    | CCEqualityExpression tNEQ CCRelationalExpression
187
                                    { $$ = ccval_bool(get_ccnum($1) != get_ccnum($3)); }
188 189 190 191 192 193 194 195
    | CCEqualityExpression tEQEQ CCRelationalExpression
                                    { FIXME("'===' expression not implemented\n"); ctx->hres = E_NOTIMPL; YYABORT; }
    | CCEqualityExpression tNEQEQ CCRelationalExpression
                                    { FIXME("'!==' expression not implemented\n"); ctx->hres = E_NOTIMPL; YYABORT; }

CCRelationalExpression
    : CCShiftExpression             { $$ = $1; }
    | CCRelationalExpression '<' CCShiftExpression
196
                                    { $$ = ccval_bool(get_ccnum($1) < get_ccnum($3)); }
197
    | CCRelationalExpression tLEQ CCShiftExpression
198
                                    { $$ = ccval_bool(get_ccnum($1) <= get_ccnum($3)); }
199
    | CCRelationalExpression '>' CCShiftExpression
200
                                    { $$ = ccval_bool(get_ccnum($1) > get_ccnum($3)); }
201
    | CCRelationalExpression tGEQ CCShiftExpression
202
                                    { $$ = ccval_bool(get_ccnum($1) >= get_ccnum($3)); }
203 204 205 206 207 208 209 210 211

CCShiftExpression
    : CCAdditiveExpression          { $$ = $1; }
    | CCShiftExpression tLSHIFT CCAdditiveExpression
                                    { FIXME("'<<' expression not implemented\n"); ctx->hres = E_NOTIMPL; YYABORT; }
    | CCShiftExpression tRSHIFT CCAdditiveExpression
                                    { FIXME("'>>' expression not implemented\n"); ctx->hres = E_NOTIMPL; YYABORT; }
    | CCShiftExpression tRRSHIFT CCAdditiveExpression
                                    { FIXME("'>>>' expression not implemented\n"); ctx->hres = E_NOTIMPL; YYABORT; }
212 213 214 215 216

CCAdditiveExpression
    : CCMultiplicativeExpression    { $$ = $1; }
    | CCAdditiveExpression '+' CCMultiplicativeExpression
                                    { $$ = ccval_num(get_ccnum($1) + get_ccnum($3)); }
217 218
    | CCAdditiveExpression '-' CCMultiplicativeExpression
                                    { $$ = ccval_num(get_ccnum($1) - get_ccnum($3)); }
219 220 221 222 223 224 225

CCMultiplicativeExpression
    : CCUnaryExpression             { $$ = $1; }
    | CCMultiplicativeExpression '*' CCUnaryExpression
                                    { $$ = ccval_num(get_ccnum($1) * get_ccnum($3)); }
    | CCMultiplicativeExpression '/' CCUnaryExpression
                                    { $$ = ccval_num(get_ccnum($1) / get_ccnum($3)); }
226 227
    | CCMultiplicativeExpression '%' CCUnaryExpression
                                    { FIXME("'%%' expression not implemented\n"); ctx->hres = E_NOTIMPL; YYABORT; }
228 229 230 231 232 233 234 235 236

%%

BOOL parse_cc_expr(parser_ctx_t *ctx)
{
    ctx->hres = S_OK;
    cc_parser_parse(ctx);
    return SUCCEEDED(ctx->hres);
}