-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlex.cpp
363 lines (361 loc) · 9.24 KB
/
lex.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# include "lex.h"
// include <vector>
# include <cassert>
// A
LEX::A::A(const char * a, size_t n){
raw = (char *)memcpy(new char[n+1], a, n);
raw[n] = '\0';
__trim = nullptr ;
}
const char * LEX::A::trim(const char * trim){
if( __trim )delete[ ]
__trim ;
char const * p = raw ;
while([&](char x){
const char * t = trim ;
while(*t)if( *t++ == x )
return true ; // haha
return false ;
}(*p)) ++ p ;
size_t n = strlen(p);
while([&](char x){
const char * t = trim ;
while(*t)if( *t++ == x )return true ;
return false ;
}( p[n-1] )) -- n ;
__trim = new char[n+1]; // RELEASE IT PLEASE !!!!!!!!!!!1
memcpy(__trim, p, n);
__trim[n] = '\0';
return __trim ;
}
double LEX::A::as_double(void){ return 0.0; }
long LEX::A::as_long(void){
char const * p = raw ;
long val = 0L ;
while( *p ){
if( *p >= '0' && *p <= '9' ){
val *= 10 ;
val += *p - '0' ;
}
++ p ;
}
return val ;
}
bool LEX::A::equals(const char * cstr){
const char * p = raw ;
if(cstr && p)while(*p){
if(*p != *cstr)switch(*p){
case ' ':
case '\t':
case '\r':
++ p ;
continue ;
default:
return false ;
}
return!strncmp(cstr, p, strlen(cstr));
}
return false ;
}
/*# include <type_traits> // remove_reference
double std::remove_reference<decltype(*LEX::X::A)>::type::as_double(void){
return 0.0;
}
long std::remove_reference<decltype(*LEX::X::A)>::type::as_long(void){
char const * p = ptr ;
long val = 0L ;
while( *p ){
if( *p >= '0' && *p <= '9' ){
val *= 10 ;
val += *p - '0' ;
}
++ p ;
}
return val ;
}*/
// LEX
LEX::~LEX(void)noexcept {
// release................
}
LEX::LEX(void){
// default ctor
}
// include <functional>
# include <new> // MinGW
LEX::LEX(RC&& a, std::initializer_list<char const*> il){
{
bool a(false);
for(const char * g : il){
assert(g);
assert(strlen(g));
assert(g[strlen(g)-1] != '\a');
if( g[0] == '\a' ) a = true ; // once begins \a
if( a ) assert(g[0] == '\a'); // always begins \a
}
}
size_t Gn(0u); // size of grammar string
for(const char * g : il) Gn += strlen(g) + 1u ;
char * Gm = new char[Gn]; // alloc writable
{
char * p = Gm ;
unsigned grammar(0u);
for(const char * g : il){
strcpy(p, g);
// ...
G * node = & root ;
if( p[0] == '\a' ){
// fork new branche
# pragma warning(suppress: 6308)
node->fork = (G *)realloc(node->fork, ++node->n*sizeof(G));
node = node->fork + node->n - 1u ; // root -> '\a' -> ...
# pragma warning(suppress: 28183)
memset(node, 0, sizeof(G)); // NEW
# pragma warning(suppress: 28182)
node->keyword = nullptr;//"";
}
// if( p[0] > '\0' )
if( const char * tok = strtok(p, "\a") )
do node = [ ](G * node, const char * tok){
assert(node); // NEW
if( node->fork ) // NEW
for( unsigned i(0u); i < node->n ; ++i )
if( !node->fork[i].keyword || // NEW
!strcmp(node->fork[i].keyword, tok) )
return node->fork + i ;
// fork new branche
# pragma warning(suppress: 6308)
node->fork = (G *)realloc(node->fork, ++node->n*sizeof(G));
node = node->fork + node->n - 1u ; // root -> ... -> leaf
new(node)G;
// memset(node, 0, sizeof(G));
node->keyword = tok ;
return node ;
}(node, tok);
while( tok = strtok(nullptr, "\a") );
// ...
node->grammar = ++ grammar ; // grammar ++
p += strlen(g) + 1u ;
}
assert(p == Gm + Gn);
}
// ...
/* std::function<void(G*,unsigned)> lambda ;
lambda = [&](G * node, unsigned tab){
if( node->keyword ){
for(unsigned i=0u;i<tab;++i)
printf("\t");
printf("\"");
for(int i=0;node->keyword[i];++i)
switch(node->keyword[i]){
case '\a': printf("\\a"); break;
case '\b': printf("\\b"); break;
case '\f': printf("\\f"); break;
case '\t': printf("\\t"); break;
case '\v': printf("\\v"); break;
case '\r': printf("\\r"); break;
case '\n': printf("\\n"); break;
default: printf("%c", node->keyword[i]); break;
}
printf("\"");
printf("\n");
}
for(unsigned i=0u; i<node->n; ++i)
lambda(node->fork + i, tab + 1u);
};
lambda(&root, -1);*/
// ...
const char * p = &a[0];
size_t n = a.size ;
while( n ){
# pragma warning(suppress: 6308)
Xs = (X *)realloc(Xs, ++Xn*sizeof(X));
new(Xs+Xn-1)X;
// memset(Xs+Xn-1, 0, sizeof(X));
size_t o = parse(p, n, Xs + Xn - 1u);
if( o == 0u )break ;
n -= o ;
p += o ;
}
}
# include <cstdio>
void LEX::operator = (LEX&& a)noexcept {
this->~LEX( ); // ~
// move.....................
printf("TODO LEX = LEX&& (move semantics)\n");
}
unsigned LEX::insert(intptr_t i, const char * fmt, ...){
char buf[512]; // lol?
va_list va ;
va_start(va, fmt);
vsprintf(buf, fmt, va);
va_end(va);
// ...
# pragma warning(suppress: 6308)
Xs = (X *)realloc(Xs, ++Xn*sizeof(X));
memmove(Xs+i+1, Xs+i, (Xn-i-1)*sizeof(X));
# pragma warning(suppress: 6387)
memset(Xs+i, 0, sizeof(X));
size_t offset = parse(buf, strlen(buf), Xs+i);
// ...
return Xs[i].G ; // return grammar-index
}
unsigned LEX::remove(intptr_t i){
assert(Xs && Xn);
unsigned G = Xs[i].G ;
memmove(Xs+i, Xs+i+1, (Xn-i-1)*sizeof(X));
# pragma warning(suppress: 6308)
Xs = (X *)realloc(Xs, --Xn*sizeof(X));
return G ;
}
# include <deque>
# include <functional>
size_t LEX::save(char const * filename){
std::deque<const char *> stack ;
std::function<bool(G *, unsigned, bool)> lambda = [&](G * node, unsigned G, bool initial){
// break on `Xs[i].G == node->grammar-1'
if( ! node->n ){
if( node->grammar-1 == G )
stack.push_back(node->keyword);
return node->grammar-1 == G ;
}
// going through whole syntax tree
for(size_t n(0u); n < node->n ; ++n){
// push & pop keywords
if(!initial)stack.push_back(node->keyword);
if( lambda(node->fork + n, G, false) )
return true ;
if(!initial)stack.pop_back( );
}
return false ;
};
// decltype(X::A) A ;
FILE * f = fopen(filename, "wb");
size_t written(0u);
for(size_t i(0u); i<Xn; ++i){
if( lambda(&root, Xs[i].G, true) ){
// fold `stack' keywords with `X[i].A'
if( const char * keyw = stack.front( ) ){ // 1
written += fwrite(keyw, 1, strlen(keyw), f); // 1
}
stack.pop_front( ); // 1
size_t offset(0u);
while( !stack.empty( ) ){
const char * ptr = Xs[i].A[offset++] ;
written += fwrite(ptr, 1, strlen(ptr), f);
if( const char * keyw = stack.front( ) ){ // 2
written += fwrite(keyw, 1, strlen(keyw), f); // 2
}
stack.pop_front( ); // 2
}
}
// stack.clear( );
}
fclose(f);
return written ;
}
// private
size_t LEX::parse(char const * a, const size_t n, X * out){
unsigned An(0u);
struct A {
const char * p ;
unsigned n ;
} * As(nullptr);
char const * p(a);
auto alarm_until = [this](const char * a, size_t n, G * node){
const char * p = a ;
while( p - a < n ){
for(unsigned i(0u); i < node->n; ++i)
if( auto * key = node->fork[i].keyword ){
size_t len = strlen(key);
if(len > n + a - p){
printf("%i...", len - n + p - a);
continue ;
}
if( !strncmp(p, key, len) ){
// store alarm string
return p - a ;
} else {
continue ;
}
} else {
assert(false); // !!!!!!!!!!!!
}
++ p ;
}
return 0;
return p - a ;
};
if( G * super = &root ; G * node = super->fork )do {
if( node->keyword ){
size_t len = strlen(node->keyword);
if( p + len > a + n || strncmp(p, node->keyword, len) ){
++ node ;
continue ;
}
out->G = node->grammar - 1 ; // NEW
p += len ;
}
size_t o = alarm_until(p, n + a - p, node);
# pragma warning(suppress: 6308)
As = (A *) realloc(As, ++An*sizeof(A));
new(As+An-1)A{p,o};
// As[An-1].p = p ; As[An-1].n = o ;
p += o ;
if( node->fork ) // NEW
node = ( super = node )->fork ;
} while( node - super->fork < super->n );
// copy............
assert(out);
assert(out->A == nullptr);
out->A = (LEX::A *)malloc(sizeof(LEX::A)*An);
//new std::remove_reference<decltype(*out->A)>::type[An];
for(unsigned n(0u); n < An; ++n){
// A & a = As[n];
// printf("As[%u].p`%s' As[%n].n`%u'\n", n, As[n].p, n, As[n].n);
new (out->A + n) LEX::A (As[n].p, As[n].n);
// out->A[n].raw = (char*)memcpy(new char[a.n+1], a.p, a.n); // RELEASE IT PLEASE !!!!!!!!!
// const_cast<char*&>(out->A[n].raw)[a.n] = '\0';
}
free(As);
return p - a ;
}
/*size_t LEX::a(const char*str, size_t len, char*val, unsigned mul, char til){
assert(str);
assert(val);
const char * ptr = str ;
while(ptr - str < len && ptr - str < mul){
if( * ptr == til )break ;
val[ptr - str] = * ptr ;
++ ptr ;
}
return ptr - str ;
}
# include <cmath> // NAN
size_t LEX::v(const char*str, size_t len, double*val, unsigned mul, char del){
assert(str);
assert(val);
const char * ptr = str ;
while(ptr - str < len){
if( * ptr == del ){
++ val ;
-- mul ;
++ ptr ;
continue ;
}
if( * ptr >= '0' && * ptr <= '9' ){
* val = * ptr - '0';
++ ptr ;
continue ;
}
if( * ptr == ' ' || * ptr == '\t' || * ptr == '\r' ){
++ ptr ;
continue ;
}
while( mul ){
-- mul ;
* val ++ = NAN ;
}
break ;
}
return ptr - str ;
}*/