Annotation of parser3/src/main/compile_tools.h, revision 1.82

1.32      paf         1: /** @file
1.33      paf         2:        Parser: compiler support helper functions decls.
                      3: 
1.82    ! misha       4:        Copyright (c) 2001-2009 ArtLebedev Group (http://www.artlebedev.com)
1.58      paf         5:        Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.1       paf         6: */
                      7: 
                      8: #ifndef COMPILE_TOOLS
                      9: #define COMPILE_TOOLS
1.62      paf        10: 
1.82    ! misha      11: static const char * const IDENT_COMPILE_TOOLS_H="$Date: 2008-09-02 16:14:29 $";
1.1       paf        12: 
1.39      paf        13: #include "pa_opcode.h"
1.1       paf        14: #include "pa_types.h"
1.10      paf        15: #include "pa_vstring.h"
1.14      paf        16: #include "pa_request.h"
1.1       paf        17: 
1.68      paf        18: /// used to track source column number
                     19: #define TAB_SIZE 8
                     20: 
1.1       paf        21: enum lexical_state {
1.48      parser     22:        LS_USER, LS_NAME_SQUARE_PART,
1.65      paf        23:        LS_USER_COMMENT,
1.7       paf        24:        LS_DEF_NAME,
                     25:        LS_DEF_PARAMS,
                     26:        LS_DEF_LOCALS,
                     27:        LS_DEF_COMMENT,
1.18      paf        28:        LS_DEF_SPECIAL_BODY,
1.42      parser     29:        LS_EXPRESSION_STRING_QUOTED,
                     30:        LS_EXPRESSION_STRING_APOSTROFED,
1.48      parser     31:        LS_EXPRESSION_VAR_NAME_WITH_COLON, LS_EXPRESSION_VAR_NAME_WITHOUT_COLON,
1.65      paf        32:        LS_EXPRESSION_COMMENT,
1.48      parser     33:        LS_VAR_NAME_SIMPLE_WITH_COLON, LS_VAR_NAME_SIMPLE_WITHOUT_COLON,
1.1       paf        34:        LS_VAR_NAME_CURLY,
1.22      paf        35:        LS_VAR_ROUND,
1.18      paf        36:        LS_VAR_SQUARE,
1.1       paf        37:        LS_VAR_CURLY,
                     38:        LS_METHOD_NAME,
1.18      paf        39:        LS_METHOD_SQUARE,
1.1       paf        40:        LS_METHOD_CURLY,
1.22      paf        41:        LS_METHOD_ROUND,
1.1       paf        42:        LS_METHOD_AFTER
                     43: };
1.68      paf        44: 
                     45: struct Pos {
1.73      paf        46:        int line;
                     47:        int col;
                     48:        Pos(int aline, int acol): line(aline), col(acol) {}
1.68      paf        49:        Pos(): line(0), col(0) {}
                     50: 
                     51:        void clear() { line=col=0; }
                     52:        operator bool() { return col!=0; }
                     53: };
                     54: 
1.38      paf        55: /// compiler status
1.68      paf        56: class Parse_control {
                     57:        const String* main_alias;
                     58:        uint last_line_end_col;
                     59: public:
                     60:        const String& alias_method(const String& name);
1.38      paf        61:        //@{
                     62:        /// @name input
1.68      paf        63:        Request& request;
                     64:        VStateless_class* cclass;
1.79      misha      65:        VStateless_class* cclass_new;
1.78      misha      66:        ArrayClass* cclasses;
1.68      paf        67:        const char* source;
                     68:        uint file_no;
                     69:        Pos pos;
1.38      paf        70:        //@}
                     71:        //@{
                     72:        /// @name state; initially
1.45      parser     73:        bool trim_bof;
1.38      paf        74:        int pending_state; ///< i=0
1.69      paf        75:        String::Body string; ///< lexical string accumulator
1.68      paf        76:        Pos string_start;
1.1       paf        77:        
                     78: #define MAX_LEXICAL_STATES 100
1.38      paf        79:        enum lexical_state ls; ///< =LS_USER;
1.54      paf        80:        int ls_sp; ///< =0
                     81:        enum lexical_state ls_stack[MAX_LEXICAL_STATES];
1.38      paf        82:        int brackets_nestages[MAX_LEXICAL_STATES]; ///< brackets nestage on each state
1.46      parser     83: 
1.56      paf        84:        bool in_call_value;
1.74      paf        85:        bool explicit_result;
1.79      misha      86:        bool append;
1.82    ! misha      87:        bool write_to_result;
1.38      paf        88:        //@}
1.1       paf        89:        
1.36      paf        90:        /// output: filled input 'methods' and 'error' if any
1.5       paf        91:        char error[MAX_STRING];
1.68      paf        92: 
                     93:        Parse_control(Request& arequest, 
                     94:                VStateless_class* aclass,
                     95:                const char* asource, const String* amain_alias, 
1.73      paf        96:                uint afile_no,
                     97:                int line_no_offset):
1.70      paf        98:                main_alias(amain_alias),
                     99:                last_line_end_col(0),
                    100: 
1.68      paf       101:                request(arequest), // input 
                    102: 
                    103:                // we were told the class to compile to?
                    104:                cclass(aclass), // until changed with @CLASS would consider operators loading
1.79      misha     105:                cclass_new(0)1.78      misha     106:                cclasses(new ArrayClass(1)),
1.70      paf       107:                source(asource), 
1.68      paf       108:                file_no(afile_no),
1.73      paf       109:                pos(line_no_offset, 0),
1.68      paf       110: 
                    111:                // initialize state
                    112:                trim_bof(true),
                    113:                pending_state(0),
                    114:                ls(LS_USER),
                    115:                ls_sp(0),
1.74      paf       116:                in_call_value(false),
1.79      misha     117:                explicit_result(false),
1.82    ! misha     118:                append(false),
        !           119:                write_to_result(false) {
1.78      misha     120: 
                    121:                *cclasses+=aclass;
                    122:        }
1.68      paf       123: 
1.79      misha     124:        void class_add(){
                    125:                if(cclass_new){
                    126:                        cclass=cclass_new;
                    127:                        // append to request's classes
                    128:                        request.classes().put(cclass->name(), cclass);
                    129:                        *cclasses+=cclass;
                    130:                        cclass_new=0;
                    131:                        append=false;
                    132:                }
                    133:        }
                    134: 
1.81      misha     135:        VStateless_class* get_existed_class(VStateless_class* aclass){
                    136:                if(aclass){
                    137:                        if(Value* class_value=request.classes().get(aclass->name())){
                    138:                                return class_value->get_class();
1.79      misha     139:                        }
                    140:                }
1.81      misha     141:                return 0;
                    142:        }
                    143: 
                    144:        bool reuse_existed_class(VStateless_class* aclass){
                    145:                if(aclass->is_partial()){
                    146:                        cclass=aclass;
                    147:                        cclass_new=0;
                    148:                        append=true;
                    149:                        return true;
                    150:                } else {
                    151:                        return false;
                    152:                }
1.79      misha     153:        }
                    154: 
                    155:        void set_all_vars_local(){
                    156:                if(cclass_new){
1.81      misha     157:                        cclass_new->set_all_vars_local();
1.79      misha     158:                } else {
1.81      misha     159:                        cclass->set_all_vars_local();
1.79      misha     160:                }
                    161:        }
                    162: 
1.68      paf       163:        void pos_next_line() {
                    164:                pos.line++;
                    165:                last_line_end_col=pos.col;
                    166:                pos.col=0;
                    167:        }
1.77      paf       168:        void pos_next_c(int c) {
1.68      paf       169:                if(c=='\t')
                    170:                        pos.col=(pos.col+TAB_SIZE)&~(TAB_SIZE-1);
                    171:                else
                    172:                        pos.col++;
                    173:        }
                    174:        /// not precise in case of \t in the middle of the text
                    175:        void pos_prev_c() {
                    176:                if(pos.col==0) {
                    177:                        --pos.line;  pos.col=last_line_end_col;
                    178:                } else
                    179:                        --pos.col;
                    180:        }
                    181:        void ungetc() {
                    182:                source--;
                    183:                pos_prev_c();
                    184:        }
1.1       paf       185: };
                    186: 
1.36      paf       187: /// New array // return empty array
1.68      paf       188: inline ArrayOperation* N() {
                    189:        return new ArrayOperation;
1.3       paf       190: }
                    191: 
1.36      paf       192: /// Assembler instruction // append ordinary instruction to ops
1.80      misha     193: inline void O(ArrayOperation& result, OP::OPCODE code) {
1.68      paf       194:        result+=Operation(code);
1.3       paf       195: }
                    196: 
1.36      paf       197: /// aPpend 'code_array' to 'result'
1.68      paf       198: inline void P(ArrayOperation& result, ArrayOperation& code_array) {
                    199:        result.append(code_array);
1.11      paf       200: }
1.36      paf       201: /// aPpend part of 'code_array', starting from offset, to 'result'
1.68      paf       202: inline void P(ArrayOperation& result, ArrayOperation& code_array, int offset) {
                    203:        result.append(code_array, offset);
1.3       paf       204: }
1.53      paf       205: 
1.68      paf       206: /// append cOde Array
1.80      misha     207: inline void OA(ArrayOperation& result, OP::OPCODE code, ArrayOperation* code_array) {
1.68      paf       208:        result+=Operation(code); // append OP_CODE
                    209:        result+=Operation(code_array); // append 'code_array'
1.61      paf       210: }
1.1       paf       211: 
1.36      paf       212: /**
                    213:        Value Literal // returns array with 
1.68      paf       214:        - first op: OP_VALUE instruction
                    215:        - second op: origin (debug information)
                    216:        - third op: string itself
1.36      paf       217: */
1.68      paf       218: inline ArrayOperation* VL(Value* value, uint file_no, uint line, uint col) {
1.61      paf       219:        // empty ops array
1.68      paf       220:        ArrayOperation& result=*N();
1.61      paf       221: 
                    222:        // append 'value' to 'result'
1.80      misha     223:        result+=Operation(OP::OP_VALUE);
1.68      paf       224:        result+=Operation(file_no, line, col); // append origin
                    225:        result+=Operation(value); // append 'value'
                    226: 
                    227:        return &result;
1.61      paf       228: }
                    229: 
1.68      paf       230: /// Literal Array to(2) Value @return Value from literal Array OP+origin+Value
                    231: Value* LA2V(ArrayOperation& literal_string_array, int offset=0);
                    232: /// Literal Array to(2) String  @return String value from literal Array OP+origin+String array
                    233: inline const String* LA2S(ArrayOperation& literal_string_array, int offset=0) {
                    234:        if(Value* value=LA2V(literal_string_array, offset))
1.37      paf       235:                return value->get_string();
                    236:        return 0;
1.36      paf       237: }
1.61      paf       238: 
1.68      paf       239: inline void change_string_literal_to_write_string_literal(ArrayOperation& literal_string_array) {
1.80      misha     240:        literal_string_array.put(0, OP::OP_STRING__WRITE);
1.61      paf       241: }
                    242: 
1.68      paf       243: 
1.75      paf       244: void maybe_change_string_literal_to_double_literal(ArrayOperation& literal_string_array);
1.68      paf       245: 
                    246: void change_string_literal_value(ArrayOperation& literal_string_array, const String& new_value);
                    247: 
                    248: void changetail_or_append(ArrayOperation& opcodes, 
1.80      misha     249:                                                  OP::OPCODE find, bool with_argument, OP::OPCODE replace, OP::OPCODE notfound);
1.61      paf       250: 
1.1       paf       251: 
1.68      paf       252: void push_LS(Parse_control& pc, lexical_state new_state);
                    253: void pop_LS(Parse_control& pc);
1.1       paf       254: 
                    255: #endif

E-mail: