1 // Copyright 2018 The Fuchsia Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef ZIRCON_SYSTEM_HOST_FIDL_INCLUDE_FIDL_JSON_GENERATOR_H_ 6 #define ZIRCON_SYSTEM_HOST_FIDL_INCLUDE_FIDL_JSON_GENERATOR_H_ 7 8 #include <memory> 9 #include <sstream> 10 #include <string> 11 #include <vector> 12 13 #include "flat_ast.h" 14 #include "string_view.h" 15 16 namespace fidl { 17 18 // Methods or functions named "Emit..." are the actual interface to 19 // the JSON output. 20 21 // Methods named "Generate..." directly generate JSON output via the 22 // "Emit" routines. 23 24 // Methods named "Produce..." indirectly generate JSON output by calling 25 // the Generate methods, and should not call the "Emit" functions 26 // directly. 27 28 class JSONGenerator { 29 public: JSONGenerator(const flat::Library * library)30 explicit JSONGenerator(const flat::Library* library) 31 : library_(library) {} 32 33 ~JSONGenerator() = default; 34 35 std::ostringstream Produce(); 36 37 private: 38 enum class Position { 39 kFirst, 40 kSubsequent, 41 }; 42 43 void GenerateEOF(); 44 45 template <typename Iterator> 46 void GenerateArray(Iterator begin, Iterator end); 47 48 template <typename Collection> 49 void GenerateArray(const Collection& collection); 50 51 void GenerateObjectPunctuation(Position position); 52 53 template <typename Callback> 54 void GenerateObject(Callback callback); 55 56 template <typename Type> 57 void GenerateObjectMember(StringView key, const Type& value, 58 Position position = Position::kSubsequent); 59 60 template <typename T> 61 void Generate(const std::unique_ptr<T>& value); 62 63 void Generate(const flat::Decl* decl); 64 65 template <typename T> 66 void Generate(const std::vector<T>& value); 67 68 void Generate(bool value); 69 void Generate(StringView value); 70 void Generate(SourceLocation value); 71 void Generate(uint32_t value); 72 73 void Generate(types::HandleSubtype value); 74 void Generate(types::Nullability value); 75 void Generate(types::PrimitiveSubtype value); 76 77 void Generate(const raw::Identifier& value); 78 void Generate(const raw::Literal& value); 79 void Generate(const raw::Type& value); 80 void Generate(const raw::Attribute& value); 81 void Generate(const raw::AttributeList& value); 82 void Generate(const raw::Ordinal& value); 83 84 void Generate(const flat::Name& value); 85 void Generate(const flat::Type& value); 86 void Generate(const flat::Constant& value); 87 void Generate(const flat::Const& value); 88 void Generate(const flat::Enum& value); 89 void Generate(const flat::Enum::Member& value); 90 void Generate(const flat::Interface& value); 91 void Generate(const flat::Interface::Method* value); 92 void GenerateRequest(const std::string& prefix, const flat::Struct& value); 93 void Generate(const flat::Struct& value); 94 void Generate(const flat::Struct::Member& value); 95 void Generate(const flat::Table& value); 96 void Generate(const flat::Table::Member& value); 97 void Generate(const flat::Union& value); 98 void Generate(const flat::Union::Member& value); 99 void Generate(const flat::Library* library); 100 101 void GenerateDeclarationsEntry(int count, const flat::Name& name, StringView decl); 102 void GenerateDeclarationsMember(const flat::Library* library, 103 Position position = Position::kSubsequent); 104 105 const flat::Library* library_; 106 int indent_level_; 107 std::ostringstream json_file_; 108 }; 109 110 } // namespace fidl 111 112 #endif // ZIRCON_SYSTEM_HOST_FIDL_INCLUDE_FIDL_JSON_GENERATOR_H_ 113