1/*
2*******************************************************************************
3* Copyright (C) 2007-2011, International Business Machines Corporation and
4* others. All Rights Reserved.
5*******************************************************************************
6*
7* File PLURRULE_IMPL.H
8*
9*******************************************************************************
10*/
11
12
13#ifndef PLURRULE_IMPLE
14#define PLURRULE_IMPLE
15
16// Internal definitions for the PluralRules implementation.
17
18#if !UCONFIG_NO_FORMATTING
19
20#include "unicode/format.h"
21#include "unicode/locid.h"
22#include "unicode/parseerr.h"
23#include "unicode/utypes.h"
24#include "uvector.h"
25#include "hash.h"
26
27U_NAMESPACE_BEGIN
28
29#define DOT               ((UChar)0x002E)
30#define SINGLE_QUOTE      ((UChar)0x0027)
31#define SLASH             ((UChar)0x002F)
32#define BACKSLASH         ((UChar)0x005C)
33#define SPACE             ((UChar)0x0020)
34#define QUOTATION_MARK    ((UChar)0x0022)
35#define NUMBER_SIGN       ((UChar)0x0023)
36#define ASTERISK          ((UChar)0x002A)
37#define COMMA             ((UChar)0x002C)
38#define HYPHEN            ((UChar)0x002D)
39#define U_ZERO            ((UChar)0x0030)
40#define U_ONE             ((UChar)0x0031)
41#define U_TWO             ((UChar)0x0032)
42#define U_THREE           ((UChar)0x0033)
43#define U_FOUR            ((UChar)0x0034)
44#define U_FIVE            ((UChar)0x0035)
45#define U_SIX             ((UChar)0x0036)
46#define U_SEVEN           ((UChar)0x0037)
47#define U_EIGHT           ((UChar)0x0038)
48#define U_NINE            ((UChar)0x0039)
49#define COLON             ((UChar)0x003A)
50#define SEMI_COLON        ((UChar)0x003B)
51#define CAP_A             ((UChar)0x0041)
52#define CAP_B             ((UChar)0x0042)
53#define CAP_R             ((UChar)0x0052)
54#define CAP_Z             ((UChar)0x005A)
55#define LOWLINE           ((UChar)0x005F)
56#define LEFTBRACE         ((UChar)0x007B)
57#define RIGHTBRACE        ((UChar)0x007D)
58
59#define LOW_A             ((UChar)0x0061)
60#define LOW_B             ((UChar)0x0062)
61#define LOW_C             ((UChar)0x0063)
62#define LOW_D             ((UChar)0x0064)
63#define LOW_E             ((UChar)0x0065)
64#define LOW_F             ((UChar)0x0066)
65#define LOW_G             ((UChar)0x0067)
66#define LOW_H             ((UChar)0x0068)
67#define LOW_I             ((UChar)0x0069)
68#define LOW_J             ((UChar)0x006a)
69#define LOW_K             ((UChar)0x006B)
70#define LOW_L             ((UChar)0x006C)
71#define LOW_M             ((UChar)0x006D)
72#define LOW_N             ((UChar)0x006E)
73#define LOW_O             ((UChar)0x006F)
74#define LOW_P             ((UChar)0x0070)
75#define LOW_Q             ((UChar)0x0071)
76#define LOW_R             ((UChar)0x0072)
77#define LOW_S             ((UChar)0x0073)
78#define LOW_T             ((UChar)0x0074)
79#define LOW_U             ((UChar)0x0075)
80#define LOW_V             ((UChar)0x0076)
81#define LOW_W             ((UChar)0x0077)
82#define LOW_Y             ((UChar)0x0079)
83#define LOW_Z             ((UChar)0x007A)
84
85
86#define PLURAL_RANGE_HIGH  0x7fffffff;
87
88
89typedef enum PluralKey {
90  pZero,
91  pOne,
92  pTwo,
93  pFew,
94  pMany,
95  pOther,
96  pLast
97}PluralKey;
98
99typedef enum tokenType {
100  none,
101  tLetter,
102  tNumber,
103  tComma,
104  tSemiColon,
105  tSpace,
106  tColon,
107  tDot,
108  tKeyword,
109  tZero,
110  tOne,
111  tTwo,
112  tFew,
113  tMany,
114  tOther,
115  tAnd,
116  tOr,
117  tMod,
118  tNot,
119  tIn,
120  tWithin,
121  tNotIn,
122  tVariableN,
123  tIs,
124  tLeftBrace,
125  tRightBrace
126}tokenType;
127
128class RuleParser : public UMemory {
129public:
130    RuleParser();
131    virtual ~RuleParser();
132    void getNextToken(const UnicodeString& ruleData, int32_t *ruleIndex, UnicodeString& token,
133                            tokenType& type, UErrorCode &status);
134    void checkSyntax(tokenType prevType, tokenType curType, UErrorCode &status);
135private:
136    void getKeyType(const UnicodeString& token, tokenType& type, UErrorCode &status);
137    UBool inRange(UChar ch, tokenType& type);
138    UBool isValidKeyword(const UnicodeString& token);
139};
140
141class AndConstraint : public UMemory  {
142public:
143    typedef enum RuleOp {
144        NONE,
145        MOD
146    } RuleOp;
147    RuleOp  op;
148    int32_t opNum;
149    int32_t  rangeLow;
150    int32_t  rangeHigh;
151    UBool   notIn;
152    UBool   integerOnly;
153    AndConstraint *next;
154
155    AndConstraint();
156    AndConstraint(const AndConstraint& other);
157    virtual ~AndConstraint();
158    AndConstraint* add();
159    UBool isFulfilled(double number);
160    UBool isLimited();
161    int32_t updateRepeatLimit(int32_t maxLimit);
162};
163
164class OrConstraint : public UMemory  {
165public:
166    AndConstraint *childNode;
167    OrConstraint *next;
168    OrConstraint();
169
170    OrConstraint(const OrConstraint& other);
171    virtual ~OrConstraint();
172    AndConstraint* add();
173    UBool isFulfilled(double number);
174    UBool isLimited();
175};
176
177class RuleChain : public UMemory  {
178public:
179    OrConstraint *ruleHeader;
180    UnicodeString keyword;
181    RuleChain();
182    RuleChain(const RuleChain& other);
183    RuleChain *next;
184
185    virtual ~RuleChain();
186    UnicodeString select(double number) const;
187    void dumpRules(UnicodeString& result);
188    int32_t getRepeatLimit();
189    UBool isLimited();
190    UErrorCode getKeywords(int32_t maxArraySize, UnicodeString *keywords, int32_t& arraySize) const;
191    UBool isKeyword(const UnicodeString& keyword) const;
192    void setRepeatLimit();
193private:
194    int32_t repeatLimit;
195};
196
197class PluralKeywordEnumeration : public StringEnumeration {
198public:
199    PluralKeywordEnumeration(RuleChain *header, UErrorCode& status);
200    virtual ~PluralKeywordEnumeration();
201    static UClassID U_EXPORT2 getStaticClassID(void);
202    virtual UClassID getDynamicClassID(void) const;
203    virtual const UnicodeString* snext(UErrorCode& status);
204    virtual void reset(UErrorCode& status);
205    virtual int32_t count(UErrorCode& status) const;
206private:
207    int32_t pos;
208    UVector fKeywordNames;
209};
210
211U_NAMESPACE_END
212
213#endif /* #if !UCONFIG_NO_FORMATTING */
214
215#endif // _PLURRULE_IMPL
216//eof
217