1 /* 2 * Copyright (c) 2021-2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef PANDA_ASSEMBLER_IDE_HELPERS_H 17 #define PANDA_ASSEMBLER_IDE_HELPERS_H 18 19 #include <sstream> 20 21 namespace panda::pandasm { 22 23 // NOLINTBEGIN(misc-non-private-member-variables-in-classes) 24 struct SourcePosition { 25 size_t line = 0; 26 size_t column = 0; 27 JsonSerializeSourcePosition28 std::string JsonSerialize() const 29 { 30 std::stringstream ss; 31 ss << "{ " 32 << "\"line\": " << line << ", " 33 << "\"column\": " << column << " }"; 34 return ss.str(); 35 } 36 }; 37 38 struct SourceLocation { 39 SourcePosition begin; 40 SourcePosition end; 41 JsonSerializeSourceLocation42 std::string JsonSerialize() const 43 { 44 std::stringstream ss; 45 ss << "{ " 46 << "\"begin\": " << begin.JsonSerialize() << ", " 47 << "\"end\": " << end.JsonSerialize() << " }"; 48 return ss.str(); 49 } 50 }; 51 // NOLINTEND(misc-non-private-member-variables-in-classes) 52 53 template <typename T> JsonSerializeItemBody(const T & item)54std::string JsonSerializeItemBody(const T &item) 55 { 56 std::stringstream ss; 57 std::string quotedName = "\"" + item.name + "\""; 58 ss << "{ " 59 << "\"name\": " << quotedName; 60 if (item.fileLocation && item.fileLocation->isDefined) { 61 ss << ", " 62 << "\"bodyLocation\": " << item.bodyLocation.JsonSerialize() << " }"; 63 } else { 64 ss << " }"; 65 } 66 return ss.str(); 67 } 68 69 template <typename T> JsonSerializeProgramItems(const T & itemTable)70std::string JsonSerializeProgramItems(const T &itemTable) 71 { 72 std::stringstream ss; 73 ss << "[ "; 74 auto it = itemTable.begin(); 75 if (it != itemTable.end()) { 76 ss << JsonSerializeItemBody(it->second); 77 ++it; 78 } 79 while (it != itemTable.end()) { 80 ss << ", " << JsonSerializeItemBody(it->second); 81 ++it; 82 } 83 ss << " ]"; 84 return ss.str(); 85 } 86 } // namespace panda::pandasm 87 88 #endif // PANDA_ASSEMBLER_IDE_HELPERS_H 89