1999-08-16 21:50:52 +00:00
|
|
|
/*
|
|
|
|
*******************************************************************************
|
1999-12-13 22:28:37 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 1998-1999, International Business Machines
|
|
|
|
* Corporation and others. All Rights Reserved.
|
|
|
|
*
|
1999-08-16 21:50:52 +00:00
|
|
|
*******************************************************************************
|
|
|
|
*
|
|
|
|
* File parse.c
|
|
|
|
*
|
|
|
|
* Modification History:
|
|
|
|
*
|
|
|
|
* Date Name Description
|
|
|
|
* 05/26/99 stephen Creation.
|
|
|
|
*******************************************************************************
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "parse.h"
|
|
|
|
#include "error.h"
|
|
|
|
#include "uhash.h"
|
|
|
|
#include "cmemory.h"
|
|
|
|
#include "read.h"
|
2000-01-28 22:40:27 +00:00
|
|
|
#include "unicode/ustdio.h"
|
1999-08-16 21:50:52 +00:00
|
|
|
#include "ustr.h"
|
|
|
|
#include "list.h"
|
|
|
|
#include "rblist.h"
|
1999-12-28 23:57:50 +00:00
|
|
|
#include "unicode/ustring.h"
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
/* Node IDs for the state transition table. */
|
|
|
|
enum ENode {
|
|
|
|
eError,
|
|
|
|
eInitial, /* Next: Locale name */
|
|
|
|
eGotLoc, /* Next: { */
|
|
|
|
eIdle, /* Next: Tag name | } */
|
|
|
|
eGotTag, /* Next: { */
|
|
|
|
eNode5, /* Next: Data | Subtag */
|
|
|
|
eNode6, /* Next: } | { | , */
|
|
|
|
eList, /* Next: List data */
|
|
|
|
eNode8, /* Next: , */
|
|
|
|
eTagList, /* Next: Subtag data */
|
|
|
|
eNode10, /* Next: } */
|
|
|
|
eNode11, /* Next: Subtag */
|
|
|
|
eNode12, /* Next: { */
|
|
|
|
e2dArray, /* Next: Data | } */
|
|
|
|
eNode14, /* Next: , | } */
|
|
|
|
eNode15, /* Next: , | } */
|
|
|
|
eNode16 /* Next: { | } */
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Action codes for the state transtiion table. */
|
|
|
|
enum EAction {
|
|
|
|
/* Generic actions */
|
|
|
|
eNOP = 0x0100, /* Do nothing */
|
|
|
|
eOpen = 0x0200, /* Open a new locale data block with the data
|
|
|
|
string as the locale name */
|
|
|
|
eClose = 0x0300, /* Close a locale data block */
|
|
|
|
eSetTag = 0x0400, /* Record the last string as the tag name */
|
|
|
|
|
|
|
|
/* Comma-delimited lists */
|
|
|
|
eBegList = 0x1100, /* Start a new string list with the last string
|
|
|
|
as the first element */
|
|
|
|
eEndList = 0x1200, /* Close a string list being built */
|
|
|
|
eListStr = 0x1300, /* Record the last string as a data string and
|
|
|
|
increment the index */
|
|
|
|
eStr = 0x1400, /* Record the last string as a singleton string */
|
|
|
|
|
|
|
|
/* 2-d lists */
|
|
|
|
eBeg2dList = 0x2100, /* Start a new 2d string list with no elements as yet */
|
|
|
|
eEnd2dList = 0x2200, /* Close a 2d string list being built */
|
|
|
|
e2dStr = 0x2300, /* Record the last string as a 2d string */
|
|
|
|
eNewRow = 0x2400, /* Start a new row */
|
|
|
|
|
|
|
|
/* Tagged lists */
|
|
|
|
eBegTagged = 0x3100, /* Start a new tagged list with the last
|
|
|
|
string as the first subtag */
|
|
|
|
eEndTagged = 0x3200, /* Close a tagged list being build */
|
|
|
|
eSubtag = 0x3300, /* Record the last string as the subtag */
|
|
|
|
eTaggedStr = 0x3400 /* Record the last string as a tagged string */
|
|
|
|
};
|
|
|
|
|
|
|
|
/* A struct which encapsulates a node ID and an action. */
|
|
|
|
struct STransition {
|
|
|
|
enum ENode fNext;
|
|
|
|
enum EAction fAction;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* This table describes an ATM (state machine) which parses resource
|
|
|
|
bundle text files rather strictly. Each row represents a node. The
|
|
|
|
columns of that row represent transitions into other nodes. Most
|
|
|
|
transitions are "eError" because most transitions are
|
|
|
|
disallowed. For example, if the parser has just seen a tag name, it
|
|
|
|
enters node 4 ("eGotTag"). The state table then marks only one
|
|
|
|
valid transition, which is into node 5, upon seeing an eOpenBrace
|
|
|
|
token. We allow an extra comma after the last element in a
|
|
|
|
comma-delimited list (transition from eList to eIdle on
|
|
|
|
kCloseBrace). */
|
|
|
|
static struct STransition gTransitionTable [] = {
|
|
|
|
/* kString kOpenBrace kCloseBrace kComma*/
|
|
|
|
{eError,eNOP}, {eError,eNOP}, {eError,eNOP}, {eError,eNOP},
|
|
|
|
|
|
|
|
{eGotLoc,eOpen}, {eError,eNOP}, {eError,eNOP}, {eError,eNOP},
|
|
|
|
{eError,eNOP}, {eIdle,eNOP}, {eError,eNOP}, {eError,eNOP},
|
|
|
|
|
|
|
|
{eGotTag,eSetTag}, {eError,eNOP}, {eInitial,eClose}, {eError,eNOP},
|
|
|
|
{eError,eNOP}, {eNode5,eNOP}, {eError,eNOP}, {eError,eNOP},
|
|
|
|
{eNode6,eNOP}, {e2dArray,eBeg2dList},{eError,eNOP}, {eError,eNOP},
|
|
|
|
{eError,eNOP}, {eTagList,eBegTagged},{eIdle,eStr}, {eList,eBegList},
|
|
|
|
|
|
|
|
{eNode8,eListStr}, {eError,eNOP}, {eIdle,eEndList}, {eError,eNOP},
|
|
|
|
{eError,eNOP}, {eError,eNOP}, {eIdle,eEndList}, {eList,eNOP},
|
|
|
|
|
|
|
|
{eNode10,eTaggedStr},{eError,eNOP}, {eError,eNOP}, {eError,eNOP},
|
|
|
|
{eError,eNOP}, {eError,eNOP}, {eNode11,eNOP}, {eError,eNOP},
|
|
|
|
{eNode12,eNOP}, {eError,eNOP}, {eIdle,eEndTagged},{eError,eNOP},
|
|
|
|
{eError,eNOP}, {eTagList,eSubtag}, {eError,eNOP}, {eError,eNOP},
|
|
|
|
|
|
|
|
{eNode14,e2dStr}, {eError,eNOP}, {eNode15,eNOP}, {eError,eNOP},
|
|
|
|
{eError,eNOP}, {eError,eNOP}, {eNode15,eNOP}, {e2dArray,eNOP},
|
|
|
|
{eError,eNOP}, {e2dArray,eNewRow}, {eIdle,eEnd2dList},{eNode16,eNOP},
|
|
|
|
{eError,eNOP}, {e2dArray,eNewRow}, {eIdle,eEnd2dList},{eError,eNOP}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Row length is 4 */
|
|
|
|
#define GETTRANSITION(row,col) (gTransitionTable[col + (row<<2)])
|
|
|
|
|
2000-03-28 22:08:58 +00:00
|
|
|
/*********************************************************************
|
|
|
|
* Hashtable glue
|
|
|
|
********************************************************************/
|
|
|
|
|
|
|
|
static bool_t get(UHashtable *hash, const struct UString* tag) {
|
|
|
|
return uhash_get(hash, tag) != NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void put(UHashtable *hash, const struct UString *tag,
|
|
|
|
UErrorCode* status) {
|
|
|
|
struct UString* key = uprv_malloc(sizeof(struct UString));
|
|
|
|
ustr_init(key);
|
|
|
|
ustr_cpy(key, tag, status);
|
|
|
|
uhash_put(hash, key, (void*)1, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void freeUString(void* ustr) {
|
|
|
|
ustr_deinit(ustr);
|
|
|
|
uprv_free(ustr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int32_t hashUString(const void* ustr) {
|
|
|
|
return uhash_hashUChars(((struct UString*)ustr)->fChars);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool_t compareUString(const void* ustr1, const void* ustr2) {
|
|
|
|
return uhash_compareUChars(((struct UString*)ustr1)->fChars,
|
|
|
|
((struct UString*)ustr2)->fChars);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* parse
|
|
|
|
********************************************************************/
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
struct SRBItemList*
|
1999-12-01 21:36:40 +00:00
|
|
|
parse(FileStream *f, const char *cp,
|
1999-08-16 21:50:52 +00:00
|
|
|
UErrorCode *status)
|
|
|
|
{
|
|
|
|
struct UFILE *file;
|
|
|
|
enum ETokenType type;
|
|
|
|
enum ENode node;
|
|
|
|
struct STransition t;
|
|
|
|
|
|
|
|
struct UString token;
|
|
|
|
struct UString tag;
|
|
|
|
struct UString subtag;
|
|
|
|
struct UString localeName;
|
|
|
|
struct UString keyname;
|
|
|
|
|
|
|
|
struct SRBItem *item;
|
|
|
|
struct SRBItemList *list;
|
|
|
|
struct SList *current;
|
|
|
|
|
|
|
|
/* Hashtable for keeping track of seen tag names */
|
|
|
|
struct UHashtable *data;
|
|
|
|
|
|
|
|
|
1999-10-18 22:48:32 +00:00
|
|
|
if(U_FAILURE(*status)) return 0;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
/* setup */
|
|
|
|
|
|
|
|
ustr_init(&token);
|
|
|
|
ustr_init(&tag);
|
|
|
|
ustr_init(&subtag);
|
|
|
|
ustr_init(&localeName);
|
|
|
|
ustr_init(&keyname);
|
|
|
|
|
|
|
|
node = eInitial;
|
|
|
|
data = 0;
|
|
|
|
current = 0;
|
|
|
|
item = 0;
|
|
|
|
|
2000-01-28 22:40:27 +00:00
|
|
|
file = u_finit((FILE *)f, 0, cp);
|
|
|
|
/* file = u_finit(f, cp, status); */
|
1999-08-16 21:50:52 +00:00
|
|
|
list = rblist_open(status);
|
2000-01-28 22:40:27 +00:00
|
|
|
if(U_FAILURE(*status) || file == NULL) goto finish;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
/* iterate through the stream */
|
|
|
|
for(;;) {
|
|
|
|
|
|
|
|
/* get next token from stream */
|
|
|
|
type = getNextToken(file, &token, status);
|
1999-10-18 22:48:32 +00:00
|
|
|
if(U_FAILURE(*status)) goto finish;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
switch(type) {
|
|
|
|
case tok_EOF:
|
1999-10-07 00:07:53 +00:00
|
|
|
*status = (node == eInitial) ? U_ZERO_ERROR : U_INVALID_FORMAT_ERROR;
|
1999-08-16 21:50:52 +00:00
|
|
|
setErrorText("Unexpected EOF encountered");
|
|
|
|
goto finish;
|
|
|
|
/*break;*/
|
|
|
|
|
|
|
|
case tok_error:
|
1999-10-07 00:07:53 +00:00
|
|
|
*status = U_INVALID_FORMAT_ERROR;
|
1999-08-16 21:50:52 +00:00
|
|
|
goto finish;
|
|
|
|
/*break;*/
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
t = GETTRANSITION(node, type);
|
|
|
|
node = t.fNext;
|
|
|
|
|
|
|
|
if(node == eError) {
|
1999-10-07 00:07:53 +00:00
|
|
|
*status = U_INVALID_FORMAT_ERROR;
|
1999-08-16 21:50:52 +00:00
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(t.fAction) {
|
|
|
|
case eNOP:
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Record the last string as the tag name */
|
|
|
|
case eSetTag:
|
|
|
|
ustr_cpy(&tag, &token, status);
|
1999-10-18 22:48:32 +00:00
|
|
|
if(U_FAILURE(*status)) goto finish;
|
2000-03-28 22:08:58 +00:00
|
|
|
if(get(data, &tag)) {
|
1999-10-08 01:43:23 +00:00
|
|
|
char *s;
|
1999-10-07 00:07:53 +00:00
|
|
|
*status = U_INVALID_FORMAT_ERROR;
|
1999-12-28 23:57:50 +00:00
|
|
|
s = uprv_malloc(1024);
|
1999-10-08 01:43:23 +00:00
|
|
|
strcpy(s, "Duplicate tag name detected: ");
|
|
|
|
u_austrcpy(s+strlen(s), tag.fChars);
|
|
|
|
setErrorText(s);
|
1999-08-16 21:50:52 +00:00
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Record a singleton string */
|
|
|
|
case eStr:
|
|
|
|
if(current != 0) {
|
1999-10-07 00:07:53 +00:00
|
|
|
*status = U_INTERNAL_PROGRAM_ERROR;
|
1999-08-16 21:50:52 +00:00
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
current = strlist_open(status);
|
|
|
|
strlist_add(current, token.fChars, status);
|
|
|
|
item = make_rbitem(tag.fChars, current, status);
|
|
|
|
rblist_add(list, item, status);
|
2000-03-28 22:08:58 +00:00
|
|
|
put(data, &tag, status);
|
1999-10-18 22:48:32 +00:00
|
|
|
if(U_FAILURE(*status)) goto finish;
|
1999-08-16 21:50:52 +00:00
|
|
|
current = 0;
|
|
|
|
item = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Begin a string list */
|
|
|
|
case eBegList:
|
|
|
|
if(current != 0) {
|
1999-10-07 00:07:53 +00:00
|
|
|
*status = U_INTERNAL_PROGRAM_ERROR;
|
1999-08-16 21:50:52 +00:00
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
current = strlist_open(status);
|
|
|
|
strlist_add(current, token.fChars, status);
|
1999-10-18 22:48:32 +00:00
|
|
|
if(U_FAILURE(*status)) goto finish;
|
1999-08-16 21:50:52 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* Record a comma-delimited list string */
|
|
|
|
case eListStr:
|
|
|
|
strlist_add(current, token.fChars, status);
|
1999-10-18 22:48:32 +00:00
|
|
|
if(U_FAILURE(*status)) goto finish;
|
1999-08-16 21:50:52 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* End a string list */
|
|
|
|
case eEndList:
|
2000-03-28 22:08:58 +00:00
|
|
|
put(data, &tag, status);
|
1999-08-16 21:50:52 +00:00
|
|
|
item = make_rbitem(tag.fChars, current, status);
|
|
|
|
rblist_add(list, item, status);
|
1999-10-18 22:48:32 +00:00
|
|
|
if(U_FAILURE(*status)) goto finish;
|
1999-08-16 21:50:52 +00:00
|
|
|
current = 0;
|
|
|
|
item = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case eBeg2dList:
|
|
|
|
if(current != 0) {
|
1999-10-07 00:07:53 +00:00
|
|
|
*status = U_INTERNAL_PROGRAM_ERROR;
|
1999-08-16 21:50:52 +00:00
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
current = strlist2d_open(status);
|
1999-10-18 22:48:32 +00:00
|
|
|
if(U_FAILURE(*status)) goto finish;
|
1999-08-16 21:50:52 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case eEnd2dList:
|
2000-03-28 22:08:58 +00:00
|
|
|
put(data, &tag, status);
|
1999-08-16 21:50:52 +00:00
|
|
|
item = make_rbitem(tag.fChars, current, status);
|
|
|
|
rblist_add(list, item, status);
|
1999-10-18 22:48:32 +00:00
|
|
|
if(U_FAILURE(*status)) goto finish;
|
1999-08-16 21:50:52 +00:00
|
|
|
current = 0;
|
|
|
|
item = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case e2dStr:
|
|
|
|
strlist2d_add(current, token.fChars, status);
|
1999-10-18 22:48:32 +00:00
|
|
|
if(U_FAILURE(*status)) goto finish;
|
1999-08-16 21:50:52 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case eNewRow:
|
|
|
|
strlist2d_newRow(current, status);
|
1999-10-18 22:48:32 +00:00
|
|
|
if(U_FAILURE(*status)) goto finish;
|
1999-08-16 21:50:52 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case eBegTagged:
|
|
|
|
if(current != 0) {
|
1999-10-07 00:07:53 +00:00
|
|
|
*status = U_INTERNAL_PROGRAM_ERROR;
|
1999-08-16 21:50:52 +00:00
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
current = taglist_open(status);
|
|
|
|
ustr_cpy(&subtag, &token, status);
|
1999-10-18 22:48:32 +00:00
|
|
|
if(U_FAILURE(*status)) goto finish;
|
1999-08-16 21:50:52 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case eEndTagged:
|
2000-03-28 22:08:58 +00:00
|
|
|
put(data, &tag, status);
|
1999-08-16 21:50:52 +00:00
|
|
|
item = make_rbitem(tag.fChars, current, status);
|
|
|
|
rblist_add(list, item, status);
|
1999-10-18 22:48:32 +00:00
|
|
|
if(U_FAILURE(*status)) goto finish;
|
1999-08-16 21:50:52 +00:00
|
|
|
current = 0;
|
|
|
|
item = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case eTaggedStr:
|
|
|
|
taglist_add(current, subtag.fChars, token.fChars, status);
|
1999-10-18 22:48:32 +00:00
|
|
|
if(U_FAILURE(*status)) goto finish;
|
1999-08-16 21:50:52 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* Record the last string as the subtag */
|
|
|
|
case eSubtag:
|
|
|
|
ustr_cpy(&subtag, &token, status);
|
1999-10-18 22:48:32 +00:00
|
|
|
if(U_FAILURE(*status)) goto finish;
|
1999-08-16 21:50:52 +00:00
|
|
|
if(taglist_get(current, subtag.fChars, status) != 0) {
|
1999-10-07 00:07:53 +00:00
|
|
|
*status = U_INVALID_FORMAT_ERROR;
|
1999-08-16 21:50:52 +00:00
|
|
|
setErrorText("Duplicate subtag found in tagged list");
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case eOpen:
|
|
|
|
if(data != 0) {
|
1999-10-07 00:07:53 +00:00
|
|
|
*status = U_INTERNAL_PROGRAM_ERROR;
|
1999-08-16 21:50:52 +00:00
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
ustr_cpy(&localeName, &token, status);
|
|
|
|
rblist_setlocale(list, localeName.fChars, status);
|
1999-10-18 22:48:32 +00:00
|
|
|
if(U_FAILURE(*status)) goto finish;
|
2000-03-28 22:08:58 +00:00
|
|
|
data = uhash_open(hashUString, compareUString, status);
|
|
|
|
uhash_setKeyDeleter(data, freeUString);
|
1999-08-16 21:50:52 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case eClose:
|
|
|
|
if(data == 0) {
|
1999-10-07 00:07:53 +00:00
|
|
|
*status = U_INTERNAL_PROGRAM_ERROR;
|
1999-08-16 21:50:52 +00:00
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
finish:
|
|
|
|
|
|
|
|
/* clean up */
|
|
|
|
|
|
|
|
if(data != 0)
|
|
|
|
uhash_close(data);
|
|
|
|
|
|
|
|
if(item != 0)
|
1999-12-28 23:57:50 +00:00
|
|
|
uprv_free(item);
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
ustr_deinit(&token);
|
|
|
|
ustr_deinit(&tag);
|
|
|
|
ustr_deinit(&subtag);
|
|
|
|
ustr_deinit(&localeName);
|
|
|
|
ustr_deinit(&keyname);
|
|
|
|
|
|
|
|
if(file != 0)
|
|
|
|
u_fclose(file);
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|