-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathp21sql.l
More file actions
2641 lines (2490 loc) · 73.8 KB
/
p21sql.l
File metadata and controls
2641 lines (2490 loc) · 73.8 KB
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
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
** 2015-08-12
**
** The author disclaims copyright to this source code. In place of
** a legal notice, here is a blessing:
**
** May you do good and not evil.
** May you find forgiveness for yourself and forgive others.
** May you share freely, never taking more than you give.
**
******************************************************************************
**
** This SQLite extension implements P21 functions. The interface is
** modeled after MySQL JSON functions:
**
** https://dev.mysql.com/doc/refman/5.7/en/json.html
**
** For the time being, all P21 params are stored as pure text.
*/
#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_P21SQL)
#if !defined(SQLITEINT_H)
#include "sqlite3ext.h"
#endif
SQLITE_EXTENSION_INIT1
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
/* Mark a function parameter as unused, to suppress nuisance compiler
** warnings. */
#ifndef UNUSED_PARAM
# define UNUSED_PARAM(X) (void)(X)
#endif
#ifndef LARGEST_INT64
# define LARGEST_INT64 (0xffffffff|(((sqlite3_int64)0x7fffffff)<<32))
# define SMALLEST_INT64 (((sqlite3_int64)-1) - LARGEST_INT64)
#endif
/*
** Versions of isspace(), isalnum() and isdigit() to which it is safe
** to pass signed char values.
*/
#ifdef sqlite3Isdigit
/* Use the SQLite core versions if this routine is part of the
** SQLite amalgamation */
# define safe_isdigit(x) sqlite3Isdigit(x)
# define safe_isalnum(x) sqlite3Isalnum(x)
# define safe_isxdigit(x) sqlite3Isxdigit(x)
#else
/* Use the standard library for separate compilation */
#include <ctype.h> /* amalgamator: keep */
# define safe_isdigit(x) isdigit((unsigned char)(x))
# define safe_isalnum(x) isalnum((unsigned char)(x))
# define safe_isxdigit(x) isxdigit((unsigned char)(x))
#endif
/*
** Growing our own isspace() routine this way is twice as fast as
** the library isspace() function, resulting in a 7% overall performance
** increase for the parser. (Ubuntu14.10 gcc 4.8.4 x64 with -Os).
*/
static const char p21IsSpace[] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
};
#define safe_isspace(x) (p21IsSpace[(unsigned char)x])
#ifndef SQLITE_AMALGAMATION
/* Unsigned integer types. These are already defined in the sqliteInt.h,
** but the definitions need to be repeated for separate compilation. */
typedef sqlite3_uint64 u64;
typedef unsigned int u32;
typedef unsigned short int u16;
typedef unsigned char u8;
#endif
/* some C implementations don't have these? (inttypes.h / stdint.h) */
#ifndef UINT16_WIDTH
# define UINT16_WIDTH 16
#endif
#ifndef UINT16_MAX
# define UINT16_MAX 65535
#endif
/* Objects */
typedef struct P21String P21String;
typedef struct P21Node P21Node;
typedef struct P21Parse P21Parse;
/* An instance of this object represents a P21 parameter string
** under construction. Really, this is a generic string accumulator
** that can be and is used to create strings other than JSON (here P21!).
*/
struct P21String {
sqlite3_context *pCtx; /* Function context - put error messages here */
char *zBuf; /* Append P21 content here */
u64 nAlloc; /* Bytes of storage available in zBuf[] */
u64 nUsed; /* Bytes of zBuf[] currently used */
u8 bStatic; /* True if zBuf is static space */
u8 bErr; /* True if an error has been encountered */
char zSpace[100]; /* Initial static space */
};
#define P21_EMPTY 0x1 /* optional attribute not provided : '$' */
#define P21_DERIVED 0x2 /* derived attribute not provided : '*' */
#define P21_ENUMERATION 0x3 /* (also) includes boolean and logical values */
#define P21_INTEGER 0x4
#define P21_REAL 0x5
#define P21_STRING 0x6
#define P21_BINARY 0x7
#define P21_EID 0x8 /* entity_instance_name */
#define P21_LIST 0x9
#define P21_RECORD 0xA /* simple_record */
#define P21_SUBTYPE 80 /* Ascii for "P" */
/*
** Names of the various P21 types:
*/
static const char * const p21Type[] = {
"",
"empty", "derived", "enumeration", "integer", "real",
"string", "binary", "eid", "list", "record"
};
/* Bit values for the P21Node.jnFlag field
*/
#define PNODE_RAW 0x01 /* Content is raw, not P21 encoded */
#define PNODE_ESCAPE 0x02 /* Content is text with \ escapes */
#define PNODE_REMOVE 0x04 /* Do not output */
#define PNODE_REPLACE 0x08 /* Replace with P21Node.u.iReplace */
#define PNODE_PATCH 0x10 /* Patch with P21Node.u.pPatch */
#define PNODE_APPEND 0x20 /* More ARRAY/OBJECT entries at u.iAppend */
#define PNODE_LABEL 0x40 /* Is a label of an object */
/* A single node of parsed P21 params
*/
struct P21Node {
u8 eType; /* One of the P21_ type values */
u8 jnFlags; /* P21Node flags */
u16 n_kw; /* store the KEYWORD length */
u32 n; /* Bytes of content, or number of sub-nodes */
union {
const char *zJContent; /* Content for INT, REAL, and STRING */
u32 iAppend; /* More terms for ARRAY and OBJECT */
u32 iKey; /* Key for ARRAY objects in p21_tree() */
u32 iReplace; /* Replacement content for PNODE_REPLACE */
P21Node *pPatch; /* Node chain of patch for PNODE_PATCH */
} u;
};
/* A completely parsed P21 string
*/
struct P21Parse {
u32 nNode; /* Number of slots of aNode[] used */
u32 nAlloc; /* Number of slots of aNode[] allocated */
P21Node *aNode; /* Array of nodes containing the parse */
const char *zP21; /* Original P21 string */
u32 *aUp; /* Index of parent of each node */
u8 oom; /* Set to true if out of memory */
u8 nErr; /* Number of errors seen */
u16 iDepth; /* Nesting depth */
int nP21; /* Length of the zP21 string in bytes */
u32 iHold; /* Replace cache line with the lowest iHold value */
};
/*
** Maximum nesting depth of P21 for this implementation.
*/
#define P21_MAX_DEPTH 20
/**************************************************************************
** Utility routines for dealing with P21String objects
**************************************************************************/
/* Set the P21String object to an empty string
*/
static void p21Zero(P21String *p){
p->zBuf = p->zSpace;
p->nAlloc = sizeof(p->zSpace);
p->nUsed = 0;
p->bStatic = 1;
}
/* Initialize the P21String object
*/
static void p21Init(P21String *p, sqlite3_context *pCtx){
p->pCtx = pCtx;
p->bErr = 0;
p21Zero(p);
}
/* Free all allocated memory and reset the P21String object back to its
** initial state.
*/
static void p21Reset(P21String *p){
if( !p->bStatic ) sqlite3_free(p->zBuf);
p21Zero(p);
}
/* Report an out-of-memory (OOM) condition
*/
static void p21Oom(P21String *p){
p->bErr = 1;
sqlite3_result_error_nomem(p->pCtx);
p21Reset(p);
}
/* Enlarge p->zBuf so that it can hold at least N more bytes.
** Return zero on success. Return non-zero on an OOM error
*/
static int p21Grow(P21String *p, u32 N){
u64 nTotal = N<p->nAlloc ? p->nAlloc*2 : p->nAlloc+N+10;
char *zNew;
if( p->bStatic ){
if( p->bErr ) return 1;
zNew = sqlite3_malloc64(nTotal);
if( zNew==0 ){
p21Oom(p);
return SQLITE_NOMEM;
}
memcpy(zNew, p->zBuf, (size_t)p->nUsed);
p->zBuf = zNew;
p->bStatic = 0;
}else{
zNew = sqlite3_realloc64(p->zBuf, nTotal);
if( zNew==0 ){
p21Oom(p);
return SQLITE_NOMEM;
}
p->zBuf = zNew;
}
p->nAlloc = nTotal;
return SQLITE_OK;
}
/* Append N bytes from zIn onto the end of the P21String string.
*/
static void p21AppendRaw(P21String *p, const char *zIn, u32 N){
if( (N+p->nUsed >= p->nAlloc) && p21Grow(p,N)!=0 ) return;
memcpy(p->zBuf+p->nUsed, zIn, N);
p->nUsed += N;
}
/* Append formatted text (not to exceed N bytes) to the P21String.
*/
static void p21Printf(int N, P21String *p, const char *zFormat, ...){
va_list ap;
if( (p->nUsed + N >= p->nAlloc) && p21Grow(p, N) ) return;
va_start(ap, zFormat);
sqlite3_vsnprintf(N, p->zBuf+p->nUsed, zFormat, ap);
va_end(ap);
p->nUsed += (int)strlen(p->zBuf+p->nUsed);
}
/* Append a single character
*/
static void p21AppendChar(P21String *p, char c){
if( p->nUsed>=p->nAlloc && p21Grow(p,1)!=0 ) return;
p->zBuf[p->nUsed++] = c;
}
/* Append a comma separator to the output buffer, if the previous
** character is not '[' or '{'.
*/
static void p21AppendSeparator(P21String *p){
char c;
if( p->nUsed==0 ) return;
c = p->zBuf[p->nUsed-1];
if( c!='(' ) p21AppendChar(p, ',');
}
/* Append the N-byte string in zIn to the end of the P21String string
** under construction. Enclose the string in '...' and escape
** any double-quotes or backslash characters contained within the
** string.
*/
static void p21AppendString(P21String *p, const char *zIn, u32 N){
u32 i;
if( (N+p->nUsed+2 >= p->nAlloc) && p21Grow(p,N+2)!=0 ) return;
p->zBuf[p->nUsed++] = '\'';
for(i=0; i<N; i++){
unsigned char c = ((unsigned const char*)zIn)[i];
/* TODO: string escapes are different e.g. '' */
if( c=='\'' || c=='\\' ){
p21_simple_escape:
if( (p->nUsed+N+3-i > p->nAlloc) && p21Grow(p,N+3-i)!=0 ) return;
p->zBuf[p->nUsed++] = '\\';
}else if( c<=0x1f ){
static const char aSpecial[] = {
0, 0, 0, 0, 0, 0, 0, 0, 'b', 't', 'n', 0, 'f', 'r', 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
assert( sizeof(aSpecial)==32 );
assert( aSpecial['\b']=='b' );
assert( aSpecial['\f']=='f' );
assert( aSpecial['\n']=='n' );
assert( aSpecial['\r']=='r' );
assert( aSpecial['\t']=='t' );
if( aSpecial[c] ){
c = aSpecial[c];
goto p21_simple_escape;
}
if( (p->nUsed+N+7+i > p->nAlloc) && p21Grow(p,N+7-i)!=0 ) return;
p->zBuf[p->nUsed++] = '\\';
p->zBuf[p->nUsed++] = 'u';
p->zBuf[p->nUsed++] = '0';
p->zBuf[p->nUsed++] = '0';
p->zBuf[p->nUsed++] = '0' + (c>>4);
c = "0123456789abcdef"[c&0xf];
}
p->zBuf[p->nUsed++] = c;
}
p->zBuf[p->nUsed++] = '\'';
assert( p->nUsed<p->nAlloc );
}
/*
** Append a function parameter value to the P21 string under
** construction.
*/
static void p21AppendValue(
P21String *p, /* Append to this P21 string */
sqlite3_value *pValue /* Value to append */
){
switch( sqlite3_value_type(pValue) ){
case SQLITE_NULL: {
p21AppendRaw(p, "$", 1);
break;
}
case SQLITE_INTEGER:
case SQLITE_FLOAT: {
const char *z = (const char*)sqlite3_value_text(pValue);
/* TODO: confirm format is valid */
u32 n = (u32)sqlite3_value_bytes(pValue);
p21AppendRaw(p, z, n);
break;
}
case SQLITE_TEXT: {
const char *z = (const char*)sqlite3_value_text(pValue);
u32 n = (u32)sqlite3_value_bytes(pValue);
if( sqlite3_value_subtype(pValue)==P21_SUBTYPE ){
p21AppendRaw(p, z, n);
}else{
p21AppendString(p, z, n);
}
break;
}
default: {
if( p->bErr==0 ){
sqlite3_result_error(p->pCtx, "P21 cannot hold BLOB values", -1);
p->bErr = 2;
p21Reset(p);
}
break;
}
}
}
/* Make the P21 in p the result of the SQL function.
*/
static void p21Result(P21String *p){
if( p->bErr==0 ){
sqlite3_result_text64(p->pCtx, p->zBuf, p->nUsed,
p->bStatic ? SQLITE_TRANSIENT : sqlite3_free,
SQLITE_UTF8);
p21Zero(p);
}
assert( p->bStatic );
}
/**************************************************************************
** Utility routines for dealing with P21Node and P21Parse objects
**************************************************************************/
/*
** Return the number of consecutive P21Node slots need to represent
** the parsed P21 at pNode. The minimum answer is 1. For ARRAY and
** OBJECT types, the number might be larger.
**
** Appended elements are not counted. The value returned is the number
** by which the P21Node counter should increment in order to go to the
** next peer value.
*/
static u32 p21NodeSize(P21Node *pNode){
return pNode->eType < P21_LIST ? 1 : pNode->n + 1;
}
/*
** Reclaim all memory allocated by a P21Parse object. But do not
** delete the P21Parse object itself.
*/
static void p21ParseReset(P21Parse *pParse){
sqlite3_free(pParse->aNode);
pParse->aNode = 0;
pParse->nNode = 0;
pParse->nAlloc = 0;
sqlite3_free(pParse->aUp);
pParse->aUp = 0;
}
/*
** Free a P21Parse object that was obtained from sqlite3_malloc().
*/
static void p21ParseFree(P21Parse *pParse){
p21ParseReset(pParse);
sqlite3_free(pParse);
}
/*
** Convert the P21Node pNode into a pure P21 string and
** append to pOut. Subsubstructure is also included. Return
** the number of P21Node objects that are encoded.
*/
static void p21RenderNode(
P21Node *pNode, /* The node to render */
P21String *pOut, /* Write P21 here */
sqlite3_value **aReplace /* Replacement values */
){
if( pNode->jnFlags & (PNODE_REPLACE|PNODE_PATCH) ){
if( pNode->jnFlags & PNODE_REPLACE ){
p21AppendValue(pOut, aReplace[pNode->u.iReplace]);
return;
}
pNode = pNode->u.pPatch;
}
switch( pNode->eType ){
default: {
assert( pNode->eType==P21_EMPTY );
p21AppendChar(pOut, '$');
break;
}
case P21_ENUMERATION: {
p21AppendRaw(pOut, pNode->u.zJContent, pNode->n);
break;
}
case P21_DERIVED: {
p21AppendChar(pOut, '*');
break;
}
case P21_BINARY: {
p21AppendRaw(pOut, pNode->u.zJContent, pNode->n);
break;
}
case P21_EID: {
p21AppendRaw(pOut, pNode->u.zJContent, pNode->n);
break;
}
case P21_STRING: {
if( pNode->jnFlags & PNODE_RAW ){
p21AppendString(pOut, pNode->u.zJContent, pNode->n);
break;
}
/* Fall through into the next case */
}
case P21_REAL:
case P21_INTEGER: {
p21AppendRaw(pOut, pNode->u.zJContent, pNode->n);
break;
}
case P21_LIST: {
u32 j = 1;
p21AppendChar(pOut, '(');
for(;;){
while( j<=pNode->n ){
if( (pNode[j].jnFlags & PNODE_REMOVE)==0 ){
p21AppendSeparator(pOut);
p21RenderNode(&pNode[j], pOut, aReplace);
}
j += p21NodeSize(&pNode[j]);
}
if( (pNode->jnFlags & PNODE_APPEND)==0 ) break;
pNode = &pNode[pNode->u.iAppend];
j = 1;
}
p21AppendChar(pOut, ')');
break;
}
case P21_RECORD: {
u32 j = 1;
p21AppendRaw(pOut, pNode->u.zJContent, pNode->n_kw);
p21AppendChar(pOut, '(');
for(;;){
while( j<= pNode->n ){
if( (pNode[j].jnFlags & PNODE_REMOVE)==0 ){
p21AppendSeparator(pOut);
p21RenderNode(&pNode[j], pOut, aReplace);
}
j += p21NodeSize(&pNode[j]);
}
if( (pNode->jnFlags & PNODE_APPEND)==0 ) break;
pNode = &pNode[pNode->u.iAppend];
j = 1;
}
p21AppendChar(pOut, ')');
break;
}
}
}
/*
** Return a P21Node and all its descendents as a P21 string.
*/
static void p21ReturnP21(
P21Node *pNode, /* Node to return */
sqlite3_context *pCtx, /* Return value for this function */
sqlite3_value **aReplace /* Array of replacement values */
){
P21String s;
p21Init(&s, pCtx);
p21RenderNode(pNode, &s, aReplace);
p21Result(&s);
sqlite3_result_subtype(pCtx, P21_SUBTYPE);
}
/*
** Translate a single byte of Hex into an integer.
** This routine only works if h really is a valid hexadecimal
** character: 0..9a..fA..F
*/
static u8 p21HexToInt(int h){
assert( (h>='0' && h<='9') || (h>='a' && h<='f') || (h>='A' && h<='F') );
#ifdef SQLITE_EBCDIC
h += 9*(1&~(h>>4));
#else
h += 9*(1&(h>>6));
#endif
return (u8)(h & 0xf);
}
/*
** Convert a 4-byte hex string into an integer
*/
static u32 p21HexToInt4(const char *z){
u32 v;
assert( safe_isxdigit(z[0]) );
assert( safe_isxdigit(z[1]) );
assert( safe_isxdigit(z[2]) );
assert( safe_isxdigit(z[3]) );
v = (p21HexToInt(z[0])<<12)
+ (p21HexToInt(z[1])<<8)
+ (p21HexToInt(z[2])<<4)
+ p21HexToInt(z[3]);
return v;
}
/*
** Make the P21Node the return value of the function.
*/
static void p21Return(
P21Node *pNode, /* Node to return */
sqlite3_context *pCtx, /* Return value for this function */
sqlite3_value **aReplace /* Array of replacement values */
){
switch( pNode->eType ){
default: {
assert( pNode->eType==P21_EMPTY );
sqlite3_result_null(pCtx);
break;
}
case P21_DERIVED: {
assert(0);
}
case P21_ENUMERATION: {
assert(0);
}
case P21_BINARY: {
assert(0);
}
case P21_EID: {
sqlite3_result_text(pCtx, pNode->u.zJContent, pNode->n, SQLITE_TRANSIENT);
break;
}
case P21_INTEGER: {
sqlite3_int64 i = 0;
const char *z = pNode->u.zJContent;
if( z[0]=='-' ){ z++; }
while( z[0]>='0' && z[0]<='9' ){
unsigned v = *(z++) - '0';
if( i>=LARGEST_INT64/10 ){
if( i>LARGEST_INT64/10 ) goto int_as_real;
if( z[0]>='0' && z[0]<='9' ) goto int_as_real;
if( v==9 ) goto int_as_real;
if( v==8 ){
if( pNode->u.zJContent[0]=='-' ){
sqlite3_result_int64(pCtx, SMALLEST_INT64);
goto int_done;
}else{
goto int_as_real;
}
}
}
i = i*10 + v;
}
if( pNode->u.zJContent[0]=='-' ){ i = -i; }
sqlite3_result_int64(pCtx, i);
int_done:
break;
int_as_real: /* fall through to real */;
}
case P21_REAL: {
double r;
#ifdef SQLITE_AMALGAMATION
const char *z = pNode->u.zJContent;
sqlite3AtoF(z, &r, sqlite3Strlen30(z), SQLITE_UTF8);
#else
r = strtod(pNode->u.zJContent, 0);
#endif
sqlite3_result_double(pCtx, r);
break;
}
case P21_STRING: {
#if 0 /* Never happens because PNODE_RAW is only set by p21_set(),
** p21_insert() and p21_replace() and those routines do not
** call p21Return() */
if( pNode->jnFlags & PNODE_RAW ){
sqlite3_result_text(pCtx, pNode->u.zJContent, pNode->n,
SQLITE_TRANSIENT);
}else
#endif
assert( (pNode->jnFlags & PNODE_RAW)==0 );
if( (pNode->jnFlags & PNODE_ESCAPE)==0 ){
/* P21 formatted without any backslash-escapes */
sqlite3_result_text(pCtx, pNode->u.zJContent+1, pNode->n-2,
SQLITE_TRANSIENT);
}else{
/* Translate P21 formatted string into raw text */
u32 i;
u32 n = pNode->n;
const char *z = pNode->u.zJContent;
char *zOut;
u32 j;
/* TODO: */
assert(0);
zOut = sqlite3_malloc( n+1 );
if( zOut==0 ){
sqlite3_result_error_nomem(pCtx);
break;
}
for(i=1, j=0; i<n-1; i++){
char c = z[i];
if( c!='\\' ){
zOut[j++] = c;
}else{
c = z[++i];
if( c=='u' ){
u32 v = p21HexToInt4(z+i+1);
i += 4;
if( v==0 ) break;
if( v<=0x7f ){
zOut[j++] = (char)v;
}else if( v<=0x7ff ){
zOut[j++] = (char)(0xc0 | (v>>6));
zOut[j++] = 0x80 | (v&0x3f);
}else{
u32 vlo;
if( (v&0xfc00)==0xd800
&& i<n-6
&& z[i+1]=='\\'
&& z[i+2]=='u'
&& ((vlo = p21HexToInt4(z+i+3))&0xfc00)==0xdc00
){
/* We have a surrogate pair */
v = ((v&0x3ff)<<10) + (vlo&0x3ff) + 0x10000;
i += 6;
zOut[j++] = 0xf0 | (v>>18);
zOut[j++] = 0x80 | ((v>>12)&0x3f);
zOut[j++] = 0x80 | ((v>>6)&0x3f);
zOut[j++] = 0x80 | (v&0x3f);
}else{
zOut[j++] = 0xe0 | (v>>12);
zOut[j++] = 0x80 | ((v>>6)&0x3f);
zOut[j++] = 0x80 | (v&0x3f);
}
}
}else{
if( c=='b' ){
c = '\b';
}else if( c=='f' ){
c = '\f';
}else if( c=='n' ){
c = '\n';
}else if( c=='r' ){
c = '\r';
}else if( c=='t' ){
c = '\t';
}
zOut[j++] = c;
}
}
}
zOut[j] = 0;
sqlite3_result_text(pCtx, zOut, j, sqlite3_free);
}
break;
}
case P21_LIST:
case P21_RECORD: {
p21ReturnP21(pNode, pCtx, aReplace);
break;
}
}
}
/* Forward reference */
static int p21ParseAddNode(P21Parse*,u32,u32,const char*);
/*
** A macro to hint to the compiler that a function should not be
** inlined.
*/
#if defined(__GNUC__)
# define P21_NOINLINE __attribute__((noinline))
#elif defined(_MSC_VER) && _MSC_VER>=1310
# define P21_NOINLINE __declspec(noinline)
#else
# define P21_NOINLINE
#endif
static P21_NOINLINE int p21ParseAddNodeExpand(
P21Parse *pParse, /* Append the node to this object */
u32 eType, /* Node type */
u32 n, /* Content size or sub-node count */
const char *zContent /* Content */
){
u32 nNew;
P21Node *pNew;
assert( pParse->nNode>=pParse->nAlloc );
if( pParse->oom ) return -1;
nNew = pParse->nAlloc*2 + 10;
pNew = sqlite3_realloc64(pParse->aNode, sizeof(P21Node)*nNew);
if( pNew==0 ){
pParse->oom = 1;
return -1;
}
pParse->nAlloc = nNew;
pParse->aNode = pNew;
assert( pParse->nNode<pParse->nAlloc );
return p21ParseAddNode(pParse, eType, n, zContent);
}
/*
** Create a new P21Node instance based on the arguments and append that
** instance to the P21Parse. Return the index in pParse->aNode[] of the
** new node, or -1 if a memory allocation fails.
*/
static int p21ParseAddNode(
P21Parse *pParse, /* Append the node to this object */
u32 eType, /* Node type */
u32 n, /* Content size or sub-node count */
const char *zContent /* Content */
){
P21Node *p;
if( pParse->nNode>=pParse->nAlloc ){
return p21ParseAddNodeExpand(pParse, eType, n, zContent);
}
p = &pParse->aNode[pParse->nNode];
p->eType = (u8)eType;
p->jnFlags = 0;
p->n = n;
p->u.zJContent = zContent;
return pParse->nNode++;
}
/*
** Return true if z[] begins with 4 (or more) hexadecimal digits
*/
static int p21Is4Hex(const char *z){
int i;
for(i=0; i<4; i++) if( !safe_isxdigit(z[i]) ) return 0;
return 1;
}
/*
** Parse P21 value which begins at pParse->zP21[i]. Return the
** index of the first character past the end of the value parsed.
**
** Return negative for a syntax error.
*/
static int p21ParseValue(P21Parse *pParse, u32 i) {
static int cxtStack[P21_MAX_DEPTH];
const unsigned char *sp, *cur, *mrk, *tok, *end;
/*!stags:re2c format = 'const unsigned char *@@;'; */
int *piThis, x;
u32 n;
P21Node *pNode;
sp = cur = tok = &pParse->zP21[i];
piThis = cxtStack + pParse->iDepth;
/*!re2c
re2c:yyfill:enable = 0;
re2c:flags:tags = 1;
re2c:define:YYCTYPE = 'unsigned char';
re2c:define:YYCURSOR = 'cur';
re2c:define:YYMARKER = 'mrk';
ascii_encoding = [][!"*$%&.#+,\-()?/:;<=>@{}|^`~0-9a-zA-Z_ ] | "''" | "\\" ;
page_encoding = "\\" [A-I] "\\" | "\\S\\" [][!"'*$%&.#+,\-()?/:;<=>@{}|^`~0-9a-zA-Z_\\ ] ;
hex_encoding = "\\X2\\" ([0-9A-F]{4})+ "\\X0\\" | "\\X4\\" ([0-9A-F]{8})+ "\\X0\\" ;
byte_encoding = "\\X\\" [0-9A-F]{2} ;
WS = [ \t\r\n] ;
KEYWORD = "!"? [A-Za-z_] [0-9A-Za-z_]* ;
REAL = [+-]* [0-9] [0-9]* "." [0-9]* ("E" [+-]* [0-9] [0-9]*)? ;
INTEGER = [+-]* [0-9] [0-9]* ;
STRING = "'" (ascii_encoding | page_encoding | hex_encoding | byte_encoding )* "'" ;
BINARY = '"' [0-3] [0-9A-F]* '"' ;
ENUMERATION = "." [A-Z_] [A-Z0-9_]* "." ;
EID = "#" [0-9]+ ;
*/
start:
tok = cur;
/*!re2c
WS+ {
goto start;
}
"(" {
/* (complex_entity_instance) parameter_list */
*piThis = p21ParseAddNode(pParse, P21_LIST, 0, 0);
if (*piThis < 0) return -1;
if( ++pParse->iDepth > P21_MAX_DEPTH ) return -1;
piThis = cxtStack + pParse->iDepth;
goto keywords;
}
"" {
/* (simple_entity_instance) parameter_list */
*piThis = p21ParseAddNode(pParse, P21_RECORD, 0, 0);
if (*piThis < 0) return -1;
if( ++pParse->iDepth > P21_MAX_DEPTH ) return -1;
piThis = cxtStack + pParse->iDepth;
goto params1;
}
*/
keywords:
tok = cur;
/*!re2c
WS+ {
goto keywords;
}
KEYWORD @end WS* "(" {
*piThis = p21ParseAddNode(pParse, P21_RECORD, 0, tok);
if (*piThis < 0) return -1;
pParse->aNode[*piThis].n_kw = (u16)(end - tok);
if( ++pParse->iDepth > P21_MAX_DEPTH ) return -1;
piThis = cxtStack + pParse->iDepth;
goto params2;
}
")" {
piThis = cxtStack + --pParse->iDepth;
pNode = pParse->aNode + *piThis;
assert(pNode->eType == P21_LIST);
pNode->n = pParse->nNode - (u32)*piThis - 1;
goto eol;
}
"" {
/* fix-up and revert to P21_RECORD */
pNode = pParse->aNode + *(piThis - 1);
pNode->eType = P21_RECORD;
assert(pParse->iDepth == 1);
goto params1;
}
*/
params1:
tok = cur;
/*!re2c
WS+ {
goto params1;
}
KEYWORD @end WS* "(" {
*piThis = p21ParseAddNode(pParse, P21_RECORD, 0, tok);
if (*piThis < 0) return -1;
pParse->aNode[*piThis].n_kw = (u16)(end - tok);
if( ++pParse->iDepth > P21_MAX_DEPTH ) return -1;
piThis = cxtStack + pParse->iDepth;
goto params1;
}
"(" {
*piThis = p21ParseAddNode(pParse, P21_LIST, 0, 0);
if (*piThis < 0) return -1;
if( ++pParse->iDepth > P21_MAX_DEPTH ) return -1;
piThis = cxtStack + pParse->iDepth;
goto params1;
}
"," {
goto params1;
}
REAL {
p21ParseAddNode(pParse, P21_REAL, cur - tok, tok);
goto params1;
}
INTEGER {
p21ParseAddNode(pParse, P21_INTEGER, cur - tok, tok);
goto params1;
}
STRING {
p21ParseAddNode(pParse, P21_STRING, cur - tok, tok);
goto params1;
}
BINARY {
p21ParseAddNode(pParse, P21_BINARY, cur - tok, tok);
goto params1;
}
ENUMERATION {
p21ParseAddNode(pParse, P21_ENUMERATION, cur - tok, tok);
goto params1;
}
EID {
p21ParseAddNode(pParse, P21_EID, cur - tok, tok);
goto params1;
}
"$" {
p21ParseAddNode(pParse, P21_EMPTY, cur - tok, tok);
goto params1;
}
"*" {
p21ParseAddNode(pParse, P21_DERIVED, cur - tok, tok);
goto params1;
}
")" {
piThis = cxtStack + --pParse->iDepth;
pNode = pParse->aNode + *piThis;
pNode->n = pParse->nNode - (u32)*piThis - 1;
goto params1;
}
"" {
if (pParse->iDepth) --pParse->iDepth;
piThis = cxtStack + pParse->iDepth;
pNode = pParse->aNode + *piThis;
assert(pNode->eType == P21_RECORD);
pNode->n = pParse->nNode - (u32)*piThis - 1;
goto eol;
}
*/
params2:
tok = cur;
/*!re2c
WS+ {
goto params2;
}
KEYWORD @end WS* "(" {
*piThis = p21ParseAddNode(pParse, P21_RECORD, 0, tok);
if (*piThis < 0) return -1;
pParse->aNode[*piThis].n_kw = (u16)(end - tok);
if( ++pParse->iDepth > P21_MAX_DEPTH ) return -1;
piThis = cxtStack + pParse->iDepth;
goto params2;
}
"(" {
*piThis = p21ParseAddNode(pParse, P21_LIST, 0, 0);
if (*piThis < 0) return -1;
if( ++pParse->iDepth > P21_MAX_DEPTH ) return -1;
piThis = cxtStack + pParse->iDepth;
goto params2;
}
"," {
goto params2;
}
REAL {
p21ParseAddNode(pParse, P21_REAL, cur - tok, tok);
goto params2;
}
INTEGER {