| 1 |
// © 2016 and later: Unicode, Inc. and others. |
| 2 |
// License & terms of use: http://www.unicode.org/copyright.html |
| 3 |
/* |
| 4 |
******************************************************************************* |
| 5 |
* |
| 6 |
* Copyright (C) 1998-2015, International Business Machines |
| 7 |
* Corporation and others. All Rights Reserved. |
| 8 |
* |
| 9 |
******************************************************************************* |
| 10 |
* |
| 11 |
* File parse.cpp |
| 12 |
* |
| 13 |
* Modification History: |
| 14 |
* |
| 15 |
* Date Name Description |
| 16 |
* 05/26/99 stephen Creation. |
| 17 |
* 02/25/00 weiv Overhaul to write udata |
| 18 |
* 5/10/01 Ram removed ustdio dependency |
| 19 |
* 06/10/2001 Dominic Ludlam <[email protected]> Rewritten |
| 20 |
******************************************************************************* |
| 21 |
*/ |
| 22 |
|
| 23 |
// Safer use of UnicodeString. |
| 24 |
#include <cstdint> |
| 25 |
#include "unicode/umachine.h" |
| 26 |
#ifndef UNISTR_FROM_CHAR_EXPLICIT |
| 27 |
# define UNISTR_FROM_CHAR_EXPLICIT explicit |
| 28 |
#endif |
| 29 |
|
| 30 |
// Less important, but still a good idea. |
| 31 |
#ifndef UNISTR_FROM_STRING_EXPLICIT |
| 32 |
# define UNISTR_FROM_STRING_EXPLICIT explicit |
| 33 |
#endif |
| 34 |
|
| 35 |
#include <assert.h> |
| 36 |
#include "parse.h" |
| 37 |
#include "errmsg.h" |
| 38 |
#include "uhash.h" |
| 39 |
#include "cmemory.h" |
| 40 |
#include "cstring.h" |
| 41 |
#include "uinvchar.h" |
| 42 |
#include "read.h" |
| 43 |
#include "ustr.h" |
| 44 |
#include "reslist.h" |
| 45 |
#include "rbt_pars.h" |
| 46 |
#include "genrb.h" |
| 47 |
#include "unicode/normalizer2.h" |
| 48 |
#include "unicode/stringpiece.h" |
| 49 |
#include "unicode/unistr.h" |
| 50 |
#include "unicode/ustring.h" |
| 51 |
#include "unicode/uscript.h" |
| 52 |
#include "unicode/utf16.h" |
| 53 |
#include "unicode/putil.h" |
| 54 |
#include "charstr.h" |
| 55 |
#include "collationbuilder.h" |
| 56 |
#include "collationdata.h" |
| 57 |
#include "collationdatareader.h" |
| 58 |
#include "collationdatawriter.h" |
| 59 |
#include "collationfastlatinbuilder.h" |
| 60 |
#include "collationinfo.h" |
| 61 |
#include "collationroot.h" |
| 62 |
#include "collationruleparser.h" |
| 63 |
#include "collationtailoring.h" |
| 64 |
#include <stdio.h> |
| 65 |
#include "writesrc.h" |
| 66 |
|
| 67 |
/* Number of tokens to read ahead of the current stream position */ |
| 68 |
#define MAX_LOOKAHEAD 3 |
| 69 |
|
| 70 |
#define CR 0x000D |
| 71 |
#define LF 0x000A |
| 72 |
#define SPACE 0x0020 |
| 73 |
#define TAB 0x0009 |
| 74 |
#define ESCAPE 0x005C |
| 75 |
#define HASH 0x0023 |
| 76 |
#define QUOTE 0x0027 |
| 77 |
#define ZERO 0x0030 |
| 78 |
#define STARTCOMMAND 0x005B |
| 79 |
#define ENDCOMMAND 0x005D |
| 80 |
#define OPENSQBRACKET 0x005B |
| 81 |
#define CLOSESQBRACKET 0x005D |
| 82 |
|
| 83 |
#define ICU4X_DIACRITIC_BASE 0x0300 |
| 84 |
#define ICU4X_DIACRITIC_LIMIT 0x034F |
| 85 |
|
| 86 |
using icu::CharString; |
| 87 |
using icu::LocalMemory; |
| 88 |
using icu::LocalPointer; |
| 89 |
using icu::LocalUCHARBUFPointer; |
| 90 |
using icu::StringPiece; |
| 91 |
using icu::UnicodeString; |
| 92 |
|
| 93 |
struct Lookahead |
| 94 |
{ |
| 95 |
enum ETokenType type; |
| 96 |
struct UString value; |
| 97 |
struct UString comment; |
| 98 |
uint32_t line; |
| 99 |
}; |
| 100 |
|
| 101 |
/* keep in sync with token defines in read.h */ |
| 102 |
const char *tokenNames[TOK_TOKEN_COUNT] = |
| 103 |
{ |
| 104 |
"string", /* A string token, such as "MonthNames" */ |
| 105 |
"'{'", /* An opening brace character */ |
| 106 |
"'}'", /* A closing brace character */ |
| 107 |
"','", /* A comma */ |
| 108 |
"':'", /* A colon */ |
| 109 |
|
| 110 |
"<end of file>", /* End of the file has been reached successfully */ |
| 111 |
"<end of line>" |
| 112 |
}; |
| 113 |
|
| 114 |
/* Just to store "TRUE" */ |
| 115 |
//static const char16_t trueValue[] = {0x0054, 0x0052, 0x0055, 0x0045, 0x0000}; |
| 116 |
|
| 117 |
typedef struct { |
| 118 |
struct Lookahead lookahead[MAX_LOOKAHEAD + 1]; |
| 119 |
uint32_t lookaheadPosition; |
| 120 |
UCHARBUF *buffer; |
| 121 |
struct SRBRoot *bundle; |
| 122 |
const char *inputdir; |
| 123 |
uint32_t inputdirLength; |
| 124 |
const char *outputdir; |
| 125 |
uint32_t outputdirLength; |
| 126 |
const char *filename; |
| 127 |
UBool makeBinaryCollation; |
| 128 |
UBool omitCollationRules; |
| 129 |
UBool icu4xMode; |
| 130 |
} ParseState; |
| 131 |
|
| 132 |
typedef struct SResource * |
| 133 |
ParseResourceFunction(ParseState* state, char *tag, uint32_t startline, const struct UString* comment, UErrorCode *status); |
| 134 |
|
| 135 |
static struct SResource *parseResource(ParseState* state, char *tag, const struct UString *comment, UErrorCode *status); |
| 136 |
|
| 137 |
/* The nature of the lookahead buffer: |
| 138 |
There are MAX_LOOKAHEAD + 1 slots, used as a circular buffer. This provides |
| 139 |
MAX_LOOKAHEAD lookahead tokens and a slot for the current token and value. |
| 140 |
When getToken is called, the current pointer is moved to the next slot and the |
| 141 |
old slot is filled with the next token from the reader by calling getNextToken. |
| 142 |
The token values are stored in the slot, which means that token values don't |
| 143 |
survive a call to getToken, ie. |
| 144 |
|
| 145 |
UString *value; |
| 146 |
|
| 147 |
getToken(&value, nullptr, status); |
| 148 |
getToken(nullptr, nullptr, status); bad - value is now a different string |
| 149 |
*/ |
| 150 |
static void |
| 151 |
initLookahead(ParseState* state, UCHARBUF *buf, UErrorCode *status) |
| 152 |
{ |
| 153 |
static uint32_t initTypeStrings = 0; |
| 154 |
uint32_t i; |
| 155 |
|
| 156 |
if (!initTypeStrings) |
| 157 |
{ |
| 158 |
initTypeStrings = 1; |
| 159 |
} |
| 160 |
|
| 161 |
state->lookaheadPosition = 0; |
| 162 |
state->buffer = buf; |
| 163 |
|
| 164 |
resetLineNumber(); |
| 165 |
|
| 166 |
for (i = 0; i < MAX_LOOKAHEAD; i++) |
| 167 |
{ |
| 168 |
state->lookahead[i].type = getNextToken(state->buffer, &state->lookahead[i].value, &state->lookahead[i].line, &state->lookahead[i].comment, status); |
| 169 |
if (U_FAILURE(*status)) |
| 170 |
{ |
| 171 |
return; |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
*status = U_ZERO_ERROR; |
| 176 |
} |
| 177 |
|
| 178 |
static void |
| 179 |
cleanupLookahead(ParseState* state) |
| 180 |
{ |
| 181 |
uint32_t i; |
| 182 |
for (i = 0; i <= MAX_LOOKAHEAD; i++) |
| 183 |
{ |
| 184 |
ustr_deinit(&state->lookahead[i].value); |
| 185 |
ustr_deinit(&state->lookahead[i].comment); |
| 186 |
} |
| 187 |
|
| 188 |
} |
| 189 |
|
| 190 |
static enum ETokenType |
| 191 |
getToken(ParseState* state, struct UString **tokenValue, struct UString* comment, uint32_t *linenumber, UErrorCode *status) |
| 192 |
{ |
| 193 |
enum ETokenType result; |
| 194 |
uint32_t i; |
| 195 |
|
| 196 |
result = state->lookahead[state->lookaheadPosition].type; |
| 197 |
|
| 198 |
if (tokenValue != nullptr) |
| 199 |
{ |
| 200 |
*tokenValue = &state->lookahead[state->lookaheadPosition].value; |
| 201 |
} |
| 202 |
|
| 203 |
if (linenumber != nullptr) |
| 204 |
{ |
| 205 |
*linenumber = state->lookahead[state->lookaheadPosition].line; |
| 206 |
} |
| 207 |
|
| 208 |
if (comment != nullptr) |
| 209 |
{ |
| 210 |
ustr_cpy(comment, &(state->lookahead[state->lookaheadPosition].comment), status); |
| 211 |
} |
| 212 |
|
| 213 |
i = (state->lookaheadPosition + MAX_LOOKAHEAD) % (MAX_LOOKAHEAD + 1); |
| 214 |
state->lookaheadPosition = (state->lookaheadPosition + 1) % (MAX_LOOKAHEAD + 1); |
| 215 |
ustr_setlen(&state->lookahead[i].comment, 0, status); |
| 216 |
ustr_setlen(&state->lookahead[i].value, 0, status); |
| 217 |
state->lookahead[i].type = getNextToken(state->buffer, &state->lookahead[i].value, &state->lookahead[i].line, &state->lookahead[i].comment, status); |
| 218 |
|
| 219 |
/* printf("getToken, returning %s\n", tokenNames[result]); */ |
| 220 |
|
| 221 |
return result; |
| 222 |
} |
| 223 |
|
| 224 |
static enum ETokenType |
| 225 |
peekToken(ParseState* state, uint32_t lookaheadCount, struct UString **tokenValue, uint32_t *linenumber, struct UString *comment, UErrorCode *status) |
| 226 |
{ |
| 227 |
uint32_t i = (state->lookaheadPosition + lookaheadCount) % (MAX_LOOKAHEAD + 1); |
| 228 |
|
| 229 |
if (U_FAILURE(*status)) |
| 230 |
{ |
| 231 |
return TOK_ERROR; |
| 232 |
} |
| 233 |
|
| 234 |
if (lookaheadCount >= MAX_LOOKAHEAD) |
| 235 |
{ |
| 236 |
*status = U_INTERNAL_PROGRAM_ERROR; |
| 237 |
return TOK_ERROR; |
| 238 |
} |
| 239 |
|
| 240 |
if (tokenValue != nullptr) |
| 241 |
{ |
| 242 |
*tokenValue = &state->lookahead[i].value; |
| 243 |
} |
| 244 |
|
| 245 |
if (linenumber != nullptr) |
| 246 |
{ |
| 247 |
*linenumber = state->lookahead[i].line; |
| 248 |
} |
| 249 |
|
| 250 |
if(comment != nullptr){ |
| 251 |
ustr_cpy(comment, &(state->lookahead[state->lookaheadPosition].comment), status); |
| 252 |
} |
| 253 |
|
| 254 |
return state->lookahead[i].type; |
| 255 |
} |
| 256 |
|
| 257 |
static void |
| 258 |
expect(ParseState* state, enum ETokenType expectedToken, struct UString **tokenValue, struct UString *comment, uint32_t *linenumber, UErrorCode *status) |
| 259 |
{ |
| 260 |
uint32_t line; |
| 261 |
|
| 262 |
enum ETokenType token = getToken(state, tokenValue, comment, &line, status); |
| 263 |
|
| 264 |
if (linenumber != nullptr) |
| 265 |
{ |
| 266 |
*linenumber = line; |
| 267 |
} |
| 268 |
|
| 269 |
if (U_FAILURE(*status)) |
| 270 |
{ |
| 271 |
return; |
| 272 |
} |
| 273 |
|
| 274 |
if (token != expectedToken) |
| 275 |
{ |
| 276 |
*status = U_INVALID_FORMAT_ERROR; |
| 277 |
error(line, "expecting %s, got %s", tokenNames[expectedToken], tokenNames[token]); |
| 278 |
} |
| 279 |
else |
| 280 |
{ |
| 281 |
*status = U_ZERO_ERROR; |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
static char *getInvariantString(ParseState* state, uint32_t *line, struct UString *comment, |
| 286 |
int32_t &stringLength, UErrorCode *status) |
| 287 |
{ |
| 288 |
struct UString *tokenValue; |
| 289 |
char *result; |
| 290 |
|
| 291 |
expect(state, TOK_STRING, &tokenValue, comment, line, status); |
| 292 |
|
| 293 |
if (U_FAILURE(*status)) |
| 294 |
{ |
| 295 |
return nullptr; |
| 296 |
} |
| 297 |
|
| 298 |
if(!uprv_isInvariantUString(tokenValue->fChars, tokenValue->fLength)) { |
| 299 |
*status = U_INVALID_FORMAT_ERROR; |
| 300 |
error(*line, "invariant characters required for table keys, binary data, etc."); |
| 301 |
return nullptr; |
| 302 |
} |
| 303 |
|
| 304 |
result = static_cast<char *>(uprv_malloc(tokenValue->fLength+1)); |
| 305 |
|
| 306 |
if (result == nullptr) |
| 307 |
{ |
| 308 |
*status = U_MEMORY_ALLOCATION_ERROR; |
| 309 |
return nullptr; |
| 310 |
} |
| 311 |
|
| 312 |
u_UCharsToChars(tokenValue->fChars, result, tokenValue->fLength+1); |
| 313 |
stringLength = tokenValue->fLength; |
| 314 |
return result; |
| 315 |
} |
| 316 |
|
| 317 |
static struct SResource * |
| 318 |
parseUCARules(ParseState* state, char *tag, uint32_t startline, const struct UString* /*comment*/, UErrorCode *status) |
| 319 |
{ |
| 320 |
struct SResource *result = nullptr; |
| 321 |
struct UString *tokenValue; |
| 322 |
FileStream *file = nullptr; |
| 323 |
char filename[256] = { '\0' }; |
| 324 |
char cs[128] = { '\0' }; |
| 325 |
uint32_t line; |
| 326 |
UBool quoted = false; |
| 327 |
UCHARBUF *ucbuf=nullptr; |
| 328 |
UChar32 c = 0; |
| 329 |
const char* cp = nullptr; |
| 330 |
char16_t *pTarget = nullptr; |
| 331 |
char16_t *target = nullptr; |
| 332 |
char16_t *targetLimit = nullptr; |
| 333 |
int32_t size = 0; |
| 334 |
|
| 335 |
expect(state, TOK_STRING, &tokenValue, nullptr, &line, status); |
| 336 |
|
| 337 |
if(isVerbose()){ |
| 338 |
printf(" %s at line %i \n", tag == nullptr ? "(null)" : tag, static_cast<int>(startline)); |
| 339 |
} |
| 340 |
|
| 341 |
if (U_FAILURE(*status)) |
| 342 |
{ |
| 343 |
return nullptr; |
| 344 |
} |
| 345 |
/* make the filename including the directory */ |
| 346 |
if (state->inputdir != nullptr) |
| 347 |
{ |
| 348 |
uprv_strcat(filename, state->inputdir); |
| 349 |
|
| 350 |
if (state->inputdir[state->inputdirLength - 1] != U_FILE_SEP_CHAR) |
| 351 |
{ |
| 352 |
uprv_strcat(filename, U_FILE_SEP_STRING); |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
u_UCharsToChars(tokenValue->fChars, cs, tokenValue->fLength); |
| 357 |
|
| 358 |
expect(state, TOK_CLOSE_BRACE, nullptr, nullptr, nullptr, status); |
| 359 |
|
| 360 |
if (U_FAILURE(*status)) |
| 361 |
{ |
| 362 |
return nullptr; |
| 363 |
} |
| 364 |
uprv_strcat(filename, cs); |
| 365 |
|
| 366 |
if(state->omitCollationRules) { |
| 367 |
return res_none(); |
| 368 |
} |
| 369 |
|
| 370 |
ucbuf = ucbuf_open(filename, &cp, getShowWarning(),false, status); |
| 371 |
|
| 372 |
if (U_FAILURE(*status)) { |
| 373 |
error(line, "An error occurred while opening the input file %s\n", filename); |
| 374 |
return nullptr; |
| 375 |
} |
| 376 |
|
| 377 |
/* We allocate more space than actually required |
| 378 |
* since the actual size needed for storing UChars |
| 379 |
* is not known in UTF-8 byte stream |
| 380 |
*/ |
| 381 |
size = ucbuf_size(ucbuf) + 1; |
| 382 |
pTarget = static_cast<char16_t*>(uprv_malloc(U_SIZEOF_UCHAR * size)); |
| 383 |
uprv_memset(pTarget, 0, size*U_SIZEOF_UCHAR); |
| 384 |
target = pTarget; |
| 385 |
targetLimit = pTarget+size; |
| 386 |
|
| 387 |
/* read the rules into the buffer */ |
| 388 |
while (target < targetLimit) |
| 389 |
{ |
| 390 |
c = ucbuf_getc(ucbuf, status); |
| 391 |
if(c == QUOTE) { |
| 392 |
quoted = static_cast<UBool>(!quoted); |
| 393 |
} |
| 394 |
/* weiv (06/26/2002): adding the following: |
| 395 |
* - preserving spaces in commands [...] |
| 396 |
* - # comments until the end of line |
| 397 |
*/ |
| 398 |
if (c == STARTCOMMAND && !quoted) |
| 399 |
{ |
| 400 |
/* preserve commands |
| 401 |
* closing bracket will be handled by the |
| 402 |
* append at the end of the loop |
| 403 |
*/ |
| 404 |
while(c != ENDCOMMAND) { |
| 405 |
U_APPEND_CHAR32_ONLY(c, target); |
| 406 |
c = ucbuf_getc(ucbuf, status); |
| 407 |
} |
| 408 |
} |
| 409 |
else if (c == HASH && !quoted) { |
| 410 |
/* skip comments */ |
| 411 |
while(c != CR && c != LF) { |
| 412 |
c = ucbuf_getc(ucbuf, status); |
| 413 |
} |
| 414 |
continue; |
| 415 |
} |
| 416 |
else if (c == ESCAPE) |
| 417 |
{ |
| 418 |
c = unescape(ucbuf, status); |
| 419 |
|
| 420 |
if (c == static_cast<UChar32>(U_ERR)) |
| 421 |
{ |
| 422 |
uprv_free(pTarget); |
| 423 |
T_FileStream_close(file); |
| 424 |
return nullptr; |
| 425 |
} |
| 426 |
} |
| 427 |
else if (!quoted && (c == SPACE || c == TAB || c == CR || c == LF)) |
| 428 |
{ |
| 429 |
/* ignore spaces carriage returns |
| 430 |
* and line feed unless in the form \uXXXX |
| 431 |
*/ |
| 432 |
continue; |
| 433 |
} |
| 434 |
|
| 435 |
/* Append char16_t * after dissembling if c > 0xffff*/ |
| 436 |
if (c != static_cast<UChar32>(U_EOF)) |
| 437 |
{ |
| 438 |
U_APPEND_CHAR32_ONLY(c, target); |
| 439 |
} |
| 440 |
else |
| 441 |
{ |
| 442 |
break; |
| 443 |
} |
| 444 |
} |
| 445 |
|
| 446 |
/* terminate the string */ |
| 447 |
if(target < targetLimit){ |
| 448 |
*target = 0x0000; |
| 449 |
} |
| 450 |
|
| 451 |
result = string_open(state->bundle, tag, pTarget, static_cast<int32_t>(target - pTarget), nullptr, status); |
| 452 |
|
| 453 |
|
| 454 |
ucbuf_close(ucbuf); |
| 455 |
uprv_free(pTarget); |
| 456 |
T_FileStream_close(file); |
| 457 |
|
| 458 |
return result; |
| 459 |
} |
| 460 |
|
| 461 |
static struct SResource * |
| 462 |
parseTransliterator(ParseState* state, char *tag, uint32_t startline, const struct UString* /*comment*/, UErrorCode *status) |
| 463 |
{ |
| 464 |
struct SResource *result = nullptr; |
| 465 |
struct UString *tokenValue; |
| 466 |
FileStream *file = nullptr; |
| 467 |
char filename[256] = { '\0' }; |
| 468 |
char cs[128] = { '\0' }; |
| 469 |
uint32_t line; |
| 470 |
UCHARBUF *ucbuf=nullptr; |
| 471 |
const char* cp = nullptr; |
| 472 |
char16_t *pTarget = nullptr; |
| 473 |
const char16_t *pSource = nullptr; |
| 474 |
int32_t size = 0; |
| 475 |
|
| 476 |
expect(state, TOK_STRING, &tokenValue, nullptr, &line, status); |
| 477 |
|
| 478 |
if(isVerbose()){ |
| 479 |
printf(" %s at line %i \n", tag == nullptr ? "(null)" : tag, static_cast<int>(startline)); |
| 480 |
} |
| 481 |
|
| 482 |
if (U_FAILURE(*status)) |
| 483 |
{ |
| 484 |
return nullptr; |
| 485 |
} |
| 486 |
/* make the filename including the directory */ |
| 487 |
if (state->inputdir != nullptr) |
| 488 |
{ |
| 489 |
uprv_strcat(filename, state->inputdir); |
| 490 |
|
| 491 |
if (state->inputdir[state->inputdirLength - 1] != U_FILE_SEP_CHAR) |
| 492 |
{ |
| 493 |
uprv_strcat(filename, U_FILE_SEP_STRING); |
| 494 |
} |
| 495 |
} |
| 496 |
|
| 497 |
u_UCharsToChars(tokenValue->fChars, cs, tokenValue->fLength); |
| 498 |
|
| 499 |
expect(state, TOK_CLOSE_BRACE, nullptr, nullptr, nullptr, status); |
| 500 |
|
| 501 |
if (U_FAILURE(*status)) |
| 502 |
{ |
| 503 |
return nullptr; |
| 504 |
} |
| 505 |
uprv_strcat(filename, cs); |
| 506 |
|
| 507 |
|
| 508 |
ucbuf = ucbuf_open(filename, &cp, getShowWarning(),false, status); |
| 509 |
|
| 510 |
if (U_FAILURE(*status)) { |
| 511 |
error(line, "An error occurred while opening the input file %s\n", filename); |
| 512 |
return nullptr; |
| 513 |
} |
| 514 |
|
| 515 |
/* We allocate more space than actually required |
| 516 |
* since the actual size needed for storing UChars |
| 517 |
* is not known in UTF-8 byte stream |
| 518 |
*/ |
| 519 |
pSource = ucbuf_getBuffer(ucbuf, &size, status); |
| 520 |
pTarget = static_cast<char16_t*>(uprv_malloc(U_SIZEOF_UCHAR * (size + 1))); |
| 521 |
uprv_memset(pTarget, 0, size*U_SIZEOF_UCHAR); |
| 522 |
|
| 523 |
#if !UCONFIG_NO_TRANSLITERATION |
| 524 |
size = utrans_stripRules(pSource, size, pTarget, status); |
| 525 |
#else |
| 526 |
size = 0; |
| 527 |
fprintf(stderr, " Warning: writing empty transliteration data ( UCONFIG_NO_TRANSLITERATION ) \n"); |
| 528 |
#endif |
| 529 |
result = string_open(state->bundle, tag, pTarget, size, nullptr, status); |
| 530 |
|
| 531 |
ucbuf_close(ucbuf); |
| 532 |
uprv_free(pTarget); |
| 533 |
T_FileStream_close(file); |
| 534 |
|
| 535 |
return result; |
| 536 |
} |
| 537 |
static ArrayResource* dependencyArray = nullptr; |
| 538 |
|
| 539 |
static struct SResource * |
| 540 |
parseDependency(ParseState* state, char *tag, uint32_t startline, const struct UString* comment, UErrorCode *status) |
| 541 |
{ |
| 542 |
struct SResource *result = nullptr; |
| 543 |
struct SResource *elem = nullptr; |
| 544 |
struct UString *tokenValue; |
| 545 |
uint32_t line; |
| 546 |
char filename[256] = { '\0' }; |
| 547 |
char cs[128] = { '\0' }; |
| 548 |
|
| 549 |
expect(state, TOK_STRING, &tokenValue, nullptr, &line, status); |
| 550 |
|
| 551 |
if(isVerbose()){ |
| 552 |
printf(" %s at line %i \n", tag == nullptr ? "(null)" : tag, static_cast<int>(startline)); |
| 553 |
} |
| 554 |
|
| 555 |
if (U_FAILURE(*status)) |
| 556 |
{ |
| 557 |
return nullptr; |
| 558 |
} |
| 559 |
/* make the filename including the directory */ |
| 560 |
if (state->outputdir != nullptr) |
| 561 |
{ |
| 562 |
uprv_strcat(filename, state->outputdir); |
| 563 |
|
| 564 |
if (state->outputdir[state->outputdirLength - 1] != U_FILE_SEP_CHAR) |
| 565 |
{ |
| 566 |
uprv_strcat(filename, U_FILE_SEP_STRING); |
| 567 |
} |
| 568 |
} |
| 569 |
|
| 570 |
u_UCharsToChars(tokenValue->fChars, cs, tokenValue->fLength); |
| 571 |
|
| 572 |
if (U_FAILURE(*status)) |
| 573 |
{ |
| 574 |
return nullptr; |
| 575 |
} |
| 576 |
uprv_strcat(filename, cs); |
| 577 |
if(!T_FileStream_file_exists(filename)){ |
| 578 |
if(isStrict()){ |
| 579 |
error(line, "The dependency file %s does not exist. Please make sure it exists.\n",filename); |
| 580 |
}else{ |
| 581 |
warning(line, "The dependency file %s does not exist. Please make sure it exists.\n",filename); |
| 582 |
} |
| 583 |
} |
| 584 |
if(dependencyArray==nullptr){ |
| 585 |
dependencyArray = array_open(state->bundle, "%%DEPENDENCY", nullptr, status); |
| 586 |
} |
| 587 |
if(tag!=nullptr){ |
| 588 |
result = string_open(state->bundle, tag, tokenValue->fChars, tokenValue->fLength, comment, status); |
| 589 |
} |
| 590 |
elem = string_open(state->bundle, nullptr, tokenValue->fChars, tokenValue->fLength, comment, status); |
| 591 |
|
| 592 |
dependencyArray->add(elem); |
| 593 |
|
| 594 |
if (U_FAILURE(*status)) |
| 595 |
{ |
| 596 |
return nullptr; |
| 597 |
} |
| 598 |
expect(state, TOK_CLOSE_BRACE, nullptr, nullptr, nullptr, status); |
| 599 |
return result; |
| 600 |
} |
| 601 |
static struct SResource * |
| 602 |
parseString(ParseState* state, char *tag, uint32_t startline, const struct UString* comment, UErrorCode *status) |
| 603 |
{ |
| 604 |
struct UString *tokenValue; |
| 605 |
struct SResource *result = nullptr; |
| 606 |
|
| 607 |
/* if (tag != nullptr && uprv_strcmp(tag, "%%UCARULES") == 0) |
| 608 |
{ |
| 609 |
return parseUCARules(tag, startline, status); |
| 610 |
}*/ |
| 611 |
if(isVerbose()){ |
| 612 |
printf(" string %s at line %i \n", tag == nullptr ? "(null)" : tag, static_cast<int>(startline)); |
| 613 |
} |
| 614 |
expect(state, TOK_STRING, &tokenValue, nullptr, nullptr, status); |
| 615 |
|
| 616 |
if (U_SUCCESS(*status)) |
| 617 |
{ |
| 618 |
/* create the string now - tokenValue doesn't survive a call to getToken (and therefore |
| 619 |
doesn't survive expect either) */ |
| 620 |
|
| 621 |
result = string_open(state->bundle, tag, tokenValue->fChars, tokenValue->fLength, comment, status); |
| 622 |
if(U_SUCCESS(*status) && result) { |
| 623 |
expect(state, TOK_CLOSE_BRACE, nullptr, nullptr, nullptr, status); |
| 624 |
|
| 625 |
if (U_FAILURE(*status)) |
| 626 |
{ |
| 627 |
res_close(result); |
| 628 |
return nullptr; |
| 629 |
} |
| 630 |
} |
| 631 |
} |
| 632 |
|
| 633 |
return result; |
| 634 |
} |
| 635 |
|
| 636 |
static struct SResource * |
| 637 |
parseAlias(ParseState* state, char *tag, uint32_t startline, const struct UString *comment, UErrorCode *status) |
| 638 |
{ |
| 639 |
struct UString *tokenValue; |
| 640 |
struct SResource *result = nullptr; |
| 641 |
|
| 642 |
expect(state, TOK_STRING, &tokenValue, nullptr, nullptr, status); |
| 643 |
|
| 644 |
if(isVerbose()){ |
| 645 |
printf(" alias %s at line %i \n", tag == nullptr ? "(null)" : tag, static_cast<int>(startline)); |
| 646 |
} |
| 647 |
|
| 648 |
if (U_SUCCESS(*status)) |
| 649 |
{ |
| 650 |
/* create the string now - tokenValue doesn't survive a call to getToken (and therefore |
| 651 |
doesn't survive expect either) */ |
| 652 |
|
| 653 |
result = alias_open(state->bundle, tag, tokenValue->fChars, tokenValue->fLength, comment, status); |
| 654 |
|
| 655 |
expect(state, TOK_CLOSE_BRACE, nullptr, nullptr, nullptr, status); |
| 656 |
|
| 657 |
if (U_FAILURE(*status)) |
| 658 |
{ |
| 659 |
res_close(result); |
| 660 |
return nullptr; |
| 661 |
} |
| 662 |
} |
| 663 |
|
| 664 |
return result; |
| 665 |
} |
| 666 |
|
| 667 |
#if !UCONFIG_NO_COLLATION |
| 668 |
|
| 669 |
namespace { |
| 670 |
|
| 671 |
struct SResource* resLookup(struct SResource* res, const char* key) { |
| 672 |
if (res == res_none() || !res->isTable()) { |
| 673 |
return nullptr; |
| 674 |
} |
| 675 |
|
| 676 |
TableResource *list = static_cast<TableResource *>(res); |
| 677 |
SResource *current = list->fFirst; |
| 678 |
while (current != nullptr) { |
| 679 |
if (uprv_strcmp(((list->fRoot->fKeys) + (current->fKey)), key) == 0) { |
| 680 |
return current; |
| 681 |
} |
| 682 |
current = current->fNext; |
| 683 |
} |
| 684 |
return nullptr; |
| 685 |
} |
| 686 |
|
| 687 |
class GenrbImporter : public icu::CollationRuleParser::Importer { |
| 688 |
public: |
| 689 |
GenrbImporter(const char *in, const char *out) : inputDir(in), outputDir(out) {} |
| 690 |
virtual ~GenrbImporter(); |
| 691 |
virtual void getRules( |
| 692 |
const char *localeID, const char *collationType, |
| 693 |
UnicodeString &rules, |
| 694 |
const char *&errorReason, UErrorCode &errorCode) override; |
| 695 |
|
| 696 |
private: |
| 697 |
const char *inputDir; |
| 698 |
const char *outputDir; |
| 699 |
}; |
| 700 |
|
| 701 |
GenrbImporter::~GenrbImporter() {} |
| 702 |
|
| 703 |
void |
| 704 |
GenrbImporter::getRules( |
| 705 |
const char *localeID, const char *collationType, |
| 706 |
UnicodeString &rules, |
| 707 |
const char *& /*errorReason*/, UErrorCode &errorCode) { |
| 708 |
CharString filename(localeID, errorCode); |
| 709 |
for(int32_t i = 0; i < filename.length(); i++){ |
| 710 |
if(filename[i] == '-'){ |
| 711 |
filename.data()[i] = '_'; |
| 712 |
} |
| 713 |
} |
| 714 |
filename.append(".txt", errorCode); |
| 715 |
if (U_FAILURE(errorCode)) { |
| 716 |
return; |
| 717 |
} |
| 718 |
CharString inputDirBuf; |
| 719 |
CharString openFileName; |
| 720 |
if(inputDir == nullptr) { |
| 721 |
const char *filenameBegin = uprv_strrchr(filename.data(), U_FILE_SEP_CHAR); |
| 722 |
if (filenameBegin != nullptr) { |
| 723 |
/* |
| 724 |
* When a filename ../../../data/root.txt is specified, |
| 725 |
* we presume that the input directory is ../../../data |
| 726 |
* This is very important when the resource file includes |
| 727 |
* another file, like UCARules.txt or thaidict.brk. |
| 728 |
*/ |
| 729 |
StringPiece dir = filename.toStringPiece(); |
| 730 |
const char *filenameLimit = filename.data() + filename.length(); |
| 731 |
dir.remove_suffix(static_cast<int32_t>(filenameLimit - filenameBegin)); |
| 732 |
inputDirBuf.append(dir, errorCode); |
| 733 |
inputDir = inputDirBuf.data(); |
| 734 |
} |
| 735 |
}else{ |
| 736 |
int32_t dirlen = static_cast<int32_t>(uprv_strlen(inputDir)); |
| 737 |
|
| 738 |
if((filename[0] != U_FILE_SEP_CHAR) && (inputDir[dirlen-1] !='.')) { |
| 739 |
/* |
| 740 |
* append the input dir to openFileName if the first char in |
| 741 |
* filename is not file separator char and the last char input directory is not '.'. |
| 742 |
* This is to support : |
| 743 |
* genrb -s. /home/icu/data |
| 744 |
* genrb -s. icu/data |
| 745 |
* The user cannot mix notations like |
| 746 |
* genrb -s. /icu/data --- the absolute path specified. -s redundant |
| 747 |
* user should use |
| 748 |
* genrb -s. icu/data --- start from CWD and look in icu/data dir |
| 749 |
*/ |
| 750 |
openFileName.append(inputDir, dirlen, errorCode); |
| 751 |
if(inputDir[dirlen-1] != U_FILE_SEP_CHAR) { |
| 752 |
openFileName.append(U_FILE_SEP_CHAR, errorCode); |
| 753 |
} |
| 754 |
} |
| 755 |
} |
| 756 |
openFileName.append(filename, errorCode); |
| 757 |
if(U_FAILURE(errorCode)) { |
| 758 |
return; |
| 759 |
} |
| 760 |
// printf("GenrbImporter::getRules(%s, %s) reads %s\n", localeID, collationType, openFileName.data()); |
| 761 |
const char* cp = ""; |
| 762 |
LocalUCHARBUFPointer ucbuf( |
| 763 |
ucbuf_open(openFileName.data(), &cp, getShowWarning(), true, &errorCode)); |
| 764 |
if(errorCode == U_FILE_ACCESS_ERROR) { |
| 765 |
fprintf(stderr, "couldn't open file %s\n", openFileName.data()); |
| 766 |
return; |
| 767 |
} |
| 768 |
if (ucbuf.isNull() || U_FAILURE(errorCode)) { |
| 769 |
fprintf(stderr, "An error occurred processing file %s. Error: %s\n", openFileName.data(), u_errorName(errorCode)); |
| 770 |
return; |
| 771 |
} |
| 772 |
|
| 773 |
/* Parse the data into an SRBRoot */ |
| 774 |
LocalPointer<SRBRoot> data( |
| 775 |
parse(ucbuf.getAlias(), inputDir, outputDir, filename.data(), false, false, false, &errorCode)); |
| 776 |
if (U_FAILURE(errorCode)) { |
| 777 |
return; |
| 778 |
} |
| 779 |
|
| 780 |
struct SResource *root = data->fRoot; |
| 781 |
struct SResource *collations = resLookup(root, "collations"); |
| 782 |
if (collations != nullptr) { |
| 783 |
struct SResource *collation = resLookup(collations, collationType); |
| 784 |
if (collation != nullptr) { |
| 785 |
struct SResource *sequence = resLookup(collation, "Sequence"); |
| 786 |
if (sequence != nullptr && sequence->isString()) { |
| 787 |
// No string pointer aliasing so that we need not hold onto the resource bundle. |
| 788 |
StringResource *sr = static_cast<StringResource *>(sequence); |
| 789 |
rules = sr->fString; |
| 790 |
} |
| 791 |
} |
| 792 |
} |
| 793 |
} |
| 794 |
|
| 795 |
// Quick-and-dirty escaping function. |
| 796 |
// Assumes that we are on an ASCII-based platform. |
| 797 |
void |
| 798 |
escape(const char16_t *s, char *buffer, size_t n) { |
| 799 |
int32_t length = u_strlen(s); |
| 800 |
int32_t i = 0; |
| 801 |
for (;;) { |
| 802 |
UChar32 c; |
| 803 |
U16_NEXT(s, i, length, c); |
| 804 |
if (c == 0) { |
| 805 |
*buffer = 0; |
| 806 |
return; |
| 807 |
} else if (0x20 <= c && c <= 0x7e) { |
| 808 |
// printable ASCII |
| 809 |
*buffer++ = static_cast<char>(c); // assumes ASCII-based platform |
| 810 |
} else { |
| 811 |
buffer += snprintf(buffer, n, "\\u%04X", static_cast<int>(c)); |
| 812 |
} |
| 813 |
} |
| 814 |
} |
| 815 |
|
| 816 |
} // namespace |
| 817 |
|
| 818 |
static FILE* |
| 819 |
openTOML(const char* outputdir, const char* name, const char* collationType, const char* structType, UErrorCode *status) { |
| 820 |
CharString baseName; |
| 821 |
baseName.append(name, *status); |
| 822 |
baseName.append("_", *status); |
| 823 |
baseName.append(collationType, *status); |
| 824 |
baseName.append("_", *status); |
| 825 |
baseName.append(structType, *status); |
| 826 |
|
| 827 |
CharString outFileName; |
| 828 |
if (outputdir && *outputdir) { |
| 829 |
outFileName.append(outputdir, *status).ensureEndsWithFileSeparator(*status); |
| 830 |
} |
| 831 |
outFileName.append(baseName, *status); |
| 832 |
outFileName.append(".toml", *status); |
| 833 |
if (U_FAILURE(*status)) { |
| 834 |
return nullptr; |
| 835 |
} |
| 836 |
|
| 837 |
FILE* f = fopen(outFileName.data(), "w"); |
| 838 |
if (!f) { |
| 839 |
*status = U_FILE_ACCESS_ERROR; |
| 840 |
return nullptr; |
| 841 |
} |
| 842 |
usrc_writeFileNameGeneratedBy(f, "#", baseName.data(), "genrb -X"); |
| 843 |
|
| 844 |
return f; |
| 845 |
} |
| 846 |
|
| 847 |
static void |
| 848 |
writeCollationMetadataTOML(const char* outputdir, const char* name, const char* collationType, const uint32_t metadataBits, UErrorCode *status) { |
| 849 |
FILE* f = openTOML(outputdir, name, collationType, "meta", status); |
| 850 |
if (!f) { |
| 851 |
return; |
| 852 |
} |
| 853 |
// printf("writeCollationMetadataTOML %s %s\n", name, collationType); |
| 854 |
fprintf(f, "bits = 0x%X\n", metadataBits); |
| 855 |
fclose(f); |
| 856 |
} |
| 857 |
|
| 858 |
static UChar32 |
| 859 |
writeCollationDiacriticsTOML(const char* outputdir, const char* name, const char* collationType, const icu::CollationData* data, UErrorCode *status) { |
| 860 |
UChar32 limit = ICU4X_DIACRITIC_LIMIT; |
| 861 |
FILE* f = openTOML(outputdir, name, collationType, "dia", status); |
| 862 |
if (!f) { |
| 863 |
return limit; |
| 864 |
} |
| 865 |
// printf("writeCollationDiacriticsTOML %s %s\n", name, collationType); |
| 866 |
uint16_t secondaries[ICU4X_DIACRITIC_LIMIT-ICU4X_DIACRITIC_BASE]; |
| 867 |
for (UChar32 c = ICU4X_DIACRITIC_BASE; c < ICU4X_DIACRITIC_LIMIT; ++c) { |
| 868 |
uint16_t secondary = 0; |
| 869 |
uint32_t ce32 = data->getCE32(c); |
| 870 |
if (ce32 == icu::Collation::FALLBACK_CE32) { |
| 871 |
ce32 = data->base->getCE32(c); |
| 872 |
} |
| 873 |
if (c == 0x0340 || c == 0x0341 || c == 0x0343 || c == 0x0344) { |
| 874 |
// These never occur in NFD data |
| 875 |
} else if (!icu::Collation::isSimpleOrLongCE32(ce32)) { |
| 876 |
if (uprv_strcmp(name, "root") == 0) { |
| 877 |
printf("UNSUPPORTED DIACRITIC CE32 in root: TAG: %X CE32: %X char: %X\n", icu::Collation::tagFromCE32(ce32), ce32, c); |
| 878 |
fclose(f); |
| 879 |
*status = U_INTERNAL_PROGRAM_ERROR; |
| 880 |
return limit; |
| 881 |
} |
| 882 |
limit = c; |
| 883 |
break; |
| 884 |
} else { |
| 885 |
uint64_t ce = static_cast<uint64_t>(icu::Collation::ceFromCE32(ce32)); |
| 886 |
if ((ce & 0xFFFFFFFF0000FFFF) != static_cast<uint64_t>(icu::Collation::COMMON_TERTIARY_CE)) { |
| 887 |
// Not a CE where only the secondary weight differs from the expected |
| 888 |
// pattern. |
| 889 |
limit = c; |
| 890 |
break; |
| 891 |
} |
| 892 |
secondary = static_cast<uint16_t>(ce >> 16); |
| 893 |
} |
| 894 |
secondaries[c - ICU4X_DIACRITIC_BASE] = secondary; |
| 895 |
|
| 896 |
} |
| 897 |
usrc_writeArray(f, "secondaries = [\n ", secondaries, 16, limit-ICU4X_DIACRITIC_BASE, " ", "\n]\n"); |
| 898 |
fclose(f); |
| 899 |
return limit; |
| 900 |
} |
| 901 |
|
| 902 |
static void |
| 903 |
writeCollationReorderingTOML(const char* outputdir, const char* name, const char* collationType, const icu::CollationSettings* settings, UErrorCode *status) { |
| 904 |
FILE* f = openTOML(outputdir, name, collationType, "reord", status); |
| 905 |
if (!f) { |
| 906 |
return; |
| 907 |
} |
| 908 |
// printf("writeCollationReorderingTOML %s %s\n", name, collationType); |
| 909 |
fprintf(f, "min_high_no_reorder = 0x%X\n", settings->minHighNoReorder); |
| 910 |
usrc_writeArray(f, "reorder_table = [\n ", settings->reorderTable, 8, 256, " ", "\n]\n"); |
| 911 |
usrc_writeArray(f, "reorder_ranges = [\n ", settings->reorderRanges, 32, settings->reorderRangesLength, " ", "\n]\n"); |
| 912 |
fclose(f); |
| 913 |
} |
| 914 |
|
| 915 |
|
| 916 |
static void |
| 917 |
writeCollationJamoTOML(const char* outputdir, const char* name, const char* collationType, const icu::CollationData* data, UErrorCode *status) { |
| 918 |
FILE* f = openTOML(outputdir, name, collationType, "jamo", status); |
| 919 |
if (!f) { |
| 920 |
printf("writeCollationJamoTOML FAILED TO OPEN FILE %s %s\n", name, collationType); |
| 921 |
return; |
| 922 |
} |
| 923 |
uint32_t jamo[0x1200-0x1100]; |
| 924 |
for (UChar32 c = 0x1100; c < 0x1200; ++c) { |
| 925 |
uint32_t ce32 = data->getCE32(c); |
| 926 |
if (ce32 == icu::Collation::FALLBACK_CE32) { |
| 927 |
ce32 = data->base->getCE32(c); |
| 928 |
} |
| 929 |
// Can't reject complex CE32s, because search collations have expansions. |
| 930 |
// These expansions refer to the tailoring, which foils the reuse of the |
| 931 |
// these jamo tables. |
| 932 |
// XXX Figure out what to do. Perhaps instead of having Latin mini expansions, |
| 933 |
// there should be Hangul mini expansions. |
| 934 |
// XXX in any case, validate that modern jamo are self-contained. |
| 935 |
jamo[c - 0x1100] = ce32; |
| 936 |
|
| 937 |
} |
| 938 |
usrc_writeArray(f, "ce32s = [\n ", jamo, 32, 0x1200-0x1100, " ", "\n]\n"); |
| 939 |
fclose(f); |
| 940 |
} |
| 941 |
|
| 942 |
static UBool |
| 943 |
convertTrie(const void *context, UChar32 start, UChar32 end, uint32_t value) { |
| 944 |
if (start >= 0x1100 && start < 0x1200 && end >= 0x1100 && end < 0x1200) { |
| 945 |
// Range entirely in conjoining jamo block. |
| 946 |
return true; |
| 947 |
} |
| 948 |
icu::IcuToolErrorCode status("genrb: convertTrie"); |
| 949 |
umutablecptrie_setRange((UMutableCPTrie*)context, start, end, value, status); |
| 950 |
return !U_FAILURE(*status); |
| 951 |
} |
| 952 |
|
| 953 |
static void |
| 954 |
writeCollationDataTOML(const char* outputdir, const char* name, const char* collationType, const icu::CollationData* data, UBool root, UChar32 diacriticLimit, UErrorCode *status) { |
| 955 |
FILE* f = openTOML(outputdir, name, collationType, "data", status); |
| 956 |
if (!f) { |
| 957 |
return; |
| 958 |
} |
| 959 |
// printf("writeCollationDataTOML %s %s\n", name, collationType); |
| 960 |
|
| 961 |
icu::UnicodeSet tailoringSet; |
| 962 |
|
| 963 |
if (data->base) { |
| 964 |
tailoringSet.addAll(*(data->unsafeBackwardSet)); |
| 965 |
tailoringSet.removeAll(*(data->base->unsafeBackwardSet)); |
| 966 |
} else { |
| 967 |
tailoringSet.addAll(*(data->unsafeBackwardSet)); |
| 968 |
} |
| 969 |
|
| 970 |
// Use the same value for out-of-range and default in the hope of not having to allocate |
| 971 |
// different blocks, since ICU4X never does out-of-range queries. |
| 972 |
uint32_t trieDefault = root ? icu::Collation::UNASSIGNED_CE32 : icu::Collation::FALLBACK_CE32; |
| 973 |
icu::LocalUMutableCPTriePointer builder(umutablecptrie_open(trieDefault, trieDefault, status)); |
| 974 |
|
| 975 |
utrie2_enum(data->trie, nullptr, &convertTrie, builder.getAlias()); |
| 976 |
|
| 977 |
// If the diacritic table was cut short, copy CE32s between the lowered |
| 978 |
// limit and the max limit from the root to the tailoring. As of June 2022, |
| 979 |
// no collation in CLDR needs this. |
| 980 |
for (UChar32 c = diacriticLimit; c < ICU4X_DIACRITIC_LIMIT; ++c) { |
| 981 |
if (c == 0x0340 || c == 0x0341 || c == 0x0343 || c == 0x0344) { |
| 982 |
// These never occur in NFD data. |
| 983 |
continue; |
| 984 |
} |
| 985 |
uint32_t ce32 = data->getCE32(c); |
| 986 |
if (ce32 == icu::Collation::FALLBACK_CE32) { |
| 987 |
ce32 = data->base->getCE32(c); |
| 988 |
umutablecptrie_set(builder.getAlias(), c, ce32, status); |
| 989 |
} |
| 990 |
} |
| 991 |
|
| 992 |
// Ensure that the range covered by the diacritic table isn't duplicated |
| 993 |
// in the trie. |
| 994 |
for (UChar32 c = ICU4X_DIACRITIC_BASE; c < diacriticLimit; ++c) { |
| 995 |
if (umutablecptrie_get(builder.getAlias(), c) != trieDefault) { |
| 996 |
umutablecptrie_set(builder.getAlias(), c, trieDefault, status); |
| 997 |
} |
| 998 |
} |
| 999 |
|
| 1000 |
icu::LocalUCPTriePointer utrie(umutablecptrie_buildImmutable( |
| 1001 |
builder.getAlias(), |
| 1002 |
UCPTRIE_TYPE_SMALL, |
| 1003 |
UCPTRIE_VALUE_BITS_32, |
| 1004 |
status)); |
| 1005 |
usrc_writeArray(f, "contexts = [\n ", data->contexts, 16, data->contextsLength, " ", "\n]\n"); |
| 1006 |
usrc_writeArray(f, "ce32s = [\n ", data->ce32s, 32, data->ce32sLength, " ", "\n]\n"); |
| 1007 |
usrc_writeArray(f, "ces = [\n ", data->ces, 64, data->cesLength, " ", "\n]\n"); |
| 1008 |
fprintf(f, "[trie]\n"); |
| 1009 |
usrc_writeUCPTrie(f, "trie", utrie.getAlias(), UPRV_TARGET_SYNTAX_TOML); |
| 1010 |
|
| 1011 |
fclose(f); |
| 1012 |
} |
| 1013 |
|
| 1014 |
static void |
| 1015 |
writeCollationSpecialPrimariesTOML(const char* outputdir, const char* name, const char* collationType, const icu::CollationData* data, UErrorCode *status) { |
| 1016 |
FILE* f = openTOML(outputdir, name, collationType, "prim", status); |
| 1017 |
if (!f) { |
| 1018 |
return; |
| 1019 |
} |
| 1020 |
// printf("writeCollationSpecialPrimariesTOML %s %s\n", name, collationType); |
| 1021 |
|
| 1022 |
uint16_t lastPrimaries[4]; |
| 1023 |
for (int32_t i = 0; i < 4; ++i) { |
| 1024 |
// getLastPrimaryForGroup subtracts one from a 16-bit value, so we add one |
| 1025 |
// back to get a value that fits in 16 bits. |
| 1026 |
lastPrimaries[i] = static_cast<uint16_t>((data->getLastPrimaryForGroup(UCOL_REORDER_CODE_FIRST + i) + 1) >> 16); |
| 1027 |
} |
| 1028 |
|
| 1029 |
uint32_t numericPrimary = data->numericPrimary; |
| 1030 |
if (numericPrimary & 0xFFFFFF) { |
| 1031 |
printf("Lower 24 bits set in numeric primary"); |
| 1032 |
*status = U_INTERNAL_PROGRAM_ERROR; |
| 1033 |
return; |
| 1034 |
} |
| 1035 |
|
| 1036 |
usrc_writeArray(f, "last_primaries = [\n ", lastPrimaries, 16, 4, " ", "\n]\n"); |
| 1037 |
fprintf(f, "numeric_primary = 0x%X\n", numericPrimary >> 24); |
| 1038 |
fclose(f); |
| 1039 |
} |
| 1040 |
|
| 1041 |
static void |
| 1042 |
writeCollationTOML(const char* outputdir, const char* name, const char* collationType, const icu::CollationData* data, const icu::CollationSettings* settings, UErrorCode *status) { |
| 1043 |
UBool tailored = false; |
| 1044 |
UBool tailoredDiacritics = false; |
| 1045 |
UBool lithuanianDotAbove = (uprv_strcmp(name, "lt") == 0); |
| 1046 |
UBool reordering = false; |
| 1047 |
UBool isRoot = uprv_strcmp(name, "root") == 0; |
| 1048 |
UChar32 diacriticLimit = ICU4X_DIACRITIC_LIMIT; |
| 1049 |
if (!data->base && isRoot) { |
| 1050 |
diacriticLimit = writeCollationDiacriticsTOML(outputdir, name, collationType, data, status); |
| 1051 |
if (U_FAILURE(*status)) { |
| 1052 |
return; |
| 1053 |
} |
| 1054 |
writeCollationJamoTOML(outputdir, name, collationType, data, status); |
| 1055 |
if (U_FAILURE(*status)) { |
| 1056 |
return; |
| 1057 |
} |
| 1058 |
writeCollationSpecialPrimariesTOML(outputdir, name, collationType, data, status); |
| 1059 |
if (U_FAILURE(*status)) { |
| 1060 |
return; |
| 1061 |
} |
| 1062 |
} else if (data->base && !lithuanianDotAbove) { |
| 1063 |
for (UChar32 c = ICU4X_DIACRITIC_BASE; c < ICU4X_DIACRITIC_LIMIT; ++c) { |
| 1064 |
if (c == 0x0340 || c == 0x0341 || c == 0x0343 || c == 0x0344) { |
| 1065 |
// These never occur in NFD data. |
| 1066 |
continue; |
| 1067 |
} |
| 1068 |
uint32_t ce32 = data->getCE32(c); |
| 1069 |
if ((ce32 != icu::Collation::FALLBACK_CE32) && (ce32 != data->base->getCE32(c))) { |
| 1070 |
tailoredDiacritics = true; |
| 1071 |
diacriticLimit = writeCollationDiacriticsTOML(outputdir, name, collationType, data, status); |
| 1072 |
if (U_FAILURE(*status)) { |
| 1073 |
return; |
| 1074 |
} |
| 1075 |
break; |
| 1076 |
} |
| 1077 |
} |
| 1078 |
} |
| 1079 |
|
| 1080 |
if (settings->hasReordering()) { |
| 1081 |
reordering = true; |
| 1082 |
// Note: There are duplicate reorderings. Expecting the ICU4X provider |
| 1083 |
// to take care of deduplication. |
| 1084 |
writeCollationReorderingTOML(outputdir, name, collationType, settings, status); |
| 1085 |
if (U_FAILURE(*status)) { |
| 1086 |
return; |
| 1087 |
} |
| 1088 |
} |
| 1089 |
|
| 1090 |
// Write collation data if either base is non-null or the name is root. |
| 1091 |
// Languages that only reorder scripts are otherwise root-like and have |
| 1092 |
// null base. |
| 1093 |
if (data->base || isRoot) { |
| 1094 |
tailored = !isRoot; |
| 1095 |
writeCollationDataTOML(outputdir, name, collationType, data, (!data->base && isRoot), diacriticLimit, status); |
| 1096 |
if (U_FAILURE(*status)) { |
| 1097 |
return; |
| 1098 |
} |
| 1099 |
} |
| 1100 |
|
| 1101 |
uint32_t maxVariable = static_cast<uint32_t>(settings->getMaxVariable()); |
| 1102 |
if (maxVariable >= 4) { |
| 1103 |
printf("Max variable out of range"); |
| 1104 |
*status = U_INTERNAL_PROGRAM_ERROR; |
| 1105 |
return; |
| 1106 |
} |
| 1107 |
|
| 1108 |
uint32_t metadataBits = maxVariable; |
| 1109 |
if (tailored) { |
| 1110 |
metadataBits |= (1 << 3); |
| 1111 |
} |
| 1112 |
if (tailoredDiacritics) { |
| 1113 |
metadataBits |= (1 << 4); |
| 1114 |
} |
| 1115 |
if (reordering) { |
| 1116 |
metadataBits |= (1 << 5); |
| 1117 |
} |
| 1118 |
if (lithuanianDotAbove) { |
| 1119 |
metadataBits |= (1 << 6); |
| 1120 |
} |
| 1121 |
if ((settings->options & icu::CollationSettings::BACKWARD_SECONDARY) != 0) { |
| 1122 |
metadataBits |= (1 << 7); |
| 1123 |
} |
| 1124 |
if (settings->getAlternateHandling() == UCOL_SHIFTED) { |
| 1125 |
metadataBits |= (1 << 8); |
| 1126 |
} |
| 1127 |
switch (settings->getCaseFirst()) { |
| 1128 |
case UCOL_OFF: |
| 1129 |
break; |
| 1130 |
case UCOL_UPPER_FIRST: |
| 1131 |
metadataBits |= (1 << 9); |
| 1132 |
metadataBits |= (1 << 10); |
| 1133 |
break; |
| 1134 |
case UCOL_LOWER_FIRST: |
| 1135 |
metadataBits |= (1 << 9); |
| 1136 |
break; |
| 1137 |
default: |
| 1138 |
*status = U_INTERNAL_PROGRAM_ERROR; |
| 1139 |
return; |
| 1140 |
} |
| 1141 |
|
| 1142 |
writeCollationMetadataTOML(outputdir, name, collationType, metadataBits, status); |
| 1143 |
} |
| 1144 |
|
| 1145 |
#endif // !UCONFIG_NO_COLLATION |
| 1146 |
|
| 1147 |
static TableResource * |
| 1148 |
addCollation(ParseState* state, TableResource *result, const char *collationType, |
| 1149 |
uint32_t startline, UErrorCode *status) |
| 1150 |
{ |
| 1151 |
// TODO: Use LocalPointer for result, or make caller close it when there is a failure. |
| 1152 |
struct SResource *member = nullptr; |
| 1153 |
struct UString *tokenValue; |
| 1154 |
struct UString comment; |
| 1155 |
enum ETokenType token; |
| 1156 |
CharString subtag; |
| 1157 |
UnicodeString rules; |
| 1158 |
UBool haveRules = false; |
| 1159 |
UVersionInfo version; |
| 1160 |
uint32_t line; |
| 1161 |
|
| 1162 |
/* '{' . (name resource)* '}' */ |
| 1163 |
version[0]=0; version[1]=0; version[2]=0; version[3]=0; |
| 1164 |
|
| 1165 |
for (;;) |
| 1166 |
{ |
| 1167 |
ustr_init(&comment); |
| 1168 |
token = getToken(state, &tokenValue, &comment, &line, status); |
| 1169 |
|
| 1170 |
if (token == TOK_CLOSE_BRACE) |
| 1171 |
{ |
| 1172 |
break; |
| 1173 |
} |
| 1174 |
|
| 1175 |
if (token != TOK_STRING) |
| 1176 |
{ |
| 1177 |
res_close(result); |
| 1178 |
*status = U_INVALID_FORMAT_ERROR; |
| 1179 |
|
| 1180 |
if (token == TOK_EOF) |
| 1181 |
{ |
| 1182 |
error(startline, "unterminated table"); |
| 1183 |
} |
| 1184 |
else |
| 1185 |
{ |
| 1186 |
error(line, "Unexpected token %s", tokenNames[token]); |
| 1187 |
} |
| 1188 |
|
| 1189 |
return nullptr; |
| 1190 |
} |
| 1191 |
|
| 1192 |
subtag.clear(); |
| 1193 |
subtag.appendInvariantChars(tokenValue->fChars, u_strlen(tokenValue->fChars), *status); |
| 1194 |
if (U_FAILURE(*status)) |
| 1195 |
{ |
| 1196 |
res_close(result); |
| 1197 |
return nullptr; |
| 1198 |
} |
| 1199 |
|
| 1200 |
member = parseResource(state, subtag.data(), nullptr, status); |
| 1201 |
|
| 1202 |
if (U_FAILURE(*status)) |
| 1203 |
{ |
| 1204 |
res_close(result); |
| 1205 |
return nullptr; |
| 1206 |
} |
| 1207 |
if (result == nullptr) |
| 1208 |
{ |
| 1209 |
// Ignore the parsed resources, continue parsing. |
| 1210 |
} |
| 1211 |
else if (uprv_strcmp(subtag.data(), "Version") == 0 && member->isString()) |
| 1212 |
{ |
| 1213 |
StringResource *sr = static_cast<StringResource *>(member); |
| 1214 |
char ver[40]; |
| 1215 |
int32_t length = sr->length(); |
| 1216 |
|
| 1217 |
if (length >= UPRV_LENGTHOF(ver)) |
| 1218 |
{ |
| 1219 |
length = UPRV_LENGTHOF(ver) - 1; |
| 1220 |
} |
| 1221 |
|
| 1222 |
sr->fString.extract(0, length, ver, UPRV_LENGTHOF(ver), US_INV); |
| 1223 |
u_versionFromString(version, ver); |
| 1224 |
|
| 1225 |
result->add(member, line, *status); |
| 1226 |
member = nullptr; |
| 1227 |
} |
| 1228 |
else if(uprv_strcmp(subtag.data(), "%%CollationBin")==0) |
| 1229 |
{ |
| 1230 |
/* discard duplicate %%CollationBin if any*/ |
| 1231 |
} |
| 1232 |
else if (uprv_strcmp(subtag.data(), "Sequence") == 0 && member->isString()) |
| 1233 |
{ |
| 1234 |
StringResource *sr = static_cast<StringResource *>(member); |
| 1235 |
rules = sr->fString; |
| 1236 |
haveRules = true; |
| 1237 |
// Defer building the collator until we have seen |
| 1238 |
// all sub-elements of the collation table, including the Version. |
| 1239 |
/* in order to achieve smaller data files, we can direct genrb */ |
| 1240 |
/* to omit collation rules */ |
| 1241 |
if(!state->omitCollationRules) { |
| 1242 |
result->add(member, line, *status); |
| 1243 |
member = nullptr; |
| 1244 |
} |
| 1245 |
} |
| 1246 |
else // Just copy non-special items. |
| 1247 |
{ |
| 1248 |
result->add(member, line, *status); |
| 1249 |
member = nullptr; |
| 1250 |
} |
| 1251 |
res_close(member); // TODO: use LocalPointer |
| 1252 |
if (U_FAILURE(*status)) |
| 1253 |
{ |
| 1254 |
res_close(result); |
| 1255 |
return nullptr; |
| 1256 |
} |
| 1257 |
} |
| 1258 |
|
| 1259 |
if (!haveRules) { return result; } |
| 1260 |
|
| 1261 |
#if UCONFIG_NO_COLLATION || UCONFIG_NO_FILE_IO |
| 1262 |
warning(line, "Not building collation elements because of UCONFIG_NO_COLLATION and/or UCONFIG_NO_FILE_IO, see uconfig.h"); |
| 1263 |
(void)collationType; |
| 1264 |
#else |
| 1265 |
// CLDR ticket #3949, ICU ticket #8082: |
| 1266 |
// Do not build collation binary data for for-import-only "private" collation rule strings. |
| 1267 |
if (uprv_strncmp(collationType, "private-", 8) == 0) { |
| 1268 |
if(isVerbose()) { |
| 1269 |
printf("Not building %s~%s collation binary\n", state->filename, collationType); |
| 1270 |
} |
| 1271 |
return result; |
| 1272 |
} |
| 1273 |
|
| 1274 |
if(!state->makeBinaryCollation) { |
| 1275 |
if(isVerbose()) { |
| 1276 |
printf("Not building %s~%s collation binary\n", state->filename, collationType); |
| 1277 |
} |
| 1278 |
return result; |
| 1279 |
} |
| 1280 |
UErrorCode intStatus = U_ZERO_ERROR; |
| 1281 |
UParseError parseError; |
| 1282 |
uprv_memset(&parseError, 0, sizeof(parseError)); |
| 1283 |
GenrbImporter importer(state->inputdir, state->outputdir); |
| 1284 |
const icu::CollationTailoring *base = icu::CollationRoot::getRoot(intStatus); |
| 1285 |
if(U_FAILURE(intStatus)) { |
| 1286 |
error(line, "failed to load root collator (ucadata.icu) - %s", u_errorName(intStatus)); |
| 1287 |
res_close(result); |
| 1288 |
return nullptr; // TODO: use LocalUResourceBundlePointer for result |
| 1289 |
} |
| 1290 |
icu::CollationBuilder builder(base, state->icu4xMode, intStatus); |
| 1291 |
if(state->icu4xMode || (uprv_strncmp(collationType, "search", 6) == 0)) { |
| 1292 |
builder.disableFastLatin(); // build fast-Latin table unless search collator or ICU4X |
| 1293 |
} |
| 1294 |
LocalPointer<icu::CollationTailoring> t( |
| 1295 |
builder.parseAndBuild(rules, version, &importer, &parseError, intStatus)); |
| 1296 |
if(U_FAILURE(intStatus)) { |
| 1297 |
const char *reason = builder.getErrorReason(); |
| 1298 |
if(reason == nullptr) { reason = ""; } |
| 1299 |
error(line, "CollationBuilder failed at %s~%s/Sequence rule offset %ld: %s %s", |
| 1300 |
state->filename, collationType, |
| 1301 |
static_cast<long>(parseError.offset), u_errorName(intStatus), reason); |
| 1302 |
if(parseError.preContext[0] != 0 || parseError.postContext[0] != 0) { |
| 1303 |
// Print pre- and post-context. |
| 1304 |
char preBuffer[100], postBuffer[100]; |
| 1305 |
escape(parseError.preContext, preBuffer, sizeof(preBuffer)); |
| 1306 |
escape(parseError.postContext, postBuffer, sizeof(postBuffer)); |
| 1307 |
error(line, " error context: \"...%s\" ! \"%s...\"", preBuffer, postBuffer); |
| 1308 |
} |
| 1309 |
if(isStrict() || t.isNull()) { |
| 1310 |
*status = intStatus; |
| 1311 |
res_close(result); |
| 1312 |
return nullptr; |
| 1313 |
} |
| 1314 |
} |
| 1315 |
if (state->icu4xMode) { |
| 1316 |
char *nameWithoutSuffix = static_cast<char *>(uprv_malloc(uprv_strlen(state->filename) + 1)); |
| 1317 |
if (nameWithoutSuffix == nullptr) { |
| 1318 |
*status = U_MEMORY_ALLOCATION_ERROR; |
| 1319 |
res_close(result); |
| 1320 |
return nullptr; |
| 1321 |
} |
| 1322 |
uprv_strcpy(nameWithoutSuffix, state->filename); |
| 1323 |
*uprv_strrchr(nameWithoutSuffix, '.') = 0; |
| 1324 |
|
| 1325 |
writeCollationTOML(state->outputdir, nameWithoutSuffix, collationType, t->data, t->settings, status); |
| 1326 |
uprv_free(nameWithoutSuffix); |
| 1327 |
} |
| 1328 |
icu::LocalMemory<uint8_t> buffer; |
| 1329 |
int32_t capacity = 100000; |
| 1330 |
uint8_t *dest = buffer.allocateInsteadAndCopy(capacity); |
| 1331 |
if(dest == nullptr) { |
| 1332 |
fprintf(stderr, "memory allocation (%ld bytes) for file contents failed\n", |
| 1333 |
static_cast<long>(capacity)); |
| 1334 |
*status = U_MEMORY_ALLOCATION_ERROR; |
| 1335 |
res_close(result); |
| 1336 |
return nullptr; |
| 1337 |
} |
| 1338 |
int32_t indexes[icu::CollationDataReader::IX_TOTAL_SIZE + 1]; |
| 1339 |
int32_t totalSize = icu::CollationDataWriter::writeTailoring( |
| 1340 |
*t, *t->settings, indexes, dest, capacity, intStatus); |
| 1341 |
if(intStatus == U_BUFFER_OVERFLOW_ERROR) { |
| 1342 |
intStatus = U_ZERO_ERROR; |
| 1343 |
capacity = totalSize; |
| 1344 |
dest = buffer.allocateInsteadAndCopy(capacity); |
| 1345 |
if(dest == nullptr) { |
| 1346 |
fprintf(stderr, "memory allocation (%ld bytes) for file contents failed\n", |
| 1347 |
static_cast<long>(capacity)); |
| 1348 |
*status = U_MEMORY_ALLOCATION_ERROR; |
| 1349 |
res_close(result); |
| 1350 |
return nullptr; |
| 1351 |
} |
| 1352 |
totalSize = icu::CollationDataWriter::writeTailoring( |
| 1353 |
*t, *t->settings, indexes, dest, capacity, intStatus); |
| 1354 |
} |
| 1355 |
if(U_FAILURE(intStatus)) { |
| 1356 |
fprintf(stderr, "CollationDataWriter::writeTailoring() failed: %s\n", |
| 1357 |
u_errorName(intStatus)); |
| 1358 |
res_close(result); |
| 1359 |
return nullptr; |
| 1360 |
} |
| 1361 |
if(isVerbose()) { |
| 1362 |
printf("%s~%s collation tailoring part sizes:\n", state->filename, collationType); |
| 1363 |
icu::CollationInfo::printSizes(totalSize, indexes); |
| 1364 |
if(t->settings->hasReordering()) { |
| 1365 |
printf("%s~%s collation reordering ranges:\n", state->filename, collationType); |
| 1366 |
icu::CollationInfo::printReorderRanges( |
| 1367 |
*t->data, t->settings->reorderCodes, t->settings->reorderCodesLength); |
| 1368 |
} |
| 1369 |
#if 0 // debugging output |
| 1370 |
} else { |
| 1371 |
printf("%s~%s collation tailoring part sizes:\n", state->filename, collationType); |
| 1372 |
icu::CollationInfo::printSizes(totalSize, indexes); |
| 1373 |
#endif |
| 1374 |
} |
| 1375 |
struct SResource *collationBin = bin_open(state->bundle, "%%CollationBin", totalSize, dest, nullptr, nullptr, status); |
| 1376 |
result->add(collationBin, line, *status); |
| 1377 |
if (U_FAILURE(*status)) { |
| 1378 |
res_close(result); |
| 1379 |
return nullptr; |
| 1380 |
} |
| 1381 |
#endif |
| 1382 |
return result; |
| 1383 |
} |
| 1384 |
|
| 1385 |
static UBool |
| 1386 |
keepCollationType(const char * /*type*/) { |
| 1387 |
return true; |
| 1388 |
} |
| 1389 |
|
| 1390 |
static struct SResource * |
| 1391 |
parseCollationElements(ParseState* state, char *tag, uint32_t startline, UBool newCollation, UErrorCode *status) |
| 1392 |
{ |
| 1393 |
TableResource *result = nullptr; |
| 1394 |
struct SResource *member = nullptr; |
| 1395 |
struct UString *tokenValue; |
| 1396 |
struct UString comment; |
| 1397 |
enum ETokenType token; |
| 1398 |
CharString subtag, typeKeyword; |
| 1399 |
uint32_t line; |
| 1400 |
|
| 1401 |
result = table_open(state->bundle, tag, nullptr, status); |
| 1402 |
|
| 1403 |
if (result == nullptr || U_FAILURE(*status)) |
| 1404 |
{ |
| 1405 |
return nullptr; |
| 1406 |
} |
| 1407 |
if(isVerbose()){ |
| 1408 |
printf(" collation elements %s at line %i \n", tag == nullptr ? "(null)" : tag, static_cast<int>(startline)); |
| 1409 |
} |
| 1410 |
if(!newCollation) { |
| 1411 |
return addCollation(state, result, "(no type)", startline, status); |
| 1412 |
} |
| 1413 |
else { |
| 1414 |
for(;;) { |
| 1415 |
ustr_init(&comment); |
| 1416 |
token = getToken(state, &tokenValue, &comment, &line, status); |
| 1417 |
|
| 1418 |
if (token == TOK_CLOSE_BRACE) |
| 1419 |
{ |
| 1420 |
return result; |
| 1421 |
} |
| 1422 |
|
| 1423 |
if (token != TOK_STRING) |
| 1424 |
{ |
| 1425 |
res_close(result); |
| 1426 |
*status = U_INVALID_FORMAT_ERROR; |
| 1427 |
|
| 1428 |
if (token == TOK_EOF) |
| 1429 |
{ |
| 1430 |
error(startline, "unterminated table"); |
| 1431 |
} |
| 1432 |
else |
| 1433 |
{ |
| 1434 |
error(line, "Unexpected token %s", tokenNames[token]); |
| 1435 |
} |
| 1436 |
|
| 1437 |
return nullptr; |
| 1438 |
} |
| 1439 |
|
| 1440 |
subtag.clear(); |
| 1441 |
subtag.appendInvariantChars(tokenValue->fChars, u_strlen(tokenValue->fChars), *status); |
| 1442 |
|
| 1443 |
if (U_FAILURE(*status)) |
| 1444 |
{ |
| 1445 |
res_close(result); |
| 1446 |
return nullptr; |
| 1447 |
} |
| 1448 |
|
| 1449 |
if (uprv_strcmp(subtag.data(), "default") == 0) |
| 1450 |
{ |
| 1451 |
member = parseResource(state, subtag.data(), nullptr, status); |
| 1452 |
|
| 1453 |
if (U_FAILURE(*status)) |
| 1454 |
{ |
| 1455 |
res_close(result); |
| 1456 |
return nullptr; |
| 1457 |
} |
| 1458 |
|
| 1459 |
result->add(member, line, *status); |
| 1460 |
} |
| 1461 |
else |
| 1462 |
{ |
| 1463 |
token = peekToken(state, 0, &tokenValue, &line, &comment, status); |
| 1464 |
/* this probably needs to be refactored or recursively use the parser */ |
| 1465 |
/* first we assume that our collation table won't have the explicit type */ |
| 1466 |
/* then, we cannot handle aliases */ |
| 1467 |
if(token == TOK_OPEN_BRACE) { |
| 1468 |
token = getToken(state, &tokenValue, &comment, &line, status); |
| 1469 |
TableResource *collationRes; |
| 1470 |
if (keepCollationType(subtag.data())) { |
| 1471 |
collationRes = table_open(state->bundle, subtag.data(), nullptr, status); |
| 1472 |
} else { |
| 1473 |
collationRes = nullptr; |
| 1474 |
} |
| 1475 |
// need to parse the collation data regardless |
| 1476 |
collationRes = addCollation(state, collationRes, subtag.data(), startline, status); |
| 1477 |
if (collationRes != nullptr) { |
| 1478 |
result->add(collationRes, startline, *status); |
| 1479 |
} |
| 1480 |
} else if(token == TOK_COLON) { /* right now, we'll just try to see if we have aliases */ |
| 1481 |
/* we could have a table too */ |
| 1482 |
token = peekToken(state, 1, &tokenValue, &line, &comment, status); |
| 1483 |
typeKeyword.clear(); |
| 1484 |
typeKeyword.appendInvariantChars(tokenValue->fChars, u_strlen(tokenValue->fChars), *status); |
| 1485 |
if (U_FAILURE(*status)) |
| 1486 |
{ |
| 1487 |
res_close(result); |
| 1488 |
return nullptr; |
| 1489 |
} |
| 1490 |
|
| 1491 |
if(uprv_strcmp(typeKeyword.data(), "alias") == 0) { |
| 1492 |
member = parseResource(state, subtag.data(), nullptr, status); |
| 1493 |
if (U_FAILURE(*status)) |
| 1494 |
{ |
| 1495 |
res_close(result); |
| 1496 |
return nullptr; |
| 1497 |
} |
| 1498 |
|
| 1499 |
result->add(member, line, *status); |
| 1500 |
} else { |
| 1501 |
res_close(result); |
| 1502 |
*status = U_INVALID_FORMAT_ERROR; |
| 1503 |
return nullptr; |
| 1504 |
} |
| 1505 |
} else { |
| 1506 |
res_close(result); |
| 1507 |
*status = U_INVALID_FORMAT_ERROR; |
| 1508 |
return nullptr; |
| 1509 |
} |
| 1510 |
} |
| 1511 |
|
| 1512 |
/*member = string_open(bundle, subtag, tokenValue->fChars, tokenValue->fLength, status);*/ |
| 1513 |
|
| 1514 |
/*expect(TOK_CLOSE_BRACE, nullptr, nullptr, status);*/ |
| 1515 |
|
| 1516 |
if (U_FAILURE(*status)) |
| 1517 |
{ |
| 1518 |
res_close(result); |
| 1519 |
return nullptr; |
| 1520 |
} |
| 1521 |
} |
| 1522 |
} |
| 1523 |
} |
| 1524 |
|
| 1525 |
/* Necessary, because CollationElements requires the bundle->fRoot member to be present which, |
| 1526 |
if this weren't special-cased, wouldn't be set until the entire file had been processed. */ |
| 1527 |
static struct SResource * |
| 1528 |
realParseTable(ParseState* state, TableResource *table, char *tag, uint32_t startline, UErrorCode *status) |
| 1529 |
{ |
| 1530 |
struct SResource *member = nullptr; |
| 1531 |
struct UString *tokenValue=nullptr; |
| 1532 |
struct UString comment; |
| 1533 |
enum ETokenType token; |
| 1534 |
CharString subtag; |
| 1535 |
uint32_t line; |
| 1536 |
UBool readToken = false; |
| 1537 |
|
| 1538 |
/* '{' . (name resource)* '}' */ |
| 1539 |
|
| 1540 |
if(isVerbose()){ |
| 1541 |
printf(" parsing table %s at line %i \n", tag == nullptr ? "(null)" : tag, static_cast<int>(startline)); |
| 1542 |
} |
| 1543 |
for (;;) |
| 1544 |
{ |
| 1545 |
ustr_init(&comment); |
| 1546 |
token = getToken(state, &tokenValue, &comment, &line, status); |
| 1547 |
|
| 1548 |
if (token == TOK_CLOSE_BRACE) |
| 1549 |
{ |
| 1550 |
if (!readToken && isVerbose()) { |
| 1551 |
warning(startline, "Encountered empty table"); |
| 1552 |
} |
| 1553 |
return table; |
| 1554 |
} |
| 1555 |
|
| 1556 |
if (token != TOK_STRING) |
| 1557 |
{ |
| 1558 |
*status = U_INVALID_FORMAT_ERROR; |
| 1559 |
|
| 1560 |
if (token == TOK_EOF) |
| 1561 |
{ |
| 1562 |
error(startline, "unterminated table"); |
| 1563 |
} |
| 1564 |
else |
| 1565 |
{ |
| 1566 |
error(line, "unexpected token %s", tokenNames[token]); |
| 1567 |
} |
| 1568 |
|
| 1569 |
return nullptr; |
| 1570 |
} |
| 1571 |
|
| 1572 |
if(uprv_isInvariantUString(tokenValue->fChars, -1)) { |
| 1573 |
subtag.clear(); |
| 1574 |
subtag.appendInvariantChars(tokenValue->fChars, u_strlen(tokenValue->fChars), *status); |
| 1575 |
} else { |
| 1576 |
*status = U_INVALID_FORMAT_ERROR; |
| 1577 |
error(line, "invariant characters required for table keys"); |
| 1578 |
return nullptr; |
| 1579 |
} |
| 1580 |
|
| 1581 |
if (U_FAILURE(*status)) |
| 1582 |
{ |
| 1583 |
error(line, "parse error. Stopped parsing tokens with %s", u_errorName(*status)); |
| 1584 |
return nullptr; |
| 1585 |
} |
| 1586 |
|
| 1587 |
member = parseResource(state, subtag.data(), &comment, status); |
| 1588 |
|
| 1589 |
if (member == nullptr || U_FAILURE(*status)) |
| 1590 |
{ |
| 1591 |
error(line, "parse error. Stopped parsing resource with %s", u_errorName(*status)); |
| 1592 |
return nullptr; |
| 1593 |
} |
| 1594 |
|
| 1595 |
table->add(member, line, *status); |
| 1596 |
|
| 1597 |
if (U_FAILURE(*status)) |
| 1598 |
{ |
| 1599 |
error(line, "parse error. Stopped parsing table with %s", u_errorName(*status)); |
| 1600 |
return nullptr; |
| 1601 |
} |
| 1602 |
readToken = true; |
| 1603 |
ustr_deinit(&comment); |
| 1604 |
} |
| 1605 |
|
| 1606 |
/* not reached */ |
| 1607 |
/* A compiler warning will appear if all paths don't contain a return statement. */ |
| 1608 |
/* *status = U_INTERNAL_PROGRAM_ERROR; |
| 1609 |
return nullptr;*/ |
| 1610 |
} |
| 1611 |
|
| 1612 |
static struct SResource * |
| 1613 |
parseTable(ParseState* state, char *tag, uint32_t startline, const struct UString *comment, UErrorCode *status) |
| 1614 |
{ |
| 1615 |
if (tag != nullptr && uprv_strcmp(tag, "CollationElements") == 0) |
| 1616 |
{ |
| 1617 |
return parseCollationElements(state, tag, startline, false, status); |
| 1618 |
} |
| 1619 |
if (tag != nullptr && uprv_strcmp(tag, "collations") == 0) |
| 1620 |
{ |
| 1621 |
return parseCollationElements(state, tag, startline, true, status); |
| 1622 |
} |
| 1623 |
if(isVerbose()){ |
| 1624 |
printf(" table %s at line %i \n", tag == nullptr ? "(null)" : tag, static_cast<int>(startline)); |
| 1625 |
} |
| 1626 |
|
| 1627 |
TableResource *result = table_open(state->bundle, tag, comment, status); |
| 1628 |
|
| 1629 |
if (result == nullptr || U_FAILURE(*status)) |
| 1630 |
{ |
| 1631 |
return nullptr; |
| 1632 |
} |
| 1633 |
return realParseTable(state, result, tag, startline, status); |
| 1634 |
} |
| 1635 |
|
| 1636 |
static struct SResource * |
| 1637 |
parseArray(ParseState* state, char *tag, uint32_t startline, const struct UString *comment, UErrorCode *status) |
| 1638 |
{ |
| 1639 |
struct SResource *member = nullptr; |
| 1640 |
struct UString *tokenValue; |
| 1641 |
struct UString memberComments; |
| 1642 |
enum ETokenType token; |
| 1643 |
UBool readToken = false; |
| 1644 |
|
| 1645 |
ArrayResource *result = array_open(state->bundle, tag, comment, status); |
| 1646 |
|
| 1647 |
if (result == nullptr || U_FAILURE(*status)) |
| 1648 |
{ |
| 1649 |
return nullptr; |
| 1650 |
} |
| 1651 |
if(isVerbose()){ |
| 1652 |
printf(" array %s at line %i \n", tag == nullptr ? "(null)" : tag, static_cast<int>(startline)); |
| 1653 |
} |
| 1654 |
|
| 1655 |
ustr_init(&memberComments); |
| 1656 |
|
| 1657 |
/* '{' . resource [','] '}' */ |
| 1658 |
for (;;) |
| 1659 |
{ |
| 1660 |
/* reset length */ |
| 1661 |
ustr_setlen(&memberComments, 0, status); |
| 1662 |
|
| 1663 |
/* check for end of array, but don't consume next token unless it really is the end */ |
| 1664 |
token = peekToken(state, 0, &tokenValue, nullptr, &memberComments, status); |
| 1665 |
|
| 1666 |
|
| 1667 |
if (token == TOK_CLOSE_BRACE) |
| 1668 |
{ |
| 1669 |
getToken(state, nullptr, nullptr, nullptr, status); |
| 1670 |
if (!readToken) { |
| 1671 |
warning(startline, "Encountered empty array"); |
| 1672 |
} |
| 1673 |
break; |
| 1674 |
} |
| 1675 |
|
| 1676 |
if (token == TOK_EOF) |
| 1677 |
{ |
| 1678 |
res_close(result); |
| 1679 |
*status = U_INVALID_FORMAT_ERROR; |
| 1680 |
error(startline, "unterminated array"); |
| 1681 |
return nullptr; |
| 1682 |
} |
| 1683 |
|
| 1684 |
/* string arrays are a special case */ |
| 1685 |
if (token == TOK_STRING) |
| 1686 |
{ |
| 1687 |
getToken(state, &tokenValue, &memberComments, nullptr, status); |
| 1688 |
member = string_open(state->bundle, nullptr, tokenValue->fChars, tokenValue->fLength, &memberComments, status); |
| 1689 |
} |
| 1690 |
else |
| 1691 |
{ |
| 1692 |
member = parseResource(state, nullptr, &memberComments, status); |
| 1693 |
} |
| 1694 |
|
| 1695 |
if (member == nullptr || U_FAILURE(*status)) |
| 1696 |
{ |
| 1697 |
res_close(result); |
| 1698 |
return nullptr; |
| 1699 |
} |
| 1700 |
|
| 1701 |
result->add(member); |
| 1702 |
|
| 1703 |
/* eat optional comma if present */ |
| 1704 |
token = peekToken(state, 0, nullptr, nullptr, nullptr, status); |
| 1705 |
|
| 1706 |
if (token == TOK_COMMA) |
| 1707 |
{ |
| 1708 |
getToken(state, nullptr, nullptr, nullptr, status); |
| 1709 |
} |
| 1710 |
|
| 1711 |
if (U_FAILURE(*status)) |
| 1712 |
{ |
| 1713 |
res_close(result); |
| 1714 |
return nullptr; |
| 1715 |
} |
| 1716 |
readToken = true; |
| 1717 |
} |
| 1718 |
|
| 1719 |
ustr_deinit(&memberComments); |
| 1720 |
return result; |
| 1721 |
} |
| 1722 |
|
| 1723 |
static struct SResource * |
| 1724 |
parseIntVector(ParseState* state, char *tag, uint32_t startline, const struct UString *comment, UErrorCode *status) |
| 1725 |
{ |
| 1726 |
enum ETokenType token; |
| 1727 |
char *string; |
| 1728 |
int32_t value; |
| 1729 |
UBool readToken = false; |
| 1730 |
char *stopstring; |
| 1731 |
struct UString memberComments; |
| 1732 |
|
| 1733 |
IntVectorResource *result = intvector_open(state->bundle, tag, comment, status); |
| 1734 |
|
| 1735 |
if (result == nullptr || U_FAILURE(*status)) |
| 1736 |
{ |
| 1737 |
return nullptr; |
| 1738 |
} |
| 1739 |
|
| 1740 |
if(isVerbose()){ |
| 1741 |
printf(" vector %s at line %i \n", tag == nullptr ? "(null)" : tag, static_cast<int>(startline)); |
| 1742 |
} |
| 1743 |
ustr_init(&memberComments); |
| 1744 |
/* '{' . string [','] '}' */ |
| 1745 |
for (;;) |
| 1746 |
{ |
| 1747 |
ustr_setlen(&memberComments, 0, status); |
| 1748 |
|
| 1749 |
/* check for end of array, but don't consume next token unless it really is the end */ |
| 1750 |
token = peekToken(state, 0, nullptr, nullptr,&memberComments, status); |
| 1751 |
|
| 1752 |
if (token == TOK_CLOSE_BRACE) |
| 1753 |
{ |
| 1754 |
/* it's the end, consume the close brace */ |
| 1755 |
getToken(state, nullptr, nullptr, nullptr, status); |
| 1756 |
if (!readToken) { |
| 1757 |
warning(startline, "Encountered empty int vector"); |
| 1758 |
} |
| 1759 |
ustr_deinit(&memberComments); |
| 1760 |
return result; |
| 1761 |
} |
| 1762 |
|
| 1763 |
int32_t stringLength; |
| 1764 |
string = getInvariantString(state, nullptr, nullptr, stringLength, status); |
| 1765 |
|
| 1766 |
if (U_FAILURE(*status)) |
| 1767 |
{ |
| 1768 |
res_close(result); |
| 1769 |
return nullptr; |
| 1770 |
} |
| 1771 |
|
| 1772 |
/* For handling illegal char in the Intvector */ |
| 1773 |
value = uprv_strtoul(string, &stopstring, 0);/* make intvector support decimal,hexdigit,octal digit ranging from -2^31-2^32-1*/ |
| 1774 |
int32_t len = static_cast<int32_t>(stopstring - string); |
| 1775 |
|
| 1776 |
if(len==stringLength) |
| 1777 |
{ |
| 1778 |
result->add(value, *status); |
| 1779 |
uprv_free(string); |
| 1780 |
token = peekToken(state, 0, nullptr, nullptr, nullptr, status); |
| 1781 |
} |
| 1782 |
else |
| 1783 |
{ |
| 1784 |
uprv_free(string); |
| 1785 |
*status=U_INVALID_CHAR_FOUND; |
| 1786 |
} |
| 1787 |
|
| 1788 |
if (U_FAILURE(*status)) |
| 1789 |
{ |
| 1790 |
res_close(result); |
| 1791 |
return nullptr; |
| 1792 |
} |
| 1793 |
|
| 1794 |
/* the comma is optional (even though it is required to prevent the reader from concatenating |
| 1795 |
consecutive entries) so that a missing comma on the last entry isn't an error */ |
| 1796 |
if (token == TOK_COMMA) |
| 1797 |
{ |
| 1798 |
getToken(state, nullptr, nullptr, nullptr, status); |
| 1799 |
} |
| 1800 |
readToken = true; |
| 1801 |
} |
| 1802 |
|
| 1803 |
/* not reached */ |
| 1804 |
/* A compiler warning will appear if all paths don't contain a return statement. */ |
| 1805 |
/* intvector_close(result, status); |
| 1806 |
*status = U_INTERNAL_PROGRAM_ERROR; |
| 1807 |
return nullptr;*/ |
| 1808 |
} |
| 1809 |
|
| 1810 |
static struct SResource * |
| 1811 |
parseBinary(ParseState* state, char *tag, uint32_t startline, const struct UString *comment, UErrorCode *status) |
| 1812 |
{ |
| 1813 |
uint32_t line; |
| 1814 |
int32_t stringLength; |
| 1815 |
LocalMemory<char> string(getInvariantString(state, &line, nullptr, stringLength, status)); |
| 1816 |
if (string.isNull() || U_FAILURE(*status)) |
| 1817 |
{ |
| 1818 |
return nullptr; |
| 1819 |
} |
| 1820 |
|
| 1821 |
expect(state, TOK_CLOSE_BRACE, nullptr, nullptr, nullptr, status); |
| 1822 |
if (U_FAILURE(*status)) |
| 1823 |
{ |
| 1824 |
return nullptr; |
| 1825 |
} |
| 1826 |
|
| 1827 |
if(isVerbose()){ |
| 1828 |
printf(" binary %s at line %i \n", tag == nullptr ? "(null)" : tag, static_cast<int>(startline)); |
| 1829 |
} |
| 1830 |
|
| 1831 |
LocalMemory<uint8_t> value; |
| 1832 |
int32_t count = 0; |
| 1833 |
if (stringLength > 0 && value.allocateInsteadAndCopy(stringLength) == nullptr) |
| 1834 |
{ |
| 1835 |
*status = U_MEMORY_ALLOCATION_ERROR; |
| 1836 |
return nullptr; |
| 1837 |
} |
| 1838 |
|
| 1839 |
char toConv[3] = {'\0', '\0', '\0'}; |
| 1840 |
for (int32_t i = 0; i < stringLength;) |
| 1841 |
{ |
| 1842 |
// Skip spaces (which may have been line endings). |
| 1843 |
char c0 = string[i++]; |
| 1844 |
if (c0 == ' ') { continue; } |
| 1845 |
if (i == stringLength) { |
| 1846 |
*status=U_INVALID_CHAR_FOUND; |
| 1847 |
error(line, "Encountered invalid binary value (odd number of hex digits)"); |
| 1848 |
return nullptr; |
| 1849 |
} |
| 1850 |
toConv[0] = c0; |
| 1851 |
toConv[1] = string[i++]; |
| 1852 |
|
| 1853 |
char *stopstring; |
| 1854 |
value[count++] = static_cast<uint8_t>(uprv_strtoul(toConv, &stopstring, 16)); |
| 1855 |
uint32_t len = static_cast<uint32_t>(stopstring - toConv); |
| 1856 |
|
| 1857 |
if(len!=2) |
| 1858 |
{ |
| 1859 |
*status=U_INVALID_CHAR_FOUND; |
| 1860 |
error(line, "Encountered invalid binary value (not all pairs of hex digits)"); |
| 1861 |
return nullptr; |
| 1862 |
} |
| 1863 |
} |
| 1864 |
|
| 1865 |
if (count == 0) { |
| 1866 |
warning(startline, "Encountered empty binary value"); |
| 1867 |
return bin_open(state->bundle, tag, 0, nullptr, "", comment, status); |
| 1868 |
} else { |
| 1869 |
return bin_open(state->bundle, tag, count, value.getAlias(), nullptr, comment, status); |
| 1870 |
} |
| 1871 |
} |
| 1872 |
|
| 1873 |
static struct SResource * |
| 1874 |
parseInteger(ParseState* state, char *tag, uint32_t startline, const struct UString *comment, UErrorCode *status) |
| 1875 |
{ |
| 1876 |
struct SResource *result = nullptr; |
| 1877 |
int32_t value; |
| 1878 |
char *string; |
| 1879 |
char *stopstring; |
| 1880 |
|
| 1881 |
int32_t stringLength; |
| 1882 |
string = getInvariantString(state, nullptr, nullptr, stringLength, status); |
| 1883 |
|
| 1884 |
if (string == nullptr || U_FAILURE(*status)) |
| 1885 |
{ |
| 1886 |
return nullptr; |
| 1887 |
} |
| 1888 |
|
| 1889 |
expect(state, TOK_CLOSE_BRACE, nullptr, nullptr, nullptr, status); |
| 1890 |
|
| 1891 |
if (U_FAILURE(*status)) |
| 1892 |
{ |
| 1893 |
uprv_free(string); |
| 1894 |
return nullptr; |
| 1895 |
} |
| 1896 |
|
| 1897 |
if(isVerbose()){ |
| 1898 |
printf(" integer %s at line %i \n", tag == nullptr ? "(null)" : tag, static_cast<int>(startline)); |
| 1899 |
} |
| 1900 |
|
| 1901 |
if (stringLength == 0) |
| 1902 |
{ |
| 1903 |
warning(startline, "Encountered empty integer. Default value is 0."); |
| 1904 |
} |
| 1905 |
|
| 1906 |
/* Allow integer support for hexdecimal, octal digit and decimal*/ |
| 1907 |
/* and handle illegal char in the integer*/ |
| 1908 |
value = uprv_strtoul(string, &stopstring, 0); |
| 1909 |
int32_t len = static_cast<int32_t>(stopstring - string); |
| 1910 |
if(len==stringLength) |
| 1911 |
{ |
| 1912 |
result = int_open(state->bundle, tag, value, comment, status); |
| 1913 |
} |
| 1914 |
else |
| 1915 |
{ |
| 1916 |
*status=U_INVALID_CHAR_FOUND; |
| 1917 |
} |
| 1918 |
uprv_free(string); |
| 1919 |
|
| 1920 |
return result; |
| 1921 |
} |
| 1922 |
|
| 1923 |
static struct SResource * |
| 1924 |
parseImport(ParseState* state, char *tag, uint32_t startline, const struct UString* comment, UErrorCode *status) |
| 1925 |
{ |
| 1926 |
uint32_t line; |
| 1927 |
int32_t stringLength; |
| 1928 |
LocalMemory<char> filename(getInvariantString(state, &line, nullptr, stringLength, status)); |
| 1929 |
if (U_FAILURE(*status)) |
| 1930 |
{ |
| 1931 |
return nullptr; |
| 1932 |
} |
| 1933 |
|
| 1934 |
expect(state, TOK_CLOSE_BRACE, nullptr, nullptr, nullptr, status); |
| 1935 |
|
| 1936 |
if (U_FAILURE(*status)) |
| 1937 |
{ |
| 1938 |
return nullptr; |
| 1939 |
} |
| 1940 |
|
| 1941 |
if(isVerbose()){ |
| 1942 |
printf(" import %s at line %i \n", tag == nullptr ? "(null)" : tag, static_cast<int>(startline)); |
| 1943 |
} |
| 1944 |
|
| 1945 |
/* Open the input file for reading */ |
| 1946 |
CharString fullname; |
| 1947 |
if (state->inputdir != nullptr) { |
| 1948 |
fullname.append(state->inputdir, *status); |
| 1949 |
} |
| 1950 |
fullname.appendPathPart(filename.getAlias(), *status); |
| 1951 |
if (U_FAILURE(*status)) { |
| 1952 |
return nullptr; |
| 1953 |
} |
| 1954 |
|
| 1955 |
FileStream *file = T_FileStream_open(fullname.data(), "rb"); |
| 1956 |
if (file == nullptr) |
| 1957 |
{ |
| 1958 |
error(line, "couldn't open input file %s", filename.getAlias()); |
| 1959 |
*status = U_FILE_ACCESS_ERROR; |
| 1960 |
return nullptr; |
| 1961 |
} |
| 1962 |
|
| 1963 |
int32_t len = T_FileStream_size(file); |
| 1964 |
LocalMemory<uint8_t> data; |
| 1965 |
if(data.allocateInsteadAndCopy(len) == nullptr) |
| 1966 |
{ |
| 1967 |
*status = U_MEMORY_ALLOCATION_ERROR; |
| 1968 |
T_FileStream_close (file); |
| 1969 |
return nullptr; |
| 1970 |
} |
| 1971 |
|
| 1972 |
/* int32_t numRead = */ T_FileStream_read(file, data.getAlias(), len); |
| 1973 |
T_FileStream_close (file); |
| 1974 |
|
| 1975 |
return bin_open(state->bundle, tag, len, data.getAlias(), fullname.data(), comment, status); |
| 1976 |
} |
| 1977 |
|
| 1978 |
static struct SResource * |
| 1979 |
parseInclude(ParseState* state, char *tag, uint32_t startline, const struct UString* comment, UErrorCode *status) |
| 1980 |
{ |
| 1981 |
struct SResource *result; |
| 1982 |
int32_t len=0; |
| 1983 |
char *filename; |
| 1984 |
uint32_t line; |
| 1985 |
char16_t *pTarget = nullptr; |
| 1986 |
|
| 1987 |
UCHARBUF *ucbuf; |
| 1988 |
char *fullname = nullptr; |
| 1989 |
const char* cp = nullptr; |
| 1990 |
const char16_t* uBuffer = nullptr; |
| 1991 |
|
| 1992 |
int32_t stringLength; |
| 1993 |
filename = getInvariantString(state, &line, nullptr, stringLength, status); |
| 1994 |
|
| 1995 |
if (U_FAILURE(*status)) |
| 1996 |
{ |
| 1997 |
return nullptr; |
| 1998 |
} |
| 1999 |
|
| 2000 |
expect(state, TOK_CLOSE_BRACE, nullptr, nullptr, nullptr, status); |
| 2001 |
|
| 2002 |
if (U_FAILURE(*status)) |
| 2003 |
{ |
| 2004 |
uprv_free(filename); |
| 2005 |
return nullptr; |
| 2006 |
} |
| 2007 |
|
| 2008 |
if(isVerbose()){ |
| 2009 |
printf(" include %s at line %i \n", tag == nullptr ? "(null)" : tag, static_cast<int>(startline)); |
| 2010 |
} |
| 2011 |
|
| 2012 |
fullname = static_cast<char*>(uprv_malloc(state->inputdirLength + stringLength + 2)); |
| 2013 |
/* test for nullptr */ |
| 2014 |
if(fullname == nullptr) |
| 2015 |
{ |
| 2016 |
*status = U_MEMORY_ALLOCATION_ERROR; |
| 2017 |
uprv_free(filename); |
| 2018 |
return nullptr; |
| 2019 |
} |
| 2020 |
|
| 2021 |
if(state->inputdir!=nullptr){ |
| 2022 |
if (state->inputdir[state->inputdirLength - 1] != U_FILE_SEP_CHAR) |
| 2023 |
{ |
| 2024 |
|
| 2025 |
uprv_strcpy(fullname, state->inputdir); |
| 2026 |
|
| 2027 |
fullname[state->inputdirLength] = U_FILE_SEP_CHAR; |
| 2028 |
fullname[state->inputdirLength + 1] = '\0'; |
| 2029 |
|
| 2030 |
uprv_strcat(fullname, filename); |
| 2031 |
} |
| 2032 |
else |
| 2033 |
{ |
| 2034 |
uprv_strcpy(fullname, state->inputdir); |
| 2035 |
uprv_strcat(fullname, filename); |
| 2036 |
} |
| 2037 |
}else{ |
| 2038 |
uprv_strcpy(fullname,filename); |
| 2039 |
} |
| 2040 |
|
| 2041 |
ucbuf = ucbuf_open(fullname, &cp,getShowWarning(),false,status); |
| 2042 |
|
| 2043 |
if (U_FAILURE(*status)) { |
| 2044 |
error(line, "couldn't open input file %s\n", filename); |
| 2045 |
return nullptr; |
| 2046 |
} |
| 2047 |
|
| 2048 |
uBuffer = ucbuf_getBuffer(ucbuf,&len,status); |
| 2049 |
result = string_open(state->bundle, tag, uBuffer, len, comment, status); |
| 2050 |
|
| 2051 |
ucbuf_close(ucbuf); |
| 2052 |
|
| 2053 |
uprv_free(pTarget); |
| 2054 |
|
| 2055 |
uprv_free(filename); |
| 2056 |
uprv_free(fullname); |
| 2057 |
|
| 2058 |
return result; |
| 2059 |
} |
| 2060 |
|
| 2061 |
|
| 2062 |
|
| 2063 |
|
| 2064 |
|
| 2065 |
U_STRING_DECL(k_type_string, "string", 6); |
| 2066 |
U_STRING_DECL(k_type_binary, "binary", 6); |
| 2067 |
U_STRING_DECL(k_type_bin, "bin", 3); |
| 2068 |
U_STRING_DECL(k_type_table, "table", 5); |
| 2069 |
U_STRING_DECL(k_type_table_no_fallback, "table(nofallback)", 17); |
| 2070 |
U_STRING_DECL(k_type_int, "int", 3); |
| 2071 |
U_STRING_DECL(k_type_integer, "integer", 7); |
| 2072 |
U_STRING_DECL(k_type_array, "array", 5); |
| 2073 |
U_STRING_DECL(k_type_alias, "alias", 5); |
| 2074 |
U_STRING_DECL(k_type_intvector, "intvector", 9); |
| 2075 |
U_STRING_DECL(k_type_import, "import", 6); |
| 2076 |
U_STRING_DECL(k_type_include, "include", 7); |
| 2077 |
|
| 2078 |
/* Various non-standard processing plugins that create one or more special resources. */ |
| 2079 |
U_STRING_DECL(k_type_plugin_uca_rules, "process(uca_rules)", 18); |
| 2080 |
U_STRING_DECL(k_type_plugin_collation, "process(collation)", 18); |
| 2081 |
U_STRING_DECL(k_type_plugin_transliterator, "process(transliterator)", 23); |
| 2082 |
U_STRING_DECL(k_type_plugin_dependency, "process(dependency)", 19); |
| 2083 |
|
| 2084 |
typedef enum EResourceType |
| 2085 |
{ |
| 2086 |
RESTYPE_UNKNOWN, |
| 2087 |
RESTYPE_STRING, |
| 2088 |
RESTYPE_BINARY, |
| 2089 |
RESTYPE_TABLE, |
| 2090 |
RESTYPE_TABLE_NO_FALLBACK, |
| 2091 |
RESTYPE_INTEGER, |
| 2092 |
RESTYPE_ARRAY, |
| 2093 |
RESTYPE_ALIAS, |
| 2094 |
RESTYPE_INTVECTOR, |
| 2095 |
RESTYPE_IMPORT, |
| 2096 |
RESTYPE_INCLUDE, |
| 2097 |
RESTYPE_PROCESS_UCA_RULES, |
| 2098 |
RESTYPE_PROCESS_COLLATION, |
| 2099 |
RESTYPE_PROCESS_TRANSLITERATOR, |
| 2100 |
RESTYPE_PROCESS_DEPENDENCY, |
| 2101 |
RESTYPE_RESERVED |
| 2102 |
} EResourceType; |
| 2103 |
|
| 2104 |
static struct { |
| 2105 |
const char *nameChars; /* only used for debugging */ |
| 2106 |
const char16_t *nameUChars; |
| 2107 |
ParseResourceFunction *parseFunction; |
| 2108 |
} gResourceTypes[] = { |
| 2109 |
{"Unknown", nullptr, nullptr}, |
| 2110 |
{"string", k_type_string, parseString}, |
| 2111 |
{"binary", k_type_binary, parseBinary}, |
| 2112 |
{"table", k_type_table, parseTable}, |
| 2113 |
{"table(nofallback)", k_type_table_no_fallback, nullptr}, /* parseFunction will never be called */ |
| 2114 |
{"integer", k_type_integer, parseInteger}, |
| 2115 |
{"array", k_type_array, parseArray}, |
| 2116 |
{"alias", k_type_alias, parseAlias}, |
| 2117 |
{"intvector", k_type_intvector, parseIntVector}, |
| 2118 |
{"import", k_type_import, parseImport}, |
| 2119 |
{"include", k_type_include, parseInclude}, |
| 2120 |
{"process(uca_rules)", k_type_plugin_uca_rules, parseUCARules}, |
| 2121 |
{"process(collation)", k_type_plugin_collation, nullptr /* not implemented yet */}, |
| 2122 |
{"process(transliterator)", k_type_plugin_transliterator, parseTransliterator}, |
| 2123 |
{"process(dependency)", k_type_plugin_dependency, parseDependency}, |
| 2124 |
{"reserved", nullptr, nullptr} |
| 2125 |
}; |
| 2126 |
|
| 2127 |
void initParser() |
| 2128 |
{ |
| 2129 |
U_STRING_INIT(k_type_string, "string", 6); |
| 2130 |
U_STRING_INIT(k_type_binary, "binary", 6); |
| 2131 |
U_STRING_INIT(k_type_bin, "bin", 3); |
| 2132 |
U_STRING_INIT(k_type_table, "table", 5); |
| 2133 |
U_STRING_INIT(k_type_table_no_fallback, "table(nofallback)", 17); |
| 2134 |
U_STRING_INIT(k_type_int, "int", 3); |
| 2135 |
U_STRING_INIT(k_type_integer, "integer", 7); |
| 2136 |
U_STRING_INIT(k_type_array, "array", 5); |
| 2137 |
U_STRING_INIT(k_type_alias, "alias", 5); |
| 2138 |
U_STRING_INIT(k_type_intvector, "intvector", 9); |
| 2139 |
U_STRING_INIT(k_type_import, "import", 6); |
| 2140 |
U_STRING_INIT(k_type_include, "include", 7); |
| 2141 |
|
| 2142 |
U_STRING_INIT(k_type_plugin_uca_rules, "process(uca_rules)", 18); |
| 2143 |
U_STRING_INIT(k_type_plugin_collation, "process(collation)", 18); |
| 2144 |
U_STRING_INIT(k_type_plugin_transliterator, "process(transliterator)", 23); |
| 2145 |
U_STRING_INIT(k_type_plugin_dependency, "process(dependency)", 19); |
| 2146 |
} |
| 2147 |
|
| 2148 |
static inline UBool isTable(enum EResourceType type) { |
| 2149 |
return type == RESTYPE_TABLE || type == RESTYPE_TABLE_NO_FALLBACK; |
| 2150 |
} |
| 2151 |
|
| 2152 |
static enum EResourceType |
| 2153 |
parseResourceType(ParseState* state, UErrorCode *status) |
| 2154 |
{ |
| 2155 |
struct UString *tokenValue; |
| 2156 |
struct UString comment; |
| 2157 |
enum EResourceType result = RESTYPE_UNKNOWN; |
| 2158 |
uint32_t line=0; |
| 2159 |
ustr_init(&comment); |
| 2160 |
expect(state, TOK_STRING, &tokenValue, &comment, &line, status); |
| 2161 |
|
| 2162 |
if (U_FAILURE(*status)) |
| 2163 |
{ |
| 2164 |
return RESTYPE_UNKNOWN; |
| 2165 |
} |
| 2166 |
|
| 2167 |
*status = U_ZERO_ERROR; |
| 2168 |
|
| 2169 |
/* Search for normal types */ |
| 2170 |
result=RESTYPE_UNKNOWN; |
| 2171 |
while ((result = static_cast<EResourceType>(result + 1)) < RESTYPE_RESERVED) { |
| 2172 |
if (u_strcmp(tokenValue->fChars, gResourceTypes[result].nameUChars) == 0) { |
| 2173 |
break; |
| 2174 |
} |
| 2175 |
} |
| 2176 |
/* Now search for the aliases */ |
| 2177 |
if (u_strcmp(tokenValue->fChars, k_type_int) == 0) { |
| 2178 |
result = RESTYPE_INTEGER; |
| 2179 |
} |
| 2180 |
else if (u_strcmp(tokenValue->fChars, k_type_bin) == 0) { |
| 2181 |
result = RESTYPE_BINARY; |
| 2182 |
} |
| 2183 |
else if (result == RESTYPE_RESERVED) { |
| 2184 |
char tokenBuffer[1024]; |
| 2185 |
u_austrncpy(tokenBuffer, tokenValue->fChars, sizeof(tokenBuffer)); |
| 2186 |
tokenBuffer[sizeof(tokenBuffer) - 1] = 0; |
| 2187 |
*status = U_INVALID_FORMAT_ERROR; |
| 2188 |
error(line, "unknown resource type '%s'", tokenBuffer); |
| 2189 |
} |
| 2190 |
|
| 2191 |
return result; |
| 2192 |
} |
| 2193 |
|
| 2194 |
/* parse a non-top-level resource */ |
| 2195 |
static struct SResource * |
| 2196 |
parseResource(ParseState* state, char *tag, const struct UString *comment, UErrorCode *status) |
| 2197 |
{ |
| 2198 |
enum ETokenType token; |
| 2199 |
enum EResourceType resType = RESTYPE_UNKNOWN; |
| 2200 |
ParseResourceFunction *parseFunction = nullptr; |
| 2201 |
struct UString *tokenValue; |
| 2202 |
uint32_t startline; |
| 2203 |
uint32_t line; |
| 2204 |
|
| 2205 |
|
| 2206 |
token = getToken(state, &tokenValue, nullptr, &startline, status); |
| 2207 |
|
| 2208 |
if(isVerbose()){ |
| 2209 |
printf(" resource %s at line %i \n", tag == nullptr ? "(null)" : tag, static_cast<int>(startline)); |
| 2210 |
} |
| 2211 |
|
| 2212 |
/* name . [ ':' type ] '{' resource '}' */ |
| 2213 |
/* This function parses from the colon onwards. If the colon is present, parse the |
| 2214 |
type then try to parse a resource of that type. If there is no explicit type, |
| 2215 |
work it out using the lookahead tokens. */ |
| 2216 |
switch (token) |
| 2217 |
{ |
| 2218 |
case TOK_EOF: |
| 2219 |
*status = U_INVALID_FORMAT_ERROR; |
| 2220 |
error(startline, "Unexpected EOF encountered"); |
| 2221 |
return nullptr; |
| 2222 |
|
| 2223 |
case TOK_ERROR: |
| 2224 |
*status = U_INVALID_FORMAT_ERROR; |
| 2225 |
return nullptr; |
| 2226 |
|
| 2227 |
case TOK_COLON: |
| 2228 |
resType = parseResourceType(state, status); |
| 2229 |
expect(state, TOK_OPEN_BRACE, &tokenValue, nullptr, &startline, status); |
| 2230 |
|
| 2231 |
if (U_FAILURE(*status)) |
| 2232 |
{ |
| 2233 |
return nullptr; |
| 2234 |
} |
| 2235 |
|
| 2236 |
break; |
| 2237 |
|
| 2238 |
case TOK_OPEN_BRACE: |
| 2239 |
break; |
| 2240 |
|
| 2241 |
default: |
| 2242 |
*status = U_INVALID_FORMAT_ERROR; |
| 2243 |
error(startline, "syntax error while reading a resource, expected '{' or ':'"); |
| 2244 |
return nullptr; |
| 2245 |
} |
| 2246 |
|
| 2247 |
|
| 2248 |
if (resType == RESTYPE_UNKNOWN) |
| 2249 |
{ |
| 2250 |
/* No explicit type, so try to work it out. At this point, we've read the first '{'. |
| 2251 |
We could have any of the following: |
| 2252 |
{ { => array (nested) |
| 2253 |
{ :/} => array |
| 2254 |
{ string , => string array |
| 2255 |
|
| 2256 |
{ string { => table |
| 2257 |
|
| 2258 |
{ string :/{ => table |
| 2259 |
{ string } => string |
| 2260 |
*/ |
| 2261 |
|
| 2262 |
token = peekToken(state, 0, nullptr, &line, nullptr,status); |
| 2263 |
|
| 2264 |
if (U_FAILURE(*status)) |
| 2265 |
{ |
| 2266 |
return nullptr; |
| 2267 |
} |
| 2268 |
|
| 2269 |
if (token == TOK_OPEN_BRACE || token == TOK_COLON ||token ==TOK_CLOSE_BRACE ) |
| 2270 |
{ |
| 2271 |
resType = RESTYPE_ARRAY; |
| 2272 |
} |
| 2273 |
else if (token == TOK_STRING) |
| 2274 |
{ |
| 2275 |
token = peekToken(state, 1, nullptr, &line, nullptr, status); |
| 2276 |
|
| 2277 |
if (U_FAILURE(*status)) |
| 2278 |
{ |
| 2279 |
return nullptr; |
| 2280 |
} |
| 2281 |
|
| 2282 |
switch (token) |
| 2283 |
{ |
| 2284 |
case TOK_COMMA: resType = RESTYPE_ARRAY; break; |
| 2285 |
case TOK_OPEN_BRACE: resType = RESTYPE_TABLE; break; |
| 2286 |
case TOK_CLOSE_BRACE: resType = RESTYPE_STRING; break; |
| 2287 |
case TOK_COLON: resType = RESTYPE_TABLE; break; |
| 2288 |
default: |
| 2289 |
*status = U_INVALID_FORMAT_ERROR; |
| 2290 |
error(line, "Unexpected token after string, expected ',', '{' or '}'"); |
| 2291 |
return nullptr; |
| 2292 |
} |
| 2293 |
} |
| 2294 |
else |
| 2295 |
{ |
| 2296 |
*status = U_INVALID_FORMAT_ERROR; |
| 2297 |
error(line, "Unexpected token after '{'"); |
| 2298 |
return nullptr; |
| 2299 |
} |
| 2300 |
|
| 2301 |
/* printf("Type guessed as %s\n", resourceNames[resType]); */ |
| 2302 |
} else if(resType == RESTYPE_TABLE_NO_FALLBACK) { |
| 2303 |
*status = U_INVALID_FORMAT_ERROR; |
| 2304 |
error(startline, "error: %s resource type not valid except on top bundle level", gResourceTypes[resType].nameChars); |
| 2305 |
return nullptr; |
| 2306 |
} |
| 2307 |
|
| 2308 |
|
| 2309 |
/* We should now know what we need to parse next, so call the appropriate parser |
| 2310 |
function and return. */ |
| 2311 |
parseFunction = gResourceTypes[resType].parseFunction; |
| 2312 |
if (parseFunction != nullptr) { |
| 2313 |
return parseFunction(state, tag, startline, comment, status); |
| 2314 |
} |
| 2315 |
else { |
| 2316 |
*status = U_INTERNAL_PROGRAM_ERROR; |
| 2317 |
error(startline, "internal error: %s resource type found and not handled", gResourceTypes[resType].nameChars); |
| 2318 |
} |
| 2319 |
|
| 2320 |
return nullptr; |
| 2321 |
} |
| 2322 |
|
| 2323 |
/* parse the top-level resource */ |
| 2324 |
struct SRBRoot * |
| 2325 |
parse(UCHARBUF *buf, const char *inputDir, const char *outputDir, const char *filename, |
| 2326 |
UBool makeBinaryCollation, UBool omitCollationRules, UBool icu4xMode, UErrorCode *status) |
| 2327 |
{ |
| 2328 |
struct UString *tokenValue; |
| 2329 |
struct UString comment; |
| 2330 |
uint32_t line; |
| 2331 |
enum EResourceType bundleType; |
| 2332 |
enum ETokenType token; |
| 2333 |
ParseState state; |
| 2334 |
uint32_t i; |
| 2335 |
|
| 2336 |
|
| 2337 |
for (i = 0; i < MAX_LOOKAHEAD + 1; i++) |
| 2338 |
{ |
| 2339 |
ustr_init(&state.lookahead[i].value); |
| 2340 |
ustr_init(&state.lookahead[i].comment); |
| 2341 |
} |
| 2342 |
|
| 2343 |
initLookahead(&state, buf, status); |
| 2344 |
|
| 2345 |
state.inputdir = inputDir; |
| 2346 |
state.inputdirLength = state.inputdir != nullptr ? static_cast<uint32_t>(uprv_strlen(state.inputdir)) : 0; |
| 2347 |
state.outputdir = outputDir; |
| 2348 |
state.outputdirLength = state.outputdir != nullptr ? static_cast<uint32_t>(uprv_strlen(state.outputdir)) : 0; |
| 2349 |
state.filename = filename; |
| 2350 |
state.makeBinaryCollation = makeBinaryCollation; |
| 2351 |
state.omitCollationRules = omitCollationRules; |
| 2352 |
state.icu4xMode = icu4xMode; |
| 2353 |
|
| 2354 |
ustr_init(&comment); |
| 2355 |
expect(&state, TOK_STRING, &tokenValue, &comment, nullptr, status); |
| 2356 |
|
| 2357 |
state.bundle = new SRBRoot(&comment, false, *status); |
| 2358 |
|
| 2359 |
if (state.bundle == nullptr || U_FAILURE(*status)) |
| 2360 |
{ |
| 2361 |
delete state.bundle; |
| 2362 |
|
| 2363 |
return nullptr; |
| 2364 |
} |
| 2365 |
|
| 2366 |
|
| 2367 |
state.bundle->setLocale(tokenValue->fChars, *status); |
| 2368 |
|
| 2369 |
/* The following code is to make Empty bundle work no matter with :table specifer or not */ |
| 2370 |
token = getToken(&state, nullptr, nullptr, &line, status); |
| 2371 |
if(token==TOK_COLON) { |
| 2372 |
*status=U_ZERO_ERROR; |
| 2373 |
bundleType=parseResourceType(&state, status); |
| 2374 |
|
| 2375 |
if(isTable(bundleType)) |
| 2376 |
{ |
| 2377 |
expect(&state, TOK_OPEN_BRACE, nullptr, nullptr, &line, status); |
| 2378 |
} |
| 2379 |
else |
| 2380 |
{ |
| 2381 |
*status=U_PARSE_ERROR; |
| 2382 |
error(line, "parse error. Stopped parsing with %s", u_errorName(*status)); |
| 2383 |
} |
| 2384 |
} |
| 2385 |
else |
| 2386 |
{ |
| 2387 |
/* not a colon */ |
| 2388 |
if(token==TOK_OPEN_BRACE) |
| 2389 |
{ |
| 2390 |
*status=U_ZERO_ERROR; |
| 2391 |
bundleType=RESTYPE_TABLE; |
| 2392 |
} |
| 2393 |
else |
| 2394 |
{ |
| 2395 |
/* neither colon nor open brace */ |
| 2396 |
*status=U_PARSE_ERROR; |
| 2397 |
bundleType=RESTYPE_UNKNOWN; |
| 2398 |
error(line, "parse error, did not find open-brace '{' or colon ':', stopped with %s", u_errorName(*status)); |
| 2399 |
} |
| 2400 |
} |
| 2401 |
|
| 2402 |
if (U_FAILURE(*status)) |
| 2403 |
{ |
| 2404 |
delete state.bundle; |
| 2405 |
return nullptr; |
| 2406 |
} |
| 2407 |
|
| 2408 |
if(bundleType==RESTYPE_TABLE_NO_FALLBACK) { |
| 2409 |
/* |
| 2410 |
* Parse a top-level table with the table(nofallback) declaration. |
| 2411 |
* This is the same as a regular table, but also sets the |
| 2412 |
* URES_ATT_NO_FALLBACK flag in indexes[URES_INDEX_ATTRIBUTES] . |
| 2413 |
*/ |
| 2414 |
state.bundle->fNoFallback=true; |
| 2415 |
} |
| 2416 |
/* top-level tables need not handle special table names like "collations" */ |
| 2417 |
assert(!state.bundle->fIsPoolBundle); |
| 2418 |
assert(state.bundle->fRoot->fType == URES_TABLE); |
| 2419 |
TableResource *rootTable = static_cast<TableResource *>(state.bundle->fRoot); |
| 2420 |
realParseTable(&state, rootTable, nullptr, line, status); |
| 2421 |
if(dependencyArray!=nullptr){ |
| 2422 |
rootTable->add(dependencyArray, 0, *status); |
| 2423 |
dependencyArray = nullptr; |
| 2424 |
} |
| 2425 |
if (U_FAILURE(*status)) |
| 2426 |
{ |
| 2427 |
delete state.bundle; |
| 2428 |
res_close(dependencyArray); |
| 2429 |
return nullptr; |
| 2430 |
} |
| 2431 |
|
| 2432 |
if (getToken(&state, nullptr, nullptr, &line, status) != TOK_EOF) |
| 2433 |
{ |
| 2434 |
warning(line, "extraneous text after resource bundle (perhaps unmatched braces)"); |
| 2435 |
if(isStrict()){ |
| 2436 |
*status = U_INVALID_FORMAT_ERROR; |
| 2437 |
return nullptr; |
| 2438 |
} |
| 2439 |
} |
| 2440 |
|
| 2441 |
cleanupLookahead(&state); |
| 2442 |
ustr_deinit(&comment); |
| 2443 |
return state.bundle; |
| 2444 |
} |