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

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

E-mail: