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

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

E-mail: