Annotation of parser3/src/classes/root.C, revision 1.55

1.55    ! paf         1: /** @file
        !             2:        Parser: @b ROOT parser class.
        !             3: 
1.10      paf         4:        Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
1.55    ! paf         5: 
1.12      paf         6:        Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
1.10      paf         7: 
1.55    ! paf         8:        $Id: root.C,v 1.54 2001/04/03 05:23:37 paf Exp $
1.1       paf         9: */
                     10: 
1.48      paf        11: #include "pa_config_includes.h"
1.29      paf        12: #include <math.h>
1.18      paf        13: 
1.46      paf        14: #include "pa_common.h"
1.1       paf        15: #include "pa_request.h"
1.8       paf        16: #include "_root.h"
1.31      paf        17: #include "pa_vint.h"
1.1       paf        18: 
1.34      paf        19: static void _if(Request& r, const String& method_name, Array *params) {
                     20:        Value& condition_code=*static_cast<Value *>(params->get(0));
                     21:        // forcing ^if(this param type)
                     22:        r.fail_if_junction_(false, condition_code, 
                     23:                method_name, "condition must be junction");
1.36      paf        24: 
1.34      paf        25:        bool condition=r.process(condition_code, 
                     26:                0/*no name*/,
1.51      paf        27:                false/*don't intercept string*/).as_bool();
1.6       paf        28:        if(condition) {
1.34      paf        29:                Value& then_code=*static_cast<Value *>(params->get(1));
1.52      paf        30:                // forcing ^if{this param type}
1.34      paf        31:                r.fail_if_junction_(false, then_code, 
                     32:                        method_name, "then-parameter must be junction");
                     33:                r.write_pass_lang(r.process(then_code));
1.6       paf        34:        } else if(params->size()==3) {
1.34      paf        35:                Value& else_code=*static_cast<Value *>(params->get(2));
1.52      paf        36:                // forcing ^if{this param type}
1.34      paf        37:                r.fail_if_junction_(false, else_code, 
                     38:                        method_name, "else-parameter must be junction");
                     39:                r.write_pass_lang(r.process(else_code));
1.6       paf        40:        }
1.1       paf        41: }
                     42: 
1.22      paf        43: static void _untaint(Request& r, const String& method_name, Array *params) {
1.18      paf        44:        const String& lang_name=r.process(*static_cast<Value *>(params->get(0))).as_string();
1.15      paf        45:        String::Untaint_lang lang=static_cast<String::Untaint_lang>(
1.23      paf        46:                untaint_lang_name2enum->get_int(lang_name));
1.15      paf        47:        if(!lang)
1.39      paf        48:                RTHROW(0, 0,
1.15      paf        49:                        &lang_name,
1.18      paf        50:                        "invalid untaint language");
1.15      paf        51: 
1.24      paf        52:        {
                     53:                Value *vbody=static_cast<Value *>(params->get(1));
                     54:                // forcing ^untaint[]{this param type}
1.26      paf        55:                r.fail_if_junction_(false, *vbody, 
                     56:                        method_name, "body must be junction");
1.24      paf        57:                
1.42      paf        58:                Temp_lang temp_lang(r, lang); // set temporarily specified ^untaint[language;
                     59:                r.write_pass_lang(r.process(*vbody)); // process marking tainted with that lang
                     60:        }
                     61: }
                     62: 
                     63: static void _taint(Request& r, const String& method_name, Array *params) {
1.54      paf        64:        String::Untaint_lang lang;
                     65:        if(params->size()==1)
                     66:                lang=String::UL_TAINTED; // mark as simply 'tainted'. useful in table:set
                     67:        else {
                     68:                const String& lang_name=r.process(*static_cast<Value *>(params->get(0))).as_string();
                     69:                lang=static_cast<String::Untaint_lang>(
                     70:                        untaint_lang_name2enum->get_int(lang_name));
                     71:                if(!lang)
                     72:                        RTHROW(0, 0,
1.42      paf        73:                        &lang_name,
                     74:                        "invalid taint language");
1.54      paf        75:        }
1.42      paf        76: 
                     77:        {
1.54      paf        78:                Value *vbody=static_cast<Value *>(params->get(params->size()-1));
                     79:                // forcing {this param type}
                     80:                r.fail_if_junction_(true, *vbody, method_name, "body must not be junction");
1.42      paf        81:                
1.44      paf        82:                // set temporarily as-is language
1.54      paf        83:                Temp_lang temp_lang(r, String::UL_PASS_APPENDED);
1.44      paf        84:                String result(r.pool());
                     85:                result.append(
                     86:                        r.process(*vbody).as_string(),  // process marking tainted with that lang
1.45      paf        87:                        lang, true);  // force result language to specified
1.42      paf        88:                r.write_pass_lang(result);
1.24      paf        89:        }
1.15      paf        90: }
                     91: 
1.22      paf        92: static void _process(Request& r, const String& method_name, Array *params) {
1.20      paf        93:        // calculate pseudo file name of processed chars
                     94:        // would be something like "/some/file(4) process"
1.18      paf        95:        char place[MAX_STRING];
                     96: #ifndef NO_STRING_ORIGIN
1.25      paf        97:        const Origin& origin=method_name.origin();
1.18      paf        98:        snprintf(place, MAX_STRING, "%s(%d) %s", 
                     99:                origin.file, 1+origin.line,
1.22      paf       100:                method_name.cstr());
1.18      paf       101: #else
1.22      paf       102:        strncpy(place, MAX_STRING, method_name.cstr());
1.18      paf       103: #endif 
                    104: 
1.38      paf       105:        VStateless_class& self_class=*r.self->get_class();
1.22      paf       106:        {
                    107:                // temporary zero @main so to maybe-replace it in processed code
1.50      paf       108:                Temp_method temp_method_main(self_class, *main_method_name, 0);
                    109:                // temporary zero @auto so it wouldn't be auto-called in Request::use_buf
                    110:                Temp_method temp_method_auto(self_class, *auto_method_name, 0);
1.22      paf       111:                
1.25      paf       112:                // evaluate source to process
                    113:                const String& source=
                    114:                        r.process(*static_cast<Value *>(params->get(0))).as_string();
                    115: 
1.22      paf       116:                // process source code, append processed methods to 'self' class
                    117:                // maybe-define new @main
                    118:                r.use_buf(source.cstr(), place, &self_class);
                    119:                
                    120:                // maybe-execute @main[]
                    121:                if(const Method *method=self_class.get_method(*main_method_name)) {
                    122:                        // execute!     
                    123:                        r.execute(*method->parser_code);
                    124:                }
1.18      paf       125:        }
                    126: }
                    127:        
1.26      paf       128: static void _rem(Request& r, const String& method_name, Array *params) {
1.27      paf       129:        // forcing ^rem{this param type}
1.26      paf       130:        r.fail_if_junction_(false, *static_cast<Value *>(params->get(0)), 
                    131:                method_name, "body must be junction");
                    132: }
1.18      paf       133: 
1.27      paf       134: static void _while(Request& r, const String& method_name, Array *params) {
                    135:        Value& vcondition=*static_cast<Value *>(params->get(0));
                    136:        // forcing ^while(this param type){}
                    137:        r.fail_if_junction_(false, vcondition, 
                    138:                method_name, "condition must be junction");
                    139:        
                    140:        Value& body=*static_cast<Value *>(params->get(1));
                    141:        // forcing ^while(){this param type}
                    142:        r.fail_if_junction_(false, body, 
                    143:                method_name, "body must be junction");
                    144: 
                    145:        // while...
                    146:        int endless_loop_count=0;
                    147:        while(true) {
                    148:                if(++endless_loop_count>=1973) // endless loop?
1.39      paf       149:                        RTHROW(0, 0,
1.27      paf       150:                                &method_name,
                    151:                                "endless loop detected");
                    152: 
                    153:                bool condition=
                    154:                        r.process(
                    155:                                vcondition, 
                    156:                                0/*no name*/,
1.51      paf       157:                                false/*don't intercept string*/).as_bool();
1.27      paf       158:                if(!condition) // ...condition is true
                    159:                        break;
                    160: 
                    161:                // write processed body
                    162:                r.write_pass_lang(r.process(body));
                    163:        }
                    164: }
                    165: 
1.28      paf       166: static void _use(Request& r, const String& method_name, Array *params) {
                    167:        Value& vfile=*static_cast<Value *>(params->get(0));
                    168:        // forcing ^rem{this param type}
                    169:        r.fail_if_junction_(true, vfile, 
                    170:                method_name, "file name must not be junction");
                    171: 
1.49      paf       172:        r.use_file(r.absolute(vfile.as_string()));
1.28      paf       173: }
                    174: 
1.31      paf       175: static void _for(Request& r, const String& method_name, Array *params) {
                    176:        // ^for[i;from-number;to-number-inclusive]{code}[delim]
                    177: 
                    178:        Pool& pool=r.pool();
                    179:        const String& var_name=r.process(*static_cast<Value *>(params->get(0))).as_string();
1.51      paf       180:        int from=(int)r.process(*static_cast<Value *>(params->get(1))).as_double();
                    181:        int to=(int)r.process(*static_cast<Value *>(params->get(2))).as_double();
1.31      paf       182:        Value& body_code=*static_cast<Value *>(params->get(3));
                    183:        // forcing ^menu{this param type}
                    184:        r.fail_if_junction_(false, body_code, 
                    185:                method_name, "body must be junction");
                    186:        Value *delim_code=params->size()==3+1+1?static_cast<Value *>(params->get(3+1)):0;
                    187: 
                    188:        bool need_delim=false;
1.37      paf       189:        VInt *vint=new(pool) VInt(pool, 0);
1.31      paf       190:        int endless_loop_count=0;
1.37      paf       191:        for(int i=from; i<=to; i++) {
1.31      paf       192:                if(++endless_loop_count>=2001) // endless loop?
1.39      paf       193:                        RTHROW(0, 0,
1.31      paf       194:                                &method_name,
                    195:                                "endless loop detected");
1.37      paf       196:                vint->set_int(i);
1.31      paf       197:                r.wcontext->put_element(var_name, vint);
                    198: 
                    199:                Value& processed_body=r.process(body_code);
                    200:                if(delim_code) { // delimiter set?
                    201:                        const String *string=processed_body.get_string();
                    202:                        if(need_delim && string && string->size()) // need delim & iteration produced string?
                    203:                                r.write_pass_lang(r.process(*delim_code));
                    204:                        need_delim=true;
                    205:                }
                    206:                r.write_pass_lang(processed_body);
                    207:        }
                    208: }
                    209: 
1.33      paf       210: static void _eval(Request& r, const String& method_name, Array *params) {
                    211:        Value& expr=*static_cast<Value *>(params->get(0));
                    212:        r.fail_if_junction_(false, expr, 
                    213:                method_name, "need expression");
                    214:        // evaluate expresion
                    215:        Value *result=r.process(expr, 
                    216:                0/*no name*/,
1.51      paf       217:                true/*don't intercept string*/).as_expr_result();
1.33      paf       218:        if(params->size()==2) {
                    219:                Value& fmt=*static_cast<Value *>(params->get(1));
                    220:                // forcing ^format[this param type]
                    221:                r.fail_if_junction_(true, fmt, 
                    222:                        method_name, "fmt must not be junction");
                    223: 
                    224:                Pool& pool=r.pool();
1.41      paf       225:                String& string=*new(pool) String(pool);
1.51      paf       226:                string.APPEND_CONST(format(pool, result->as_double(), fmt.as_string().cstr()));
1.41      paf       227:                result=new(pool) VString(string);
1.33      paf       228:        }
1.40      paf       229:        r.write_no_lang(*result);
1.33      paf       230: }
1.31      paf       231: 
                    232: 
1.29      paf       233: typedef double (*math_one_double_op_func_ptr)(double);
                    234: static double round(double op) { return floor(op+0.5); }
                    235: static double sign(double op) { return op > 0 ? 1 : ( op < 0 ? -1 : 0 ); }
                    236: 
1.37      paf       237: static void double_one_op(
1.30      paf       238:                                                                Request& r, 
                    239:                                                                const String& method_name, Array *params,
                    240:                                                                math_one_double_op_func_ptr func) {
1.29      paf       241:        Pool& pool=r.pool();
                    242:        Value& param=*static_cast<Value *>(params->get(0));
                    243: 
                    244:        // forcing ^round(this param type)
                    245:        r.fail_if_junction_(false, param, 
                    246:                method_name, "parameter must be expression");
                    247: 
1.51      paf       248:        Value& result=*new(pool) VDouble(pool, (*func)(r.process(param).as_double()));
1.40      paf       249:        r.write_no_lang(result);
1.29      paf       250: }
                    251: 
                    252: static void _round(Request& r, const String& method_name, Array *params) {
1.37      paf       253:        double_one_op(r, method_name, params,   &round);
1.29      paf       254: }
                    255: 
                    256: static void _floor(Request& r, const String& method_name, Array *params) {
1.37      paf       257:        double_one_op(r, method_name, params,   &floor);
1.29      paf       258: }
                    259: 
                    260: static void _ceiling(Request& r, const String& method_name, Array *params) {
1.37      paf       261:        double_one_op(r, method_name, params,   &ceil);
1.29      paf       262: }
                    263: 
                    264: static void _abs(Request& r, const String& method_name, Array *params) {
1.37      paf       265:        double_one_op(r, method_name, params,   &fabs);
1.29      paf       266: }
                    267: 
                    268: static void _sign(Request& r, const String& method_name, Array *params) {
1.37      paf       269:        double_one_op(r, method_name, params,   &sign);
1.29      paf       270: }
1.40      paf       271: 
                    272: // initialize
1.29      paf       273: 
1.38      paf       274: void initialize_root_class(Pool& pool, VStateless_class& vclass) {
1.15      paf       275:        // ^if(condition){code-when-true}
                    276:        // ^if(condition){code-when-true}{code-when-false}
1.53      paf       277:        vclass.add_native_method("if", Method::CT_ANY, _if, 2, 3);
1.15      paf       278: 
1.42      paf       279:        // ^untaint[as-is|uri|sql|js|html|html-typo]{code}
1.53      paf       280:        vclass.add_native_method("untaint", Method::CT_ANY, _untaint, 2, 2);
1.42      paf       281: 
                    282:        // ^taint[as-is|uri|sql|js|html|html-typo]{code}
1.54      paf       283:        vclass.add_native_method("taint", Method::CT_ANY, _taint, 1, 2);
1.18      paf       284: 
                    285:        // ^process[code]
1.53      paf       286:        vclass.add_native_method("process", Method::CT_ANY, _process, 1, 1);
1.26      paf       287: 
                    288:        // ^rem{code}
1.53      paf       289:        vclass.add_native_method("rem", Method::CT_ANY, _rem, 1, 1);
1.27      paf       290: 
                    291:        // ^while(condition){code}
1.53      paf       292:        vclass.add_native_method("while", Method::CT_ANY, _while, 2, 2);
1.28      paf       293: 
                    294:        // ^use[file]
1.53      paf       295:        vclass.add_native_method("use", Method::CT_ANY, _use, 1, 1);
1.29      paf       296: 
1.31      paf       297:        // ^for[i;from-number;to-number-inclusive]{code}[delim]
1.53      paf       298:        vclass.add_native_method("for", Method::CT_ANY, _for, 3+1, 3+1+1);
1.33      paf       299: 
                    300:        // ^eval(expr)
                    301:        // ^eval(expr)[format]
1.53      paf       302:        vclass.add_native_method("eval", Method::CT_ANY, _eval, 1, 2);
1.31      paf       303: 
1.29      paf       304: 
                    305:        // math functions
                    306: 
                    307:        // ^round(expr) 
1.53      paf       308:        vclass.add_native_method("round", Method::CT_ANY, _round, 1, 1);
1.29      paf       309: 
                    310:        // ^floor(expr) 
1.53      paf       311:        vclass.add_native_method("floor", Method::CT_ANY, _floor, 1, 1);
1.29      paf       312: 
                    313:        // ^ceiling(expr)       
1.53      paf       314:        vclass.add_native_method("ceiling", Method::CT_ANY, _ceiling, 1, 1);
1.29      paf       315: 
                    316:        // ^abs(expr)   
1.53      paf       317:        vclass.add_native_method("abs", Method::CT_ANY, _abs, 1, 1);
1.29      paf       318: 
                    319:        // ^sign(expr)
1.53      paf       320:        vclass.add_native_method("sign", Method::CT_ANY, _sign, 1, 1);
1.1       paf       321: }

E-mail: