1 // © 2018 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 
4 #include "unicode/utypes.h"
5 
6 #if !UCONFIG_NO_FORMATTING
7 #ifndef __NUMPARSE_IMPL_H__
8 #define __NUMPARSE_IMPL_H__
9 
10 #include "numparse_types.h"
11 #include "numparse_decimal.h"
12 #include "numparse_symbols.h"
13 #include "numparse_scientific.h"
14 #include "unicode/uniset.h"
15 #include "numparse_currency.h"
16 #include "numparse_affixes.h"
17 #include "number_decimfmtprops.h"
18 #include "unicode/localpointer.h"
19 #include "numparse_validators.h"
20 #include "number_multiplier.h"
21 #include "string_segment.h"
22 
23 U_NAMESPACE_BEGIN
24 
25 // Export an explicit template instantiation of the MaybeStackArray that is used as a data member of NumberParserImpl.
26 // When building DLLs for Windows this is required even though no direct access to the MaybeStackArray leaks out of the i18n library.
27 // (See numparse_compositions.h, numparse_affixes.h, datefmt.h, and others for similar examples.)
28 #if U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN
29 template class U_I18N_API MaybeStackArray<const numparse::impl::NumberParseMatcher*, 10>;
30 #endif
31 
32 namespace numparse {
33 namespace impl {
34 
35 // Exported as U_I18N_API for tests
36 class U_I18N_API NumberParserImpl : public MutableMatcherCollection, public UMemory {
37   public:
38     virtual ~NumberParserImpl();
39 
40     static NumberParserImpl* createSimpleParser(const Locale& locale, const UnicodeString& patternString,
41                                                 parse_flags_t parseFlags, UErrorCode& status);
42 
43     static NumberParserImpl* createParserFromProperties(
44             const number::impl::DecimalFormatProperties& properties, const DecimalFormatSymbols& symbols,
45             bool parseCurrency, UErrorCode& status);
46 
47     /**
48      * Does NOT take ownership of the matcher. The matcher MUST remain valid for the lifespan of the
49      * NumberParserImpl.
50      * @param matcher The matcher to reference.
51      */
52     void addMatcher(NumberParseMatcher& matcher) override;
53 
54     void freeze();
55 
56     parse_flags_t getParseFlags() const;
57 
58     void parse(const UnicodeString& input, bool greedy, ParsedNumber& result, UErrorCode& status) const;
59 
60     void parse(const UnicodeString& input, int32_t start, bool greedy, ParsedNumber& result,
61                UErrorCode& status) const;
62 
63     UnicodeString toString() const;
64 
65   private:
66     parse_flags_t fParseFlags;
67     int32_t fNumMatchers = 0;
68     // NOTE: The stack capacity for fMatchers and fLeads should be the same
69     MaybeStackArray<const NumberParseMatcher*, 10> fMatchers;
70     bool fFrozen = false;
71 
72     // WARNING: All of these matchers start in an undefined state (default-constructed).
73     // You must use an assignment operator on them before using.
74     struct {
75         IgnorablesMatcher ignorables;
76         InfinityMatcher infinity;
77         MinusSignMatcher minusSign;
78         NanMatcher nan;
79         PaddingMatcher padding;
80         PercentMatcher percent;
81         PermilleMatcher permille;
82         PlusSignMatcher plusSign;
83         DecimalMatcher decimal;
84         ScientificMatcher scientific;
85         CombinedCurrencyMatcher currency;
86         AffixMatcherWarehouse affixMatcherWarehouse;
87         AffixTokenMatcherWarehouse affixTokenMatcherWarehouse;
88     } fLocalMatchers;
89     struct {
90         RequireAffixValidator affix;
91         RequireCurrencyValidator currency;
92         RequireDecimalSeparatorValidator decimalSeparator;
93         RequireNumberValidator number;
94         MultiplierParseHandler multiplier;
95     } fLocalValidators;
96 
97     explicit NumberParserImpl(parse_flags_t parseFlags);
98 
99     void parseGreedy(StringSegment& segment, ParsedNumber& result, UErrorCode& status) const;
100 
101     void parseLongestRecursive(
102         StringSegment& segment, ParsedNumber& result, int32_t recursionLevels, UErrorCode& status) const;
103 };
104 
105 
106 } // namespace impl
107 } // namespace numparse
108 U_NAMESPACE_END
109 
110 #endif //__NUMPARSE_IMPL_H__
111 #endif /* #if !UCONFIG_NO_FORMATTING */
112