• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2024 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 ark::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         ss << " }";
49         return ss.str();
50     }
51 };
52 // NOLINTEND(misc-non-private-member-variables-in-classes)
53 
54 template <typename T>
JsonSerializeItemBody(const T & item)55 std::string JsonSerializeItemBody(const T &item)
56 {
57     std::stringstream ss;
58     std::string quotedName = "\"" + item.name + "\"";
59     ss << "{ "
60        << "\"name\": " << quotedName;
61     if (item.fileLocation && item.fileLocation->isDefined) {
62         ss << ", "
63            << "\"bodyLocation\": " << item.bodyLocation.JsonSerialize() << " }";
64     } else {
65         ss << " }";
66     }
67     return ss.str();
68 }
69 
70 template <typename T>
JsonSerializeProgramItems(const T & itemTable)71 std::string JsonSerializeProgramItems(const T &itemTable)
72 {
73     std::stringstream ss;
74     ss << "[ ";
75     auto it = itemTable.begin();
76     if (it != itemTable.end()) {
77         ss << JsonSerializeItemBody(it->second);
78         ++it;
79     }
80     while (it != itemTable.end()) {
81         ss << ", " << JsonSerializeItemBody(it->second);
82         ++it;
83     }
84     ss << " ]";
85     return ss.str();
86 }
87 }  // namespace ark::pandasm
88 
89 #endif  // PANDA_ASSEMBLER_IDE_HELPERS_H
90