]> git.saurik.com Git - apple/icu.git/blobdiff - icuSources/tools/genrb/parse.c
ICU-400.37.tar.gz
[apple/icu.git] / icuSources / tools / genrb / parse.c
index bd9a8550a44d25574cab722dcaa40f84a4d5849e..f7b1a63a607d25a65e4a75ac838dafbe008e00e7 100644 (file)
@@ -1,7 +1,7 @@
 /*
 *******************************************************************************
 *
-*   Copyright (C) 1998-2006, International Business Machines
+*   Copyright (C) 1998-2008, International Business Machines
 *   Corporation and others.  All Rights Reserved.
 *
 *******************************************************************************
@@ -87,6 +87,7 @@ static const char     *outputdir;
 static uint32_t        outputdirLength;
 
 static UBool gMakeBinaryCollation = TRUE;
+static UBool gOmitCollationRules  = FALSE;
 
 static struct SResource *parseResource(char *tag, const struct UString *comment, UErrorCode *status);
 
@@ -131,6 +132,18 @@ initLookahead(UCHARBUF *buf, UErrorCode *status)
     *status = U_ZERO_ERROR;
 }
 
+static void
+cleanupLookahead()
+{
+    uint32_t i;
+    for (i = 0; i < MAX_LOOKAHEAD; i++)
+    {
+        ustr_deinit(&lookahead[i].value);
+        ustr_deinit(&lookahead[i].comment);
+    }
+
+}
+
 static enum ETokenType
 getToken(struct UString **tokenValue, struct UString* comment, uint32_t *linenumber, UErrorCode *status)
 {
@@ -220,7 +233,7 @@ expect(enum ETokenType expectedToken, struct UString **tokenValue, struct UStrin
         *status = U_INVALID_FORMAT_ERROR;
         error(line, "expecting %s, got %s", tokenNames[expectedToken], tokenNames[token]);
     }
-    else /* "else" is added by Jing/GCL */
+    else
     {
         *status = U_ZERO_ERROR;
     }
@@ -308,6 +321,9 @@ parseUCARules(char *tag, uint32_t startline, const struct UString* comment, UErr
     }
     uprv_strcat(filename, cs);
 
+    if(gOmitCollationRules) {
+        return res_none();
+    }
 
     ucbuf = ucbuf_open(filename, &cp, getShowWarning(),FALSE, status);
 
@@ -465,7 +481,7 @@ parseTransliterator(char *tag, uint32_t startline, const struct UString* comment
 #if !UCONFIG_NO_TRANSLITERATION
     size = utrans_stripRules(pSource, size, pTarget, status);
 #else
-       size = 0;
+    size = 0;
     fprintf(stderr, " Warning: writing empty transliteration data ( UCONFIG_NO_TRANSLITERATION ) \n");
 #endif
     result = string_open(bundle, tag, pTarget, size, NULL, status);
@@ -562,13 +578,13 @@ parseString(char *tag, uint32_t startline, const struct UString* comment, UError
 
         result = string_open(bundle, tag, tokenValue->fChars, tokenValue->fLength, comment, status);
         if(U_SUCCESS(*status) && result) {
-          expect(TOK_CLOSE_BRACE, NULL, NULL, NULL, status);
+            expect(TOK_CLOSE_BRACE, NULL, NULL, NULL, status);
 
-          if (U_FAILURE(*status))
-          {
-              string_close(result, status);
-              return NULL;
-          }
+            if (U_FAILURE(*status))
+            {
+                res_close(result);
+                return NULL;
+            }
         }
     }
 
@@ -598,7 +614,7 @@ parseAlias(char *tag, uint32_t startline, const struct UString *comment, UErrorC
 
         if (U_FAILURE(*status))
         {
-            alias_close(result, status);
+            res_close(result);
             return NULL;
         }
     }
@@ -632,7 +648,7 @@ addCollation(struct SResource  *result, uint32_t startline, UErrorCode *status)
 
         if (token != TOK_STRING)
         {
-            table_close(result, status);
+            res_close(result);
             *status = U_INVALID_FORMAT_ERROR;
 
             if (token == TOK_EOF)
@@ -651,7 +667,7 @@ addCollation(struct SResource  *result, uint32_t startline, UErrorCode *status)
 
         if (U_FAILURE(*status))
         {
-            table_close(result, status);
+            res_close(result);
             return NULL;
         }
 
@@ -659,7 +675,7 @@ addCollation(struct SResource  *result, uint32_t startline, UErrorCode *status)
 
         if (U_FAILURE(*status))
         {
-            table_close(result, status);
+            res_close(result);
             return NULL;
         }
 
@@ -699,8 +715,12 @@ addCollation(struct SResource  *result, uint32_t startline, UErrorCode *status)
 #if UCONFIG_NO_COLLATION
             warning(line, "Not building collation elements because of UCONFIG_NO_COLLATION, see uconfig.h");
 #else
-            /* first we add the "Sequence", so that we always have rules */
-            table_add(result, member, line, status);
+            /* in order to achieve smaller data files, we can direct genrb */
+            /* to omit collation rules */
+            if(!gOmitCollationRules) {
+              /* first we add the "Sequence", so that we always have rules */
+              table_add(result, member, line, status);
+            }
             if(gMakeBinaryCollation) {
                 UErrorCode intStatus = U_ZERO_ERROR;
 
@@ -772,7 +792,7 @@ addCollation(struct SResource  *result, uint32_t startline, UErrorCode *status)
 
         if (U_FAILURE(*status))
         {
-            table_close(result, status);
+            res_close(result);
             return NULL;
         }
     }
@@ -819,7 +839,7 @@ parseCollationElements(char *tag, uint32_t startline, UBool newCollation, UError
 
             if (token != TOK_STRING)
             {
-                table_close(result, status);
+                res_close(result);
                 *status = U_INVALID_FORMAT_ERROR;
 
                 if (token == TOK_EOF)
@@ -838,7 +858,7 @@ parseCollationElements(char *tag, uint32_t startline, UBool newCollation, UError
 
             if (U_FAILURE(*status))
             {
-                table_close(result, status);
+                res_close(result);
                 return NULL;
             }
 
@@ -848,7 +868,7 @@ parseCollationElements(char *tag, uint32_t startline, UBool newCollation, UError
 
                 if (U_FAILURE(*status))
                 {
-                    table_close(result, status);
+                    res_close(result);
                     return NULL;
                 }
 
@@ -873,16 +893,18 @@ parseCollationElements(char *tag, uint32_t startline, UBool newCollation, UError
 
                         if (U_FAILURE(*status))
                         {
-                            table_close(result, status);
+                            res_close(result);
                             return NULL;
                         }
 
                         table_add(result, member, line, status);
                     } else {
+                        res_close(result);
                         *status = U_INVALID_FORMAT_ERROR;
                         return NULL;
                     }
                 } else {
+                    res_close(result);
                     *status = U_INVALID_FORMAT_ERROR;
                     return NULL;
                 }
@@ -894,7 +916,7 @@ parseCollationElements(char *tag, uint32_t startline, UBool newCollation, UError
 
             if (U_FAILURE(*status))
             {
-                table_close(result, status);
+                res_close(result);
                 return NULL;
             }
         }
@@ -933,7 +955,6 @@ realParseTable(struct SResource *table, char *tag, uint32_t startline, UErrorCod
 
         if (token != TOK_STRING)
         {
-            table_close(table, status);
             *status = U_INVALID_FORMAT_ERROR;
 
             if (token == TOK_EOF)
@@ -953,14 +974,12 @@ realParseTable(struct SResource *table, char *tag, uint32_t startline, UErrorCod
         } else {
             *status = U_INVALID_FORMAT_ERROR;
             error(line, "invariant characters required for table keys");
-            table_close(table, status);
             return NULL;
         }
 
         if (U_FAILURE(*status))
         {
             error(line, "parse error. Stopped parsing with %s", u_errorName(*status));
-            table_close(table, status);
             return NULL;
         }
 
@@ -969,7 +988,6 @@ realParseTable(struct SResource *table, char *tag, uint32_t startline, UErrorCod
         if (member == NULL || U_FAILURE(*status))
         {
             error(line, "parse error. Stopped parsing with %s", u_errorName(*status));
-            table_close(table, status);
             return NULL;
         }
 
@@ -978,10 +996,10 @@ realParseTable(struct SResource *table, char *tag, uint32_t startline, UErrorCod
         if (U_FAILURE(*status))
         {
             error(line, "parse error. Stopped parsing with %s", u_errorName(*status));
-            table_close(table, status);
             return NULL;
         }
         readToken = TRUE;
+        ustr_deinit(&comment);
     }
 
     /* not reached */
@@ -1060,7 +1078,7 @@ parseArray(char *tag, uint32_t startline, const struct UString *comment, UErrorC
 
         if (token == TOK_EOF)
         {
-            array_close(result, status);
+            res_close(result);
             *status = U_INVALID_FORMAT_ERROR;
             error(startline, "unterminated array");
             return NULL;
@@ -1079,7 +1097,7 @@ parseArray(char *tag, uint32_t startline, const struct UString *comment, UErrorC
 
         if (member == NULL || U_FAILURE(*status))
         {
-            array_close(result, status);
+            res_close(result);
             return NULL;
         }
 
@@ -1087,7 +1105,7 @@ parseArray(char *tag, uint32_t startline, const struct UString *comment, UErrorC
 
         if (U_FAILURE(*status))
         {
-            array_close(result, status);
+            res_close(result);
             return NULL;
         }
 
@@ -1101,12 +1119,13 @@ parseArray(char *tag, uint32_t startline, const struct UString *comment, UErrorC
 
         if (U_FAILURE(*status))
         {
-            array_close(result, status);
+            res_close(result);
             return NULL;
         }
         readToken = TRUE;
     }
 
+    ustr_deinit(&memberComments);
     return result;
 }
 
@@ -1118,7 +1137,6 @@ parseIntVector(char *tag, uint32_t startline, const struct UString *comment, UEr
     char              *string;
     int32_t            value;
     UBool              readToken = FALSE;
-    /* added by Jing/GCL */
     char              *stopstring;
     uint32_t           len;
     struct UString     memberComments;
@@ -1149,6 +1167,7 @@ parseIntVector(char *tag, uint32_t startline, const struct UString *comment, UEr
             if (!readToken) {
                 warning(startline, "Encountered empty int vector");
             }
+            ustr_deinit(&memberComments);
             return result;
         }
 
@@ -1156,18 +1175,11 @@ parseIntVector(char *tag, uint32_t startline, const struct UString *comment, UEr
 
         if (U_FAILURE(*status))
         {
-            intvector_close(result, status);
+            res_close(result);
             return NULL;
         }
-        /* Commented by Jing/GCL */
-        /*value = uprv_strtol(string, NULL, 10);
-        intvector_add(result, value, status);
-
-          uprv_free(string);
-
-        token = peekToken(0, NULL, NULL, status);*/
 
-        /* The following is added by Jing/GCL to handle illegal char in the Intvector */
+        /* For handling illegal char in the Intvector */
         value = uprv_strtoul(string, &stopstring, 0);/* make intvector support decimal,hexdigit,octal digit ranging from -2^31-2^32-1*/
         len=(uint32_t)(stopstring-string);
 
@@ -1182,11 +1194,10 @@ parseIntVector(char *tag, uint32_t startline, const struct UString *comment, UEr
             uprv_free(string);
             *status=U_INVALID_CHAR_FOUND;
         }
-        /* The above is added by Jing/GCL */
 
         if (U_FAILURE(*status))
         {
-            intvector_close(result, status);
+            res_close(result);
             return NULL;
         }
 
@@ -1216,7 +1227,6 @@ parseBinary(char *tag, uint32_t startline, const struct UString *comment, UError
     uint32_t          count;
     uint32_t          i;
     uint32_t          line;
-    /* added by Jing/GCL */
     char             *stopstring;
     uint32_t          len;
 
@@ -1295,7 +1305,6 @@ parseInteger(char *tag, uint32_t startline, const struct UString *comment, UErro
     struct SResource *result = NULL;
     int32_t           value;
     char             *string;
-    /* added by Jing/GCL */
     char             *stopstring;
     uint32_t          len;
 
@@ -1323,12 +1332,8 @@ parseInteger(char *tag, uint32_t startline, const struct UString *comment, UErro
         warning(startline, "Encountered empty integer. Default value is 0.");
     }
 
-    /* commented by Jing/GCL */
-    /* value  = uprv_strtol(string, NULL, 10);*/
-    /* result = int_open(bundle, tag, value, status);*/
-    /* The following is added by Jing/GCL*/
-    /* to make integer support hexdecimal, octal digit and decimal*/
-    /* to handle illegal char in the integer*/
+    /* Allow integer support for hexdecimal, octal digit and decimal*/
+    /* and handle illegal char in the integer*/
     value = uprv_strtoul(string, &stopstring, 0);
     len=(uint32_t)(stopstring-string);
     if(len==uprv_strlen(string))
@@ -1377,6 +1382,17 @@ parseImport(char *tag, uint32_t startline, const struct UString* comment, UError
     /* Open the input file for reading */
     if (inputdir == NULL)
     {
+#if 1
+        /* 
+         * Always save file file name, even if there's
+         * no input directory specified. MIGHT BREAK SOMETHING
+         */
+        int32_t filenameLength = uprv_strlen(filename);
+
+        fullname = (char *) uprv_malloc(filenameLength + 1);
+        uprv_strcpy(fullname, filename);
+#endif
+
         file = T_FileStream_open(filename, "rb");
     }
     else
@@ -1599,7 +1615,7 @@ static struct {
     {"reserved", NULL, NULL}
 };
 
-void initParser(UBool makeBinaryCollation)
+void initParser(UBool omitBinaryCollation, UBool omitCollationRules)
 {
     uint32_t i;
 
@@ -1626,7 +1642,8 @@ void initParser(UBool makeBinaryCollation)
     {
         ustr_init(&lookahead[i].value);
     }
-    gMakeBinaryCollation = makeBinaryCollation;
+    gMakeBinaryCollation = !omitBinaryCollation;
+    gOmitCollationRules = omitCollationRules;
 }
 
 static U_INLINE UBool isTable(enum EResourceType type) {
@@ -1735,11 +1752,8 @@ parseResource(char *tag, const struct UString *comment, UErrorCode *status)
         { :/}       => array
         { string ,  => string array
 
-        commented by Jing/GCL
         { string {  => table
 
-        added by Jing/GCL
-
         { string :/{    => table
         { string }      => string
         */
@@ -1751,8 +1765,6 @@ parseResource(char *tag, const struct UString *comment, UErrorCode *status)
             return NULL;
         }
 
-        /* Commented by Jing/GCL */
-        /* if (token == TOK_OPEN_BRACE || token == TOK_COLON )*/
         if (token == TOK_OPEN_BRACE || token == TOK_COLON ||token ==TOK_CLOSE_BRACE )
         {
             resType = RT_ARRAY;
@@ -1771,7 +1783,6 @@ parseResource(char *tag, const struct UString *comment, UErrorCode *status)
             case TOK_COMMA:         resType = RT_ARRAY;  break;
             case TOK_OPEN_BRACE:    resType = RT_TABLE;  break;
             case TOK_CLOSE_BRACE:   resType = RT_STRING; break;
-                /* added by Jing/GCL to make table work when :table is omitted */
             case TOK_COLON:         resType = RT_TABLE;  break;
             default:
                 *status = U_INVALID_FORMAT_ERROR;
@@ -1814,7 +1825,6 @@ parse(UCHARBUF *buf, const char *inputDir, const char *outputDir, UErrorCode *st
     struct UString    *tokenValue;
     struct UString    comment;
     uint32_t           line;
-    /* added by Jing/GCL */
     enum EResourceType bundleType;
     enum ETokenType    token;
 
@@ -1837,8 +1847,6 @@ parse(UCHARBUF *buf, const char *inputDir, const char *outputDir, UErrorCode *st
 
 
     bundle_setlocale(bundle, tokenValue->fChars, status);
-    /* Commented by Jing/GCL */
-    /* expect(TOK_OPEN_BRACE, NULL, &line, status); */
     /* The following code is to make Empty bundle work no matter with :table specifer or not */
     token = getToken(NULL, NULL, &line, status);
     if(token==TOK_COLON) {
@@ -1871,7 +1879,6 @@ parse(UCHARBUF *buf, const char *inputDir, const char *outputDir, UErrorCode *st
             error(line, "parse error, did not find open-brace '{' or colon ':', stopped with %s", u_errorName(*status));
         }
     }
-    /* The above is added by Jing/GCL */
 
     if (U_FAILURE(*status))
     {
@@ -1897,7 +1904,7 @@ parse(UCHARBUF *buf, const char *inputDir, const char *outputDir, UErrorCode *st
     if (U_FAILURE(*status))
     {
         bundle_close(bundle, status);
-        array_close(dependencyArray, status);
+        res_close(dependencyArray);
         return NULL;
     }
 
@@ -1910,5 +1917,8 @@ parse(UCHARBUF *buf, const char *inputDir, const char *outputDir, UErrorCode *st
         }
     }
 
+    cleanupLookahead();
+    ustr_deinit(&comment);
     return bundle;
 }
+