Rev 5598 | Rev 5676 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 5598 | Rev 5631 | ||
---|---|---|---|
Line 2... | Line 2... | ||
2 | #define INCLUDE_LEXER_H |
2 | #define INCLUDE_LEXER_H |
Line 3... | Line 3... | ||
3 | 3 | ||
4 | #ifndef INCLUDE_STRING_H |
4 | #ifndef INCLUDE_STRING_H |
5 | #include "../lib/strings.h" |
5 | #include "../lib/strings.h" |
- | 6 | #endif |
|
- | 7 | ||
- | 8 | #ifndef INCLUDE_MEM_H |
|
- | 9 | #include "../lib/mem.h" |
|
6 | #endif |
10 | #endif |
7 | /** Splits text into tokens |
11 | /** Splits text into tokens |
8 | * Author :Pavel Yakovlev |
12 | * Author :Pavel Yakovlev |
- | 13 | * Homepage:https://vk.com/pavelyakov39 |
|
9 | * Homepage:https://vk.com/pavelyakov39 |
14 | * Ver. : 1.51 |
Line 10... | Line 15... | ||
10 | */ |
15 | */ |
11 | 16 | ||
12 | /** Example: |
17 | /** Example: |
Line 47... | Line 52... | ||
47 | 52 | ||
48 | :char const_token_lexer[1024]; |
53 | :char const_token_lexer[1024]; |
49 | :struct lexer |
54 | :struct lexer |
50 | { |
55 | { |
- | 56 | byte cmd; |
|
51 | byte cmd; |
57 | dword token,text,mem_list,count,buffer_loading; |
52 | dword token,text; |
58 | dword str_buffer; |
53 | byte type; |
59 | byte type; |
54 | char quote; |
60 | char quote; |
55 | signed count,length; |
61 | signed length; |
56 | dword next(void); |
62 | dword next(void); |
- | 63 | dword back(void); |
|
- | 64 | dword list(void); |
|
- | 65 | void free(void); |
|
57 | dword back(void); |
66 | dword position(dword __); |
58 | void load(dword _text); |
67 | void load(dword _text); |
59 | void expected(dword _text); |
68 | void expected(dword _text); |
- | 69 | }; |
|
- | 70 | :dword back(void) |
|
Line -... | Line 71... | ||
- | 71 | { |
|
- | 72 | ||
- | 73 | } |
|
- | 74 | :dword lexer::list(void) |
|
- | 75 | { |
|
- | 76 | dword count_mem,buf_loop,pos; |
|
- | 77 | count_mem = 0; |
|
- | 78 | buf_loop = 5000; // на тыс элементов. |
|
- | 79 | count = 0; |
|
- | 80 | buffer_loading = malloc(buf_loop); |
|
- | 81 | pos = buffer_loading; |
|
- | 82 | while(type!=LEX_END) |
|
- | 83 | { |
|
- | 84 | pos+=count_mem; |
|
- | 85 | next(); |
|
- | 86 | DSDWORD[pos] = strndup(token,length); |
|
- | 87 | pos+=4; |
|
- | 88 | DSBYTE [pos] = type; |
|
- | 89 | pos++; |
|
- | 90 | count++; |
|
- | 91 | if(pos-buffer_loading>buf_loop) |
|
- | 92 | { |
|
- | 93 | buf_loop*=2; |
|
- | 94 | buffer_loading = realloc(buffer_loading,buf_loop); |
|
- | 95 | } |
|
- | 96 | } |
|
- | 97 | return buffer_loading; |
|
- | 98 | } |
|
- | 99 | :void lexer::free(void) |
|
- | 100 | { |
|
- | 101 | dword z; |
|
- | 102 | z = count; |
|
- | 103 | while(z) |
|
- | 104 | { |
|
- | 105 | z--; |
|
- | 106 | position(z); |
|
- | 107 | ::free(token); |
|
- | 108 | } |
|
- | 109 | count = 0; |
|
- | 110 | ::free(buffer_loading); |
|
- | 111 | } |
|
- | 112 | :dword lexer::position(dword __) |
|
- | 113 | { |
|
- | 114 | dword pos1; |
|
- | 115 | if(!count)list(); |
|
- | 116 | if(__>=count)__=count-1; |
|
- | 117 | else if(__<0)__=0; |
|
- | 118 | pos1 = __*5; |
|
- | 119 | pos1 += buffer_loading; |
|
- | 120 | token = DSDWORD[pos1]; |
|
- | 121 | pos1++; |
|
60 | }; |
122 | type = DSBYTE[pos1]; |
61 | 123 | return token; |
|
62 | 124 | } |
|
63 | :void expected(dword _text) |
125 | :void lexer::expected(dword _text) |
64 | { |
126 | { |
65 | notify(_text); |
127 | notify(_text); |
Line 66... | Line 128... | ||
66 | ExitProcess(); |
128 | ExitProcess(); |
67 | } |
129 | } |
68 | 130 | ||
- | 131 | :void lexer::load(dword _text) |
|
- | 132 | { |
|
69 | :void lexer::load(dword _text) |
133 | text = _text; |
Line 70... | Line 134... | ||
70 | { |
134 | count = 0; |
71 | text = _text; |
135 | str_buffer = 0; |
72 | } |
136 | } |
- | 137 | ||
73 | 138 | :dword lexer::next(void) |
|
74 | :dword lexer::next(void) |
139 | { |
75 | { |
140 | char s; |
76 | char s; |
- | |
- | 141 | dword len_str_buf,tmp; |
|
- | 142 | dword pos,in; |
|
77 | dword pos,in; |
143 | pos = #const_token_lexer; |
78 | pos = #const_token_lexer; |
144 | in = text; |
79 | in = text; |
145 | //len_str_buf = 1024; |
80 | 146 | if(str_buffer)::free(str_buffer); |
|
81 | NEXT_TOKEN: |
147 | NEXT_TOKEN: |
Line 182... | Line 248... | ||
182 | } |
248 | } |
183 | else if(s=='"')||(s=='\'') |
249 | else if(s=='"')||(s=='\'') |
184 | { |
250 | { |
185 | quote = s; |
251 | quote = s; |
186 | in++; |
252 | in++; |
- | 253 | tmp = in; |
|
187 | s = DSBYTE[in]; |
254 | s = DSBYTE[in]; |
188 | loop() |
255 | loop() |
189 | { |
256 | { |
190 | if(s=='\\') |
257 | if(s=='\\') |
191 | { |
258 | { |
Line 208... | Line 275... | ||
208 | else if(s==quote)break; |
275 | else if(s==quote)break; |
209 | LEX_STEP_1: |
276 | LEX_STEP_1: |
210 | DSBYTE[pos] = s; |
277 | DSBYTE[pos] = s; |
211 | pos++; |
278 | pos++; |
212 | in++; |
279 | in++; |
- | 280 | /*if(in-tmp>len_str_buf) |
|
- | 281 | { |
|
- | 282 | if(str_buffer) |
|
- | 283 | { |
|
- | 284 | tmp = len_str_buf; |
|
- | 285 | len_str_buf+=1024; |
|
- | 286 | str_buffer = realloc(str_buffer,len_str_buf+1); |
|
- | 287 | strlcpy(str_buffer+tmp,#const_token_lexer,1024); |
|
- | 288 | pos = #const_token_lexer; |
|
- | 289 | } |
|
- | 290 | else { |
|
- | 291 | len_str_buf+=1024; |
|
- | 292 | str_buffer = malloc(len_str_buf+1); |
|
- | 293 | strlcpy(str_buffer,#const_token_lexer,1024); |
|
- | 294 | pos = #const_token_lexer; |
|
- | 295 | } |
|
- | 296 | }*/ |
|
213 | s = DSBYTE[in]; |
297 | s = DSBYTE[in]; |
214 | } |
298 | } |
215 | in++; |
299 | in++; |
- | 300 | /*tmp = pos-in; |
|
- | 301 | if(str_buffer) |
|
- | 302 | { |
|
- | 303 | if(tmp) |
|
- | 304 | { |
|
- | 305 | str_buffer = realloc(str_buffer,tmp+1); |
|
- | 306 | strlcpy(str_buffer+len_str_buf,#const_token_lexer,tmp); |
|
- | 307 | } |
|
- | 308 | type = LEX_STR; |
|
- | 309 | length = len_str_buf+tmp; |
|
- | 310 | text = in; |
|
- | 311 | tmp = str_buffer+length; |
|
- | 312 | DSBYTE[tmp] = 0; |
|
- | 313 | token = str_buffer; |
|
- | 314 | return token; |
|
- | 315 | }*/ |
|
216 | type = LEX_STR; |
316 | type = LEX_STR; |
217 | } |
317 | } |
218 | else { |
318 | else { |
219 | in++; |
319 | in++; |
220 | type = LEX_NUL; |
320 | type = LEX_NUL; |