xref: /aosp_15_r20/external/icu/libicu/cts_headers/messageformat2_checker.h (revision 0e209d3975ff4a8c132096b14b0e9364a753506e)
1 // © 2024 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 #ifndef U_HIDE_DEPRECATED_API
7 
8 #ifndef MESSAGEFORMAT_CHECKER_H
9 #define MESSAGEFORMAT_CHECKER_H
10 
11 #if U_SHOW_CPLUSPLUS_API
12 
13 #if !UCONFIG_NO_FORMATTING
14 
15 #if !UCONFIG_NO_MF2
16 
17 #include "unicode/messageformat2_data_model.h"
18 #include "messageformat2_errors.h"
19 
20 U_NAMESPACE_BEGIN
21 
22 namespace message2 {
23 
24     using namespace data_model;
25 
26     // Used for checking missing selector annotation errors
27     // and duplicate declaration errors (specifically for
28     // implicit declarations)
29     class TypeEnvironment : public UMemory {
30     public:
31         // MessageFormat has a simple type system;
32         // variables are in-scope and annotated; in-scope and unannotated;
33         // or free (a free variable has no explicit declaration in the scope
34         // of its use.)
35         enum Type {
36             Annotated,
37             Unannotated,
38             FreeVariable
39         };
40         void extend(const VariableName&, Type, UErrorCode& status);
41         Type get(const VariableName&) const;
42         bool known(const VariableName&) const;
43         TypeEnvironment(UErrorCode& status);
44 
45         virtual ~TypeEnvironment();
46 
47     private:
48         // Stores variables known to be annotated.
49         LocalPointer<UVector> annotated; // Vector of `VariableName`s
50         // Stores variables that are in-scope but unannotated.
51         LocalPointer<UVector> unannotated; // Vector of `VariableName`s
52         // Stores free variables that are used in the RHS of a declaration
53         LocalPointer<UVector> freeVars; // Vector of `VariableNames`; tracks free variables
54                                         // This can't just be "variables that don't appear in
55                                         // `annotated` or `unannotated`", as a use introduces
56                                         // an explicit declaration
57     }; // class TypeEnvironment
58 
59     // Checks a data model for semantic errors
60     // (Errors are defined in https://github.com/unicode-org/message-format-wg/blob/main/spec/formatting.md       )
61     class Checker {
62     public:
63         void check(UErrorCode&);
Checker(const MFDataModel & m,StaticErrors & e)64         Checker(const MFDataModel& m, StaticErrors& e) : dataModel(m), errors(e) {}
65     private:
66 
67         void requireAnnotated(const TypeEnvironment&, const Expression&, UErrorCode&);
68         void addFreeVars(TypeEnvironment& t, const Operand&, UErrorCode&);
69         void addFreeVars(TypeEnvironment& t, const Operator&, UErrorCode&);
70         void addFreeVars(TypeEnvironment& t, const OptionMap&, UErrorCode&);
71         void addFreeVars(TypeEnvironment& t, const Expression&, UErrorCode&);
72         void checkDeclarations(TypeEnvironment&, UErrorCode&);
73         void checkSelectors(const TypeEnvironment&, UErrorCode&);
74         void checkVariants(UErrorCode&);
75         void check(const OptionMap&);
76         void check(const Operand&);
77         void check(const Expression&);
78         void check(const Pattern&);
79         const MFDataModel& dataModel;
80         StaticErrors& errors;
81     }; // class Checker
82 
83 } // namespace message2
84 
85 U_NAMESPACE_END
86 
87 #endif /* #if !UCONFIG_NO_MF2 */
88 
89 #endif /* #if !UCONFIG_NO_FORMATTING */
90 
91 #endif /* U_SHOW_CPLUSPLUS_API */
92 
93 #endif // MESSAGEFORMAT_CHECKER_H
94 
95 #endif // U_HIDE_DEPRECATED_API
96 // eof
97 
98