Unnamed Fossil Project

Check-in [f63f8f419c]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:updated
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:f63f8f419c06c67683b1f5e0753cc55b23e55ca1
User & Date: alexey@sudachen.name 2012-07-22 06:45:28
Context
2012-07-23
02:57
(no comment) check-in: 0543846e85 user: monster tags: trunk
2012-07-22
06:45
updated check-in: f63f8f419c user: alexey@sudachen.name tags: trunk
2012-07-20
05:07
updated check-in: 6d74618ec6 user: alexey@sudachen.name tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to buffer.hc.

114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
...
178
179
180
181
182
183
184









185
186
187
188
189
190
191
  ;

void Buffer_Resize(YOYO_BUFFER *bf,int count)
#ifdef _YOYO_BUFFER_BUILTIN
  {
    Buffer_Reserve(bf,count);
    bf->count = count;
    bf->at[bf->count] = 0;
  }
#endif
  ;

void Buffer_Grow(YOYO_BUFFER *bf,int count)
#ifdef _YOYO_BUFFER_BUILTIN
  {
................................................................................
  {
    Buffer_Append(bf,S,-1);
    Buffer_Fill_Append(bf,'\n',1);
  }
#endif
  ;
  









void Buffer_Printf_Va(YOYO_BUFFER *bf, char *fmt, va_list va)
#ifdef _YOYO_BUFFER_BUILTIN
  {
    int q, rq_len;
    
    rq_len = Yo_Detect_Required_Buffer_Size(fmt,va)+1;
    Buffer_Grow_Reserve(bf,bf->count+rq_len);







|







 







>
>
>
>
>
>
>
>
>







114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
...
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
  ;

void Buffer_Resize(YOYO_BUFFER *bf,int count)
#ifdef _YOYO_BUFFER_BUILTIN
  {
    Buffer_Reserve(bf,count);
    bf->count = count;
    if ( bf->at ) bf->at[bf->count] = 0;
  }
#endif
  ;

void Buffer_Grow(YOYO_BUFFER *bf,int count)
#ifdef _YOYO_BUFFER_BUILTIN
  {
................................................................................
  {
    Buffer_Append(bf,S,-1);
    Buffer_Fill_Append(bf,'\n',1);
  }
#endif
  ;
  
void Buffer_Set(YOYO_BUFFER *bf, char *S, int L)
#ifdef _YOYO_BUFFER_BUILTIN
  {
    Buffer_Resize(bf,0);    
    Buffer_Append(bf,S,L);
  }
#endif
  ;

void Buffer_Printf_Va(YOYO_BUFFER *bf, char *fmt, va_list va)
#ifdef _YOYO_BUFFER_BUILTIN
  {
    int q, rq_len;
    
    rq_len = Yo_Detect_Required_Buffer_Size(fmt,va)+1;
    Buffer_Grow_Reserve(bf,bf->count+rq_len);

Added ctoken.hc.











































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501

/*

Copyright © 2010-2011, Alexéy Sudachén, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    http://www.gnu.org/licenses/

Otherwise:

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

Except as contained in this notice, the name of a copyright holder shall not
be used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization of the copyright holder.

*/

#ifndef C_once_DD56FBFA_631C_47CD_B2A4_E6C8932594C8
#define C_once_DD56FBFA_631C_47CD_B2A4_E6C8932594C8

#include "yoyo.hc"
#include "string.hc"
#include "buffer.hc"

#ifdef _LIBYOYO
#define _YOYO_CTOKEN_BUILTIN
#endif

enum
  {
      YO_CTOKEN_ERR         = -2,
      YO_CTOKEN_END         = -1,
      YO_CTOKEN_DIGITS      = __FOUR_CHARS('D','I','G','T'),
      YO_CTOKEN_XDIGITS     = __FOUR_CHARS('X','D','G','T'),
      YO_CTOKEN_FLOAT       = __FOUR_CHARS('F','L','O','T'),
      YO_CTOKEN_CSTRING     = __FOUR_CHARS('C','S','T','R'),
      YO_CTOKEN_ID          = __FOUR_CHARS('I','D','N','T'),
      YO_CTOKEN_CHAR        = __FOUR_CHARS('C','H','A','R'),
      YO_CTOKEN_LOR         = __FOUR_CHARS(' ',' ','|','|'),
      YO_CTOKEN_LOR_SET     = __FOUR_CHARS(' ',' ','|','='),
      YO_CTOKEN_OR_SET      = __FOUR_CHARS(' ',' ','|','='),
      YO_CTOKEN_LAND        = __FOUR_CHARS(' ',' ','&','&'),
      YO_CTOKEN_LAND_SET    = __FOUR_CHARS(' ','&','&','='),
      YO_CTOKEN_AND_SET     = __FOUR_CHARS(' ',' ','&','='),
      YO_CTOKEN_LSHIFT      = __FOUR_CHARS(' ',' ','<','<'),
      YO_CTOKEN_LSH_SET     = __FOUR_CHARS(' ','<','<','='),
      YO_CTOKEN_LTEQ        = __FOUR_CHARS(' ',' ','<','='),
      YO_CTOKEN_RSHIFT      = __FOUR_CHARS(' ',' ','>','>'),
      YO_CTOKEN_RSH_SET     = __FOUR_CHARS(' ','>','>','='),
      YO_CTOKEN_GTEQ        = __FOUR_CHARS(' ',' ','>','='),
      YO_CTOKEN_INC         = __FOUR_CHARS(' ',' ','+','+'),
      YO_CTOKEN_DEC         = __FOUR_CHARS(' ',' ','-','-'),
      //YO_CTOKEN_INC_SET     = __FOUR_CHARS(' ',' ','+','='),
      //YO_CTOKEN_DEC_SET     = __FOUR_CHARS(' ',' ','-','='),
      YO_CTOKEN_ADD_SET     = __FOUR_CHARS(' ',' ','+','='),
      YO_CTOKEN_SUB_SET     = __FOUR_CHARS(' ',' ','-','='),
      YO_CTOKEN_MUL_SET     = __FOUR_CHARS(' ',' ','*','='),
      YO_CTOKEN_DIV_SET     = __FOUR_CHARS(' ',' ','/','='),
      YO_CTOKEN_MOD_SET     = __FOUR_CHARS(' ',' ','%','='),
      YO_CTOKEN_XOR_SET     = __FOUR_CHARS(' ',' ','^','='),
      YO_CTOKEN_ELS         = __FOUR_CHARS(' ','.','.','.'),
      YO_CTOKEN_EQUAL       = __FOUR_CHARS(' ',' ','=','='),
      YO_CTOKEN_NSPACE      = __FOUR_CHARS(' ',' ',':',':'),
      YO_CTOKEN_NEG_SET     = __FOUR_CHARS(' ',' ','~','='),
      YO_CTOKEN_NOT_SET     = __FOUR_CHARS(' ',' ','!','='),
  };

typedef struct _YOYO_CTOKMR
  {
    char *m;
    int lineno;
    int charno;
  } YOYO_CTOKMR;
  
typedef struct _YOYO_CTOKST
  {
    char *pS;
    char *pE;
    char *pI;
    char *source;
    int lineno;
    int charno;

    YOYO_CTOKMR last;
    YOYO_CTOKMR prelast;
  } YOYO_CTOKST;

void Ctok_Init(YOYO_CTOKST *a, char *S, char *E, char *source)
#ifdef _YOYO_CTOKEN_BUILTIN
  {
    memset(a,0,sizeof(*a));
    a->pI = a->pS = S;
    a->pE = E;
    a->source = source;
    a->lineno = 0;
    a->charno = 0;
  }  
#endif
  ;
  
int Ctok_GetXchr(YOYO_CTOKST *a)
#ifdef _YOYO_CTOKEN_BUILTIN
  {
    STRICT_REQUIRE( a->pI <= a->pE && a->pI >= a->pS );
    if ( a->pI != a->pE )
      {
        if ( *a->pI == '\n' ) 
          { ++a->lineno; a->charno = 1; } 
        else 
          ++a->charno;
        return *a->pI++;
      }
    return -1;
  }
#endif
  ;
  
int Ctok_SkipXchrUntil(char c, YOYO_CTOKST *a)
#ifdef _YOYO_CTOKEN_BUILTIN
  {
    for (;;)
      {
        STRICT_REQUIRE( a->pI <= a->pE && a->pI >= a->pS );
        if ( a->pE == a->pI )
          return 0;
        if ( *a->pI != c )
          {
            if ( *a->pI == '\n' ) 
              { ++a->lineno; a->charno = 1; } 
            else 
              ++a->charno;
            ++a->pI;
          }
        else
          return c;
      }
  }
#endif
  ;
  
int Ctok_GetXchrIfIs(char c, YOYO_CTOKST *a)
#ifdef _YOYO_CTOKEN_BUILTIN
  {
    STRICT_REQUIRE( a->pI <= a->pE && a->pI >= a->pS );
    if ( a->pE == a->pI || *a->pI != c )
      return 0;
    ++a->pI;
    return c;
  }
#endif
  ;
  
int Ctok_GetXchrIfIs_Rep(char c, YOYO_CTOKST *a, int no)
#ifdef _YOYO_CTOKEN_BUILTIN
  {
    int i;
    STRICT_REQUIRE( a->pI <= a->pE && a->pI >= a->pS );
    if ( a->pE-a->pI < no )
      return 0;
    for ( i = 0; i < no; ++i )
      if ( a->pI[i] != c ) 
        return 0;
    a->pI += no;
    return c;
  }
#endif
  ;
  
void Ctok_Stepback(YOYO_CTOKST *a)
#ifdef _YOYO_CTOKEN_BUILTIN
  {
    STRICT_REQUIRE( a->pI <= a->pE && a->pI >= a->pS );
    if ( a->pI != a->pS ) 
      {
        --a->pI;
        if ( *a->pI == '\n' ) --a->lineno;
      }
  }
#endif
  ;
    
int Ctok_Hasmore(YOYO_CTOKST *a)
#ifdef _YOYO_CTOKEN_BUILTIN
  {
    STRICT_REQUIRE( a->pI <= a->pE && a->pI >= a->pS );
    return a->pE - a->pI;
  }
#endif
  ;
    
void Ctok_Skip_Spaces(YOYO_CTOKST *a)
#ifdef _YOYO_CTOKEN_BUILTIN
  {
    while ( a->pI != a->pE && ( *a->pI == ' ' || *a->pI == '\t' || *a->pI == '\n' || *a->pI == '\r' ) )
      Ctok_GetXchr(a);
  }
#endif
  ;
  
void Ctok_Mark(YOYO_CTOKST *a)
#ifdef _YOYO_CTOKEN_BUILTIN
  { 
    Ctok_Skip_Spaces(a);
    a->prelast = a->last;
    a->last.lineno = a->lineno;
    a->last.charno = a->charno;
    a->last.m = a->pI;
  }
#endif
  ;
  
void Ctok_Backmark(YOYO_CTOKST *a)
#ifdef _YOYO_CTOKEN_BUILTIN
  { 
    if ( a->last.m )
     {
       a->pI = a->last.m;
       a->charno = a->last.charno;
       a->lineno = a->last.lineno;
      }
    a->last = a->prelast;
    memset(&a->prelast,0,sizeof(a->prelast));
  }
#endif
  ;
  
#define Ctok_Token(Area,Buf) Ctok_Token_(Area,Buf,0,0)
#define Ctok_Token_Local(Area,Buf,Plen) Ctok_Token_(Area,Buf,Plen,1)
#define Ctok_Token_Srcref(Area,Pptr,Plen) Ctok_Token_(Area,Pptr,Plen,2)

int Ctok_Token_(YOYO_CTOKST *a, void *dst, void *opt, int srccp)
#ifdef _YOYO_CTOKEN_BUILTIN
  {
    int tk;
    char *S;
    int c;
    STRICT_REQUIRE(a != 0);    
  
  l_repeat:  
  
    Ctok_Skip_Spaces(a);
    S = a->pI;
    
    if ( (c=Ctok_GetXchr(a)) > 0 )
      {
        switch ( c )
          {
            case '{': case '}':
            case '(': case ')':
            case '[': case ']':
            case ';': case ',':
              return c;
            case ':': 
              if ( Ctok_GetXchrIfIs(c,a) ) return YO_CTOKEN_NSPACE; 
              return c;
            case '<': 
              if ( Ctok_GetXchrIfIs(c,a) ) 
                {
                  if ( Ctok_GetXchrIfIs('=',a) ) return YO_CTOKEN_LSH_SET; 
                  return YO_CTOKEN_LSHIFT; 
                }
              if ( Ctok_GetXchrIfIs('=',a) ) return YO_CTOKEN_LTEQ; 
              return c;
            case '>':
              if ( Ctok_GetXchrIfIs(c,a) ) 
                {
                  if ( Ctok_GetXchrIfIs('=',a) ) return YO_CTOKEN_RSH_SET; 
                  return YO_CTOKEN_RSHIFT; 
                }
              if ( Ctok_GetXchrIfIs('=',a) ) return YO_CTOKEN_GTEQ; 
              return c;
            case '*': 
              if ( Ctok_GetXchrIfIs('=',a) ) return YO_CTOKEN_MUL_SET; 
              return c;
            case '/':
              if ( Ctok_GetXchrIfIs('/',a) ) 
                { 
                  Ctok_SkipXchrUntil('\n',a); 
                  goto l_repeat; 
                }
              if ( Ctok_GetXchrIfIs('*',a) )
                { 
                  do 
                    Ctok_SkipXchrUntil('*',a); 
                  while ( Ctok_GetXchrIfIs('*',a) && Ctok_Hasmore(a) && !Ctok_GetXchrIfIs('/',a) ); 
                  goto l_repeat; 
                }
              if ( Ctok_GetXchrIfIs('=',a) ) return YO_CTOKEN_DIV_SET; 
              return c;
           case '%': 
              if ( Ctok_GetXchrIfIs('=',a) ) return YO_CTOKEN_MOD_SET; 
              return c;
            case '~': 
              if ( Ctok_GetXchrIfIs('=',a) ) return YO_CTOKEN_NEG_SET; 
              return c;
            case '^': 
              if ( Ctok_GetXchrIfIs('=',a) ) return YO_CTOKEN_XOR_SET; 
              return c;
            case '!':
              if ( Ctok_GetXchrIfIs('=',a) ) return YO_CTOKEN_NOT_SET; 
              return c;
            case '+': 
              if ( Ctok_GetXchrIfIs(c,a) ) return YO_CTOKEN_INC; 
              if ( Ctok_GetXchrIfIs('=',a) ) return YO_CTOKEN_ADD_SET; 
              return '+';
            case '-': 
              if ( Ctok_GetXchrIfIs(c,a) ) return YO_CTOKEN_DEC; 
              if ( Ctok_GetXchrIfIs('=',a) ) return YO_CTOKEN_SUB_SET; 
              return '-';
            case '&': 
              if ( Ctok_GetXchrIfIs(c,a) ) 
                { 
                  if ( Ctok_GetXchrIfIs('=',a) ) return YO_CTOKEN_LAND_SET; 
                  return YO_CTOKEN_LAND;
                } 
              if ( Ctok_GetXchrIfIs('=',a) ) return YO_CTOKEN_AND_SET; 
              return '&';
            case '|': 
              if ( Ctok_GetXchrIfIs(c,a) ) 
                { 
                  if ( Ctok_GetXchrIfIs('=',a) ) return YO_CTOKEN_LOR_SET; 
                  return YO_CTOKEN_LOR;
                } 
              if ( Ctok_GetXchrIfIs('=',a) ) return YO_CTOKEN_OR_SET; 
              return '|';
            case '=': 
              if ( Ctok_GetXchrIfIs(c,a) ) return YO_CTOKEN_EQUAL; 
              return '=';
            case '.': 
              if ( Ctok_GetXchrIfIs_Rep(c,a,2) ) return YO_CTOKEN_ELS; 
              return '.';
          }
      
        if ( c == '"' || c == '\'')
          {
            int cc = c;
            while ( Ctok_Hasmore(a) )
              {
                c = Ctok_GetXchr(a);
                if ( c == cc )
                  break;
                if ( c == '\\' ) 
                  c = Ctok_GetXchr(a);
              }
            tk = (cc == '"' ? YO_CTOKEN_CSTRING : YO_CTOKEN_CHAR);
            goto end_tok; 
          }
      
        if ( isalpha(c) || c == '_' )
          {
            while ( Ctok_Hasmore(a) )
              if ( isalnum(c=Ctok_GetXchr(a)) || c == '_' )
                ;
              else { Ctok_Stepback(a); break; }
            tk = YO_CTOKEN_ID;
            goto end_tok;
          }

        if ( isdigit(c) )
          {
            if ( c == '0' && Ctok_GetXchrIfIs('x',a) )
              {
                while ( Ctok_Hasmore(a) )
                  if ( isxdigit(c=Ctok_GetXchr(a)) )
                   ;
                  else { Ctok_Stepback(a); break; }
                tk = YO_CTOKEN_XDIGITS;
                goto end_tok;
              }
        
            if ( c == '0' && Ctok_GetXchrIfIs('.',a) )
              { goto float_part; }
        
          digit_0: if ( c == '0' )
              {
                char cc;
                if ( Ctok_Hasmore(a) )
                  if ( isdigit(cc = Ctok_GetXchr(a)) ) { c = cc; goto digit_0; }
                  else Ctok_Stepback(a); 
                tk = YO_CTOKEN_DIGITS;
                goto end_tok;
              }
        
            ;
        
            while ( Ctok_Hasmore(a) )
              if ( isdigit(c=Ctok_GetXchr(a)) )
                ;
              else { Ctok_Stepback(a); break; }
        
            if ( Ctok_GetXchrIfIs('.',a) )
              {
               float_part:
                while ( Ctok_Hasmore(a) )
                  if ( isdigit(c=Ctok_GetXchr(a)) )
                    ;
                  else { Ctok_Stepback(a); break; }
                tk = YO_CTOKEN_FLOAT;
                goto end_tok;
              }
          
            tk = YO_CTOKEN_DIGITS;
            goto end_tok;
          }
      }
  
    return YO_CTOKEN_END;
  
  end_tok:
    c = a->pI-S;
    if ( tk == YO_CTOKEN_CSTRING || tk == YO_CTOKEN_CHAR ) { ++S; c-=2; } 
    if ( srccp == 0 )
      { 
        if (dst) Buffer_Set(dst,S,c); 
      }
    else
      {
      }
    return tk;     
  }
#endif
  ;
  
void Ctok_Expect_Error(char *text,YOYO_CTOKST *a)
#ifdef _YOYO_CTOKEN_BUILTIN
  {
    __Raise_Format(YOYO_ERROR_SYNTAX,("synax error: %s at %s:%d:%d"
                          ,text ,a->source ,a->lineno ,a->charno) );
  }
#endif
  ;
  
char *Ctok_To_String(int tok)
#ifdef _YOYO_CTOKEN_BUILTIN
  {
    if ( tok > 30 && tok < 128 ) return Str_Copy_L((char*)&tok,1);
    return __Format("token(%4s)",&tok);
  }
#endif
  ;
  
void Ctok_Expect(int comp, char *p, YOYO_CTOKST *a, YOYO_BUFFER *r)
#ifdef _YOYO_CTOKEN_BUILTIN
  {
    int t;
    Ctok_Mark(a);
    t = Ctok_Token(a,r);
    if ( t != comp || ( p && !!strcmp(r->at,p) ) ) 
      {
        Ctok_Backmark(a);
        Ctok_Expect_Error(__Format("expected %s",Ctok_To_String(comp)),a);
      }
  }
#endif
  ;
  
int Ctok_GetIfIs(int comp, char *p, YOYO_CTOKST *a, YOYO_BUFFER *r)
#ifdef _YOYO_CTOKEN_BUILTIN
  {
    int t;
    Ctok_Mark(a);
    t = Ctok_Token(a,r);
    if ( t != comp || ( p && !!strcmp(r->at,p) ) ) 
      {
        Ctok_Backmark(a);
        return 0;
      }
    return 1;
  }
#endif
  ;
  
#endif /* C_once_DD56FBFA_631C_47CD_B2A4_E6C8932594C8 */

Deleted defpars.hc.

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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709

/*

Copyright © 2010-2011, Alexéy Sudachén, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    http://www.gnu.org/licenses/

Otherwise:

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

Except as contained in this notice, the name of a copyright holder shall not
be used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization of the copyright holder.

*/

#ifndef C_once_750A77B2_260B_4E33_B9EA_15F01DDD61FF
#define C_once_750A77B2_260B_4E33_B9EA_15F01DDD61FF

#ifdef _LIBYOYO
#define _YOYO_DEFPARS_BUILTIN
#endif

#include "yoyo.hc"
#include "string.hc"
#include "buffer.hc"
#include "xdata.hc"
#include "file.hc"

enum
  {
    YOYO_DEFPARS_INDENT_WIDTH = 2,
  };

typedef struct _YOYO_DEFPARSE_STATE
  {
    char *text;
    int lineno;
  } YOYO_DEFPARSE_STATE;

void Def_Parse_Skip_Spaces(YOYO_DEFPARSE_STATE *st)
#ifdef _YOYO_DEFPARS_BUILTIN
  {
    while ( *st->text && Isspace(*st->text) )
      {
        if ( *st->text == '\n' ) ++st->lineno;
        ++st->text;
      }
  }
#endif
  ;

char *Def_Parse_Get_Literal(YOYO_DEFPARSE_STATE *st)
#ifdef _YOYO_DEFPARS_BUILTIN
  {
    int capacity = 127;
    int len = 0;
    char *out = 0;
    
    if ( *st->text == '"' || *st->text == '\'')
      {
        char brk = *st->text;
        ++st->text;
        for ( ; *st->text && *st->text != brk; ++st->text )
          {
            if ( *st->text == '\\' )
              {
                ++st->text;
                if ( *st->text == '\n' )
                  ++st->lineno;
                else if ( *st->text == '\r' )
                  ; /* none */
                else if ( *st->text == '\\' || *st->text == '"' || *st->text == '\'' )
                  __Vector_Append(&out,&len,&capacity,st->text,1);
                else if ( *st->text == 'n' )
                  __Vector_Append(&out,&len,&capacity,"\n",1);
                else if ( *st->text == 'r' )
                  __Vector_Append(&out,&len,&capacity,"\r",1);
                else if ( *st->text == 't' )
                  __Vector_Append(&out,&len,&capacity,"\t",1);
                else 
                  __Raise(YOYO_ERROR_ILLFORMED,__Format("invalid esc sequence at line %d",st->lineno));
              }
            else
              __Vector_Append(&out,&len,&capacity,st->text,1);
          }
          
        if ( *st->text == brk ) ++st->text;
      }
    else
      {
        char *q = st->text;
        while ( *st->text && !Isspace(*st->text) 
              && *st->text != '(' && *st->text != ')' 
              && *st->text != '{' && *st->text != '}'
              && *st->text != '[' && *st->text != ']'  
              && *st->text != ',' && *st->text != '=' )
          ++st->text;
        __Vector_Append(&out,&len,&capacity,q,st->text-q);
      }
    
    return out;
  }
#endif
  ;


char *Def_Parse_Get_Node_Literal(YOYO_DEFPARSE_STATE *st)
#ifdef _YOYO_DEFPARS_BUILTIN
  {
    if ( *st->text == '"' || *st->text == '\'' )
      return Def_Parse_Get_Literal(st);
    else
      {
        int capacity = 127;
        int len = 0;
        char *out = 0;
        char *q = st->text;
        while ( *st->text && *st->text != ')' )
          ++st->text;
        __Vector_Append(&out,&len,&capacity,q,st->text-q);
        return out;
      }
  }
#endif
  ;

typedef struct
  {
    int type;
    union
      {
        YOYO_ARRAY  *arr;
        YOYO_BUFFER *dat;
        char   *txt;
        int     dec;
        double  flt;
      };
  } YOYO_DEFPARS_VALUE;

void Def_Parse_Get_Value(YOYO_DEFPARSE_STATE *st, YOYO_DEFPARS_VALUE *val)
#ifdef _YOYO_DEFPARS_BUILTIN
  {
    if ( *st->text == '[' )
      {
        ++st->text;
        if ( st->text[1] == '[' ) /* binary data */
          {
            byte_t b;
            val->dat = Buffer_Init(0);
            Def_Parse_Skip_Spaces(st);
            
            while ( Isxdigit(*st->text) )
              {
                if ( !Isxdigit(st->text[1]) )
                  __Raise(YOYO_ERROR_ILLFORMED,
                      __Format("expected hex byte value at line %d",st->lineno));
                
                b = Str_Unhex_Byte(st->text,0,0);
                Buffer_Append(val->dat,&b,1);
                st->text += 2;
                Def_Parse_Skip_Spaces(st);
              }
            
            if ( !*st->text || *st->text != ']' || st->text[1] != ']' )
              __Raise(YOYO_ERROR_ILLFORMED,
                  __Format("expected ']]' at line %d",st->lineno));
            
            val->type = XVALUE_OPT_VALTYPE_BIN;
            st->text += 2;
          }
        else /* array */
          { 
            YOYO_DEFPARS_VALUE lval;
            Def_Parse_Skip_Spaces(st);
            Def_Parse_Get_Value(st,&lval);
            if ( lval.type == XVALUE_OPT_VALTYPE_STR )
              {
                val->arr = Array_Ptrs();
                val->type= XVALUE_OPT_VALTYPE_STR_ARR;
                Array_Push(val->arr,__Retain(lval.txt));
              }
            else if ( lval.type == XVALUE_OPT_VALTYPE_INT 
                   || lval.type == XVALUE_OPT_VALTYPE_FLT )
              {
                val->dat = Buffer_Init(sizeof(double));
                val->type= XVALUE_OPT_VALTYPE_FLT_ARR;
                if ( lval.type == XVALUE_OPT_VALTYPE_INT )
                  *(double*)val->dat->at = lval.dec;
                else
                  *(double*)val->dat->at = lval.flt;
              }
            
            Def_Parse_Skip_Spaces(st);
            while ( *st->text == ',' )
              {
                ++st->text;
                Def_Parse_Skip_Spaces(st);
                Def_Parse_Get_Value(st,&lval);
                if ( val->type == XVALUE_OPT_VALTYPE_STR )
                  {
                    if ( val->type != XVALUE_OPT_VALTYPE_STR )
                      __Raise(YOYO_ERROR_ILLFORMED,
                              __Format("expected string value at line %d",st->lineno));
                    Array_Push(val->arr,__Retain(lval.txt));
                  }
                else /* XVALUE_OPT_VALUETYPE_FLT_ARR */
                  {
                    
                    if ( lval.type == XVALUE_OPT_VALTYPE_INT )
                      {
                        double f = lval.dec;
                        Buffer_Append(val->dat,&f,sizeof(double));
                      }
                    else if ( lval.type == XVALUE_OPT_VALTYPE_FLT )
                        Buffer_Append(val->dat,&lval.flt,sizeof(double));
                    else
                      __Raise(YOYO_ERROR_ILLFORMED,
                              __Format("expected numeric value at line %d",st->lineno));
                  }
                Def_Parse_Skip_Spaces(st);
              }
            
            if ( *st->text != ']' )
              __Raise(YOYO_ERROR_ILLFORMED,
                  __Format("expected ']' at line %d",st->lineno));
                  
            ++st->text;
          }
      }
    else if ( *st->text == '#' )
      {
        int l = 0;
        if ( (Str_Starts_With(st->text+1,"yes") && (l = 4))
          || (Str_Starts_With(st->text+1,"true") && (l = 5))
          || (Str_Starts_With(st->text+1,"on") && (l = 3))
          || (Str_Starts_With(st->text+1,"1") && (l = 2)) )
          {
            val->dec = 1;
            val->type = XVALUE_OPT_VALTYPE_BOOL;
            st->text += l;
          }
        else if ( (Str_Starts_With(st->text+1,"no") && (l = 3))
          || (Str_Starts_With(st->text+1,"false") && (l = 6))
          || (Str_Starts_With(st->text+1,"off") && (l = 4))
          || (Str_Starts_With(st->text+1,"0") && (l = 2)) )
          {
            val->dec = 0;
            val->type = XVALUE_OPT_VALTYPE_BOOL;
            st->text += l;
          }
        else
          __Raise(YOYO_ERROR_ILLFORMED,
              __Format("expected boolean value at line %d",st->lineno));
      }
    else if ( !Isdigit(*st->text) 
            && ( (*st->text != '.' && *st->text != '-') || !Isdigit(st->text[1]) ) )
      {
        val->txt = Def_Parse_Get_Literal(st);
        val->type = XVALUE_OPT_VALTYPE_STR;
      }
    else /* number */
      {
        if ( *st->text == '0' )
          {
            ++st->text;
            val->dec = 0;
            
            if ( *st->text == 'x' && Isxdigit(st->text[1]) ) /* hex value */
              {
                ++st->text;
                do
                  {
                    val->dec = val->dec << 4;
                    STR_UNHEX_HALF_OCTET(st->text,val->dec,0);
                    ++st->text;
                  }
                while ( Isxdigit(*st->text) );
              }
            else if ( *st->text >= '0' && *st->text <= '7' )
              {
                ++st->text;
                do
                  {
                    val->dec = (val->dec << 3) | (*st->text-'0'); 
                    ++st->text;
                  }
                while ( *st->text >= '0' && *st->text <= '7' );
              }
            else if ( Isspace(*st->text) )
              {
                ; /* nothing, it's zero value */
              }
            else
              goto invalid_numeric;
              
            val->type = XVALUE_OPT_VALTYPE_INT;
            
          }
        else if ( Isdigit(*st->text) || *st->text == '.' || *st->text == '-' ) /* decimal or float value */
          {
            int neg = 1;
            int value = 0;
            
            if ( *st->text == '-' ) { neg = -1; ++st->text; }
            
            for ( ; Isdigit(*st->text); ++st->text )
              value = value * 10 + ( *st->text - '0' );
            
            if ( *st->text == '.' )
              {
                double exp = 1;
                double d = value*neg;
                ++st->text;
                for ( ; Isdigit(*st->text); ++st->text )
                  {
                    d = d * 10 + ( *st->text - '0' );
                    exp *= 10;
                  }
                val->flt = d/exp;
                val->type = XVALUE_OPT_VALTYPE_FLT;
              }
            else
              {
                val->type = XVALUE_OPT_VALTYPE_INT;
                val->dec = value*neg;
              }
          }

        if ( *st->text 
          && !Isspace(*st->text) && *st->text != ')' 
          && *st->text != '}' && *st->text != ',' && *st->text != ']' )
      invalid_numeric:
          __Raise(YOYO_ERROR_ILLFORMED,
              __Format("invalid numeric value at line %d",st->lineno));
      }
  }
#endif
  ;

void Def_Parse_In_Node_Set_Value(YOYO_XNODE *n, char *name, YOYO_DEFPARS_VALUE *val)
#ifdef _YOYO_DEFPARS_BUILTIN
  {
    YOYO_XVALUE *xv = Xnode_Value(n,name,1);;
    switch ( val->type )
      {
        case XVALUE_OPT_VALTYPE_INT:
          Xvalue_Set_Int(xv,val->dec);
          break;
        case XVALUE_OPT_VALTYPE_BOOL:
          Xvalue_Set_Bool(xv,val->dec);
          break;
        case XVALUE_OPT_VALTYPE_FLT:
          Xvalue_Set_Flt(xv,val->flt);
          break;
        case XVALUE_OPT_VALTYPE_STR:
          Xvalue_Put_Str(xv,__Retain(val->txt));
          break;
        case XVALUE_OPT_VALTYPE_BIN:
          Xvalue_Put_Binary(xv,__Refe(val->dat));
          break;
        case XVALUE_OPT_VALTYPE_FLT_ARR:
          Xvalue_Put_Flt_Array(xv,__Refe(val->dat));
          break;
        case XVALUE_OPT_VALTYPE_STR_ARR:
          Xvalue_Put_Str_Array(xv,__Refe(val->arr));
          break;
        default:
          __Raise(YOYO_ERROR_UNEXPECTED_VALUE,0);
      }
      
  }
#endif
  ;

void Def_Parse_In_Node( YOYO_DEFPARSE_STATE *st, YOYO_XNODE *n )
#ifdef _YOYO_DEFPARS_BUILTIN
  {
    Def_Parse_Skip_Spaces(st);
    
    for ( ; *st->text && *st->text != '}' ; Def_Parse_Skip_Spaces(st) ) 
      {
        int go_deeper = 0;
        YOYO_XNODE *nn = 0;
        
        __Auto_Release
          {
            YOYO_DEFPARS_VALUE value = {0};
            char *name; 
            
            name = Def_Parse_Get_Literal(st);
            Def_Parse_Skip_Spaces(st);

            if ( *st->text == '(' )
              {
                char *dflt;
                ++st->text;
                Def_Parse_Skip_Spaces(st);
                dflt = Def_Parse_Get_Node_Literal(st);
                Def_Parse_Skip_Spaces(st);
                if ( *st->text != ')' )
                  __Raise(YOYO_ERROR_ILLFORMED,__Format("expected ')' at line %d",st->lineno));
                ++st->text;
                Def_Parse_Skip_Spaces(st);

                nn = Xnode_Append(n,name);
                Xnode_Value_Set_Str(nn,"@",dflt);
                name = 0;
              }
            
            
            if ( *st->text == '=' )
              {
                ++st->text;
                Def_Parse_Skip_Spaces(st);
                Def_Parse_Get_Value(st,&value);
                Def_Parse_Skip_Spaces(st);
              }
            
            if ( *st->text == '{' )
              {
                ++st->text;
                go_deeper = 1;
                if ( !nn )
                  nn = Xnode_Append(n,name);
                if ( value.type )
                  Def_Parse_In_Node_Set_Value(nn,"$",&value);
              }
            else if ( value.type )
              {
                if ( name || !nn )
                  Def_Parse_In_Node_Set_Value(n,name,&value);
                else
                  Def_Parse_In_Node_Set_Value(nn,"$",&value);
              }
              
          }
        
        if ( go_deeper )
          {
            Def_Parse_In_Node(st,nn);
            if ( *st->text != '}' )
              __Raise(YOYO_ERROR_ILLFORMED,__Format("expected '}' at line %d",st->lineno));
            ++st->text;
          }
      }
  }
#endif
  ;

YOYO_XDATA *Def_Parse_Str(char *text)
#ifdef _YOYO_DEFPARS_BUILTIN
  {
    YOYO_DEFPARSE_STATE st = { text, 1 };
    YOYO_XDATA *doc = Xdata_Init();
    Def_Parse_In_Node(&st,&doc->root);
    return doc;
  }
#endif
  ;

YOYO_XDATA *Def_Parse_File(char *filename)
#ifdef _YOYO_DEFPARS_BUILTIN
  {
    YOYO_XDATA *ret = 0;
    
    __Auto_Ptr(ret)
      {
        YOYO_BUFFER *bf = Oj_Read_All(Cfile_Open(filename,"rt"));
        ret = Def_Parse_Str((char*)bf->at);
      }
    return ret;
  }
#endif
  ;

void Def_Format_Node_In_Depth(YOYO_BUFFER *bf, YOYO_XNODE *r, int flags, int indent)
#ifdef _YOYO_DEFPARS_BUILTIN
  {
    __Gogo
      {
        YOYO_XVALUE *val = Xnode_First_Value(r);
        for ( ; val; val = Xnode_Next_Value(r,val) )
          {
            char *tag = Xnode_Value_Get_Tag(r,val);
            
            Buffer_Fill_Append(bf,' ',indent*YOYO_DEFPARS_INDENT_WIDTH);
            Buffer_Append(bf,tag,-1);
            Buffer_Append(bf," = ",-1);
            
            switch ( val->opt&XVALUE_OPT_VALTYPE_MASK )
              {
                case XVALUE_OPT_VALTYPE_NONE:
                  Buffer_Append(bf,"#none",-1);
                  break;
                  
                case XVALUE_OPT_VALTYPE_BOOL:
                  if ( val->bval )
                    Buffer_Append(bf,"#true",-1);
                  else
                    Buffer_Append(bf,"#false",-1);
                  break;
                  
                case XVALUE_OPT_VALTYPE_INT:
                  Buffer_Printf(bf,"%ld",val->dec);
                  break;
                  
                case XVALUE_OPT_VALTYPE_FLT:
                  if ( val->flt - (double)((long)val->flt) > 0.0009999999 )
                    Buffer_Printf(bf,"%.3f",val->flt);
                  else
                    Buffer_Printf(bf,"%.f",val->flt);
                  break;
                  
                case XVALUE_OPT_VALTYPE_STR:
                  Buffer_Append(bf,"\"",1);
                  Buffer_Quote_Append(bf,val->txt,-1,'"');
                  Buffer_Append(bf,"\"",1);
                  break;
                  
                case XVALUE_OPT_VALTYPE_LIT:
                  Buffer_Append(bf,"\"",1);
                  Buffer_Quote_Append(bf,(char*)&val->down,-1,'"');
                  Buffer_Append(bf,"\"",1);
                  break;
                  
                case XVALUE_OPT_VALTYPE_BIN:
                  {
                    int bytes_per_line = 30;
                    int q = 0;
                    
                    Buffer_Append(bf,"[[",2);
                    
                    if ( val->binary->count > bytes_per_line )
                      {  
                        Buffer_Append(bf,"\n",1);
                        Buffer_Fill_Append(bf,' ',(indent+1)*YOYO_DEFPARS_INDENT_WIDTH);
                      }
                      
                    while ( q < val->binary->count )
                      {
                        int l = val->binary->count - q;
                        if ( l > bytes_per_line ) l = bytes_per_line;
                        Buffer_Hex_Append(bf,val->binary->at+q,l);
                        q += l;
                        if ( q < val->binary->count )
                          {
                            Buffer_Append(bf,"\n",1);
                            Buffer_Fill_Append(bf,' ',(indent+1)*YOYO_DEFPARS_INDENT_WIDTH);
                          }
                      }
                      
                    Buffer_Append(bf,"]]",2);
                    break;
                  }
                  
                case XVALUE_OPT_VALTYPE_STR_ARR:
                  {
                    int q = 0;
                    int count = Array_Count(val->strarr);
                    
                    Buffer_Append(bf,"[",2);
                    Buffer_Append(bf,"\n",1);
                    Buffer_Fill_Append(bf,' ',(indent+1)*YOYO_DEFPARS_INDENT_WIDTH);
                      
                    for ( ; q < count; ++q )
                      {
                        Buffer_Append(bf,"\"",1);
                        Buffer_Quote_Append(bf,val->strarr->at[q],-1,'"');
                        Buffer_Append(bf,"\"\n",2);
                        Buffer_Fill_Append(bf,' ',(indent+1)*YOYO_DEFPARS_INDENT_WIDTH);
                      }
                      
                    Buffer_Append(bf,"]",2);
                    break;
                  }
                case XVALUE_OPT_VALTYPE_FLT_ARR:
                  {
                    int q = 0;
                    int nums_per_line = 5;
                    
                    Buffer_Append(bf,"[",2);
                    
                    if ( val->binary->count > nums_per_line*iszof_double )
                      {  
                        Buffer_Append(bf,"\n",1);
                        Buffer_Fill_Append(bf,' ',(indent+1)*YOYO_DEFPARS_INDENT_WIDTH);
                      }
                      
                    while ( q+iszof_double <= val->binary->count )
                      {
                        int l = (val->binary->count - q)/iszof_double;
                        if ( l > nums_per_line ) l = nums_per_line;
                        for ( ; l > 0; --l )
                          {
                            double d = *(double*)(val->binary->at+q*iszof_double);
                            
                            if ( (d - (double)((long)d)) > 0.000999999 )
                              Buffer_Printf(bf,"%.3f",d);
                            else
                              Buffer_Printf(bf,"%.f",d);
                            
                            q += iszof_double;
                            if ( q+iszof_double <= val->binary->count )
                            Buffer_Append(bf,",",1);
                          }
                        if ( q+iszof_double <= val->binary->count )
                          {
                            Buffer_Append(bf,"\n",1);
                            Buffer_Fill_Append(bf,' ',(indent+1)*YOYO_DEFPARS_INDENT_WIDTH);
                          }
                      }
                      
                    Buffer_Append(bf,"]",2);
                    break;
                  }
              }
            
            Buffer_Append(bf,"\n",1);
          }
      }

    __Gogo
      {
        YOYO_XNODE *n = Xnode_Down(r);
        for ( ; n; n = Xnode_Next(n) )
          {
            Buffer_Fill_Append(bf,' ',indent*YOYO_DEFPARS_INDENT_WIDTH);
            Buffer_Append(bf,Xnode_Get_Tag(n),-1);
            Buffer_Append(bf," {\n",3);
            Def_Format_Node_In_Depth(bf,n,flags,indent+1);
            Buffer_Fill_Append(bf,' ',indent*YOYO_DEFPARS_INDENT_WIDTH);
            Buffer_Append(bf,"}\n",2);
          }
      }
  }
#endif
  ;

char *Def_Format_Into(YOYO_BUFFER *bf, YOYO_XNODE *r, int flags)
#ifdef _YOYO_DEFPARS_BUILTIN
  {
    int indent = (flags&0xff);
    if ( !bf ) bf = Buffer_Init(0);
    Def_Format_Node_In_Depth(bf,r,flags,indent);
    return (char*)bf->at;
  }
#endif
  ;
  
char *Def_Format(YOYO_XNODE *r, int flags)
#ifdef _YOYO_DEFPARS_BUILTIN
  {
    char *ret = 0;
    __Auto_Ptr(ret)
      {
        YOYO_BUFFER *bf = Buffer_Init(0);
        Def_Format_Into(bf,r,flags);
        ret = Buffer_Take_Data(bf);
      }
    return ret;
  }
#endif
  ;

void Def_Format_File(char *fname, YOYO_XNODE *r, int flags)
#ifdef _YOYO_DEFPARS_BUILTIN
  {
    __Auto_Release
      {
        YOYO_BUFFER *bf = Buffer_Init(0);
        Def_Format_Into(bf,r,flags);
        Oj_Write_Full(Cfile_Open(fname,"w+P"),bf->at,bf->count);
      }
  }
#endif
  ;

#endif /* C_once_750A77B2_260B_4E33_B9EA_15F01DDD61FF */

<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<










































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































Changes to tests/httpd.c.

45
46
47
48
49
50
51
52
53
54
55
56
57
58
59

#define _LIBYOYO
#include <httpd.hc>
#include <webhost.hc>
#include <webcfg.hc>
#include <xdata.hc>
#include <prog.hc>
#include <defpars.hc>

int main(int argc,char **argv)
  {

    Prog_Init(argc,argv,
      "?|help,"
      "d|debug,"







|







45
46
47
48
49
50
51
52
53
54
55
56
57
58
59

#define _LIBYOYO
#include <httpd.hc>
#include <webhost.hc>
#include <webcfg.hc>
#include <xdata.hc>
#include <prog.hc>
#include <xdef.hc>

int main(int argc,char **argv)
  {

    Prog_Init(argc,argv,
      "?|help,"
      "d|debug,"

Changes to xdata.hc.

670
671
672
673
674
675
676





















677
678
679
680
681
682
683
      {
        n = node;
        node = Xnode_Next(node);
      }
    
    return n;
  }





















#endif
  ;

void *Xdata_Allocate(YOYO_XDATA *doc, char *tag, ushort_t *idx)
#ifdef _YOYO_XDATA_BUILTIN
  {
    int no,ref,newidx;







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
      {
        n = node;
        node = Xnode_Next(node);
      }
    
    return n;
  }
#endif
  ;

int Xnode_Count(YOYO_XNODE *node)
#ifdef _YOYO_XDATA_BUILTIN
  {
    int i = 0;

    STRICT_REQUIRE( node );
    STRICT_REQUIRE( (node->opt&XVALUE_OPT_IS_VALUE) == 0 );
    
    node = Xnode_Down(node);
    
    while ( node ) 
      {
        ++i;
        node = Xnode_Next(node);
      }
    
    return i;
  }
#endif
  ;

void *Xdata_Allocate(YOYO_XDATA *doc, char *tag, ushort_t *idx)
#ifdef _YOYO_XDATA_BUILTIN
  {
    int no,ref,newidx;

Added xdef.hc.

































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800

/*

Copyright © 2010-2011, Alexéy Sudachén, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    http://www.gnu.org/licenses/

Otherwise:

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

Except as contained in this notice, the name of a copyright holder shall not
be used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization of the copyright holder.

*/

#ifndef C_once_750A77B2_260B_4E33_B9EA_15F01DDD61FF
#define C_once_750A77B2_260B_4E33_B9EA_15F01DDD61FF

#ifdef _LIBYOYO
#define _YOYO_XDEF_BUILTIN
#endif

#include "yoyo.hc"
#include "string.hc"
#include "buffer.hc"
#include "xdata.hc"
#include "file.hc"

enum
  {
    YOYO_XDEF_INDENT_WIDTH = 2,
    XDEF_FORMAT_XDEF = 0,
    XDEF_FORMAT_JSON = 0x100,
    XDEF_FORMAT_MASK = 0x300,
  };

typedef struct _YOYO_XDEF_STATE
  {
    char *text;
    int lineno;
  } YOYO_XDEF_STATE;

void Def_Parse_Skip_Spaces(YOYO_XDEF_STATE *st)
#ifdef _YOYO_XDEF_BUILTIN
  {
    while ( *st->text && Isspace(*st->text) )
      {
        if ( *st->text == '\n' ) ++st->lineno;
        ++st->text;
      }
  }
#endif
  ;

char *Def_Parse_Get_Literal(YOYO_XDEF_STATE *st)
#ifdef _YOYO_XDEF_BUILTIN
  {
    int capacity = 127;
    int len = 0;
    char *out = 0;
    
    if ( *st->text == '"' || *st->text == '\'')
      {
        char brk = *st->text;
        ++st->text;
        for ( ; *st->text && *st->text != brk; ++st->text )
          {
            if ( *st->text == '\\' )
              {
                ++st->text;
                if ( *st->text == '\n' )
                  ++st->lineno;
                else if ( *st->text == '\r' )
                  ; /* none */
                else if ( *st->text == '\\' || *st->text == '"' || *st->text == '\'' )
                  __Vector_Append(&out,&len,&capacity,st->text,1);
                else if ( *st->text == 'n' )
                  __Vector_Append(&out,&len,&capacity,"\n",1);
                else if ( *st->text == 'r' )
                  __Vector_Append(&out,&len,&capacity,"\r",1);
                else if ( *st->text == 't' )
                  __Vector_Append(&out,&len,&capacity,"\t",1);
                else 
                  __Raise(YOYO_ERROR_ILLFORMED,__Format("invalid esc sequence at line %d",st->lineno));
              }
            else
              __Vector_Append(&out,&len,&capacity,st->text,1);
          }
          
        if ( *st->text == brk ) ++st->text;
      }
    else
      {
        char *q = st->text;
        while ( *st->text && !Isspace(*st->text) 
              && *st->text != '(' && *st->text != ')' 
              && *st->text != '{' && *st->text != '}'
              && *st->text != '[' && *st->text != ']'  
              && *st->text != ',' && *st->text != '=' )
          ++st->text;
        __Vector_Append(&out,&len,&capacity,q,st->text-q);
      }
    
    return out;
  }
#endif
  ;


char *Def_Parse_Get_Node_Literal(YOYO_XDEF_STATE *st)
#ifdef _YOYO_XDEF_BUILTIN
  {
    if ( *st->text == '"' || *st->text == '\'' )
      return Def_Parse_Get_Literal(st);
    else
      {
        int capacity = 127;
        int len = 0;
        char *out = 0;
        char *q = st->text;
        while ( *st->text && *st->text != ')' )
          ++st->text;
        __Vector_Append(&out,&len,&capacity,q,st->text-q);
        return out;
      }
  }
#endif
  ;

typedef struct
  {
    int type;
    union
      {
        YOYO_ARRAY  *arr;
        YOYO_BUFFER *dat;
        char   *txt;
        int     dec;
        double  flt;
      };
  } YOYO_XDEF_VALUE;

void Def_Parse_Get_Value(YOYO_XDEF_STATE *st, YOYO_XDEF_VALUE *val)
#ifdef _YOYO_XDEF_BUILTIN
  {
    if ( *st->text == '[' )
      {
        ++st->text;
        if ( st->text[1] == '[' ) /* binary data */
          {
            byte_t b;
            val->dat = Buffer_Init(0);
            Def_Parse_Skip_Spaces(st);
            
            while ( Isxdigit(*st->text) )
              {
                if ( !Isxdigit(st->text[1]) )
                  __Raise(YOYO_ERROR_ILLFORMED,
                      __Format("expected hex byte value at line %d",st->lineno));
                
                b = Str_Unhex_Byte(st->text,0,0);
                Buffer_Append(val->dat,&b,1);
                st->text += 2;
                Def_Parse_Skip_Spaces(st);
              }
            
            if ( !*st->text || *st->text != ']' || st->text[1] != ']' )
              __Raise(YOYO_ERROR_ILLFORMED,
                  __Format("expected ']]' at line %d",st->lineno));
            
            val->type = XVALUE_OPT_VALTYPE_BIN;
            st->text += 2;
          }
        else /* array */
          { 
            YOYO_XDEF_VALUE lval;
            Def_Parse_Skip_Spaces(st);
            Def_Parse_Get_Value(st,&lval);
            if ( lval.type == XVALUE_OPT_VALTYPE_STR )
              {
                val->arr = Array_Ptrs();
                val->type= XVALUE_OPT_VALTYPE_STR_ARR;
                Array_Push(val->arr,__Retain(lval.txt));
              }
            else if ( lval.type == XVALUE_OPT_VALTYPE_INT 
                   || lval.type == XVALUE_OPT_VALTYPE_FLT )
              {
                val->dat = Buffer_Init(sizeof(double));
                val->type= XVALUE_OPT_VALTYPE_FLT_ARR;
                if ( lval.type == XVALUE_OPT_VALTYPE_INT )
                  *(double*)val->dat->at = lval.dec;
                else
                  *(double*)val->dat->at = lval.flt;
              }
            
            Def_Parse_Skip_Spaces(st);
            while ( *st->text == ',' )
              {
                ++st->text;
                Def_Parse_Skip_Spaces(st);
                Def_Parse_Get_Value(st,&lval);
                if ( val->type == XVALUE_OPT_VALTYPE_STR )
                  {
                    if ( val->type != XVALUE_OPT_VALTYPE_STR )
                      __Raise(YOYO_ERROR_ILLFORMED,
                              __Format("expected string value at line %d",st->lineno));
                    Array_Push(val->arr,__Retain(lval.txt));
                  }
                else /* XVALUE_OPT_VALUETYPE_FLT_ARR */
                  {
                    
                    if ( lval.type == XVALUE_OPT_VALTYPE_INT )
                      {
                        double f = lval.dec;
                        Buffer_Append(val->dat,&f,sizeof(double));
                      }
                    else if ( lval.type == XVALUE_OPT_VALTYPE_FLT )
                        Buffer_Append(val->dat,&lval.flt,sizeof(double));
                    else
                      __Raise(YOYO_ERROR_ILLFORMED,
                              __Format("expected numeric value at line %d",st->lineno));
                  }
                Def_Parse_Skip_Spaces(st);
              }
            
            if ( *st->text != ']' )
              __Raise(YOYO_ERROR_ILLFORMED,
                  __Format("expected ']' at line %d",st->lineno));
                  
            ++st->text;
          }
      }
    else if ( *st->text == '#' )
      {
        int l = 0;
        if ( (Str_Starts_With(st->text+1,"yes") && (l = 4))
          || (Str_Starts_With(st->text+1,"true") && (l = 5))
          || (Str_Starts_With(st->text+1,"on") && (l = 3))
          || (Str_Starts_With(st->text+1,"1") && (l = 2)) )
          {
            val->dec = 1;
            val->type = XVALUE_OPT_VALTYPE_BOOL;
            st->text += l;
          }
        else if ( (Str_Starts_With(st->text+1,"no") && (l = 3))
          || (Str_Starts_With(st->text+1,"false") && (l = 6))
          || (Str_Starts_With(st->text+1,"off") && (l = 4))
          || (Str_Starts_With(st->text+1,"0") && (l = 2)) )
          {
            val->dec = 0;
            val->type = XVALUE_OPT_VALTYPE_BOOL;
            st->text += l;
          }
        else
          __Raise(YOYO_ERROR_ILLFORMED,
              __Format("expected boolean value at line %d",st->lineno));
      }
    else if ( !Isdigit(*st->text) 
            && ( (*st->text != '.' && *st->text != '-') || !Isdigit(st->text[1]) ) )
      {
        val->txt = Def_Parse_Get_Literal(st);
        val->type = XVALUE_OPT_VALTYPE_STR;
      }
    else /* number */
      {
        if ( *st->text == '0' )
          {
            ++st->text;
            val->dec = 0;
            
            if ( *st->text == 'x' && Isxdigit(st->text[1]) ) /* hex value */
              {
                ++st->text;
                do
                  {
                    val->dec = val->dec << 4;
                    STR_UNHEX_HALF_OCTET(st->text,val->dec,0);
                    ++st->text;
                  }
                while ( Isxdigit(*st->text) );
              }
            else if ( *st->text >= '0' && *st->text <= '7' )
              {
                ++st->text;
                do
                  {
                    val->dec = (val->dec << 3) | (*st->text-'0'); 
                    ++st->text;
                  }
                while ( *st->text >= '0' && *st->text <= '7' );
              }
            else if ( Isspace(*st->text) )
              {
                ; /* nothing, it's zero value */
              }
            else
              goto invalid_numeric;
              
            val->type = XVALUE_OPT_VALTYPE_INT;
            
          }
        else if ( Isdigit(*st->text) || *st->text == '.' || *st->text == '-' ) /* decimal or float value */
          {
            int neg = 1;
            int value = 0;
            
            if ( *st->text == '-' ) { neg = -1; ++st->text; }
            
            for ( ; Isdigit(*st->text); ++st->text )
              value = value * 10 + ( *st->text - '0' );
            
            if ( *st->text == '.' )
              {
                double exp = 1;
                double d = value*neg;
                ++st->text;
                for ( ; Isdigit(*st->text); ++st->text )
                  {
                    d = d * 10 + ( *st->text - '0' );
                    exp *= 10;
                  }
                val->flt = d/exp;
                val->type = XVALUE_OPT_VALTYPE_FLT;
              }
            else
              {
                val->type = XVALUE_OPT_VALTYPE_INT;
                val->dec = value*neg;
              }
          }

        if ( *st->text 
          && !Isspace(*st->text) && *st->text != ')' 
          && *st->text != '}' && *st->text != ',' && *st->text != ']' )
      invalid_numeric:
          __Raise(YOYO_ERROR_ILLFORMED,
              __Format("invalid numeric value at line %d",st->lineno));
      }
  }
#endif
  ;

void Def_Parse_In_Node_Set_Value(YOYO_XNODE *n, char *name, YOYO_XDEF_VALUE *val)
#ifdef _YOYO_XDEF_BUILTIN
  {
    YOYO_XVALUE *xv = Xnode_Value(n,name,1);;
    switch ( val->type )
      {
        case XVALUE_OPT_VALTYPE_INT:
          Xvalue_Set_Int(xv,val->dec);
          break;
        case XVALUE_OPT_VALTYPE_BOOL:
          Xvalue_Set_Bool(xv,val->dec);
          break;
        case XVALUE_OPT_VALTYPE_FLT:
          Xvalue_Set_Flt(xv,val->flt);
          break;
        case XVALUE_OPT_VALTYPE_STR:
          Xvalue_Put_Str(xv,__Retain(val->txt));
          break;
        case XVALUE_OPT_VALTYPE_BIN:
          Xvalue_Put_Binary(xv,__Refe(val->dat));
          break;
        case XVALUE_OPT_VALTYPE_FLT_ARR:
          Xvalue_Put_Flt_Array(xv,__Refe(val->dat));
          break;
        case XVALUE_OPT_VALTYPE_STR_ARR:
          Xvalue_Put_Str_Array(xv,__Refe(val->arr));
          break;
        default:
          __Raise(YOYO_ERROR_UNEXPECTED_VALUE,0);
      }
      
  }
#endif
  ;

void Def_Parse_In_Node( YOYO_XDEF_STATE *st, YOYO_XNODE *n )
#ifdef _YOYO_XDEF_BUILTIN
  {
    Def_Parse_Skip_Spaces(st);
    
    for ( ; *st->text && *st->text != '}' ; Def_Parse_Skip_Spaces(st) ) 
      {
        int go_deeper = 0;
        YOYO_XNODE *nn = 0;
        
        __Auto_Release
          {
            YOYO_XDEF_VALUE value = {0};
            char *name; 
            
            name = Def_Parse_Get_Literal(st);
            Def_Parse_Skip_Spaces(st);

            if ( *st->text == '(' )
              {
                char *dflt;
                ++st->text;
                Def_Parse_Skip_Spaces(st);
                dflt = Def_Parse_Get_Node_Literal(st);
                Def_Parse_Skip_Spaces(st);
                if ( *st->text != ')' )
                  __Raise(YOYO_ERROR_ILLFORMED,__Format("expected ')' at line %d",st->lineno));
                ++st->text;
                Def_Parse_Skip_Spaces(st);

                nn = Xnode_Append(n,name);
                Xnode_Value_Set_Str(nn,"@",dflt);
                name = 0;
              }
            
            
            if ( *st->text == '=' )
              {
                ++st->text;
                Def_Parse_Skip_Spaces(st);
                Def_Parse_Get_Value(st,&value);
                Def_Parse_Skip_Spaces(st);
              }
            
            if ( *st->text == '{' )
              {
                ++st->text;
                go_deeper = 1;
                if ( !nn )
                  nn = Xnode_Append(n,name);
                if ( value.type )
                  Def_Parse_In_Node_Set_Value(nn,"$",&value);
              }
            else if ( value.type )
              {
                if ( name || !nn )
                  Def_Parse_In_Node_Set_Value(n,name,&value);
                else
                  Def_Parse_In_Node_Set_Value(nn,"$",&value);
              }
              
          }
        
        if ( go_deeper )
          {
            Def_Parse_In_Node(st,nn);
            if ( *st->text != '}' )
              __Raise(YOYO_ERROR_ILLFORMED,__Format("expected '}' at line %d",st->lineno));
            ++st->text;
          }
      }
  }
#endif
  ;

YOYO_XDATA *Def_Parse_Str(char *text)
#ifdef _YOYO_XDEF_BUILTIN
  {
    YOYO_XDEF_STATE st = { text, 1 };
    YOYO_XDATA *doc = Xdata_Init();
    Def_Parse_In_Node(&st,&doc->root);
    return doc;
  }
#endif
  ;

YOYO_XDATA *Def_Parse_File(char *filename)
#ifdef _YOYO_XDEF_BUILTIN
  {
    YOYO_XDATA *ret = 0;
    
    __Auto_Ptr(ret)
      {
        YOYO_BUFFER *bf = Oj_Read_All(Cfile_Open(filename,"rt"));
        ret = Def_Parse_Str((char*)bf->at);
      }
    return ret;
  }
#endif
  ;

void Def_Format_Node_In_Depth(YOYO_BUFFER *bf, YOYO_XNODE *r, int flags, int indent)
#ifdef _YOYO_XDEF_BUILTIN
  {
    __Gogo
      {
        YOYO_XVALUE *val = Xnode_First_Value(r);
        for ( ; val; val = Xnode_Next_Value(r,val) )
          {
            char *tag = Xnode_Value_Get_Tag(r,val);
            
            Buffer_Fill_Append(bf,' ',indent*YOYO_XDEF_INDENT_WIDTH);
            if ( (flags&XDEF_FORMAT_MASK) == XDEF_FORMAT_JSON )
              {
                Buffer_Fill_Append(bf,'"',1);
                Buffer_Append(bf,tag,-1);
                Buffer_Fill_Append(bf,'"',1);
                Buffer_Append(bf," : ",3);
              }
            else /* XDEF */
              {
                Buffer_Append(bf,tag,-1);
                Buffer_Append(bf," = ",3);
              }
                            
            switch ( val->opt&XVALUE_OPT_VALTYPE_MASK )
              {
                case XVALUE_OPT_VALTYPE_NONE:
                  if ( (flags&XDEF_FORMAT_MASK) == XDEF_FORMAT_JSON )
                    Buffer_Append(bf,"null",-1);
                  else /* XDEF */
                    Buffer_Append(bf,"#none",-1);
                  break;
                  
                case XVALUE_OPT_VALTYPE_BOOL:
                  if ( val->bval )
                    if ( (flags&XDEF_FORMAT_MASK) == XDEF_FORMAT_JSON )
                      Buffer_Append(bf,"true",-1);
                    else /* XDEF */
                      Buffer_Append(bf,"#true",-1);
                  else
                    if ( (flags&XDEF_FORMAT_MASK) == XDEF_FORMAT_JSON )
                      Buffer_Append(bf,"false",-1);
                    else /* XDEF */
                      Buffer_Append(bf,"#false",-1);
                  break;
                  
                case XVALUE_OPT_VALTYPE_INT:
                  Buffer_Printf(bf,"%ld",val->dec);
                  break;
                  
                case XVALUE_OPT_VALTYPE_FLT:
                  if ( val->flt - (double)((long)val->flt) > 0.0009999999 )
                    Buffer_Printf(bf,"%.3f",val->flt);
                  else
                    Buffer_Printf(bf,"%.f",val->flt);
                  break;
                  
                case XVALUE_OPT_VALTYPE_STR:
                  Buffer_Append(bf,"\"",1);
                  Buffer_Quote_Append(bf,val->txt,-1,'"');
                  Buffer_Append(bf,"\"",1);
                  break;
                  
                case XVALUE_OPT_VALTYPE_LIT:
                  Buffer_Append(bf,"\"",1);
                  Buffer_Quote_Append(bf,(char*)&val->down,-1,'"');
                  Buffer_Append(bf,"\"",1);
                  break;
                  
                case XVALUE_OPT_VALTYPE_BIN:
                  if ( (flags&XDEF_FORMAT_MASK) == XDEF_FORMAT_XDEF )
                    {
                      int bytes_per_line = 30;
                      int q = 0;
                      
                      Buffer_Append(bf,"[[",2);
                      
                      if ( val->binary->count > bytes_per_line )
                        {  
                          Buffer_Append(bf,"\n",1);
                          Buffer_Fill_Append(bf,' ',(indent+1)*YOYO_XDEF_INDENT_WIDTH);
                        }
                        
                      while ( q < val->binary->count )
                        {
                          int l = val->binary->count - q;
                          if ( l > bytes_per_line ) l = bytes_per_line;
                          Buffer_Hex_Append(bf,val->binary->at+q,l);
                          q += l;
                          if ( q < val->binary->count )
                            {
                              Buffer_Append(bf,"\n",1);
                              Buffer_Fill_Append(bf,' ',(indent+1)*YOYO_XDEF_INDENT_WIDTH);
                            }
                        }
                        
                      Buffer_Append(bf,"]]",2);
                    }
                  break;
                  
                case XVALUE_OPT_VALTYPE_STR_ARR:
                  {
                    int q = 0;
                    int count = Array_Count(val->strarr);
                    
                    Buffer_Append(bf,"[\n",2);
                    Buffer_Fill_Append(bf,' ',(indent+1)*YOYO_XDEF_INDENT_WIDTH);
                      
                    for ( ; q < count; ++q )
                      {
                        Buffer_Append(bf,"\"",1);
                        Buffer_Quote_Append(bf,val->strarr->at[q],-1,'"');
                        Buffer_Fill_Append(bf,'"',1);
                        if ( q+1 < count ) Buffer_Fill_Append(bf,',',1);
                        Buffer_Fill_Append(bf,'\n',1);
                        Buffer_Fill_Append(bf,' ',(indent+1)*YOYO_XDEF_INDENT_WIDTH);
                      }
                      
                    Buffer_Fill_Append(bf,']',1);
                    break;
                  }
                case XVALUE_OPT_VALTYPE_FLT_ARR:
                  {
                    int q = 0;
                    int nums_per_line = 5;
                    
                    Buffer_Fill_Append(bf,'[',1);
                    
                    if ( val->binary->count > nums_per_line*iszof_double )
                      {  
                        Buffer_Fill_Append(bf,'\n',1);
                        Buffer_Fill_Append(bf,' ',(indent+1)*YOYO_XDEF_INDENT_WIDTH);
                      }
                      
                    while ( q+iszof_double <= val->binary->count )
                      {
                        int l = (val->binary->count - q)/iszof_double;
                        if ( l > nums_per_line ) l = nums_per_line;
                        for ( ; l > 0; --l )
                          {
                            double d = *(double*)(val->binary->at+q*iszof_double);
                            
                            if ( (d - (double)((long)d)) > 0.000999999 )
                              Buffer_Printf(bf,"%.3f",d);
                            else
                              Buffer_Printf(bf,"%.f",d);
                            
                            q += iszof_double;
                            if ( q+iszof_double <= val->binary->count )
                            Buffer_Append(bf,",",1);
                          }
                        if ( q+iszof_double <= val->binary->count )
                          {
                            Buffer_Fill_Append(bf,'\n',1);
                            Buffer_Fill_Append(bf,' ',(indent+1)*YOYO_XDEF_INDENT_WIDTH);
                          }
                      }
                      
                    Buffer_Fill_Append(bf,']',1);
                    break;
                  }
              }
            
            if ( (flags&XDEF_FORMAT_MASK) == XDEF_FORMAT_JSON )
              if ( Xnode_Next_Value(r,val) || r->down )
                Buffer_Fill_Append(bf,',',1);
                
            Buffer_Fill_Append(bf,'\n',1);
          }
      }

    if ( (flags&XDEF_FORMAT_MASK) == XDEF_FORMAT_JSON )
      {
        u32_t *L;
        int i, ncount;
        YOYO_XNODE *n;
        r = Xnode_Refacc(r);
        n = Xnode_Down(r);
        if ( n )
          {
            ncount = Xnode_Count(r);
            L = alloca(32*ncount);
            L[0] = r->down|((u32_t)n->tag << 16);
            for ( i = 1; n->next; ++i ) 
              {
                YOYO_XNODE *nn = Xnode_Next(n);
                STRICT_REQUIRE(i<ncount);
                L[i] = n->next|((u32_t)nn->tag << 16);
                n = nn;
              }
            qsort(L,ncount,4,Compare_u32);
            for ( i = 0; i < ncount; )
              {
                char *tag = r->xdata->tags[(L[i]>>16)-1];
                n = Xdata_Idxref(r->xdata,L[i]&0x0ffff);
                Buffer_Fill_Append(bf,' ',indent*YOYO_XDEF_INDENT_WIDTH);
                Buffer_Fill_Append(bf,'"',1);
                Buffer_Append(bf,tag,-1);
                if ( i + 1 < ncount && !((L[i]^L[i+1])>>16) )
                  {
                    Buffer_Append(bf,"\" : [",5);
                    for(;;) 
                      {
                        Buffer_Append(bf,"{\n",2);
                        Def_Format_Node_In_Depth(bf,n,flags,indent+1);
                        Buffer_Fill_Append(bf,' ',indent*YOYO_XDEF_INDENT_WIDTH);
                        Buffer_Fill_Append(bf,'}',1);
                        ++i;
                        if ( i < ncount && !((L[i-1]^L[i])>>16) )
                          {
                            Buffer_Fill_Append(bf,',',1);
                            Buffer_Fill_Append(bf,'\n',1);
                            Buffer_Fill_Append(bf,' ',indent*YOYO_XDEF_INDENT_WIDTH);
                            n = Xdata_Idxref(r->xdata,L[i]&0x0ffff);
                          }
                        else
                          break;
                      }
                    Buffer_Fill_Append(bf,']',1);
                  }
                else
                  {
                    Buffer_Append(bf,"\" : {\n",6);
                    Def_Format_Node_In_Depth(bf,n,flags,indent+1);
                    Buffer_Fill_Append(bf,' ',indent*YOYO_XDEF_INDENT_WIDTH);
                    Buffer_Fill_Append(bf,'}',1);
                    ++i;
                  }
                if ( i && i < ncount )
                  Buffer_Fill_Append(bf,',',1);
                Buffer_Fill_Append(bf,'\n',1);
              }
          }
      }
    else /* XDEF */
      {
        YOYO_XNODE *n = Xnode_Down(r);
        for ( ; n; n = Xnode_Next(n) )
          {
            Buffer_Fill_Append(bf,' ',indent*YOYO_XDEF_INDENT_WIDTH);
            Buffer_Append(bf,Xnode_Get_Tag(n),-1);
            Buffer_Append(bf," {\n",3);
            Def_Format_Node_In_Depth(bf,n,flags,indent+1);
            Buffer_Fill_Append(bf,' ',indent*YOYO_XDEF_INDENT_WIDTH);
            Buffer_Append(bf,"}\n",2);
          }
      }
  }
#endif
  ;

char *Def_Format_Into(YOYO_BUFFER *bf, YOYO_XNODE *r, int flags)
#ifdef _YOYO_XDEF_BUILTIN
  {
    int indent = (flags&0xff);
    if ( !bf ) bf = Buffer_Init(0);
    Def_Format_Node_In_Depth(bf,r,flags,indent);
    return (char*)bf->at;
  }
#endif
  ;
  
char *Def_Format(YOYO_XNODE *r, int flags)
#ifdef _YOYO_XDEF_BUILTIN
  {
    char *ret = 0;
    __Auto_Ptr(ret)
      {
        YOYO_BUFFER *bf = Buffer_Init(0);
        Def_Format_Into(bf,r,flags);
        ret = Buffer_Take_Data(bf);
      }
    return ret;
  }
#endif
  ;

void Def_Format_File(char *fname, YOYO_XNODE *r, int flags)
#ifdef _YOYO_XDEF_BUILTIN
  {
    __Auto_Release
      {
        YOYO_BUFFER *bf = Buffer_Init(0);
        Def_Format_Into(bf,r,flags);
        Oj_Write_Full(Cfile_Open(fname,"w+P"),bf->at,bf->count);
      }
  }
#endif
  ;

#endif /* C_once_750A77B2_260B_4E33_B9EA_15F01DDD61FF */

Added xjson.hc.

























































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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

/*

Copyright © 2010-2011, Alexéy Sudachén, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    http://www.gnu.org/licenses/

Otherwise:

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

Except as contained in this notice, the name of a copyright holder shall not
be used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization of the copyright holder.

*/

#ifndef C_once_9163E833_294D_46EF_8F36_5F86432BD5F8
#define C_once_9163E833_294D_46EF_8F36_5F86432BD5F8

#ifdef _LIBYOYO
#define _YOYO_JSON_BUILTIN
#endif

#include "yoyo.hc"
#include "string.hc"
#include "buffer.hc"
#include "xdata.hc"
#include "file.hc"
#include "ctoken.hc"
#include "xdef.hc"

void Json_Parse_Block(YOYO_CTOKST *a, YOYO_XNODE *n, YOYO_BUFFER *bf1, YOYO_BUFFER *bf2)
#ifdef _YOYO_JSON_BUILTIN
  {
    YOYO_XVALUE *val;
    YOYO_ARRAY *arr;
    YOYO_XNODE *n2;
    int tk;
    for(;;)
      {
        tk = Ctok_Token(a,bf1);
        if ( tk != YO_CTOKEN_CSTRING && tk != '}' )
          __Raise_Format(YOYO_ERROR_ILLFORMED,(__yoTa("JSON expects string or '}' at %s:%d:%d",0),a->source,a->lineno,a->charno));
        if ( tk == '}' )
          break;
          
        tk = Ctok_Token(a,0);
        if ( tk != ':' )
          __Raise_Format(YOYO_ERROR_ILLFORMED,(__yoTa("JSON expects ':' at %s:%d:%d",0),a->source,a->lineno,a->charno));
        tk = Ctok_Token(a,bf2);
        if ( tk == '{' )
          {
            n2 = Xnode_Append(n,bf1->at);
            Json_Parse_Block(a,n2,bf1,bf2);
          }
        else if ( tk == '[' )
          {
            tk = Ctok_Token(a,bf2);
            if ( tk == '{' ) 
              {
                n2 = Xnode_Append(n,bf1->at);
                for (;;)
                  {
                    Json_Parse_Block(a,n2,bf1,bf2);
                    tk = Ctok_Token(a,bf2);
                    if ( tk != ']' && tk != ',' )
                      __Raise_Format(YOYO_ERROR_ILLFORMED,(__yoTa("JSON expects ']' or ',' at %s:%d:%d",0),a->source,a->lineno,a->charno));
                    if ( tk == ']' ) break;
                    if ( Ctok_Token(a,bf2) != '{' )
                      __Raise_Format(YOYO_ERROR_ILLFORMED,(__yoTa("JSON expects '{' at %s:%d:%d",0),a->source,a->lineno,a->charno));
                    n2 = Xnode_Append(n,Xnode_Get_Tag(n2));
                  }
              }
            else if ( tk == ']' )
              {
                /* empty array */
                val = Xnode_Value(n,bf1->at,1);
                Xvalue_Put_Str_Array(val,__Retain(Array_Ptrs()));
              }
            else
              {
                /* array of strings */
                val = Xnode_Value(n,bf1->at,1);
                arr = Array_Ptrs();
                for (;;)
                  {
                    switch( tk )
                      {
                      dafault:
                        __Raise_Format(YOYO_ERROR_ILLFORMED,(__yoTa("JSON expects value at %s:%d:%d",0),a->source,a->lineno,a->charno));
                      case YO_CTOKEN_CSTRING:
                      case YO_CTOKEN_DIGITS:
                      case YO_CTOKEN_XDIGITS:
                      case YO_CTOKEN_FLOAT:
                        Array_Push(arr,Str_Copy_Npl(bf2->at,-1));
                      }
                    tk = Ctok_Token(a,bf2);
                    if ( tk != ']' && tk != ',' )
                      __Raise_Format(YOYO_ERROR_ILLFORMED,(__yoTa("JSON expects ']' or ',' at %s:%d:%d",0),a->source,a->lineno,a->charno));
                    if ( tk == ']' ) 
                      break;
                    tk = Ctok_Token(a,bf2);
                    if ( tk == '{' )
                      __Raise_Format(YOYO_ERROR_ILLFORMED,(__yoTa("JSON disallow objects and values in one array at %s:%d:%d",0),a->source,a->lineno,a->charno));
                  }
                Xvalue_Put_Str_Array(val,__Retain(arr));
              }            
          }
        else
          {
            val = Xnode_Value(n,bf1->at,1);
            switch ( tk )
              {
              default:
              bad_value:
                __Raise_Format(YOYO_ERROR_ILLFORMED,(__yoTa("JSON expects value at %s:%d:%d",0),a->source,a->lineno,a->charno));
              case YO_CTOKEN_CSTRING:
                Xvalue_Set_Str(val,bf2->at,-1);
                break;
              case YO_CTOKEN_DIGITS:
              case YO_CTOKEN_XDIGITS:
                Xvalue_Set_Int(val,strtol(bf2->at,0,0));
                break;
              case YO_CTOKEN_FLOAT:
                Xvalue_Set_Flt(val,strtod(bf2->at,0));
                break;
              case YO_CTOKEN_ID:
                if ( !strcmp(bf2->at,"true") )
                  Xvalue_Set_Bool(val,1);
                else if ( !strcmp(bf2->at,"false") )
                  Xvalue_Set_Bool(val,0);
                else if ( !strcmp(bf2->at,"null") )
                  ;
                else goto bad_value;
                break;
              }
          }
          
        tk = Ctok_Token(a,0);
        if ( tk != ',' && tk != '}' )
          __Raise_Format(YOYO_ERROR_ILLFORMED,(__yoTa("JSON expects ',' or '}' at %s:%d:%d",0),a->source,a->lineno,a->charno));
        if ( tk == '}' )
          break;
      }
  }
#endif
  ;
  
YOYO_XDATA *Json_Parse_Text(char *text, char *source)
#ifdef _YOYO_JSON_BUILTIN
  {
    YOYO_CTOKST a; 
    YOYO_BUFFER *bf1,*bf2;
    YOYO_XNODE  *n = Xdata_Init();
    
    __Auto_Ptr(n)
      {
        Ctok_Init(&a,text,text+strlen(text),source);
        bf1 = Buffer_Init(0);
        bf2 = Buffer_Init(0);
        
        if ( Ctok_Token(&a,0) == '{' )
          Json_Parse_Block(&a,n,bf1,bf2);
        else
          __Raise_Format(YOYO_ERROR_ILLFORMED,(__yoTa("JSON expects '{' at %s:%d:%d",0),a.source,a.lineno,a.charno));
      }
      
    return n->xdata;
  }
#endif
  ;
  
YOYO_XDATA *Json_Parse_Str(char *text)
#ifdef _YOYO_JSON_BUILTIN
  {
    return Json_Parse_Text(text,__yoTa("<str>",0));
  }
#endif
  ;

YOYO_XDATA *Json_Parse_File(char *filename)
#ifdef _YOYO_JSON_BUILTIN
  {
    YOYO_XDATA *ret = 0;
    
    __Auto_Ptr(ret)
      {
        YOYO_BUFFER *bf = Oj_Read_All(Cfile_Open(filename,"rt"));
        ret = Json_Parse_Text((char*)bf->at,filename);
      }
      
    return ret;
  }
#endif
  ;

char *Json_Format_Into(YOYO_BUFFER *bf, YOYO_XNODE *r, int flags)
#ifdef _YOYO_JSON_BUILTIN
  {
    int indent = (flags&0xff);
    if ( !bf ) bf = Buffer_Init(0);
    Buffer_Append(bf,"{\n",2);
    Def_Format_Node_In_Depth(bf,r,flags|XDEF_FORMAT_JSON,indent+1);
    Buffer_Append(bf,"\n}\n",3);
    return (char*)bf->at;
  }
#endif
  ;
  
char *Json_Format(YOYO_XNODE *r, int flags)
#ifdef _YOYO_JSON_BUILTIN
  {
    char *ret = 0;
    __Auto_Ptr(ret)
      {
        YOYO_BUFFER *bf = Buffer_Init(0);
        Json_Format_Into(bf,r,flags);
        ret = Buffer_Take_Data(bf);
      }
    return ret;
  }
#endif
  ;

void Json_Format_File(char *fname, YOYO_XNODE *r, int flags)
#ifdef _YOYO_JSON_BUILTIN
  {
    __Auto_Release
      {
        YOYO_BUFFER *bf = Buffer_Init(0);
        Json_Format_Into(bf,r,flags);
        Oj_Write_Full(Cfile_Open(fname,"w+P"),bf->at,bf->count);
      }
  }
#endif
  ;

#endif /* C_once_750A77B2_260B_4E33_B9EA_15F01DDD61FF */

Changes to yoyo.hc.

369
370
371
372
373
374
375

376
377
378
379
380
381
382
...
713
714
715
716
717
718
719

















720
721
722
723
724
725
726
    YOYO_ERROR_ZERODIVIDE       = YOYO_FATAL_ERROR_GROUP|(YOYO_ERROR_BASE+27),
    YOYO_ERROR_LIMIT_REACHED    = YOYO_RUNTIME_ERROR_GROUP|(YOYO_ERROR_BASE+28),
    YOYO_ERROR_UNSUPPORTED      = YOYO_RUNTIME_ERROR_GROUP|(YOYO_ERROR_BASE+29),
    YOYO_ERROR_IO_EOF           = YOYO_IO_ERROR_GROUP|(YOYO_ERROR_BASE+30),
    YOYO_ERROR_DNS              = YOYO_IO_ERROR_GROUP|(YOYO_ERROR_BASE+31),
    YOYO_ERROR_SUBSYSTEM_INIT   = YOYO_RUNTIME_ERROR_GROUP|(YOYO_ERROR_BASE+32),
    YOYO_ERROR_SYSTEM           = YOYO_RUNTIME_ERROR_GROUP|(YOYO_ERROR_BASE+33),

  };

#define YOYO_ERROR_IS_USER_ERROR(err) !(err&YOYO_XXXX_ERROR_GROUP)

enum _YOYO_FLAGS
  {
    YO_RAISE_ERROR            = 0x70000000,
................................................................................
    uint_t Q;
    if ( !mod ) mod = 1;
    Q = Min_Pow2(mod) - 1;
    return (a+Q)&~Q;
  }
#endif
  ;


















#ifdef __windoze
size_t malloc_size(void *p) _YOYO_CORE_BUILTIN_CODE({return _msize(p);});
#endif /* __windoze */

void *Yo_Malloc_Npl(int size)
#ifdef _YOYO_CORE_BUILTIN







>







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
...
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
    YOYO_ERROR_ZERODIVIDE       = YOYO_FATAL_ERROR_GROUP|(YOYO_ERROR_BASE+27),
    YOYO_ERROR_LIMIT_REACHED    = YOYO_RUNTIME_ERROR_GROUP|(YOYO_ERROR_BASE+28),
    YOYO_ERROR_UNSUPPORTED      = YOYO_RUNTIME_ERROR_GROUP|(YOYO_ERROR_BASE+29),
    YOYO_ERROR_IO_EOF           = YOYO_IO_ERROR_GROUP|(YOYO_ERROR_BASE+30),
    YOYO_ERROR_DNS              = YOYO_IO_ERROR_GROUP|(YOYO_ERROR_BASE+31),
    YOYO_ERROR_SUBSYSTEM_INIT   = YOYO_RUNTIME_ERROR_GROUP|(YOYO_ERROR_BASE+32),
    YOYO_ERROR_SYSTEM           = YOYO_RUNTIME_ERROR_GROUP|(YOYO_ERROR_BASE+33),
    YOYO_ERROR_SYNTAX           = YOYO_ILLFORMED_ERROR_GROUP|(YOYO_ERROR_BASE+34),
  };

#define YOYO_ERROR_IS_USER_ERROR(err) !(err&YOYO_XXXX_ERROR_GROUP)

enum _YOYO_FLAGS
  {
    YO_RAISE_ERROR            = 0x70000000,
................................................................................
    uint_t Q;
    if ( !mod ) mod = 1;
    Q = Min_Pow2(mod) - 1;
    return (a+Q)&~Q;
  }
#endif
  ;

int Compare_u32(void const *a, void const *b)
#ifdef _YOYO_CORE_BUILTIN
  {
    return *(u32_t*)a - *(u32_t*)b;
  }
#endif
  ;

int Compare_Int(void const *a, void const *b)
#ifdef _YOYO_CORE_BUILTIN
  {
    return *(int*)a - *(int*)b;
  }
#endif
  ;


#ifdef __windoze
size_t malloc_size(void *p) _YOYO_CORE_BUILTIN_CODE({return _msize(p);});
#endif /* __windoze */

void *Yo_Malloc_Npl(int size)
#ifdef _YOYO_CORE_BUILTIN