~ubuntu-branches/ubuntu/gutsy/icu/gutsy-updates

« back to all changes in this revision

Viewing changes to source/tools/genrb/parse.c

  • Committer: Package Import Robot
  • Author(s): Jay Berkenbilt
  • Date: 2005-11-19 11:29:31 UTC
  • mfrom: (1.1.2)
  • Revision ID: package-import@ubuntu.com-20051119112931-vcizkrp10tli4enw
Tags: 3.4-3
Explicitly build with g++ 3.4.  The current ICU fails its test suite
with 4.0 but not with 3.4.  Future versions should work properly with
4.0.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
*******************************************************************************
3
 
*
4
 
*   Copyright (C) 1998-2002, International Business Machines
5
 
*   Corporation and others.  All Rights Reserved.
6
 
*
7
 
*******************************************************************************
8
 
*
9
 
* File parse.c
10
 
*
11
 
* Modification History:
12
 
*
13
 
*   Date          Name          Description
14
 
*   05/26/99     stephen       Creation.
15
 
*   02/25/00     weiv          Overhaul to write udata
16
 
*   5/10/01      Ram           removed ustdio dependency
17
 
*   06/10/2001  Dominic Ludlam <dom@recoil.org> Rewritten
18
 
*******************************************************************************
19
 
*/
20
 
 
21
 
#include "ucol_imp.h"
22
 
#include "parse.h"
23
 
#include "error.h"
24
 
#include "uhash.h"
25
 
#include "cmemory.h"
26
 
#include "cstring.h"
27
 
#include "read.h"
28
 
#include "ustr.h"
29
 
#include "reslist.h"
30
 
#include "unicode/ustring.h"
31
 
#include "unicode/putil.h"
32
 
 
33
 
/* Number of tokens to read ahead of the current stream position */
34
 
#define MAX_LOOKAHEAD   2
35
 
 
36
 
#define U_ICU_UNIDATA   "unidata"
37
 
#define CR               0x000D
38
 
#define LF               0x000A
39
 
#define SPACE            0x0020
40
 
#define ESCAPE           0x005C
41
 
 
42
 
U_STRING_DECL(k_type_string,    "string",    6);
43
 
U_STRING_DECL(k_type_binary,    "binary",    6);
44
 
U_STRING_DECL(k_type_bin,       "bin",       3);
45
 
U_STRING_DECL(k_type_table,     "table",     5);
46
 
U_STRING_DECL(k_type_int,       "int",       3);
47
 
U_STRING_DECL(k_type_integer,   "integer",   7);
48
 
U_STRING_DECL(k_type_array,     "array",     5);
49
 
U_STRING_DECL(k_type_intvector, "intvector", 9);
50
 
U_STRING_DECL(k_type_import,    "import",    6);
51
 
U_STRING_DECL(k_type_reserved,  "reserved",  8);
52
 
 
53
 
enum EResourceType
54
 
{
55
 
     RT_UNKNOWN,
56
 
     RT_STRING,
57
 
     RT_BINARY,
58
 
     RT_TABLE,
59
 
     RT_INTEGER,
60
 
     RT_ARRAY,
61
 
     RT_INTVECTOR,
62
 
     RT_IMPORT,
63
 
     RT_RESERVED
64
 
};
65
 
 
66
 
/* only used for debugging */
67
 
const char *resourceNames[] =
68
 
{
69
 
     "Unknown",
70
 
     "String",
71
 
     "Binary",
72
 
     "Table",
73
 
     "Integer",
74
 
     "Array",
75
 
     "Int vector",
76
 
     "Import",
77
 
     "Reserved",
78
 
};
79
 
 
80
 
struct Lookahead
81
 
{
82
 
     enum   ETokenType type;
83
 
     struct UString     value;
84
 
     uint32_t            line;
85
 
};
86
 
 
87
 
/* keep in sync with token defines in read.h */
88
 
const char *tokenNames[] =
89
 
{
90
 
     "string",             /* A string token, such as "MonthNames" */
91
 
     "'{'",                 /* An opening brace character */
92
 
     "'}'",                 /* A closing brace character */
93
 
     "','",                 /* A comma */
94
 
     "':'",                 /* A colon */
95
 
 
96
 
     "<end of file>",     /* End of the file has been reached successfully */
97
 
     "<error>",            /* An error, such an unterminated quoted string */
98
 
};
99
 
 
100
 
/* Just to store "TRUE" */
101
 
static const UChar trueValue[] = {0x0054, 0x0052, 0x0055, 0x0045, 0x0000};
102
 
 
103
 
static struct Lookahead  lookahead[MAX_LOOKAHEAD + 1];
104
 
static uint32_t          lookaheadPosition;
105
 
static UCHARBUF         *buffer;
106
 
 
107
 
static struct SRBRoot *bundle;
108
 
static const char     *inputdir;
109
 
static uint32_t        inputdirLength;
110
 
 
111
 
static struct SResource *parseResource(char *tag, UErrorCode *status);
112
 
 
113
 
void initParser(void)
114
 
{
115
 
    uint32_t i;
116
 
 
117
 
    U_STRING_INIT(k_type_string,    "string",    6);
118
 
    U_STRING_INIT(k_type_binary,    "binary",    6);
119
 
    U_STRING_INIT(k_type_bin,       "bin",       3);
120
 
    U_STRING_INIT(k_type_table,     "table",     5);
121
 
    U_STRING_INIT(k_type_int,       "int",       3);
122
 
    U_STRING_INIT(k_type_integer,   "integer",   7);
123
 
    U_STRING_INIT(k_type_array,     "array",     5);
124
 
    U_STRING_INIT(k_type_intvector, "intvector", 9);
125
 
    U_STRING_INIT(k_type_import,    "import",    6);
126
 
    U_STRING_INIT(k_type_reserved,  "reserved",  8);
127
 
 
128
 
    for (i = 0; i < MAX_LOOKAHEAD + 1; i++)
129
 
    {
130
 
        ustr_init(&lookahead[i].value);
131
 
    }
132
 
}
133
 
 
134
 
/* The nature of the lookahead buffer:
135
 
   There are MAX_LOOKAHEAD + 1 slots, used as a circular buffer.  This provides
136
 
   MAX_LOOKAHEAD lookahead tokens and a slot for the current token and value.
137
 
   When getToken is called, the current pointer is moved to the next slot and the
138
 
   old slot is filled with the next token from the reader by calling getNextToken.
139
 
   The token values are stored in the slot, which means that token values don't
140
 
   survive a call to getToken, ie.
141
 
 
142
 
   UString *value;
143
 
 
144
 
   getToken(&value, NULL, status);
145
 
   getToken(NULL,   NULL, status);       bad - value is now a different string
146
 
*/
147
 
static void
148
 
initLookahead(UCHARBUF *buf, UErrorCode *status)
149
 
{
150
 
    static uint32_t initTypeStrings = 0;
151
 
    uint32_t i;
152
 
 
153
 
    if (!initTypeStrings)
154
 
    {
155
 
        initTypeStrings = 1;
156
 
    }
157
 
 
158
 
    lookaheadPosition   = 0;
159
 
    buffer              = buf;
160
 
 
161
 
    resetLineNumber();
162
 
 
163
 
    for (i = 0; i < MAX_LOOKAHEAD; i++)
164
 
    {
165
 
        lookahead[i].type = getNextToken(buffer, &lookahead[i].value, &lookahead[i].line, status);
166
 
 
167
 
        if (U_FAILURE(*status))
168
 
        {
169
 
            return;
170
 
        }
171
 
    }
172
 
 
173
 
    *status = U_ZERO_ERROR;
174
 
}
175
 
 
176
 
static enum ETokenType
177
 
getToken(struct UString **tokenValue, uint32_t *linenumber, UErrorCode *status)
178
 
{
179
 
    enum ETokenType result;
180
 
    uint32_t          i;
181
 
 
182
 
    result = lookahead[lookaheadPosition].type;
183
 
 
184
 
    if (tokenValue != NULL)
185
 
    {
186
 
        *tokenValue = &lookahead[lookaheadPosition].value;
187
 
    }
188
 
 
189
 
    if (linenumber != NULL)
190
 
    {
191
 
        *linenumber = lookahead[lookaheadPosition].line;
192
 
    }
193
 
 
194
 
    i = (lookaheadPosition + MAX_LOOKAHEAD) % (MAX_LOOKAHEAD + 1);
195
 
    lookaheadPosition = (lookaheadPosition + 1) % (MAX_LOOKAHEAD + 1);
196
 
    lookahead[i].type = getNextToken(buffer, &lookahead[i].value, &lookahead[i].line, status);
197
 
 
198
 
    /* printf("getToken, returning %s\n", tokenNames[result]); */
199
 
 
200
 
    return result;
201
 
}
202
 
 
203
 
static enum ETokenType
204
 
peekToken(uint32_t lookaheadCount, struct UString **tokenValue, uint32_t *linenumber, UErrorCode *status)
205
 
{
206
 
    uint32_t i = (lookaheadPosition + lookaheadCount) % (MAX_LOOKAHEAD + 1);
207
 
 
208
 
    if (U_FAILURE(*status))
209
 
    {
210
 
        return TOK_ERROR;
211
 
    }
212
 
 
213
 
    if (lookaheadCount >= MAX_LOOKAHEAD)
214
 
    {
215
 
        *status = U_INTERNAL_PROGRAM_ERROR;
216
 
        return TOK_ERROR;
217
 
    }
218
 
 
219
 
    if (tokenValue != NULL)
220
 
    {
221
 
        *tokenValue = &lookahead[i].value;
222
 
    }
223
 
 
224
 
    if (linenumber != NULL)
225
 
    {
226
 
        *linenumber = lookahead[i].line;
227
 
    }
228
 
 
229
 
    return lookahead[i].type;
230
 
}
231
 
 
232
 
static void
233
 
expect(enum ETokenType expectedToken, struct UString **tokenValue, uint32_t *linenumber, UErrorCode *status)
234
 
{
235
 
    uint32_t        line;
236
 
    enum ETokenType token = getToken(tokenValue, &line, status);
237
 
 
238
 
    if (U_FAILURE(*status))
239
 
    {
240
 
        return;
241
 
    }
242
 
 
243
 
    if (linenumber != NULL)
244
 
    {
245
 
        *linenumber = line;
246
 
    }
247
 
 
248
 
    if (token != expectedToken)
249
 
    {
250
 
        *status = U_INVALID_FORMAT_ERROR;
251
 
        error(line, "expecting %s, got %s", tokenNames[expectedToken], tokenNames[token]);
252
 
    }
253
 
    else /* "else" is added by Jing/GCL */
254
 
    {
255
 
        *status = U_ZERO_ERROR;
256
 
    }
257
 
}
258
 
 
259
 
static char *getInvariantString(uint32_t *line, UErrorCode *status)
260
 
{
261
 
    struct UString *tokenValue;
262
 
    char           *result;
263
 
    uint32_t        count;
264
 
 
265
 
    expect(TOK_STRING, &tokenValue, line, status);
266
 
 
267
 
    if (U_FAILURE(*status))
268
 
    {
269
 
        return NULL;
270
 
    }
271
 
 
272
 
    count  = u_strlen(tokenValue->fChars) + 1;
273
 
    result = uprv_malloc(count);
274
 
 
275
 
    if (result == NULL)
276
 
    {
277
 
        *status = U_MEMORY_ALLOCATION_ERROR;
278
 
        return NULL;
279
 
    }
280
 
 
281
 
    u_UCharsToChars(tokenValue->fChars, result, count);
282
 
    return result;
283
 
}
284
 
 
285
 
static enum EResourceType
286
 
parseResourceType(UErrorCode *status)
287
 
{
288
 
    struct UString        *tokenValue;
289
 
    enum   EResourceType  result = RT_UNKNOWN;
290
 
    uint32_t              line=0;
291
 
 
292
 
    expect(TOK_STRING, &tokenValue, &line, status);
293
 
 
294
 
    if (U_FAILURE(*status))
295
 
    {
296
 
        return RT_UNKNOWN;
297
 
    }
298
 
 
299
 
    *status = U_ZERO_ERROR;
300
 
 
301
 
    if (u_strcmp(tokenValue->fChars, k_type_string) == 0) {
302
 
        result = RT_STRING;
303
 
    } else if (u_strcmp(tokenValue->fChars, k_type_array) == 0) {
304
 
        result = RT_ARRAY;
305
 
    } else if (u_strcmp(tokenValue->fChars, k_type_table) == 0) {
306
 
        result = RT_TABLE;
307
 
    } else if (u_strcmp(tokenValue->fChars, k_type_binary) == 0) {
308
 
        result = RT_BINARY;
309
 
    } else if (u_strcmp(tokenValue->fChars, k_type_bin) == 0) {
310
 
        result = RT_BINARY;
311
 
    } else if (u_strcmp(tokenValue->fChars, k_type_int) == 0) {
312
 
        result = RT_INTEGER;
313
 
    } else if (u_strcmp(tokenValue->fChars, k_type_integer) == 0) {
314
 
        result = RT_INTEGER;
315
 
    } else if (u_strcmp(tokenValue->fChars, k_type_intvector) == 0) {
316
 
        result = RT_INTVECTOR;
317
 
    } else if (u_strcmp(tokenValue->fChars, k_type_import) == 0) {
318
 
        result = RT_IMPORT;
319
 
    } else if (u_strcmp(tokenValue->fChars, k_type_reserved) == 0) {
320
 
        result = RT_RESERVED;
321
 
    } else {
322
 
        char tokenBuffer[1024];
323
 
        u_austrncpy(tokenBuffer, tokenValue->fChars, sizeof(tokenBuffer));
324
 
        tokenBuffer[sizeof(tokenBuffer) - 1] = 0;
325
 
        *status = U_INVALID_FORMAT_ERROR;
326
 
        error(line, "unknown resource type '%s'", tokenBuffer);
327
 
    }
328
 
 
329
 
    return result;
330
 
}
331
 
 
332
 
static struct SResource *
333
 
parseUCARules(char *tag, uint32_t startline, UErrorCode *status)
334
 
{
335
 
    struct SResource *result = NULL;
336
 
    struct UString   *tokenValue;
337
 
    FileStream       *file          = NULL;
338
 
    char              filename[256] = { '\0' };
339
 
    char              cs[128]       = { '\0' };
340
 
    uint32_t          line;
341
 
    int               len=0;
342
 
    expect(TOK_STRING, &tokenValue, &line, status);
343
 
 
344
 
    /* make the filename including the directory */
345
 
    if (inputdir != NULL)
346
 
    {
347
 
        uprv_strcat(filename, inputdir);
348
 
 
349
 
        if (inputdir[inputdirLength - 1] != U_FILE_SEP_CHAR)
350
 
        {
351
 
            uprv_strcat(filename, U_FILE_SEP_STRING);
352
 
        }
353
 
    }
354
 
 
355
 
    u_UCharsToChars(tokenValue->fChars, cs, tokenValue->fLength);
356
 
 
357
 
    expect(TOK_CLOSE_BRACE, NULL, NULL, status);
358
 
 
359
 
    if (U_FAILURE(*status))
360
 
    {
361
 
        return NULL;
362
 
    }
363
 
    uprv_strcat(filename,"..");
364
 
    uprv_strcat(filename,U_FILE_SEP_STRING);
365
 
    uprv_strcat(filename, U_ICU_UNIDATA);
366
 
    uprv_strcat(filename, U_FILE_SEP_STRING);
367
 
    uprv_strcat(filename, cs);
368
 
 
369
 
    /* open the file */
370
 
    file = T_FileStream_open(filename, "rb");
371
 
 
372
 
    if (file != NULL)
373
 
    {
374
 
        UCHARBUF *ucbuf;
375
 
        UChar32   c     = 0;
376
 
        uint32_t  size = T_FileStream_size(file);
377
 
 
378
 
        /* We allocate more space than actually required
379
 
        * since the actual size needed for storing UChars
380
 
        * is not known in UTF-8 byte stream
381
 
        */
382
 
        UChar *pTarget      = (UChar *) uprv_malloc(sizeof(UChar) * size);
383
 
        UChar *target       = pTarget;
384
 
        UChar *targetLimit = pTarget + size;
385
 
 
386
 
        ucbuf = ucbuf_open(file, NULL,getShowWarning(), status);
387
 
 
388
 
        if (U_FAILURE(*status)) {
389
 
            error(line, "couldn't open input file %s\n", filename);
390
 
            return NULL;
391
 
        }
392
 
 
393
 
        /* read the rules into the buffer */
394
 
        while (target < targetLimit)
395
 
        {
396
 
            c = ucbuf_getc(ucbuf, status);
397
 
 
398
 
            if (c == ESCAPE)
399
 
            {
400
 
                c = unescape(ucbuf, status);
401
 
 
402
 
                if (c == U_ERR)
403
 
                {
404
 
                    uprv_free(pTarget);
405
 
                    T_FileStream_close(file);
406
 
                    return NULL;
407
 
                }
408
 
            }
409
 
            else if (c == SPACE || c == CR || c == LF)
410
 
            {
411
 
            /* ignore spaces carriage returns
412
 
            * and line feed unless in the form \uXXXX
413
 
                */
414
 
                continue;
415
 
            }
416
 
 
417
 
            /* Append UChar * after dissembling if c > 0xffff*/
418
 
            if (c != U_EOF)
419
 
            {
420
 
                U_APPEND_CHAR32(c, target,len);
421
 
            }
422
 
            else
423
 
            {
424
 
                break;
425
 
            }
426
 
        }
427
 
 
428
 
        result = string_open(bundle, tag, pTarget, target - pTarget, status);
429
 
 
430
 
        uprv_free(pTarget);
431
 
        T_FileStream_close(file);
432
 
 
433
 
        return result;
434
 
    }
435
 
    else
436
 
    {
437
 
        error(line, "couldn't open input file %s\n", filename);
438
 
        *status = U_FILE_ACCESS_ERROR;
439
 
        return NULL;
440
 
    }
441
 
}
442
 
 
443
 
static struct SResource *
444
 
parseString(char *tag, uint32_t startline, UErrorCode *status)
445
 
{
446
 
    struct UString   *tokenValue;
447
 
    struct SResource *result = NULL;
448
 
 
449
 
    if (tag != NULL && uprv_strcmp(tag, "%%UCARULES") == 0)
450
 
    {
451
 
        return parseUCARules(tag, startline, status);
452
 
    }
453
 
 
454
 
    expect(TOK_STRING, &tokenValue, NULL, status);
455
 
 
456
 
    if (U_SUCCESS(*status))
457
 
    {
458
 
        /* create the string now - tokenValue doesn't survive a call to getToken (and therefore
459
 
        doesn't survive expect either) */
460
 
 
461
 
        result = string_open(bundle, tag, tokenValue->fChars, tokenValue->fLength, status);
462
 
 
463
 
        expect(TOK_CLOSE_BRACE, NULL, NULL, status);
464
 
 
465
 
        if (U_FAILURE(*status))
466
 
        {
467
 
            string_close(result, status);
468
 
            return NULL;
469
 
        }
470
 
    }
471
 
 
472
 
    return result;
473
 
}
474
 
 
475
 
static struct SResource *
476
 
parseCollationElements(char *tag, uint32_t startline, UErrorCode *status)
477
 
{
478
 
    struct SResource  *result = NULL;
479
 
    struct SResource  *member = NULL;
480
 
    struct UString    *tokenValue;
481
 
    enum   ETokenType  token;
482
 
    char               subtag[1024];
483
 
    UVersionInfo       version;
484
 
    UBool              override = FALSE;
485
 
    uint32_t           line;
486
 
 
487
 
    result = table_open(bundle, tag, status);
488
 
 
489
 
    if (result == NULL || U_FAILURE(*status))
490
 
    {
491
 
        return NULL;
492
 
    }
493
 
 
494
 
    /* '{' . (name resource)* '}' */
495
 
    for (;;)
496
 
    {
497
 
        token = getToken(&tokenValue, &line, status);
498
 
 
499
 
        if (token == TOK_CLOSE_BRACE)
500
 
        {
501
 
            return result;
502
 
        }
503
 
 
504
 
        if (token != TOK_STRING)
505
 
        {
506
 
            table_close(result, status);
507
 
            *status = U_INVALID_FORMAT_ERROR;
508
 
 
509
 
            if (token == TOK_EOF)
510
 
            {
511
 
                error(startline, "unterminated table");
512
 
            }
513
 
            else
514
 
            {
515
 
                error(line, "enexpected token %s", tokenNames[token]);
516
 
            }
517
 
 
518
 
            return NULL;
519
 
        }
520
 
 
521
 
        u_UCharsToChars(tokenValue->fChars, subtag, u_strlen(tokenValue->fChars) + 1);
522
 
 
523
 
        if (U_FAILURE(*status))
524
 
        {
525
 
            table_close(result, status);
526
 
            return NULL;
527
 
        }
528
 
 
529
 
        expect(TOK_OPEN_BRACE, NULL,          NULL,  status);
530
 
        expect(TOK_STRING,      &tokenValue, &line, status);
531
 
 
532
 
        if (U_FAILURE(*status))
533
 
        {
534
 
            table_close(result, status);
535
 
            return NULL;
536
 
        }
537
 
 
538
 
        if (uprv_strcmp(subtag, "Version") == 0)
539
 
        {
540
 
            char     ver[40];
541
 
            int32_t length = u_strlen(tokenValue->fChars);
542
 
 
543
 
            if (length >= (int32_t) sizeof(ver))
544
 
            {
545
 
                length = (int32_t) sizeof(ver) - 1;
546
 
            }
547
 
 
548
 
            u_UCharsToChars(tokenValue->fChars, ver, length);
549
 
            u_versionFromString(version, ver);
550
 
        }
551
 
        else if (uprv_strcmp(subtag, "Override") == 0)
552
 
        {
553
 
            override = FALSE;
554
 
 
555
 
            if (u_strncmp(tokenValue->fChars, trueValue, u_strlen(trueValue)) == 0)
556
 
            {
557
 
                override = TRUE;
558
 
            }
559
 
        }
560
 
        else if (uprv_strcmp(subtag, "Sequence") == 0)
561
 
        {
562
 
            UErrorCode intStatus = U_ZERO_ERROR;
563
 
 
564
 
            /* do the collation elements */
565
 
            int32_t     len   = 0;
566
 
            uint8_t   *data  = NULL;
567
 
            UCollator *coll  = NULL;
568
 
            UChar      *rules = NULL;
569
 
            UParseError parseError;
570
 
            coll = ucol_openRules(tokenValue->fChars, tokenValue->fLength,
571
 
                UCOL_OFF, UCOL_DEFAULT_STRENGTH,&parseError, &intStatus);
572
 
 
573
 
            if (U_SUCCESS(intStatus) && coll != NULL)
574
 
            {
575
 
                data = ucol_cloneRuleData(coll, &len, &intStatus);
576
 
 
577
 
                /* tailoring rules version */
578
 
                coll->dataInfo.dataVersion[1] = version[0];
579
 
 
580
 
                if (U_SUCCESS(intStatus) && data != NULL)
581
 
                {
582
 
                    member = bin_open(bundle, "%%CollationBin", len, data, status);
583
 
                    table_add(bundle->fRoot, member, line, status);
584
 
                    uprv_free(data);
585
 
                }
586
 
                else
587
 
                {
588
 
                    warning(line, "could not obtain rules from collator");
589
 
                }
590
 
 
591
 
                ucol_close(coll);
592
 
            }
593
 
            else
594
 
            {
595
 
                warning(line, "%%Collation could not be constructed from CollationElements - check context!");
596
 
            }
597
 
 
598
 
            uprv_free(rules);
599
 
        }
600
 
 
601
 
        member = string_open(bundle, subtag, tokenValue->fChars, tokenValue->fLength, status);
602
 
        table_add(result, member, line, status);
603
 
 
604
 
        expect(TOK_CLOSE_BRACE, NULL, NULL, status);
605
 
 
606
 
        if (U_FAILURE(*status))
607
 
        {
608
 
            table_close(result, status);
609
 
            return NULL;
610
 
        }
611
 
     }
612
 
 
613
 
     /* not reached */
614
 
     *status = U_INTERNAL_PROGRAM_ERROR;
615
 
     return NULL;
616
 
}
617
 
 
618
 
/* Necessary, because CollationElements requires the bundle->fRoot member to be present which,
619
 
   if this weren't special-cased, wouldn't be set until the entire file had been processed. */
620
 
static struct SResource *
621
 
realParseTable(struct SResource *table, char *tag, uint32_t startline, UErrorCode *status)
622
 
{
623
 
    struct SResource  *member = NULL;
624
 
    struct UString    *tokenValue;
625
 
    enum   ETokenType token;
626
 
    char              subtag[1024];
627
 
    uint32_t          line;
628
 
    UBool             readToken = FALSE;
629
 
 
630
 
    /* '{' . (name resource)* '}' */
631
 
    for (;;)
632
 
    {
633
 
        token = getToken(&tokenValue, &line, status);
634
 
 
635
 
        if (token == TOK_CLOSE_BRACE)
636
 
        {
637
 
            if (!readToken) {
638
 
                warning(startline, "Encountered empty table");
639
 
            }
640
 
            return table;
641
 
        }
642
 
 
643
 
        if (token != TOK_STRING)
644
 
        {
645
 
            table_close(table, status);
646
 
            *status = U_INVALID_FORMAT_ERROR;
647
 
 
648
 
            if (token == TOK_EOF)
649
 
            {
650
 
                error(startline, "unterminated table");
651
 
            }
652
 
            else
653
 
            {
654
 
                error(line, "enexpected token %s", tokenNames[token]);
655
 
            }
656
 
 
657
 
            return NULL;
658
 
        }
659
 
 
660
 
        u_UCharsToChars(tokenValue->fChars, subtag, u_strlen(tokenValue->fChars) + 1);
661
 
 
662
 
        if (U_FAILURE(*status))
663
 
        {
664
 
            error(line, "parse error. Stopped parsing with %s", u_errorName(*status));
665
 
            table_close(table, status);
666
 
            return NULL;
667
 
        }
668
 
 
669
 
        member = parseResource(subtag, status);
670
 
 
671
 
        if (member == NULL || U_FAILURE(*status))
672
 
        {
673
 
            error(line, "parse error. Stopped parsing with %s", u_errorName(*status));
674
 
            table_close(table, status);
675
 
            return NULL;
676
 
        }
677
 
 
678
 
        table_add(table, member, line, status);
679
 
 
680
 
        if (U_FAILURE(*status))
681
 
        {
682
 
            error(line, "parse error. Stopped parsing with %s", u_errorName(*status));
683
 
            table_close(table, status);
684
 
            return NULL;
685
 
        }
686
 
        readToken = TRUE;
687
 
    }
688
 
 
689
 
    /* not reached */
690
 
    *status = U_INTERNAL_PROGRAM_ERROR;
691
 
    return NULL;
692
 
}
693
 
 
694
 
static struct SResource *
695
 
parseTable(char *tag, uint32_t startline, UErrorCode *status)
696
 
{
697
 
    struct SResource *result;
698
 
 
699
 
    if (tag != NULL && uprv_strcmp(tag, "CollationElements") == 0)
700
 
    {
701
 
        return parseCollationElements(tag, startline, status);
702
 
    }
703
 
 
704
 
    result = table_open(bundle, tag, status);
705
 
 
706
 
    if (result == NULL || U_FAILURE(*status))
707
 
    {
708
 
        return NULL;
709
 
    }
710
 
 
711
 
    return realParseTable(result, tag, startline, status);
712
 
}
713
 
 
714
 
static struct SResource *
715
 
parseArray(char *tag, uint32_t startline, UErrorCode *status)
716
 
{
717
 
    struct SResource  *result = NULL;
718
 
    struct SResource  *member = NULL;
719
 
    struct UString    *tokenValue;
720
 
    enum   ETokenType token;
721
 
    UBool             readToken = FALSE;
722
 
 
723
 
    result = array_open(bundle, tag, status);
724
 
 
725
 
    if (result == NULL || U_FAILURE(*status))
726
 
    {
727
 
        return NULL;
728
 
    }
729
 
 
730
 
    /* '{' . resource [','] '}' */
731
 
    for (;;)
732
 
    {
733
 
        /* check for end of array, but don't consume next token unless it really is the end */
734
 
        token = peekToken(0, &tokenValue, NULL, status);
735
 
 
736
 
        if (token == TOK_CLOSE_BRACE)
737
 
        {
738
 
            getToken(NULL, NULL, status);
739
 
            if (!readToken) {
740
 
                warning(startline, "Encountered empty array");
741
 
            }
742
 
            break;
743
 
        }
744
 
 
745
 
        if (token == TOK_EOF)
746
 
        {
747
 
            array_close(result, status);
748
 
            *status = U_INVALID_FORMAT_ERROR;
749
 
            error(startline, "unterminated array");
750
 
            return NULL;
751
 
        }
752
 
 
753
 
        /* string arrays are a special case */
754
 
        if (token == TOK_STRING)
755
 
        {
756
 
            getToken(&tokenValue, NULL, status);
757
 
            member = string_open(bundle, NULL, tokenValue->fChars, tokenValue->fLength, status);
758
 
        }
759
 
        else
760
 
        {
761
 
            member = parseResource(NULL, status);
762
 
        }
763
 
 
764
 
        if (member == NULL || U_FAILURE(*status))
765
 
        {
766
 
            array_close(result, status);
767
 
            return NULL;
768
 
        }
769
 
 
770
 
        array_add(result, member, status);
771
 
 
772
 
        if (U_FAILURE(*status))
773
 
        {
774
 
            array_close(result, status);
775
 
            return NULL;
776
 
        }
777
 
 
778
 
        /* eat optional comma if present */
779
 
        token = peekToken(0, NULL, NULL, status);
780
 
 
781
 
        if (token == TOK_COMMA)
782
 
        {
783
 
            getToken(NULL, NULL, status);
784
 
        }
785
 
 
786
 
        if (U_FAILURE(*status))
787
 
        {
788
 
            array_close(result, status);
789
 
            return NULL;
790
 
        }
791
 
        readToken = TRUE;
792
 
    }
793
 
 
794
 
    return result;
795
 
}
796
 
 
797
 
static struct SResource *
798
 
parseIntVector(char *tag, uint32_t startline, UErrorCode *status)
799
 
{
800
 
    struct SResource  *result = NULL;
801
 
    enum   ETokenType  token;
802
 
    char              *string;
803
 
    int32_t            value;
804
 
    UBool              readToken = FALSE;
805
 
    /* added by Jing/GCL */
806
 
    char              *stopstring;
807
 
    uint32_t           len;
808
 
 
809
 
    result = intvector_open(bundle, tag, status);
810
 
 
811
 
    if (result == NULL || U_FAILURE(*status))
812
 
    {
813
 
        return NULL;
814
 
    }
815
 
 
816
 
    /* '{' . string [','] '}' */
817
 
    for (;;)
818
 
    {
819
 
        /* check for end of array, but don't consume next token unless it really is the end */
820
 
        token = peekToken(0, NULL, NULL, status);
821
 
 
822
 
        if (token == TOK_CLOSE_BRACE)
823
 
        {
824
 
            /* it's the end, consume the close brace */
825
 
            getToken(NULL, NULL, status);
826
 
            if (!readToken) {
827
 
                warning(startline, "Encountered empty int vector");
828
 
            }
829
 
            return result;
830
 
        }
831
 
 
832
 
        string = getInvariantString(NULL, status);
833
 
 
834
 
        if (U_FAILURE(*status))
835
 
        {
836
 
            intvector_close(result, status);
837
 
            return NULL;
838
 
        }
839
 
        /* Commented by Jing/GCL */
840
 
        /*value = uprv_strtol(string, NULL, 10);
841
 
        intvector_add(result, value, status);
842
 
 
843
 
          uprv_free(string);
844
 
 
845
 
        token = peekToken(0, NULL, NULL, status);*/
846
 
 
847
 
        /* The following is added by Jing/GCL to handle illegal char in the Intvector */
848
 
        value = uprv_strtoul(string, &stopstring, 0);/* make intvector support decimal,hexdigit,octal digit ranging from -2^31-2^32-1*/
849
 
        len=stopstring-string;
850
 
 
851
 
        if(len==uprv_strlen(string))
852
 
        {
853
 
            intvector_add(result, value, status);
854
 
            uprv_free(string);
855
 
            token = peekToken(0, NULL, NULL, status);
856
 
        }
857
 
        else
858
 
        {
859
 
            uprv_free(string);
860
 
            *status=U_INVALID_CHAR_FOUND;
861
 
        }
862
 
        /* The above is added by Jing/GCL */
863
 
 
864
 
        if (U_FAILURE(*status))
865
 
        {
866
 
            intvector_close(result, status);
867
 
            return NULL;
868
 
        }
869
 
 
870
 
        /* the comma is optional (even though it is required to prevent the reader from concatenating
871
 
        consecutive entries) so that a missing comma on the last entry isn't an error */
872
 
        if (token == TOK_COMMA)
873
 
        {
874
 
            getToken(NULL, NULL, status);
875
 
        }
876
 
        readToken = TRUE;
877
 
    }
878
 
 
879
 
    /* not reached */
880
 
    intvector_close(result, status);
881
 
    *status = U_INTERNAL_PROGRAM_ERROR;
882
 
    return NULL;
883
 
}
884
 
 
885
 
static struct SResource *
886
 
parseBinary(char *tag, uint32_t startline, UErrorCode *status)
887
 
{
888
 
    struct SResource *result = NULL;
889
 
    uint8_t          *value;
890
 
    char             *string;
891
 
    char              toConv[3] = {'\0', '\0', '\0'};
892
 
    uint32_t          count;
893
 
    uint32_t          i;
894
 
    uint32_t          line;
895
 
    /* added by Jing/GCL */
896
 
    char             *stopstring;
897
 
    uint32_t          len;
898
 
 
899
 
    string = getInvariantString(&line, status);
900
 
 
901
 
    if (string == NULL || U_FAILURE(*status))
902
 
    {
903
 
        return NULL;
904
 
    }
905
 
 
906
 
    expect(TOK_CLOSE_BRACE, NULL, NULL, status);
907
 
 
908
 
    if (U_FAILURE(*status))
909
 
    {
910
 
        uprv_free(string);
911
 
        return NULL;
912
 
    }
913
 
 
914
 
    count = uprv_strlen(string);
915
 
    if (count > 0){
916
 
        if((count % 2)==0){
917
 
            value = uprv_malloc(sizeof(uint8_t) * count);
918
 
 
919
 
            if (value == NULL)
920
 
            {
921
 
                uprv_free(string);
922
 
                *status = U_MEMORY_ALLOCATION_ERROR;
923
 
                return NULL;
924
 
            }
925
 
 
926
 
            for (i = 0; i < count; i += 2)
927
 
            {
928
 
                toConv[0] = string[i];
929
 
                toConv[1] = string[i + 1];
930
 
 
931
 
                value[i >> 1] = (uint8_t) uprv_strtoul(toConv, &stopstring, 16);
932
 
                len=stopstring-toConv;
933
 
 
934
 
                if(len!=uprv_strlen(toConv))
935
 
                {
936
 
                    uprv_free(string);
937
 
                    *status=U_INVALID_CHAR_FOUND;
938
 
                    return NULL;
939
 
                }
940
 
            }
941
 
 
942
 
            result = bin_open(bundle, tag, (i >> 1), value, status);
943
 
 
944
 
            uprv_free(value);
945
 
        }
946
 
        else
947
 
        {
948
 
            *status = U_INVALID_CHAR_FOUND;
949
 
            uprv_free(string);
950
 
            error(line, "Encountered invalid binary string");
951
 
            return NULL;
952
 
        }
953
 
    }
954
 
    else
955
 
    {
956
 
        result = bin_open(bundle, tag, 0, NULL, status);
957
 
        warning(startline, "Encountered empty binary tag");
958
 
    }
959
 
    uprv_free(string);
960
 
 
961
 
    return result;
962
 
}
963
 
 
964
 
static struct SResource *
965
 
parseInteger(char *tag, uint32_t startline, UErrorCode *status)
966
 
{
967
 
    struct SResource *result = NULL;
968
 
    int32_t           value;
969
 
    char             *string;
970
 
    /* added by Jing/GCL */
971
 
    char             *stopstring;
972
 
    uint32_t          len;
973
 
 
974
 
    string = getInvariantString(NULL, status);
975
 
 
976
 
    if (string == NULL || U_FAILURE(*status))
977
 
    {
978
 
        return NULL;
979
 
    }
980
 
 
981
 
    expect(TOK_CLOSE_BRACE, NULL, NULL, status);
982
 
 
983
 
    if (U_FAILURE(*status))
984
 
    {
985
 
        uprv_free(string);
986
 
        return NULL;
987
 
    }
988
 
 
989
 
    if (uprv_strlen(string) <= 0)
990
 
    {
991
 
        warning(startline, "Encountered empty integer. Default value is 0.");
992
 
    }
993
 
 
994
 
    /* commented by Jing/GCL */
995
 
    /* value  = uprv_strtol(string, NULL, 10);*/
996
 
    /* result = int_open(bundle, tag, value, status);*/
997
 
    /* The following is added by Jing/GCL*/
998
 
    /* to make integer support hexdecimal, octal digit and decimal*/
999
 
    /* to handle illegal char in the integer*/
1000
 
    value = uprv_strtoul(string, &stopstring, 0);
1001
 
    len=stopstring-string;
1002
 
    if(len==uprv_strlen(string))
1003
 
    {
1004
 
        result = int_open(bundle, tag, value, status);
1005
 
    }
1006
 
    else
1007
 
    {
1008
 
        *status=U_INVALID_CHAR_FOUND;
1009
 
    }
1010
 
    uprv_free(string);
1011
 
 
1012
 
    return result;
1013
 
}
1014
 
 
1015
 
static struct SResource *
1016
 
parseImport(char *tag, uint32_t startline, UErrorCode *status)
1017
 
{
1018
 
    struct SResource *result;
1019
 
    FileStream       *file;
1020
 
    int32_t           len;
1021
 
    uint8_t          *data;
1022
 
    char             *filename;
1023
 
    uint32_t          line;
1024
 
 
1025
 
    filename = getInvariantString(&line, status);
1026
 
 
1027
 
    if (U_FAILURE(*status))
1028
 
    {
1029
 
        return NULL;
1030
 
    }
1031
 
 
1032
 
    expect(TOK_CLOSE_BRACE, NULL, NULL, status);
1033
 
 
1034
 
    if (U_FAILURE(*status))
1035
 
    {
1036
 
        uprv_free(filename);
1037
 
        return NULL;
1038
 
    }
1039
 
 
1040
 
    /* Open the input file for reading */
1041
 
    if (inputdir == NULL)
1042
 
    {
1043
 
        file = T_FileStream_open(filename, "rb");
1044
 
    }
1045
 
    else
1046
 
    {
1047
 
        char     *fullname = NULL;
1048
 
        int32_t  count     = uprv_strlen(filename);
1049
 
 
1050
 
        if (inputdir[inputdirLength - 1] != U_FILE_SEP_CHAR)
1051
 
        {
1052
 
            fullname = (char *) uprv_malloc(inputdirLength + count + 2);
1053
 
 
1054
 
            uprv_strcpy(fullname, inputdir);
1055
 
 
1056
 
            fullname[inputdirLength]      = U_FILE_SEP_CHAR;
1057
 
            fullname[inputdirLength + 1] = '\0';
1058
 
 
1059
 
            uprv_strcat(fullname, filename);
1060
 
        }
1061
 
        else
1062
 
        {
1063
 
            fullname = (char *) uprv_malloc(inputdirLength + count + 1);
1064
 
 
1065
 
            uprv_strcpy(fullname, inputdir);
1066
 
            uprv_strcat(fullname, filename);
1067
 
        }
1068
 
 
1069
 
        file = T_FileStream_open(fullname, "rb");
1070
 
        uprv_free(fullname);
1071
 
    }
1072
 
 
1073
 
    if (file == NULL)
1074
 
    {
1075
 
        error(line, "couldn't open input file %s", filename);
1076
 
        *status = U_FILE_ACCESS_ERROR;
1077
 
        return NULL;
1078
 
    }
1079
 
 
1080
 
    len  = T_FileStream_size(file);
1081
 
    data = uprv_malloc(len);
1082
 
 
1083
 
    T_FileStream_read  (file, data, len);
1084
 
    T_FileStream_close (file);
1085
 
 
1086
 
    result = bin_open(bundle, tag, len, data, status);
1087
 
 
1088
 
    uprv_free(data);
1089
 
    uprv_free(filename);
1090
 
 
1091
 
    return result;
1092
 
}
1093
 
 
1094
 
static struct SResource *
1095
 
parseResource(char *tag, UErrorCode *status)
1096
 
{
1097
 
    enum   ETokenType      token;
1098
 
    enum   EResourceType  resType = RT_UNKNOWN;
1099
 
    struct UString        *tokenValue;
1100
 
    uint32_t                 startline;
1101
 
    uint32_t                 line;
1102
 
 
1103
 
    token = getToken(&tokenValue, &startline, status);
1104
 
 
1105
 
    /* name . [ ':' type ] '{' resource '}' */
1106
 
    /* This function parses from the colon onwards.  If the colon is present, parse the
1107
 
    type then try to parse a resource of that type.  If there is no explicit type,
1108
 
    work it out using the lookahead tokens. */
1109
 
    switch (token)
1110
 
    {
1111
 
    case TOK_EOF:
1112
 
        *status = U_INVALID_FORMAT_ERROR;
1113
 
        error(startline, "Unexpected EOF encountered");
1114
 
        return NULL;
1115
 
 
1116
 
    case TOK_ERROR:
1117
 
        *status = U_INVALID_FORMAT_ERROR;
1118
 
        return NULL;
1119
 
 
1120
 
    case TOK_COLON:
1121
 
        resType = parseResourceType(status);
1122
 
        expect(TOK_OPEN_BRACE, &tokenValue, &startline, status);
1123
 
 
1124
 
        if (U_FAILURE(*status))
1125
 
        {
1126
 
            return NULL;
1127
 
        }
1128
 
 
1129
 
        break;
1130
 
 
1131
 
    case TOK_OPEN_BRACE:
1132
 
        break;
1133
 
 
1134
 
    default:
1135
 
        *status = U_INVALID_FORMAT_ERROR;
1136
 
        error(startline, "syntax error while reading a resource, expected '{' or ':'");
1137
 
        return NULL;
1138
 
    }
1139
 
 
1140
 
    if (resType == RT_UNKNOWN)
1141
 
    {
1142
 
        /* No explicit type, so try to work it out.  At this point, we've read the first '{'.
1143
 
        We could have any of the following:
1144
 
        { {         => array (nested)
1145
 
        { :/}       => array
1146
 
        { string ,  => string array
1147
 
 
1148
 
        commented by Jing/GCL
1149
 
        { string {  => table
1150
 
 
1151
 
        added by Jing/GCL
1152
 
 
1153
 
        { string :/{    => table
1154
 
        { string }      => string
1155
 
        */
1156
 
 
1157
 
        token = peekToken(0, NULL, &line, status);
1158
 
 
1159
 
        if (U_FAILURE(*status))
1160
 
        {
1161
 
            return NULL;
1162
 
        }
1163
 
 
1164
 
        /* Commented by Jing/GCL */
1165
 
        /* if (token == TOK_OPEN_BRACE || token == TOK_COLON )*/
1166
 
        if (token == TOK_OPEN_BRACE || token == TOK_COLON ||token ==TOK_CLOSE_BRACE )
1167
 
        {
1168
 
            resType = RT_ARRAY;
1169
 
        }
1170
 
        else if (token == TOK_STRING)
1171
 
        {
1172
 
            token = peekToken(1, NULL, &line, status);
1173
 
 
1174
 
            if (U_FAILURE(*status))
1175
 
            {
1176
 
                return NULL;
1177
 
            }
1178
 
 
1179
 
            switch (token)
1180
 
            {
1181
 
            case TOK_COMMA:         resType = RT_ARRAY;  break;
1182
 
            case TOK_OPEN_BRACE:    resType = RT_TABLE;  break;
1183
 
            case TOK_CLOSE_BRACE:   resType = RT_STRING; break;
1184
 
                /* added by Jing/GCL to make table work when :table is omitted */
1185
 
            case TOK_COLON:         resType = RT_TABLE;  break;
1186
 
            default:
1187
 
                *status = U_INVALID_FORMAT_ERROR;
1188
 
                error(line, "Unexpected token after string, expected ',', '{' or '}'");
1189
 
                return NULL;
1190
 
            }
1191
 
        }
1192
 
        else
1193
 
        {
1194
 
            *status = U_INVALID_FORMAT_ERROR;
1195
 
            error(line, "Unexpected token after '{'");
1196
 
            return NULL;
1197
 
        }
1198
 
 
1199
 
        /* printf("Type guessed as %s\n", resourceNames[resType]); */
1200
 
    }
1201
 
 
1202
 
    /* We should now know what we need to parse next, so call the appropriate parser
1203
 
    function and return. */
1204
 
    switch (resType)
1205
 
    {
1206
 
    case RT_STRING:     return parseString    (tag, startline, status);
1207
 
    case RT_TABLE:      return parseTable     (tag, startline, status);
1208
 
    case RT_ARRAY:      return parseArray     (tag, startline, status);
1209
 
    case RT_BINARY:     return parseBinary    (tag, startline, status);
1210
 
    case RT_INTEGER:    return parseInteger   (tag, startline, status);
1211
 
    case RT_IMPORT:     return parseImport    (tag, startline, status);
1212
 
    case RT_INTVECTOR:  return parseIntVector (tag, startline, status);
1213
 
 
1214
 
    default:
1215
 
        *status = U_INTERNAL_PROGRAM_ERROR;
1216
 
        error(startline, "internal error: unknown resource type found and not handled");
1217
 
    }
1218
 
 
1219
 
    return NULL;
1220
 
}
1221
 
 
1222
 
struct SRBRoot *
1223
 
parse(UCHARBUF *buf, const char *currentInputDir, UErrorCode *status)
1224
 
{
1225
 
    struct UString    *tokenValue;
1226
 
    uint32_t           line;
1227
 
    /* added by Jing/GCL */
1228
 
    enum EResourceType bundleType;
1229
 
    enum ETokenType    token;
1230
 
 
1231
 
    initLookahead(buf, status);
1232
 
 
1233
 
    inputdir       = currentInputDir;
1234
 
    inputdirLength = (inputdir != NULL) ? uprv_strlen(inputdir) : 0;
1235
 
 
1236
 
    bundle = bundle_open(status);
1237
 
 
1238
 
    if (bundle == NULL || U_FAILURE(*status))
1239
 
    {
1240
 
        return NULL;
1241
 
    }
1242
 
 
1243
 
    expect(TOK_STRING, &tokenValue, NULL, status);
1244
 
    bundle_setlocale(bundle, tokenValue->fChars, status);
1245
 
    /* Commented by Jing/GCL */
1246
 
    /* expect(TOK_OPEN_BRACE, NULL, &line, status); */
1247
 
    /* The following code is to make Empty bundle work no matter with :table specifer or not */
1248
 
    token = getToken(NULL, &line, status);
1249
 
 
1250
 
    if(token==TOK_COLON)
1251
 
    {
1252
 
        *status=U_ZERO_ERROR;
1253
 
    }
1254
 
    else
1255
 
    {
1256
 
        *status=U_PARSE_ERROR;
1257
 
    }
1258
 
 
1259
 
    if(U_SUCCESS(*status)){
1260
 
 
1261
 
        bundleType=parseResourceType(status);
1262
 
 
1263
 
        if(bundleType==RT_TABLE)
1264
 
        {
1265
 
            expect(TOK_OPEN_BRACE, NULL, &line, status);
1266
 
        }
1267
 
        else
1268
 
        {
1269
 
            *status=U_PARSE_ERROR;
1270
 
            error(line, "parse error. Stopped parsing with %s", u_errorName(*status));
1271
 
        }
1272
 
    }
1273
 
    else
1274
 
    {
1275
 
        if(token==TOK_OPEN_BRACE)
1276
 
        {
1277
 
            *status=U_ZERO_ERROR;
1278
 
        }
1279
 
        else
1280
 
        {
1281
 
            error(line, "parse error, did not find open-brace '{' or colon ':', stopped with %s", u_errorName(*status));
1282
 
        }
1283
 
    }
1284
 
    /* The above is added by Jing/GCL */
1285
 
 
1286
 
    if (U_FAILURE(*status))
1287
 
    {
1288
 
        bundle_close(bundle, status);
1289
 
        return NULL;
1290
 
    }
1291
 
 
1292
 
    realParseTable(bundle->fRoot, NULL, line, status);
1293
 
 
1294
 
    if (U_FAILURE(*status))
1295
 
    {
1296
 
        /* realParseTable has already closed the table */
1297
 
        bundle->fRoot = NULL;
1298
 
        bundle_close(bundle, status);
1299
 
        return NULL;
1300
 
    }
1301
 
 
1302
 
    if (getToken(NULL, &line, status) != TOK_EOF)
1303
 
    {
1304
 
        warning(line, "extraneous text after resource bundle (perhaps unmatched braces)");
1305
 
    }
1306
 
 
1307
 
    return bundle;
1308
 
}