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

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

E-mail: