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

1.32      paf         1: /** @file
1.33      paf         2:        Parser: compiler support helper functions decls.
                      3: 
1.72      paf         4:        Copyright (c) 2001-2004 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.73    ! paf        11: static const char * const IDENT_COMPILE_TOOLS_H="$Date: 2004/02/11 15:33:15 $";
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;
                     65:        const char* source;
                     66:        uint file_no;
                     67:        Pos pos;
1.38      paf        68:        //@}
                     69:        //@{
                     70:        /// @name state; initially
1.45      parser     71:        bool trim_bof;
1.38      paf        72:        int pending_state; ///< i=0
1.69      paf        73:        String::Body string; ///< lexical string accumulator
1.68      paf        74:        Pos string_start;
1.1       paf        75:        
                     76: #define MAX_LEXICAL_STATES 100
1.38      paf        77:        enum lexical_state ls; ///< =LS_USER;
1.54      paf        78:        int ls_sp; ///< =0
                     79:        enum lexical_state ls_stack[MAX_LEXICAL_STATES];
1.38      paf        80:        int brackets_nestages[MAX_LEXICAL_STATES]; ///< brackets nestage on each state
1.46      parser     81: 
1.56      paf        82:        bool in_call_value;
1.38      paf        83:        //@}
1.1       paf        84:        
1.36      paf        85:        /// output: filled input 'methods' and 'error' if any
1.5       paf        86:        char error[MAX_STRING];
1.68      paf        87: 
                     88:        Parse_control(Request& arequest, 
                     89:                VStateless_class* aclass,
                     90:                const char* asource, const String* amain_alias, 
1.73    ! paf        91:                uint afile_no,
        !            92:                int line_no_offset):
1.70      paf        93:                main_alias(amain_alias),
                     94:                last_line_end_col(0),
                     95: 
1.68      paf        96:                request(arequest), // input 
                     97: 
                     98:                // we were told the class to compile to?
                     99:                cclass(aclass), // until changed with @CLASS would consider operators loading
1.70      paf       100:                source(asource), 
1.68      paf       101:                file_no(afile_no),
1.73    ! paf       102:                pos(line_no_offset, 0),
1.68      paf       103: 
                    104:                // initialize state
                    105:                trim_bof(true),
                    106:                pending_state(0),
                    107:                ls(LS_USER),
                    108:                ls_sp(0),
                    109:                in_call_value(false) {}
                    110: 
                    111:        void pos_next_line() {
                    112:                pos.line++;
                    113:                last_line_end_col=pos.col;
                    114:                pos.col=0;
                    115:        }
                    116:        void pos_next_c(char c) {
                    117:                if(c=='\t')
                    118:                        pos.col=(pos.col+TAB_SIZE)&~(TAB_SIZE-1);
                    119:                else
                    120:                        pos.col++;
                    121:        }
                    122:        /// not precise in case of \t in the middle of the text
                    123:        void pos_prev_c() {
                    124:                if(pos.col==0) {
                    125:                        --pos.line;  pos.col=last_line_end_col;
                    126:                } else
                    127:                        --pos.col;
                    128:        }
                    129:        void ungetc() {
                    130:                source--;
                    131:                pos_prev_c();
                    132:        }
1.1       paf       133: };
                    134: 
1.36      paf       135: /// New array // return empty array
1.68      paf       136: inline ArrayOperation* N() {
                    137:        return new ArrayOperation;
1.3       paf       138: }
                    139: 
1.36      paf       140: /// Assembler instruction // append ordinary instruction to ops
1.68      paf       141: inline void O(ArrayOperation& result, OPCODE code) {
                    142:        result+=Operation(code);
1.3       paf       143: }
                    144: 
1.36      paf       145: /// aPpend 'code_array' to 'result'
1.68      paf       146: inline void P(ArrayOperation& result, ArrayOperation& code_array) {
                    147:        result.append(code_array);
1.11      paf       148: }
1.36      paf       149: /// aPpend part of 'code_array', starting from offset, to 'result'
1.68      paf       150: inline void P(ArrayOperation& result, ArrayOperation& code_array, int offset) {
                    151:        result.append(code_array, offset);
1.3       paf       152: }
1.53      paf       153: 
1.68      paf       154: /// append cOde Array
                    155: inline void OA(ArrayOperation& result, OPCODE code, ArrayOperation* code_array) {
                    156:        result+=Operation(code); // append OP_CODE
                    157:        result+=Operation(code_array); // append 'code_array'
1.61      paf       158: }
1.1       paf       159: 
1.36      paf       160: /**
                    161:        Value Literal // returns array with 
1.68      paf       162:        - first op: OP_VALUE instruction
                    163:        - second op: origin (debug information)
                    164:        - third op: string itself
1.36      paf       165: */
1.68      paf       166: inline ArrayOperation* VL(Value* value, uint file_no, uint line, uint col) {
1.61      paf       167:        // empty ops array
1.68      paf       168:        ArrayOperation& result=*N();
1.61      paf       169: 
                    170:        // append 'value' to 'result'
1.68      paf       171:        result+=Operation(OP_VALUE);
                    172:        result+=Operation(file_no, line, col); // append origin
                    173:        result+=Operation(value); // append 'value'
                    174: 
                    175:        return &result;
1.61      paf       176: }
                    177: 
1.68      paf       178: /// Literal Array to(2) Value @return Value from literal Array OP+origin+Value
                    179: Value* LA2V(ArrayOperation& literal_string_array, int offset=0);
                    180: /// Literal Array to(2) String  @return String value from literal Array OP+origin+String array
                    181: inline const String* LA2S(ArrayOperation& literal_string_array, int offset=0) {
                    182:        if(Value* value=LA2V(literal_string_array, offset))
1.37      paf       183:                return value->get_string();
                    184:        return 0;
1.36      paf       185: }
1.61      paf       186: 
1.68      paf       187: inline void change_string_literal_to_write_string_literal(ArrayOperation& literal_string_array) {
                    188:        literal_string_array.put(0, OP_STRING__WRITE);
1.61      paf       189: }
                    190: 
1.68      paf       191: 
                    192: void change_string_literal_to_double_literal(ArrayOperation& literal_string_array);
                    193: 
                    194: void change_string_literal_value(ArrayOperation& literal_string_array, const String& new_value);
                    195: 
                    196: void changetail_or_append(ArrayOperation& opcodes, 
1.61      paf       197:                                                  OPCODE find, bool with_argument, OPCODE replace, OPCODE notfound);
                    198: 
1.1       paf       199: 
1.68      paf       200: void push_LS(Parse_control& pc, lexical_state new_state);
                    201: void pop_LS(Parse_control& pc);
1.1       paf       202: 
                    203: #endif

E-mail: