A single validation to apply to an AST. May be empty if default constructed or moved from. use operator bool() to check if the validation is empty.
| 71 | // May be empty if default constructed or moved from. |
| 72 | // use operator bool() to check if the validation is empty. |
| 73 | class Validation { |
| 74 | public: |
| 75 | // Tests the AST reports any issues to the context. |
| 76 | // |
| 77 | // Returns false if the AST is invalid. |
| 78 | // |
| 79 | // The same instance is used across Validate() so must be thread safe |
| 80 | // (typically stateless). |
| 81 | using ImplFunction = |
| 82 | absl::AnyInvocable<bool(ValidationContext& context) const>; |
| 83 | |
| 84 | Validation() = default; |
| 85 | explicit Validation(ImplFunction impl); |
| 86 | Validation(ImplFunction impl, absl::string_view id); |
| 87 | |
| 88 | const ImplFunction& impl() const { |
| 89 | ABSL_DCHECK(rep_ != nullptr); |
| 90 | return rep_->impl; |
| 91 | } |
| 92 | |
| 93 | absl::string_view id() const { |
| 94 | ABSL_DCHECK(rep_ != nullptr); |
| 95 | return rep_->id; |
| 96 | } |
| 97 | |
| 98 | bool operator()(ValidationContext& context) const { |
| 99 | ABSL_DCHECK(rep_ != nullptr); |
| 100 | return rep_->impl(context); |
| 101 | } |
| 102 | |
| 103 | explicit operator bool() const { return rep_ != nullptr; } |
| 104 | |
| 105 | private: |
| 106 | struct Rep { |
| 107 | ImplFunction impl; |
| 108 | // Optional id if supported in environment config. |
| 109 | std::string id; |
| 110 | }; |
| 111 | |
| 112 | std::shared_ptr<const Rep> rep_; |
| 113 | }; |
| 114 | |
| 115 | // A validator checks a set of semantic rules for a given AST. |
| 116 | class Validator { |
no outgoing calls