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

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

E-mail: