123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943 |
- #ifndef __MESSAGEPATTERN_H__
- #define __MESSAGEPATTERN_H__
- #include "unicode/utypes.h"
- #if !UCONFIG_NO_FORMATTING
- #include "unicode/parseerr.h"
- #include "unicode/unistr.h"
- enum UMessagePatternApostropheMode {
-
- UMSGPAT_APOS_DOUBLE_OPTIONAL,
-
- UMSGPAT_APOS_DOUBLE_REQUIRED
- };
- typedef enum UMessagePatternApostropheMode UMessagePatternApostropheMode;
- enum UMessagePatternPartType {
-
- UMSGPAT_PART_TYPE_MSG_START,
-
- UMSGPAT_PART_TYPE_MSG_LIMIT,
-
- UMSGPAT_PART_TYPE_SKIP_SYNTAX,
-
- UMSGPAT_PART_TYPE_INSERT_CHAR,
-
- UMSGPAT_PART_TYPE_REPLACE_NUMBER,
-
- UMSGPAT_PART_TYPE_ARG_START,
-
- UMSGPAT_PART_TYPE_ARG_LIMIT,
-
- UMSGPAT_PART_TYPE_ARG_NUMBER,
-
- UMSGPAT_PART_TYPE_ARG_NAME,
-
- UMSGPAT_PART_TYPE_ARG_TYPE,
-
- UMSGPAT_PART_TYPE_ARG_STYLE,
-
- UMSGPAT_PART_TYPE_ARG_SELECTOR,
-
- UMSGPAT_PART_TYPE_ARG_INT,
-
- UMSGPAT_PART_TYPE_ARG_DOUBLE
- };
- typedef enum UMessagePatternPartType UMessagePatternPartType;
- enum UMessagePatternArgType {
-
- UMSGPAT_ARG_TYPE_NONE,
-
- UMSGPAT_ARG_TYPE_SIMPLE,
-
- UMSGPAT_ARG_TYPE_CHOICE,
-
- UMSGPAT_ARG_TYPE_PLURAL,
-
- UMSGPAT_ARG_TYPE_SELECT,
-
- UMSGPAT_ARG_TYPE_SELECTORDINAL
- };
- typedef enum UMessagePatternArgType UMessagePatternArgType;
- #define UMSGPAT_ARG_TYPE_HAS_PLURAL_STYLE(argType) \
- ((argType)==UMSGPAT_ARG_TYPE_PLURAL || (argType)==UMSGPAT_ARG_TYPE_SELECTORDINAL)
- enum {
-
- UMSGPAT_ARG_NAME_NOT_NUMBER=-1,
-
- UMSGPAT_ARG_NAME_NOT_VALID=-2
- };
- #define UMSGPAT_NO_NUMERIC_VALUE ((double)(-123456789))
- U_NAMESPACE_BEGIN
- class MessagePatternDoubleList;
- class MessagePatternPartsList;
- /**
- * Parses and represents ICU MessageFormat patterns.
- * Also handles patterns for ChoiceFormat, PluralFormat and SelectFormat.
- * Used in the implementations of those classes as well as in tools
- * for message validation, translation and format conversion.
- * <p>
- * The parser handles all syntax relevant for identifying message arguments.
- * This includes "complex" arguments whose style strings contain
- * nested MessageFormat pattern substrings.
- * For "simple" arguments (with no nested MessageFormat pattern substrings),
- * the argument style is not parsed any further.
- * <p>
- * The parser handles named and numbered message arguments and allows both in one message.
- * <p>
- * Once a pattern has been parsed successfully, iterate through the parsed data
- * with countParts(), getPart() and related methods.
- * <p>
- * The data logically represents a parse tree, but is stored and accessed
- * as a list of "parts" for fast and simple parsing and to minimize object allocations.
- * Arguments and nested messages are best handled via recursion.
- * For every _START "part", MessagePattern.getLimitPartIndex() efficiently returns
- * the index of the corresponding _LIMIT "part".
- * <p>
- * List of "parts":
- * <pre>
- * message = MSG_START (SKIP_SYNTAX | INSERT_CHAR | REPLACE_NUMBER | argument)* MSG_LIMIT
- * argument = noneArg | simpleArg | complexArg
- * complexArg = choiceArg | pluralArg | selectArg
- *
- * noneArg = ARG_START.NONE (ARG_NAME | ARG_NUMBER) ARG_LIMIT.NONE
- * simpleArg = ARG_START.SIMPLE (ARG_NAME | ARG_NUMBER) ARG_TYPE [ARG_STYLE] ARG_LIMIT.SIMPLE
- * choiceArg = ARG_START.CHOICE (ARG_NAME | ARG_NUMBER) choiceStyle ARG_LIMIT.CHOICE
- * pluralArg = ARG_START.PLURAL (ARG_NAME | ARG_NUMBER) pluralStyle ARG_LIMIT.PLURAL
- * selectArg = ARG_START.SELECT (ARG_NAME | ARG_NUMBER) selectStyle ARG_LIMIT.SELECT
- *
- * choiceStyle = ((ARG_INT | ARG_DOUBLE) ARG_SELECTOR message)+
- * pluralStyle = [ARG_INT | ARG_DOUBLE] (ARG_SELECTOR [ARG_INT | ARG_DOUBLE] message)+
- * selectStyle = (ARG_SELECTOR message)+
- * </pre>
- * <ul>
- * <li>Literal output text is not represented directly by "parts" but accessed
- * between parts of a message, from one part's getLimit() to the next part's getIndex().
- * <li><code>ARG_START.CHOICE</code> stands for an ARG_START Part with ArgType CHOICE.
- * <li>In the choiceStyle, the ARG_SELECTOR has the '<', the '#' or
- * the less-than-or-equal-to sign (U+2264).
- * <li>In the pluralStyle, the first, optional numeric Part has the "offset:" value.
- * The optional numeric Part between each (ARG_SELECTOR, message) pair
- * is the value of an explicit-number selector like "=2",
- * otherwise the selector is a non-numeric identifier.
- * <li>The REPLACE_NUMBER Part can occur only in an immediate sub-message of the pluralStyle.
- * </ul>
- * <p>
- * This class is not intended for public subclassing.
- *
- * @stable ICU 4.8
- */
- class U_COMMON_API MessagePattern : public UObject {
- public:
-
- MessagePattern(UErrorCode &errorCode);
-
- MessagePattern(UMessagePatternApostropheMode mode, UErrorCode &errorCode);
-
- MessagePattern(const UnicodeString &pattern, UParseError *parseError, UErrorCode &errorCode);
-
- MessagePattern(const MessagePattern &other);
-
- MessagePattern &operator=(const MessagePattern &other);
-
- virtual ~MessagePattern();
-
- MessagePattern &parse(const UnicodeString &pattern,
- UParseError *parseError, UErrorCode &errorCode);
-
- MessagePattern &parseChoiceStyle(const UnicodeString &pattern,
- UParseError *parseError, UErrorCode &errorCode);
-
- MessagePattern &parsePluralStyle(const UnicodeString &pattern,
- UParseError *parseError, UErrorCode &errorCode);
-
- MessagePattern &parseSelectStyle(const UnicodeString &pattern,
- UParseError *parseError, UErrorCode &errorCode);
-
- void clear();
-
- void clearPatternAndSetApostropheMode(UMessagePatternApostropheMode mode) {
- clear();
- aposMode=mode;
- }
-
- UBool operator==(const MessagePattern &other) const;
-
- inline UBool operator!=(const MessagePattern &other) const {
- return !operator==(other);
- }
-
- int32_t hashCode() const;
-
- UMessagePatternApostropheMode getApostropheMode() const {
- return aposMode;
- }
-
-
-
- const UnicodeString &getPatternString() const {
- return msg;
- }
-
- UBool hasNamedArguments() const {
- return hasArgNames;
- }
-
- UBool hasNumberedArguments() const {
- return hasArgNumbers;
- }
-
- static int32_t validateArgumentName(const UnicodeString &name);
-
- UnicodeString autoQuoteApostropheDeep() const;
- class Part;
- /**
- * Returns the number of "parts" created by parsing the pattern string.
- * Returns 0 if no pattern has been parsed or clear() was called.
- * @return the number of pattern parts.
- * @stable ICU 4.8
- */
- int32_t countParts() const {
- return partsLength;
- }
-
- const Part &getPart(int32_t i) const {
- return parts[i];
- }
-
- UMessagePatternPartType getPartType(int32_t i) const {
- return getPart(i).type;
- }
-
- int32_t getPatternIndex(int32_t partIndex) const {
- return getPart(partIndex).index;
- }
-
- UnicodeString getSubstring(const Part &part) const {
- return msg.tempSubString(part.index, part.length);
- }
-
- UBool partSubstringMatches(const Part &part, const UnicodeString &s) const {
- return 0==msg.compare(part.index, part.length, s);
- }
-
- double getNumericValue(const Part &part) const;
-
- double getPluralOffset(int32_t pluralStart) const;
-
- int32_t getLimitPartIndex(int32_t start) const {
- int32_t limit=getPart(start).limitPartIndex;
- if(limit<start) {
- return start;
- }
- return limit;
- }
-
- class Part : public UMemory {
- public:
-
- Part() {}
-
- UMessagePatternPartType getType() const {
- return type;
- }
-
- int32_t getIndex() const {
- return index;
- }
-
- int32_t getLength() const {
- return length;
- }
-
- int32_t getLimit() const {
- return index+length;
- }
-
- int32_t getValue() const {
- return value;
- }
-
- UMessagePatternArgType getArgType() const {
- UMessagePatternPartType type=getType();
- if(type==UMSGPAT_PART_TYPE_ARG_START || type==UMSGPAT_PART_TYPE_ARG_LIMIT) {
- return (UMessagePatternArgType)value;
- } else {
- return UMSGPAT_ARG_TYPE_NONE;
- }
- }
-
- static UBool hasNumericValue(UMessagePatternPartType type) {
- return type==UMSGPAT_PART_TYPE_ARG_INT || type==UMSGPAT_PART_TYPE_ARG_DOUBLE;
- }
-
- UBool operator==(const Part &other) const;
-
- inline UBool operator!=(const Part &other) const {
- return !operator==(other);
- }
-
- int32_t hashCode() const {
- return ((type*37+index)*37+length)*37+value;
- }
- private:
- friend class MessagePattern;
- static const int32_t MAX_LENGTH=0xffff;
- static const int32_t MAX_VALUE=0x7fff;
- // Some fields are not final because they are modified during pattern parsing.
- // After pattern parsing, the parts are effectively immutable.
- UMessagePatternPartType type;
- int32_t index;
- uint16_t length;
- int16_t value;
- int32_t limitPartIndex;
- };
- private:
- void preParse(const UnicodeString &pattern, UParseError *parseError, UErrorCode &errorCode);
- void postParse();
- int32_t parseMessage(int32_t index, int32_t msgStartLength,
- int32_t nestingLevel, UMessagePatternArgType parentType,
- UParseError *parseError, UErrorCode &errorCode);
- int32_t parseArg(int32_t index, int32_t argStartLength, int32_t nestingLevel,
- UParseError *parseError, UErrorCode &errorCode);
- int32_t parseSimpleStyle(int32_t index, UParseError *parseError, UErrorCode &errorCode);
- int32_t parseChoiceStyle(int32_t index, int32_t nestingLevel,
- UParseError *parseError, UErrorCode &errorCode);
- int32_t parsePluralOrSelectStyle(UMessagePatternArgType argType, int32_t index, int32_t nestingLevel,
- UParseError *parseError, UErrorCode &errorCode);
- /**
- * Validates and parses an argument name or argument number string.
- * This internal method assumes that the input substring is a "pattern identifier".
- * @return >=0 if the name is a valid number,
- * ARG_NAME_NOT_NUMBER (-1) if it is a "pattern identifier" but not all ASCII digits,
- * ARG_NAME_NOT_VALID (-2) if it is neither.
- * @see #validateArgumentName(String)
- */
- static int32_t parseArgNumber(const UnicodeString &s, int32_t start, int32_t limit);
- int32_t parseArgNumber(int32_t start, int32_t limit) {
- return parseArgNumber(msg, start, limit);
- }
-
- void parseDouble(int32_t start, int32_t limit, UBool allowInfinity,
- UParseError *parseError, UErrorCode &errorCode);
-
-
- int32_t skipWhiteSpace(int32_t index);
- int32_t skipIdentifier(int32_t index);
-
- int32_t skipDouble(int32_t index);
- static UBool isArgTypeChar(UChar32 c);
- UBool isChoice(int32_t index);
- UBool isPlural(int32_t index);
- UBool isSelect(int32_t index);
- UBool isOrdinal(int32_t index);
-
- UBool inMessageFormatPattern(int32_t nestingLevel);
-
- UBool inTopLevelChoiceMessage(int32_t nestingLevel, UMessagePatternArgType parentType);
- void addPart(UMessagePatternPartType type, int32_t index, int32_t length,
- int32_t value, UErrorCode &errorCode);
- void addLimitPart(int32_t start,
- UMessagePatternPartType type, int32_t index, int32_t length,
- int32_t value, UErrorCode &errorCode);
- void addArgDoublePart(double numericValue, int32_t start, int32_t length, UErrorCode &errorCode);
- void setParseError(UParseError *parseError, int32_t index);
- UBool init(UErrorCode &errorCode);
- UBool copyStorage(const MessagePattern &other, UErrorCode &errorCode);
- UMessagePatternApostropheMode aposMode;
- UnicodeString msg;
-
- MessagePatternPartsList *partsList;
- Part *parts;
- int32_t partsLength;
-
- MessagePatternDoubleList *numericValuesList;
- double *numericValues;
- int32_t numericValuesLength;
- UBool hasArgNames;
- UBool hasArgNumbers;
- UBool needsAutoQuoting;
- };
- U_NAMESPACE_END
- #endif
- #endif
|