Annotation of parser3/src/types/pa_value.h, revision 1.34

1.22      paf         1: /** @file
1.24      paf         2:        Parser: Value, Method, Junction class decls.
                      3: 
1.1       paf         4:        Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
1.24      paf         5: 
1.2       paf         6:        Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
1.1       paf         7: 
1.34    ! paf         8:        $Id: pa_value.h,v 1.33 2001/03/26 08:27:28 paf Exp $
1.1       paf         9: */
                     10: 
                     11: #ifndef PA_VALUE_H
                     12: #define PA_VALUE_H
                     13: 
                     14: #include "pa_pool.h"
                     15: #include "pa_string.h"
                     16: #include "pa_array.h"
                     17: #include "pa_exception.h"
1.13      paf        18: #include "pa_globals.h"
1.1       paf        19: 
1.9       paf        20: class VStateless_class;
1.1       paf        21: class WContext;
                     22: class VAliased;
                     23: class Request;
1.7       paf        24: class VTable;
1.17      paf        25: class Junction;
                     26: class Method;
1.20      paf        27: class Hash;
1.1       paf        28: 
1.31      paf        29: ///    grandfather of all @a values in @b Parser
1.1       paf        30: class Value : public Pooled {
                     31: public: // Value
                     32: 
1.22      paf        33:        /// - all: for error reporting after fail(), etc
1.1       paf        34:        virtual const char *type() const =0;
1.22      paf        35:        /// - all: for error reporting after fail(), etc
1.1       paf        36:        const String& name() const { return *fname; }
1.23      paf        37:        /** is this value defined?
1.22      paf        38:                @return for
1.25      paf        39:                - VUnknown: false
1.22      paf        40:                - others: true
                     41:        */
1.1       paf        42:        virtual bool get_defined() { return true; }
1.23      paf        43:        /** what's the meaning of this value in context of expression?
1.22      paf        44:                @return for
1.34    ! paf        45:                - VString: fstring as VDouble or this depending on return_string_as_is
1.25      paf        46:                - VBool: this
                     47:                - VDouble: this
                     48:                - VInt: this
                     49:                - VUnknown: this
1.27      paf        50:                - VFile: this
1.22      paf        51:        */
1.34    ! paf        52:        virtual Value *get_expr_result(bool return_string_as_is=false) { 
        !            53:                bark("(%s) can not be used in expression"); return 0; 
        !            54:        }
1.23      paf        55:        /** extract Hash
1.22      paf        56:                @return for
1.25      paf        57:                - VHash: fhash
                     58:                - VResponse: ffields
1.22      paf        59:        */
1.20      paf        60:        virtual Hash *get_hash() { return 0; }
1.23      paf        61:        /** extract const String
1.22      paf        62:                @return for
1.25      paf        63:                - VString: value
                     64:                - VUnknown: ""
                     65:                - VDouble: value
                     66:                - VBool: must be 0: so in ^if(1>2) it would'nt become "FALSE" string which is 'true'
1.22      paf        67:                - others: 0
                     68:                - WContext: accumulated fstring
                     69:        */
1.1       paf        70:        virtual const String *get_string() { return 0; }
1.23      paf        71:        /** extract double
1.22      paf        72:                @return for
1.25      paf        73:                - VString: value
                     74:                - VDouble: value
                     75:                - VInt: finteger
                     76:                - VBool: value
1.22      paf        77:        */
1.7       paf        78:        virtual double get_double() { bark("(%s) does not have numerical value"); return 0; }
1.23      paf        79:        /** extract bool
1.22      paf        80:                @return for
1.25      paf        81:                - VUnknown: false
                     82:                - VBool: value
                     83:                - VInt: 0 or !0
                     84:                - VDouble: 0 or !0
1.27      paf        85:                - VFile: true
1.22      paf        86:        */
1.15      paf        87:        virtual bool get_bool() { bark("(%s) does not have logical value"); return 0; }
1.23      paf        88:        /** extract Junction
1.22      paf        89:                @return for
                     90:                - junction: itself
                     91:        */
1.1       paf        92:        virtual Junction *get_junction() { return 0; }
1.23      paf        93:        /** extract Value element
1.22      paf        94:                @return for
1.25      paf        95:                - VHash: (key)=value
1.29      paf        96:                - VAliased: $CLASS, $BASE
                     97:                - VStateless_class: +$method
                     98:                - VStateless_object: +$method
1.25      paf        99:                - VCode_frame: wcontext_transparent
1.26      paf       100:                - VMethod_frame: my or self_transparent
1.25      paf       101:                - VTable: column
1.28      paf       102:                - VEnv: field
1.25      paf       103:                - VForm: CLASS,BASE,method,field
                    104:                - VString: $CLASS,$BASE,$method
1.30      paf       105:                - VRequest: fields
1.25      paf       106:                - VResponse: CLASS,BASE,method,fields
1.28      paf       107:                - VCookie: field
1.27      paf       108:                - VFile: CLASS,BASE,method,field
1.26      paf       109:                */
1.7       paf       110:        virtual Value *get_element(const String& name) { bark("(%s) does not have elements"); return 0; }
1.31      paf       111:        /** store Value element under @a name
1.22      paf       112:                @return for
1.25      paf       113:                - VHash: (key)=value
                    114:                - VStateless_object: (CLASS)=vclass;(BASE)=base;(method)=method_ref
1.26      paf       115:                - VStateless_class: (field)=value - static values only
                    116:                - VStateless_object: (field)=value
1.25      paf       117:                - VCode_frame: wcontext_transparent
1.26      paf       118:                - VMethod_frame: my or self_transparent
1.25      paf       119:                - VResponse: (attribute)=value
                    120:                - VCookie: field
1.22      paf       121:        */
1.7       paf       122:        virtual void put_element(const String& name, Value *value) { bark("(%s) does not accept elements"); }
1.23      paf       123:        /** extract VStateless_class
1.22      paf       124:                @return for
1.25      paf       125:                - VStateless_class: this
                    126:                - VStateless_object: fclass_real
1.26      paf       127:                - WContext: none yet | transparent
1.22      paf       128:        */
1.9       paf       129:        virtual VStateless_class *get_class() { return 0; }
1.23      paf       130:        /** extract VAliased
1.22      paf       131:                @return for
1.33      paf       132:                - VAliased: this
1.26      paf       133:                - WContext: transparent
                    134:                - VMethod_frame: self_transparent
1.22      paf       135:        */
1.1       paf       136:        virtual VAliased *get_aliased() { return 0; }
1.32      paf       137: 
                    138:        /** extract VTable
                    139:                @return for
                    140:                - VTable: this
                    141:        */
                    142:        virtual VTable *get_vtable() { return 0; }
1.1       paf       143: 
                    144: public: // usage
                    145: 
                    146:        Value(Pool& apool) : Pooled(apool), fname(unnamed_name) {
                    147:        }
                    148: 
1.22      paf       149:        /// set's the name which is used in error messages
1.1       paf       150:        void set_name(const String& aname) { fname=&aname; }
                    151: 
1.26      paf       152:        /// @return sure String. if it doesn't have string value barks
1.1       paf       153:        const String& as_string() {
                    154:                const String *result=get_string(); 
                    155:                if(!result)
1.7       paf       156:                        bark("(%s) not a string");
1.6       paf       157: 
1.1       paf       158:                return *result;
                    159:        }
                    160: 
                    161: private:
                    162: 
                    163:        const String *fname;
                    164: 
1.6       paf       165: protected: 
1.1       paf       166: 
1.22      paf       167:        /// throws exception specifying bark-reason and name() type() of problematic value
                    168:        void bark(char *reason) const {
1.1       paf       169:                THROW(0,0,
                    170:                        &name(),
1.22      paf       171:                        reason, type());
1.1       paf       172:        }
                    173: 
1.17      paf       174: };
                    175: 
1.22      paf       176: /// native code method
1.17      paf       177: typedef void (*Native_code_ptr)(Request& request, 
                    178:                                                                const String& method_name, Array *params);
                    179: 
1.23      paf       180: /** \b junction is some code joined with context of it's evaluation.
1.22      paf       181: 
                    182:        there are code-junctions and method-junctions
                    183:        - code-junctions are used when some parameter passed in cury brackets
                    184:        - method-junctions used in ^method[] calls or $method references
                    185: */
1.17      paf       186: class Junction : public Pooled {
                    187: public:
                    188: 
                    189:        Junction(Pool& apool,
                    190:                Value& aself,
                    191:                VStateless_class *avclass, const Method *amethod,
                    192:                Value *aroot,
                    193:                Value *arcontext,
                    194:                WContext *awcontext,
                    195:                const Array *acode) : Pooled(apool),
                    196:                
                    197:                self(aself),
                    198:                vclass(avclass), method(amethod),
                    199:                root(aroot),
                    200:                rcontext(arcontext),
                    201:                wcontext(awcontext),
                    202:                code(acode) {
                    203:        }
                    204: 
1.22      paf       205:        /// always present
1.17      paf       206:        Value& self;
1.22      paf       207:        //@{
                    208:        /// @name either these // so called 'method-junction'
1.17      paf       209:        VStateless_class *vclass;  const Method *method;
1.22      paf       210:        //@}
                    211:        //@{
                    212:        /// @name or these are present // so called 'code-junction'
1.17      paf       213:        Value *root;
                    214:        Value *rcontext;
                    215:        WContext *wcontext;
                    216:        const Array *code;
1.22      paf       217:        //@}
1.17      paf       218: };
                    219: 
1.23      paf       220: /** 
1.22      paf       221:        class method.
                    222: 
                    223:        methods can have 
                    224:        - named or
                    225:        - numbered parameters
                    226: 
                    227:        methods can be
                    228:        - parser or 
                    229:        - native onces
                    230: 
                    231:        hold
                    232:        - parameter names or number limits
                    233:        - local names
                    234:        - code [parser or native]
                    235: */
1.17      paf       236: class Method : public Pooled {
                    237: public:
1.22      paf       238:        /// method name for error reporting
1.17      paf       239:        const String& name;
1.22      paf       240:        //@{
                    241:        /// @name either numbered params // for native-code methods = operators
1.17      paf       242:        int min_numbered_params_count, max_numbered_params_count;
1.22      paf       243:        //@}
                    244:        //@{
                    245:        /// @name or named params&locals // for parser-code methods
1.17      paf       246:        Array *params_names;  Array *locals_names;
1.22      paf       247:        //@}
                    248:        //@{
                    249:        /// @name the Code
1.17      paf       250:        const Array *parser_code;/*OR*/Native_code_ptr native_code;
1.22      paf       251:        //@}
1.17      paf       252: 
                    253:        Method(
                    254:                Pool& apool,
                    255:                const String& aname,
                    256:                int amin_numbered_params_count, int amax_numbered_params_count,
                    257:                Array *aparams_names, Array *alocals_names,
                    258:                const Array *aparser_code, Native_code_ptr anative_code) : 
                    259: 
                    260:                Pooled(apool),
                    261:                name(aname),
                    262:                min_numbered_params_count(amin_numbered_params_count),
                    263:                max_numbered_params_count(amax_numbered_params_count),
                    264:                params_names(aparams_names), locals_names(alocals_names),
                    265:                parser_code(aparser_code), native_code(anative_code) {
                    266:        }
                    267: 
1.22      paf       268:        /// call this before invoking to ensure proper actual numbered params count
1.17      paf       269:        void check_actual_numbered_params(
                    270:                Value& self, const String& actual_name, Array *actual_numbered_params) const {
                    271:                int actual_count=actual_numbered_params?actual_numbered_params->size():0;
                    272:                if(actual_count<min_numbered_params_count) // not proper count? bark
                    273:                        THROW(0, 0,
                    274:                                &actual_name,
                    275:                                "native method of %s (%s) accepts minimum %d parameter(s)", 
                    276:                                        self.name().cstr(),
                    277:                                        self.type(),
                    278:                                        min_numbered_params_count);
                    279: 
                    280:        }
1.1       paf       281: };
                    282: 
                    283: #endif

E-mail: