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

1.22      paf         1: /** @file
1.24      paf         2:        Parser: Value, Method, Junction class decls.
                      3: 
1.109     paf         4:        Copyright (c) 2001, 2003 ArtLebedev Group (http://www.artlebedev.com)
1.79      paf         5:        Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.1       paf         6: */
                      7: 
                      8: #ifndef PA_VALUE_H
                      9: #define PA_VALUE_H
1.87      paf        10: 
1.109.6.1! paf        11: static const char* IDENT_VALUE_H="$Date: 2003/01/21 15:51:17 $";
1.1       paf        12: 
                     13: #include "pa_pool.h"
                     14: #include "pa_string.h"
                     15: #include "pa_array.h"
                     16: #include "pa_exception.h"
1.13      paf        17: #include "pa_globals.h"
1.1       paf        18: 
1.99      paf        19: // forwards
                     20: 
1.9       paf        21: class VStateless_class;
1.1       paf        22: class WContext;
                     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.42      paf        28: class VFile;
1.64      parser     29: class MethodParams;
1.93      paf        30: class VObject;
1.99      paf        31: class VMethodFrame;
1.1       paf        32: 
1.31      paf        33: ///    grandfather of all @a values in @b Parser
1.1       paf        34: class Value : public Pooled {
                     35: public: // Value
                     36: 
1.106     paf        37:        /// value type, used for error reporting and 'is' expression operator
1.1       paf        38:        virtual const char *type() const =0;
1.89      paf        39: 
1.91      paf        40:        /** remember derived class instance 
                     41:            - VObject: the only client
                     42:        */
1.93      paf        43:        virtual VObject *set_derived(VObject * /*aderived*/) { return 0; }
1.91      paf        44: 
1.89      paf        45:        /**
1.95      paf        46:                all except VObject/VClass: this if @atype eq type()
                     47:                VObject/VClass: can locate parent class by it's type
1.89      paf        48:        */
1.96      paf        49:        virtual Value *as(const char *atype, bool looking_up) {
                     50:                return atype && strcmp(type(), atype)==0?this:0;
1.89      paf        51:        }
1.97      paf        52:        /// type checking helper, uses Value::as
                     53:        bool is(const char *atype) { return as(atype, false)!=0; }
1.59      parser     54:        
1.106     paf        55:        /// is this value defined?
1.38      paf        56:        virtual bool is_defined() const { return true; }
1.59      parser     57:        
1.106     paf        58:        /// is this value string?
1.38      paf        59:        virtual bool is_string() const { return false; }
1.59      parser     60:        
1.106     paf        61:        /// what's the meaning of this value in context of expression?
1.75      parser     62:        virtual Value *as_expr_result(bool /*return_string_as_is*/=false) { 
1.34      paf        63:                bark("(%s) can not be used in expression"); return 0; 
                     64:        }
1.59      parser     65:        
1.106     paf        66:        /// extract Hash
1.76      parser     67:        virtual Hash *get_hash(const String * /*source*/) { return 0; }
1.59      parser     68:        
1.106     paf        69:        /// extract const String
1.1       paf        70:        virtual const String *get_string() { return 0; }
1.59      parser     71:        
1.106     paf        72:        /// extract double
1.72      parser     73:        virtual double as_double() const { bark("(%s) does not have numerical (double) value"); return 0; }
1.59      parser     74:        
1.106     paf        75:        /// extract integer
1.72      parser     76:        virtual int as_int () const { bark("(%s) does not have numerical (int) value"); return 0; }
1.59      parser     77: 
1.106     paf        78:        /// extract bool
1.72      parser     79:        virtual bool as_bool() const { bark("(%s) does not have logical value"); return 0; }
1.59      parser     80:        
1.106     paf        81:        /// extract file
1.75      parser     82:        virtual VFile *as_vfile(String::Untaint_lang /*lang*/=String::UL_UNSPECIFIED,
                     83:                bool /*origins_mode*/=false) { 
1.42      paf        84:                bark("(%s) does not have file value"); return 0; 
                     85:        }
1.59      parser     86:        
1.106     paf        87:        /// extract Junction
1.1       paf        88:        virtual Junction *get_junction() { return 0; }
1.59      parser     89:        
1.93      paf        90:        /** extract base object of Value
                     91:                @return for
                     92:                - VObject: fbase
                     93:        */
                     94:        virtual Value *base_object() { bark("(%s) has no base object"); return 0; }
                     95:        
1.106     paf        96:        /// extract Value element
1.108     paf        97:        virtual Value *get_element(const String& /*aname*/, Value& /*aself*/, bool /*looking_up*/) { bark("(%s) has no elements"); return 0; }
1.92      paf        98: 
1.106     paf        99:        /// store Value element under @a name
1.93      paf       100:        virtual bool put_element(const String& name, Value * /*value*/, bool /*replace*/) { 
1.65      parser    101:                // to prevent modification of system classes,
                    102:                // created at system startup, and not having exception
                    103:                // handler installed, we neet to bark using request.pool
                    104:                bark("(%s) does not accept elements", 
                    105:                        "element can not be stored to %s", &name); 
1.93      paf       106:                return false;
1.65      parser    107:        }
1.59      parser    108:        
1.106     paf       109:        /// extract VStateless_class
1.85      paf       110:        virtual VStateless_class *get_class()=0;
1.107     paf       111: 
                    112:        /// extract VStateless_class
                    113:        virtual VStateless_class *get_last_derived_class() {
                    114:                return get_class();
                    115:        };
1.108     paf       116:        /// extract base object or class of Value, if any
                    117:        virtual Value *base() { bark("(%s) has can not have base"); return 0; }
1.32      paf       118: 
1.106     paf       119:        /// extract VTable
1.35      paf       120:        virtual Table *get_table() { return 0; }
1.1       paf       121: 
                    122: public: // usage
                    123: 
1.84      paf       124:        Value(Pool& apool) : Pooled(apool) {
1.68      parser    125:        }
1.1       paf       126: 
1.26      paf       127:        /// @return sure String. if it doesn't have string value barks
1.1       paf       128:        const String& as_string() {
                    129:                const String *result=get_string(); 
                    130:                if(!result)
1.35      paf       131:                        bark("(%s) has no string representation");
1.6       paf       132: 
1.1       paf       133:                return *result;
                    134:        }
                    135: 
1.6       paf       136: protected: 
1.1       paf       137: 
1.22      paf       138:        /// throws exception specifying bark-reason and name() type() of problematic value
1.109.6.1! paf       139:        void bark(const char *reason, 
1.65      parser    140:                const char *alt_reason=0, const String *problem_source=0) const {
1.80      paf       141:                throw Exception("parser.runtime",
1.84      paf       142:                        problem_source,
                    143:                        alt_reason?alt_reason:reason, type());
1.1       paf       144:        }
                    145: 
1.17      paf       146: };
                    147: 
1.23      paf       148: /** \b junction is some code joined with context of it's evaluation.
1.22      paf       149: 
                    150:        there are code-junctions and method-junctions
                    151:        - code-junctions are used when some parameter passed in cury brackets
                    152:        - method-junctions used in ^method[] calls or $method references
1.99      paf       153: 
                    154:        Junctions register themselves in method_frame [if any] for consequent invalidation.
                    155:        This prevents evaluation of junctions in outdated context
                    156: 
                    157:        To stop situations like this:
                    158: @code
                    159:        @main[]
                    160:        ^method1[]
                    161:        ^method2[]
                    162: 
                    163:        @method1[]
                    164:        $junction{
                    165:                some code
                    166:        }
                    167: 
                    168:        @method2[]
                    169:        ^junction[]
                    170: @endcode
                    171: 
1.101     paf       172:        On wcontext[most dynamic context of all] scope exit (WContext::~WContext()) got cleaned - 
                    173:        Junction::wcontext becomes WContext.fparent (if any), 
                    174:        or Junction::method_frame becomes 0 (if no parent), which later in Request::process triggers exception
                    175: 
                    176:        parent changing helps ^switch implementation to remain simple
1.22      paf       177: */
1.17      paf       178: class Junction : public Pooled {
                    179: public:
                    180: 
                    181:        Junction(Pool& apool,
1.104     paf       182:                Value& aself,
1.103     paf       183:                const Method *amethod,
1.99      paf       184:                VMethodFrame *amethod_frame,
1.17      paf       185:                Value *arcontext,
                    186:                WContext *awcontext,
1.99      paf       187:                const Array *acode);
1.81      paf       188: 
1.100     paf       189:        void reattach(WContext *new_wcontext);
1.17      paf       190: 
1.105     paf       191:        /// always present
1.104     paf       192:        Value& self;
1.22      paf       193:        //@{
                    194:        /// @name either these // so called 'method-junction'
1.103     paf       195:        const Method *method;
1.22      paf       196:        //@}
                    197:        //@{
                    198:        /// @name or these are present // so called 'code-junction'
1.99      paf       199:        VMethodFrame *method_frame;
1.17      paf       200:        Value *rcontext;
                    201:        WContext *wcontext;
                    202:        const Array *code;
1.22      paf       203:        //@}
1.48      paf       204: };
                    205: 
                    206: /**
                    207:        native code method
                    208:        params can be NULL when 
1.53      paf       209:        method min&max params (see VStateless_class::add_native_method)
1.48      paf       210:        counts are zero.        
                    211: */
1.39      paf       212: typedef void (*Native_code_ptr)(Request& request, 
                    213:                                                                const String& method_name, 
1.48      paf       214:                                                                MethodParams *params);
1.39      paf       215: 
1.23      paf       216: /** 
1.22      paf       217:        class method.
                    218: 
                    219:        methods can have 
                    220:        - named or
                    221:        - numbered parameters
                    222: 
                    223:        methods can be
                    224:        - parser or 
                    225:        - native onces
                    226: 
1.40      paf       227:        holds
1.22      paf       228:        - parameter names or number limits
                    229:        - local names
                    230:        - code [parser or native]
                    231: */
1.17      paf       232: class Method : public Pooled {
                    233: public:
1.39      paf       234: 
1.41      paf       235:        /// allowed method call types
1.39      paf       236:        enum Call_type {
1.41      paf       237:                CT_ANY, ///< method can be called either statically or dynamically
                    238:                CT_STATIC, ///< method can be called only statically
                    239:                CT_DYNAMIC ///< method can be called only dynamically
1.39      paf       240:        };
                    241:        
                    242:        /// name for error reporting
1.17      paf       243:        const String& name;
1.39      paf       244:        ///
                    245:        Call_type call_type;
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,
1.39      paf       262:                Call_type call_type,
1.17      paf       263:                int amin_numbered_params_count, int amax_numbered_params_count,
                    264:                Array *aparams_names, Array *alocals_names,
                    265:                const Array *aparser_code, Native_code_ptr anative_code) : 
                    266: 
                    267:                Pooled(apool),
                    268:                name(aname),
1.39      paf       269:                call_type(call_type),
1.17      paf       270:                min_numbered_params_count(amin_numbered_params_count),
                    271:                max_numbered_params_count(amax_numbered_params_count),
                    272:                params_names(aparams_names), locals_names(alocals_names),
                    273:                parser_code(aparser_code), native_code(anative_code) {
                    274:        }
                    275: 
1.22      paf       276:        /// call this before invoking to ensure proper actual numbered params count
1.75      parser    277:        void check_actual_numbered_params(
1.104     paf       278:                Value& self, const String& actual_name, Array *actual_numbered_params) const;
1.86      paf       279: };
                    280: 
                    281: ///    Auto-object used for temporarily substituting/removing elements
                    282: class Temp_value_element {
                    283:        Value& fwhere;
                    284:        const String& fname;
                    285:        Value *saved;
                    286: public:
                    287:        Temp_value_element(Value& awhere, const String& aname, Value *awhat) : 
                    288:                fwhere(awhere),
                    289:                fname(aname),
1.108     paf       290:                saved(awhere.get_element(aname, awhere, false)) {
1.93      paf       291:                fwhere.put_element(aname, awhat, false);
1.86      paf       292:        }
                    293:        ~Temp_value_element() { 
1.93      paf       294:                fwhere.put_element(fname, saved, false);
1.86      paf       295:        }
1.1       paf       296: };
                    297: 
                    298: #endif

E-mail: