UnQL

Check-in [52ddc082d1]
Login

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

Overview
Comment:JSON string parsing and rendering appears to work.
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 52ddc082d124f682d93d789fce2e2134b29d3156
User & Date: drh 2011-06-14 14:33:53
Context
2011-06-14
17:16
Begin adding error message handling logic and code to implement statements. Incremental check-in because I have to stop to work on something else. check-in: 6299c43154 user: drh tags: trunk
14:33
JSON string parsing and rendering appears to work. check-in: 52ddc082d1 user: drh tags: trunk
02:36
JSON parser and renderer compiles but is otherwise untested. check-in: 060765b4fc user: drh tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/json.c.

57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
...
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
...
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
...
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
void renderString(String *pOut, const char *z){
  int n, i, j, c;
  char *zOut;
  for(i=n=0; (c=z[i])!=0; i++, n++){
    if( c=='"' || c=='\\' ) n++;
  }
  xjd1StringAppend(pOut, 0, n+3);
  zOut = xjd1StringText(pOut) + xjd1StringLen(pOut);
  if( zOut ){
    zOut += xjd1StringLen(pOut);
    zOut[0] = '"';
    for(i=0, j=1; (c=z[i])!=0; i++){
      if( c=='"' || c=='\\' ) zOut[j++] = '\\';
      zOut[j++] = c;
    }
................................................................................
      }
    }
  }
}


/* JSON parser token types */
#define JSON_BEGIN_ARRAY    1
#define JSON_END_ARRAY      2
#define JSON_COMMA          3
#define JSON_BEGIN_STRUCT   4
#define JSON_END_STRUCT     5
#define JSON_STRING         6
#define JSON_TRUE           7


#define JSON_FALSE          8
#define JSON_NULL           9
#define JSON_REAL          10
#define JSON_COLON         11
#define JSON_EOF           12
#define JSON_ERROR         13

/* State of a JSON string tokenizer */
typedef struct JsonStr JsonStr;
struct JsonStr {
  const char *zIn;        /* Complete input string */
  int iCur;               /* First character of current token */
  int n;                  /* Number of charaters in current token */
................................................................................
    case 0: {
      p->n = 0;
      p->eType = JSON_EOF;
      break;
    }
    case '"': {
      char c;
      for(n=0; (c = z[i+n])!=0 && c!='"'; n++){
        if( c=='\\' ) n++;
      }
      if( c=='"' ) n++;
      p->n = n;
      p->eType = JSON_STRING;
      break;
    }
................................................................................
      p->n = 1;
      p->eType = JSON_ERROR;
      break;
    }
  }
}











































/* Enter point to the first token of the JSON object.
** Exit pointing to the first token past end end of the
** JSON object.
*/
static JsonNode *parseJson(JsonStr *pIn){
  JsonNode *pNew;
  pNew = malloc( sizeof(*pNew) );
  if( pNew==0 ) return 0;
  memset(pNew, 0, sizeof(*pNew));
  pNew->eJType = tokenType(pIn);
  switch( pNew->eJType ){
    case JSON_BEGIN_STRUCT: {

      tokenNext(pIn);
      while( tokenType(pIn)!=JSON_END_STRUCT ){


        JsonStructElem *pElem;
        if( tokenType(pIn)!=JSON_STRING ){
          goto json_error; 
        }
        pElem = malloc( sizeof(*pElem) );
        if( pElem==0 ) goto json_error;
        memset(pElem, 0, sizeof(pElem));
        pElem->pNext = pNew->u.pStruct;
        pNew->u.pStruct = pElem;
        pElem->zLabel = tokenDequoteString(pIn);
        tokenNext(pIn);
        if( tokenType(pIn)!=JSON_COLON ){
          goto json_error;
        }
        tokenNext(pIn);
        pElem->pValue = parseJson(pIn);








      }
      break;
    }
    case JSON_BEGIN_ARRAY: {
      int nAlloc = 0;
      tokenNext(pIn);
      if( tokenType(pIn)==JSON_END_ARRAY ) break;
      while( 1 ){
        if( pNew->u.array.nElem>=nAlloc ){
          JsonNode *pNewArray;
          nAlloc = nAlloc*2 + 5;
          pNewArray = realloc(pNew->u.array.apElem,
                              sizeof(JsonNode*)*nAlloc);
          if( pNewArray==0 ) goto json_error;
          pNew->u.array.apElem = pNewArray;
        }
        pNew->u.array.apElem[pNew->u.array.nElem++] = parseJson(pIn);







|







 







|
|
|
|
<
|
|
>
>
|
|
|
|
|
|







 







|







 







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













>

|
>
>






|
|
|







>
>
>
>
>
>
>
>









|







57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
...
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
...
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
...
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
void renderString(String *pOut, const char *z){
  int n, i, j, c;
  char *zOut;
  for(i=n=0; (c=z[i])!=0; i++, n++){
    if( c=='"' || c=='\\' ) n++;
  }
  xjd1StringAppend(pOut, 0, n+3);
  zOut = xjd1StringText(pOut);
  if( zOut ){
    zOut += xjd1StringLen(pOut);
    zOut[0] = '"';
    for(i=0, j=1; (c=z[i])!=0; i++){
      if( c=='"' || c=='\\' ) zOut[j++] = '\\';
      zOut[j++] = c;
    }
................................................................................
      }
    }
  }
}


/* JSON parser token types */
#define JSON_FALSE          XJD1_FALSE
#define JSON_TRUE           XJD1_TRUE
#define JSON_REAL           XJD1_REAL
#define JSON_NULL           XJD1_NULL

#define JSON_STRING         XJD1_STRING
#define JSON_BEGIN_ARRAY    XJD1_ARRAY
#define JSON_BEGIN_STRUCT   XJD1_STRUCT

#define JSON_END_ARRAY     20
#define JSON_COMMA         21
#define JSON_END_STRUCT    22
#define JSON_COLON         23
#define JSON_EOF           24
#define JSON_ERROR         25

/* State of a JSON string tokenizer */
typedef struct JsonStr JsonStr;
struct JsonStr {
  const char *zIn;        /* Complete input string */
  int iCur;               /* First character of current token */
  int n;                  /* Number of charaters in current token */
................................................................................
    case 0: {
      p->n = 0;
      p->eType = JSON_EOF;
      break;
    }
    case '"': {
      char c;
      for(n=1; (c = z[i+n])!=0 && c!='"'; n++){
        if( c=='\\' ) n++;
      }
      if( c=='"' ) n++;
      p->n = n;
      p->eType = JSON_STRING;
      break;
    }
................................................................................
      p->n = 1;
      p->eType = JSON_ERROR;
      break;
    }
  }
}

/* Convert the current token (which must be a string) into a true
** string (resolving all of the backslash escapes) and return a pointer
** to the true string.  Space is obtained form malloc().
*/
static char *tokenDequoteString(JsonStr *pIn){
  const char *zIn;
  char *zOut;
  int i, j, n;
  char c;
  zIn = &pIn->zIn[pIn->iCur];
  zOut = malloc( pIn->n );
  if( zOut==0 ) return 0;
  assert( zIn[0]=='"' && zIn[pIn->n-1]=='"' );
  n = pIn->n-1;
  for(i=1, j=0; i<n; i++){
    if( zIn[i]!='\\' ){
      zOut[j++] = zIn[i];
    }else{
      i++;
      c = zIn[i];
      if( c=='b' ){
        zOut[j++] = '\b';
      }else if( c=='f' ){
        zOut[j++] = '\f';
      }else if( c=='n' ){
        zOut[j++] = '\n';
      }else if( c=='r' ){
        zOut[j++] = '\r';
      }else if( c=='t' ){
        zOut[j++] = '\t';
      }else if( c=='u' && i<n-4 ){

      }else{
        zOut[j++] = c;
      }
    }
  }
  zOut[j] = 0;
  return zOut;
}


/* Enter point to the first token of the JSON object.
** Exit pointing to the first token past end end of the
** JSON object.
*/
static JsonNode *parseJson(JsonStr *pIn){
  JsonNode *pNew;
  pNew = malloc( sizeof(*pNew) );
  if( pNew==0 ) return 0;
  memset(pNew, 0, sizeof(*pNew));
  pNew->eJType = tokenType(pIn);
  switch( pNew->eJType ){
    case JSON_BEGIN_STRUCT: {
      JsonStructElem **ppTail;
      tokenNext(pIn);
      if( tokenType(pIn)==JSON_END_STRUCT ) break;
      ppTail = &pNew->u.pStruct;
      while( 1 ){
        JsonStructElem *pElem;
        if( tokenType(pIn)!=JSON_STRING ){
          goto json_error; 
        }
        pElem = malloc( sizeof(*pElem) );
        if( pElem==0 ) goto json_error;
        memset(pElem, 0, sizeof(*pElem));
        *ppTail = pElem;
        ppTail = &pElem->pNext;
        pElem->zLabel = tokenDequoteString(pIn);
        tokenNext(pIn);
        if( tokenType(pIn)!=JSON_COLON ){
          goto json_error;
        }
        tokenNext(pIn);
        pElem->pValue = parseJson(pIn);
        if( tokenType(pIn)==JSON_COMMA ){
          tokenNext(pIn);
        }else if( tokenType(pIn)==JSON_END_STRUCT ){
          tokenNext(pIn);
          break;
        }else{
          goto json_error;
        }
      }
      break;
    }
    case JSON_BEGIN_ARRAY: {
      int nAlloc = 0;
      tokenNext(pIn);
      if( tokenType(pIn)==JSON_END_ARRAY ) break;
      while( 1 ){
        if( pNew->u.array.nElem>=nAlloc ){
          JsonNode **pNewArray;
          nAlloc = nAlloc*2 + 5;
          pNewArray = realloc(pNew->u.array.apElem,
                              sizeof(JsonNode*)*nAlloc);
          if( pNewArray==0 ) goto json_error;
          pNew->u.array.apElem = pNewArray;
        }
        pNew->u.array.apElem[pNew->u.array.nElem++] = parseJson(pIn);

Changes to src/shell.c.

13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
...
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
**   drh@hwaci.com
**   http://www.hwaci.com/drh/
**
*************************************************************************
** This file contains C code that implements a simple command-line
** wrapper shell around xjd1.
*/
#include "xjd1.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <assert.h>


................................................................................
  xjd1 *pDb;
  int rc, n;
  char *zStmt = 0;
  int nStmt = 0;
  int nAlloc = 0;
  int isTTY = 1;
  int parserTrace;

  const char *zPrompt = "xjd1> ";
  char zLine[2000];

  parserTrace = find_option(argv, &argc, "trace", 0, 0)!=0;

  if( argc!=2 ) usage(argv[0]);
  rc = xjd1_open(0, argv[1], &pDb);
  if( rc!=XJD1_OK ){
    fprintf(stderr, "cannot open \"%s\"\n", argv[1]);
    exit(1);
  }
  xjd1_config(pDb, XJD1_CONFIG_PARSERTRACE, parserTrace);
  isTTY = isatty(0);
  while( !feof(stdin) ){
    if( isTTY ) printf("%s", zPrompt);
    if( fgets(zLine, sizeof(zLine), stdin)==0 ) break;












    n = strlen(zLine);
    if( n+nStmt+1 >= nAlloc ){
      nAlloc = nAlloc + n + nStmt + 1000;
      zStmt = realloc(zStmt, nAlloc);
      if( zStmt==0 ){
        fprintf(stderr, "out of memory\n");
        exit(1);







|







 







>




>











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







13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
...
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
**   drh@hwaci.com
**   http://www.hwaci.com/drh/
**
*************************************************************************
** This file contains C code that implements a simple command-line
** wrapper shell around xjd1.
*/
#include "xjd1Int.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <assert.h>


................................................................................
  xjd1 *pDb;
  int rc, n;
  char *zStmt = 0;
  int nStmt = 0;
  int nAlloc = 0;
  int isTTY = 1;
  int parserTrace;
  int testJson;
  const char *zPrompt = "xjd1> ";
  char zLine[2000];

  parserTrace = find_option(argv, &argc, "trace", 0, 0)!=0;
  testJson = find_option(argv, &argc, "json", 0, 0)!=0;
  if( argc!=2 ) usage(argv[0]);
  rc = xjd1_open(0, argv[1], &pDb);
  if( rc!=XJD1_OK ){
    fprintf(stderr, "cannot open \"%s\"\n", argv[1]);
    exit(1);
  }
  xjd1_config(pDb, XJD1_CONFIG_PARSERTRACE, parserTrace);
  isTTY = isatty(0);
  while( !feof(stdin) ){
    if( isTTY ) printf("%s", zPrompt);
    if( fgets(zLine, sizeof(zLine), stdin)==0 ) break;
    if( testJson ){
      JsonNode *pNode = xjd1JsonParse(zLine);
      if( pNode ){
        String out;
        xjd1StringInit(&out, 0, 0);
        xjd1JsonRender(&out, pNode);
        xjd1JsonFree(pNode);
        printf("%s\n", xjd1StringText(&out));
        xjd1StringClear(&out);
      }
      continue;
    }
    n = strlen(zLine);
    if( n+nStmt+1 >= nAlloc ){
      nAlloc = nAlloc + n + nStmt + 1000;
      zStmt = realloc(zStmt, nAlloc);
      if( zStmt==0 ){
        fprintf(stderr, "out of memory\n");
        exit(1);