1 #ifndef __TOKEN_H__ 2 #define __TOKEN_H__ 3 4 #include "main.h" 5 #include "nmtbl.h" 6 #include "dprintf.h" 7 8 class token; 9 extern int yylex(); 10 extern char *yytext; 11 extern int yyleng; 12 extern token *curr_token; 13 14 #define TAB_WIDTH 8 15 16 17 // Token definitions 18 enum lex_token { 19 #define DEF_TOKEN(o,c,t,k) t, 20 #include "token.dpp" 21 TKN_LAST 22 }; 23 24 // Special token TAGs < 0 !!! 25 26 enum category { 27 CAT_ID, 28 CAT_STR, 29 CAT_NUM, 30 CAT_KWD, 31 CAT_WSPC, 32 CAT_PUT, 33 CAT_SPEC, 34 CAT_GEN 35 }; 36 37 class token : public heap_object { 38 public: 39 40 // First we define dl-list members 41 token *prev, *next; 42 static token dummy; // Begin of MAIN token list 43 44 static int const token_cat[]; 45 static char* const token_name[]; 46 47 static char* resolve_name_conflict(char* str); 48 token(int v_cat,int v_tag)49 token(int v_cat, int v_tag) 50 { 51 next = prev = this; 52 cat = v_cat; 53 tag = v_tag; 54 fname = NULL; 55 clone = bind = NULL; 56 } 57 token(char const* v_text, int v_tag = TKN_GEN, int v_line = 0, int v_pos = 0, 58 nm_entry *nm = NULL) 59 { 60 line = v_line; 61 attr = 0; 62 pos = v_pos; 63 tag = v_tag; 64 cat = token_cat[v_tag]; 65 out_text = in_text = (char*)v_text; 66 fname = NULL; 67 name = nm; 68 clone = bind = NULL; 69 } 70 token(token& t); 71 72 enum token_attributes { 73 fix_pos = 1, 74 from_include_file = 2 75 }; 76 77 unsigned char cat; // Token category 78 unsigned char attr; // Attribute of token 79 unsigned short tag; // Exact token code 80 unsigned short pos; // Pos. within the line of token start 81 unsigned short line; // Line number where token was found 82 83 nm_entry* name; // Corresponded name entry (!=NULL for ID) 84 85 char* in_text; // Input text representation of token 86 char* out_text; // Output text representation of token 87 88 char* fname; // Token file name 89 90 token* bind; // token position of which is taken 91 token* clone; // cloned token 92 93 insert_b(token * t)94 token* insert_b(token* t) { // insert token before (returns this) 95 next = t; prev = t->prev; 96 return t->prev = t->prev->next = this; 97 } insert_a(token * t)98 token* insert_a(token* t) { // insert token after (returns this) 99 prev = t; next = t->next; 100 return t->next = t->next->prev = this; 101 } 102 void remove(); // remove token from DL-ring 103 static void remove(token* head, token* tail); 104 static void disable(token* head, token* tail); 105 106 token* get_first_token(); // get first relevant token at line 107 prepend(char const * s)108 token* prepend(char const* s) { 109 token* t = new token(s); 110 t->pos = pos; 111 return t->insert_b(this); 112 } append(char const * s)113 token* append(char const* s) { 114 return (new token(s))->insert_a(this); 115 } disable()116 void disable() { 117 if (cat != CAT_WSPC || tag < TKN_GEN) { 118 cat = CAT_WSPC; tag = TKN_GEN; out_text = NULL; 119 } 120 } 121 void disappear(); // disable token with all white spaces after it 122 set_pos(token * t)123 void set_pos(token* t) { pos = t->pos; } 124 set_bind(token * t)125 void set_bind(token* t) { bind = t; } 126 127 token* copy(token* from, token* till); // copy list of tokens before 128 // this token, return pointer to 129 // image of 'from' token 130 token* move(token* from, token* till); // move list of tokens before this 131 // token, return pointer to from 132 token* move_region(token* head, token* tail); 133 // move region (together with 134 // comments and white spaces) 135 static void swap(token* left_head, token* left_tail, 136 token* right_head, token* right_tail); 137 138 139 token* next_relevant(); 140 token* prev_relevant(); 141 static token* first_relevant(); 142 static token* last_relevant(); 143 set_trans(char const * str)144 void set_trans (char const* str) { 145 cat = CAT_GEN; 146 out_text = (char*)str; 147 } 148 149 static void input(char *file); 150 static void output(char *file); 151 reset()152 static void reset() { dummy.next = dummy.prev = &dummy; curr_token = NULL; } 153 }; 154 155 class output_context { 156 private: 157 FILE* f; 158 int pos; 159 int line; 160 int prev_tag; 161 162 public: file()163 FILE* file() { return f; } 164 165 void output(token* t); 166 output_context(char* file_name); 167 ~output_context(); 168 }; 169 170 #endif 171 172 173