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

1.32      paf         1: /** @file
1.33      paf         2:        Parser: compiler support helper functions decls.
                      3: 
1.67.2.6  paf         4:        Copyright (c) 2001-2003 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.67.2.8.2.10! (paf       11:: static const char* IDENT_COMPILE_TOOLS_H="$Date: 2003/04/03 12:40:53 $";
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.67.2.8.2.2  paf        17: 
1.1       paf        18: enum lexical_state {
1.48      parser     19:        LS_USER, LS_NAME_SQUARE_PART,
1.65      paf        20:        LS_USER_COMMENT,
1.7       paf        21:        LS_DEF_NAME,
                     22:        LS_DEF_PARAMS,
                     23:        LS_DEF_LOCALS,
                     24:        LS_DEF_COMMENT,
1.18      paf        25:        LS_DEF_SPECIAL_BODY,
1.42      parser     26:        LS_EXPRESSION_STRING_QUOTED,
                     27:        LS_EXPRESSION_STRING_APOSTROFED,
1.48      parser     28:        LS_EXPRESSION_VAR_NAME_WITH_COLON, LS_EXPRESSION_VAR_NAME_WITHOUT_COLON,
1.65      paf        29:        LS_EXPRESSION_COMMENT,
1.48      parser     30:        LS_VAR_NAME_SIMPLE_WITH_COLON, LS_VAR_NAME_SIMPLE_WITHOUT_COLON,
1.1       paf        31:        LS_VAR_NAME_CURLY,
1.22      paf        32:        LS_VAR_ROUND,
1.18      paf        33:        LS_VAR_SQUARE,
1.1       paf        34:        LS_VAR_CURLY,
                     35:        LS_METHOD_NAME,
1.18      paf        36:        LS_METHOD_SQUARE,
1.1       paf        37:        LS_METHOD_CURLY,
1.22      paf        38:        LS_METHOD_ROUND,
1.1       paf        39:        LS_METHOD_AFTER
                     40: };
1.67.2.8.2.7  paf        41: 
                     42: struct Pos {
                     43:        uint line;
                     44:        uint col;
                     45:        //Pos(uint aline, uint acol): line(aline), col(acol) {}
                     46:        Pos(): line(0), col(0) {}
                     47: 
                     48:        void clear() { line=col=0; }
                     49:        operator bool() { return col!=0; }
                     50: };
                     51: 
1.38      paf        52: /// compiler status
1.67.2.8.2.8  paf        53: class Parse_control {
                     54:        const String* main_alias;
1.67.2.8.2.9  paf        55:        uint last_line_end_col;
1.67.2.8.2.8  paf        56: public:
                     57:        const String& alias_method(const String& name);
1.38      paf        58:        //@{
                     59:        /// @name input
1.67.2.8.2.2  paf        60:        Request& request;
1.67.2.7  paf        61:        VStateless_class* cclass;
1.67.2.6  paf        62:        const char* source;
1.67.2.8.2.6  paf        63:        uint file_no;
1.67.2.8.2.7  paf        64:        Pos pos;
1.38      paf        65:        //@}
                     66:        //@{
                     67:        /// @name state; initially
1.45      parser     68:        bool trim_bof;
1.38      paf        69:        int pending_state; ///< i=0
1.67.2.8.2.2  paf        70:        StringBody string; ///< lexical string accumulator
1.67.2.8.2.7  paf        71:        Pos string_start;
1.1       paf        72:        
                     73: #define MAX_LEXICAL_STATES 100
1.38      paf        74:        enum lexical_state ls; ///< =LS_USER;
1.54      paf        75:        int ls_sp; ///< =0
                     76:        enum lexical_state ls_stack[MAX_LEXICAL_STATES];
1.38      paf        77:        int brackets_nestages[MAX_LEXICAL_STATES]; ///< brackets nestage on each state
1.46      parser     78: 
1.56      paf        79:        bool in_call_value;
1.38      paf        80:        //@}
1.1       paf        81:        
1.36      paf        82:        /// output: filled input 'methods' and 'error' if any
1.5       paf        83:        char error[MAX_STRING];
1.67.2.8.2.2  paf        84: 
1.67.2.8.2.7  paf        85:        Parse_control(Request& arequest, 
1.67.2.8.2.2  paf        86:                VStateless_class* aclass,
1.67.2.8.2.8  paf        87:                const char* asource, const String* amain_alias, 
1.67.2.8.2.6  paf        88:                uint afile_no):
1.67.2.8.2.2  paf        89:                request(arequest), // input 
                     90: 
                     91:                // we were told the class to compile to?
                     92:                cclass(aclass), // until changed with @CLASS would consider operators loading
                     93: 
1.67.2.8.2.8  paf        94:                source(asource), main_alias(amain_alias),
1.67.2.8.2.2  paf        95: 
1.67.2.8.2.6  paf        96:                file_no(afile_no),
1.67.2.8.2.9  paf        97:                last_line_end_col(0),
1.67.2.8.2.2  paf        98: 
1.67.2.8.2.7  paf        99:                // initialize state
1.67.2.8.2.2  paf       100:                trim_bof(true),
                    101:                pending_state(0),
                    102:                ls(LS_USER),
                    103:                ls_sp(0),
1.67.2.8.2.9  paf       104:                in_call_value(false) {}
                    105: 
                    106:        void pos_next_line() {
                    107:                pos.line++;
                    108:                last_line_end_col=pos.col;
                    109:                pos.col=0;
                    110:        }
                    111:        void pos_prev_c() {
                    112:                if(pos.col==0) {
                    113:                        --pos.line;  pos.col=last_line_end_col;
                    114:                } else
                    115:                        --pos.col;
                    116:        }
                    117:        void ungetc() {
                    118:                source--;
                    119:                pos_prev_c();
1.67.2.8.2.2  paf       120:        }
1.1       paf       121: };
                    122: 
1.36      paf       123: /// New array // return empty array
1.67.2.8.2.2  paf       124: inline ArrayOperation* N() {
                    125:        return new ArrayOperation;
1.3       paf       126: }
                    127: 
1.67.2.8.2.10! (paf      128:: /// Assembler instruction // append ordinary instruction to ops
1.67.2.3  paf       129: inline void O(ArrayOperation& result, OPCODE code) {
                    130:        result+=Operation(code);
1.3       paf       131: }
                    132: 
1.36      paf       133: /// aPpend 'code_array' to 'result'
1.67.2.4  paf       134: inline void P(ArrayOperation& result, ArrayOperation& code_array) {
                    135:        result.append(code_array);
1.11      paf       136: }
1.36      paf       137: /// aPpend part of 'code_array', starting from offset, to 'result'
1.67.2.4  paf       138: inline void P(ArrayOperation& result, ArrayOperation& code_array, int offset) {
                    139:        result.append(code_array, offset);
1.3       paf       140: }
1.53      paf       141: 
1.67.2.2  paf       142: /// append cOde Array
1.67.2.8.2.2  paf       143: inline void OA(ArrayOperation& result, OPCODE code, ArrayOperation* code_array) {
1.67.2.3  paf       144:        result+=Operation(code); // append OP_CODE
                    145:        result+=Operation(code_array); // append 'code_array'
1.61      paf       146: }
1.1       paf       147: 
1.36      paf       148: /**
                    149:        Value Literal // returns array with 
1.67.2.8.2.10! (paf      150::       - first op: OP_VALUE instruction
        !           151::       - second op: origin (debug information)
        !           152::       - third op: string itself
1.36      paf       153: */
1.67.2.8.2.6  paf       154: inline ArrayOperation* VL(Value* value, uint file_no, uint line, uint col) {
1.61      paf       155:        // empty ops array
1.67.2.8.2.10! (paf      156::       ArrayOperation& result=*N();
1.61      paf       157: 
                    158:        // append 'value' to 'result'
1.67.2.8.2.10! (paf      159::       result+=Operation(OP_VALUE);
        !           160::       result+=Operation(file_no, line, col); // append origin
        !           161::       result+=Operation(value); // append 'value'
1.61      paf       162: 
1.67.2.8.2.10! (paf      163::       return &result;
1.61      paf       164: }
                    165: 
1.67.2.8.2.10! (paf      166:: /// Literal Array to(2) Value @return Value from literal Array OP+origin+Value
1.67.2.8.2.2  paf       167: Value* LA2V(ArrayOperation& literal_string_array, int offset=0);
1.67.2.8.2.10! (paf      168:: /// Literal Array to(2) String  @return String value from literal Array OP+origin+String array
1.67.2.8.2.2  paf       169: inline const String* LA2S(ArrayOperation& literal_string_array, int offset=0) {
                    170:        if(Value* value=LA2V(literal_string_array, offset))
                    171:                return value->get_string();
1.67.2.8.2.1  paf       172:        return 0;
1.36      paf       173: }
1.61      paf       174: 
1.67.2.3  paf       175: inline void change_string_literal_to_write_string_literal(ArrayOperation& literal_string_array) {
1.67.2.8.2.4  paf       176:        literal_string_array.put(0, OP_STRING__WRITE);
1.61      paf       177: }
                    178: 
                    179: 
1.67.2.3  paf       180: void change_string_literal_to_double_literal(ArrayOperation& literal_string_array);
                    181: 
1.67.2.8.2.1  paf       182: void change_string_literal_value(ArrayOperation& literal_string_array, const String& new_value);
1.67.2.3  paf       183: 
                    184: void changetail_or_append(ArrayOperation& opcodes, 
                    185:                                                  OPCODE find, bool with_argument, OPCODE replace, OPCODE notfound);
1.64      paf       186: 
1.1       paf       187: 
1.67.2.8.2.7  paf       188: void push_LS(Parse_control& pc, lexical_state new_state);
                    189: void pop_LS(Parse_control& pc);
1.1       paf       190: 
                    191: #endif

E-mail: