• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-2022 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 ASSEMBLER_IDE_HELPERS_H
17 #define ASSEMBLER_IDE_HELPERS_H
18 
19 #include <sstream>
20 
21 namespace panda::pandasm {
22 
23 struct SourcePosition {
24     size_t line = 0;
25     size_t column = 0;
26 
JsonSerializeSourcePosition27     std::string JsonSerialize() const
28     {
29         std::stringstream ss;
30         ss << "{ "
31            << "\"line\": " << line << ", "
32            << "\"column\": " << column << " }";
33         return ss.str();
34     }
35 };
36 
37 struct SourceLocation {
38     SourcePosition begin;
39     SourcePosition end;
40 
JsonSerializeSourceLocation41     std::string JsonSerialize() const
42     {
43         std::stringstream ss;
44         ss << "{ "
45            << "\"begin\": " << begin.JsonSerialize() << ", "
46            << "\"end\": " << end.JsonSerialize() << " }";
47         return ss.str();
48     }
49 };
50 
51 template <typename T>
JsonSerializeItemBody(const T & item)52 std::string JsonSerializeItemBody(const T &item)
53 {
54     std::stringstream ss;
55     std::string quoted_name = "\"" + item.name + "\"";
56     ss << "{ "
57        << "\"name\": " << quoted_name;
58     if (item.file_location->is_defined) {
59         ss << ", "
60            << "\"bodyLocation\": " << item.body_location.JsonSerialize() << " }";
61     } else {
62         ss << " }";
63     }
64     return ss.str();
65 }
66 
67 template <typename T>
JsonSerializeProgramItems(const T & item_table)68 std::string JsonSerializeProgramItems(const T &item_table)
69 {
70     std::stringstream ss;
71     ss << "[ ";
72     auto it = item_table.begin();
73     if (it != item_table.end()) {
74         ss << JsonSerializeItemBody(it->second);
75         ++it;
76     }
77     while (it != item_table.end()) {
78         ss << ", " << JsonSerializeItemBody(it->second);
79         ++it;
80     }
81     ss << " ]";
82     return ss.str();
83 }
84 }  // namespace panda::pandasm
85 
86 #endif  // ASSEMBLER_IDE_HELPERS_H
87