| 1774 | } |
| 1775 | |
| 1776 | absl::StatusOr<std::unique_ptr<cel::Parser>> Build() override { |
| 1777 | using std::swap; |
| 1778 | // Save the old configured macros so they aren't affected by applying the |
| 1779 | // libraries and can be restored if an error occurs. |
| 1780 | std::vector<cel::Macro> individual_macros; |
| 1781 | swap(individual_macros, macros_); |
| 1782 | absl::Cleanup cleanup([&] { swap(macros_, individual_macros); }); |
| 1783 | |
| 1784 | cel::MacroRegistry macro_registry; |
| 1785 | |
| 1786 | for (const auto& library : libraries_) { |
| 1787 | CEL_RETURN_IF_ERROR(library.configure(*this)); |
| 1788 | if (!library.id.empty()) { |
| 1789 | auto it = library_subsets_.find(library.id); |
| 1790 | if (it != library_subsets_.end()) { |
| 1791 | const cel::ParserLibrarySubset& subset = it->second; |
| 1792 | for (const auto& macro : macros_) { |
| 1793 | if (subset.should_include_macro(macro)) { |
| 1794 | CEL_RETURN_IF_ERROR(macro_registry.RegisterMacro(macro)); |
| 1795 | } |
| 1796 | } |
| 1797 | macros_.clear(); |
| 1798 | continue; |
| 1799 | } |
| 1800 | } |
| 1801 | |
| 1802 | CEL_RETURN_IF_ERROR(macro_registry.RegisterMacros(macros_)); |
| 1803 | macros_.clear(); |
| 1804 | } |
| 1805 | |
| 1806 | absl::flat_hash_set<std::string> library_ids(library_ids_); |
| 1807 | |
| 1808 | // Hack to support adding the standard library macros either by option or |
| 1809 | // with a library configurer. |
| 1810 | if (!options_.disable_standard_macros && !library_ids_.contains("stdlib")) { |
| 1811 | CEL_RETURN_IF_ERROR(macro_registry.RegisterMacros(Macro::AllMacros())); |
| 1812 | library_ids.insert("stdlib"); |
| 1813 | } |
| 1814 | |
| 1815 | if (options_.enable_optional_syntax && !library_ids_.contains("optional")) { |
| 1816 | CEL_RETURN_IF_ERROR(macro_registry.RegisterMacro(cel::OptMapMacro())); |
| 1817 | CEL_RETURN_IF_ERROR(macro_registry.RegisterMacro(cel::OptFlatMapMacro())); |
| 1818 | library_ids.insert("optional"); |
| 1819 | } |
| 1820 | CEL_RETURN_IF_ERROR(macro_registry.RegisterMacros(individual_macros)); |
| 1821 | return std::make_unique<ParserImpl>(options_, std::move(macro_registry), |
| 1822 | std::move(library_ids)); |
| 1823 | } |
| 1824 | |
| 1825 | private: |
| 1826 | friend class ParserImpl; |