Skip to content

Commit 9c7b7cb

Browse files
committed
info tree util
1 parent dd13f20 commit 9c7b7cb

File tree

14 files changed

+481
-231
lines changed

14 files changed

+481
-231
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,25 @@
11
#include "CodeService/Config/LuaDiagnosticStyle.h"
22

3+
#define BOOL_OPTION(op) \
4+
if (auto n = root.GetValue(#op); n.IsBool()) { \
5+
op = n.AsBool(); \
6+
}
7+
8+
void LuaDiagnosticStyle::ParseTree(InfoTree &tree) {
9+
auto root = tree.GetRoot();
10+
11+
BOOL_OPTION(code_style_check);
12+
13+
BOOL_OPTION(name_style_check);
14+
15+
BOOL_OPTION(spell_check);
16+
17+
// if (auto n = root.GetValue("local_name_style"); n.IsObject()) {
18+
// if (n.IsArray()) {
19+
// local_name_style.clear();
20+
// for (auto c: n.GetChildren()) {
21+
// if (c.IsNull())
22+
// }
23+
// }
24+
// }
25+
}

Util/CMakeLists.txt

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,33 @@ add_subdirectory(${LuaCodeStyle_SOURCE_DIR}/3rd/uriparser uriparser.out)
77
add_library(Util STATIC)
88

99
target_include_directories(Util PUBLIC
10-
${LuaCodeStyle_SOURCE_DIR}/include
11-
PRIVATE
12-
src
13-
${LuaCodeStyle_SOURCE_DIR}/3rd/uriparser/include
14-
${LuaCodeStyle_SOURCE_DIR}/3rd/wildcards/include
15-
)
10+
${LuaCodeStyle_SOURCE_DIR}/include
11+
PRIVATE
12+
src
13+
${LuaCodeStyle_SOURCE_DIR}/3rd/uriparser/include
14+
${LuaCodeStyle_SOURCE_DIR}/3rd/wildcards/include
15+
)
1616

1717
target_sources(Util
18-
PUBLIC
19-
${LuaCodeStyle_SOURCE_DIR}/include/Util/CommandLine.h
20-
${LuaCodeStyle_SOURCE_DIR}/include/Util/StringUtil.h
21-
${LuaCodeStyle_SOURCE_DIR}/include/Util/Utf8.h
22-
${LuaCodeStyle_SOURCE_DIR}/include/Util/Url.h
23-
${LuaCodeStyle_SOURCE_DIR}/include/Util/SymSpell/SymSpell.h
24-
25-
PRIVATE
26-
${Util_SOURCE_DIR}/src/CommandLine.cpp
27-
${Util_SOURCE_DIR}/src/StringUtil.cpp
28-
${Util_SOURCE_DIR}/src/Utf8.cpp
29-
${Util_SOURCE_DIR}/src/Url.cpp
30-
${Util_SOURCE_DIR}/src/FileFinder.cpp
31-
${Util_SOURCE_DIR}/src/SymSpell/SymSpell.cpp
32-
${Util_SOURCE_DIR}/src/SymSpell/SuggestItem.cpp
33-
${Util_SOURCE_DIR}/src/SymSpell/EditDistance.cpp
34-
)
18+
PUBLIC
19+
${LuaCodeStyle_SOURCE_DIR}/include/Util/CommandLine.h
20+
${LuaCodeStyle_SOURCE_DIR}/include/Util/StringUtil.h
21+
${LuaCodeStyle_SOURCE_DIR}/include/Util/Utf8.h
22+
${LuaCodeStyle_SOURCE_DIR}/include/Util/Url.h
23+
${LuaCodeStyle_SOURCE_DIR}/include/Util/SymSpell/SymSpell.h
24+
25+
PRIVATE
26+
${Util_SOURCE_DIR}/src/CommandLine.cpp
27+
${Util_SOURCE_DIR}/src/StringUtil.cpp
28+
${Util_SOURCE_DIR}/src/Utf8.cpp
29+
${Util_SOURCE_DIR}/src/Url.cpp
30+
${Util_SOURCE_DIR}/src/FileFinder.cpp
31+
${Util_SOURCE_DIR}/src/SymSpell/SymSpell.cpp
32+
${Util_SOURCE_DIR}/src/SymSpell/SuggestItem.cpp
33+
${Util_SOURCE_DIR}/src/SymSpell/EditDistance.cpp
34+
${Util_SOURCE_DIR}/src/InfoTree/InfoTree.cpp
35+
${Util_SOURCE_DIR}/src/InfoTree/InfoNode.cpp
36+
)
3537

3638

3739
target_link_libraries(Util PUBLIC uriparser)

Util/src/InfoTree/InfoNode.cpp

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
#include "Util/InfoTree/InfoNode.h"
2+
#include "Util/InfoTree/InfoTree.h"
3+
4+
InfoNode::InfoNode(std::size_t index, InfoTree *tree)
5+
: _index(index), _tree(tree) {
6+
}
7+
8+
InfoKind InfoNode::GetKind() const {
9+
if (_index < _tree->_nodeOrInfos.size()) {
10+
return _tree->_nodeOrInfos[_index].Kind;
11+
}
12+
return InfoKind::None;
13+
}
14+
15+
bool InfoNode::IsNull() const {
16+
return _index == 0;
17+
}
18+
19+
bool InfoNode::IsObject() const {
20+
return GetKind() == InfoKind::Object;
21+
}
22+
23+
bool InfoNode::IsArray() const {
24+
return GetKind() == InfoKind::Array;
25+
}
26+
27+
bool InfoNode::IsNumber() const {
28+
return GetKind() == InfoKind::Number;
29+
}
30+
31+
bool InfoNode::IsString() const {
32+
return GetKind() == InfoKind::String;
33+
}
34+
35+
bool InfoNode::IsBool() const {
36+
return GetKind() == InfoKind::Bool;
37+
}
38+
39+
void InfoNode::AddChild(InfoNode n) {
40+
if (_index < _tree->_nodeOrInfos.size()) {
41+
auto node = _tree->_nodeOrInfos[_index];
42+
if (node.Kind == InfoKind::Array) {
43+
_tree->_arrayChildren[node.Data.ChildIndex].push_back(n._index);
44+
}
45+
}
46+
}
47+
48+
void InfoNode::AddChild(std::string_view key, InfoNode n) {
49+
if (_index < _tree->_nodeOrInfos.size()) {
50+
auto node = _tree->_nodeOrInfos[_index];
51+
if (node.Kind == InfoKind::Object) {
52+
_tree->_mapChildren[node.Data.ChildIndex].insert({std::string(key), n._index});
53+
}
54+
}
55+
}
56+
57+
std::string InfoNode::AsString() const {
58+
if (_index < _tree->_nodeOrInfos.size()) {
59+
if (_tree->_nodeOrInfos[_index].Kind == InfoKind::String) {
60+
return _tree->_stringValues[_tree->_nodeOrInfos[_index].Data.StringIndex];
61+
}
62+
}
63+
return "";
64+
}
65+
66+
double InfoNode::AsDouble() const {
67+
if (_index < _tree->_nodeOrInfos.size()) {
68+
auto &node = _tree->_nodeOrInfos[_index];
69+
if (node.Kind == InfoKind::Number) {
70+
return node.Data.NumberValue;
71+
}
72+
}
73+
return 0.0;
74+
}
75+
76+
int InfoNode::AsInt() const {
77+
if (_index < _tree->_nodeOrInfos.size()) {
78+
auto &node = _tree->_nodeOrInfos[_index];
79+
if (node.Kind == InfoKind::Number) {
80+
return node.Data.IntValue;
81+
}
82+
}
83+
return 0;
84+
}
85+
86+
bool InfoNode::AsBool() const {
87+
if (_index < _tree->_nodeOrInfos.size()) {
88+
auto &node = _tree->_nodeOrInfos[_index];
89+
if (node.Kind == InfoKind::Bool) {
90+
return node.Data.BoolValue;
91+
}
92+
}
93+
return false;
94+
}
95+
96+
std::vector<InfoNode> InfoNode::GetChildren() const {
97+
std::vector<InfoNode> result;
98+
if (_index < _tree->_nodeOrInfos.size()) {
99+
auto &node = _tree->_nodeOrInfos[_index];
100+
if (node.Kind == InfoKind::Array) {
101+
for (auto i: _tree->_arrayChildren[node.Data.ChildIndex]) {
102+
result.emplace_back(i, _tree);
103+
}
104+
}
105+
}
106+
return result;
107+
}
108+
109+
InfoNode InfoNode::GetValue(std::string_view key) const {
110+
if (_index < _tree->_nodeOrInfos.size()) {
111+
auto &node = _tree->_nodeOrInfos[_index];
112+
if (node.Kind == InfoKind::Object) {
113+
auto &umap = _tree->_mapChildren[node.Data.ChildIndex];
114+
std::string keyString(key);
115+
auto it = umap.find(keyString);
116+
if (it != umap.end()) {
117+
return InfoNode(it->second, _tree);
118+
}
119+
}
120+
}
121+
return InfoNode(0, _tree);
122+
}
123+

Util/src/InfoTree/InfoTree.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#include "Util/InfoTree/InfoTree.h"
2+
3+
InfoTree::InfoTree() {
4+
InfoOrNode n;
5+
n.Kind = InfoKind::None;
6+
n.Data.ChildIndex = 0;
7+
_nodeOrInfos.push_back(n);
8+
}
9+
10+
InfoNode InfoTree::CreateObject() {
11+
auto nodeIndex = _nodeOrInfos.size();
12+
auto childIndex = _mapChildren.size();
13+
_mapChildren.emplace_back();
14+
15+
InfoOrNode n;
16+
n.Kind = InfoKind::Object;
17+
n.Data.ChildIndex = childIndex;
18+
_nodeOrInfos.push_back(n);
19+
20+
return InfoNode(nodeIndex, this);
21+
}
22+
23+
InfoNode InfoTree::CreateArray() {
24+
auto nodeIndex = _nodeOrInfos.size();
25+
auto childIndex = _arrayChildren.size();
26+
_arrayChildren.emplace_back();
27+
28+
InfoOrNode n;
29+
n.Kind = InfoKind::Array;
30+
n.Data.ChildIndex = childIndex;
31+
_nodeOrInfos.push_back(n);
32+
33+
return InfoNode(nodeIndex, this);
34+
}
35+
36+
InfoNode InfoTree::CreateString(std::string_view s) {
37+
auto nodeIndex = _nodeOrInfos.size();
38+
auto valueIndex = _stringValues.size();
39+
_arrayChildren.emplace_back();
40+
41+
InfoOrNode n;
42+
n.Kind = InfoKind::String;
43+
n.Data.StringIndex = valueIndex;
44+
_nodeOrInfos.push_back(n);
45+
46+
return InfoNode(nodeIndex, this);
47+
}
48+
49+
InfoNode InfoTree::GetRoot() {
50+
if (_nodeOrInfos.size() <= 1) {
51+
CreateObject();
52+
}
53+
return InfoNode(1, this);
54+
}

include/CodeService/Config/LuaDiagnosticStyle.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
#pragma once
22

3+
#include "LuaDiagnosticStyleEnum.h"
4+
#include "Util/InfoTree/InfoTree.h"
5+
#include <memory>
36
#include <string>
47
#include <string_view>
5-
#include <memory>
68
#include <vector>
7-
#include "LuaDiagnosticStyleEnum.h"
89

910
class LuaDiagnosticStyle {
1011
public:
1112
LuaDiagnosticStyle() = default;
1213

14+
void ParseTree(InfoTree& tree);
15+
1316
bool code_style_check = true;
1417

1518
bool name_style_check = true;

include/Util/CommandLine.h

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
#pragma once
22

3-
#include <string>
43
#include <map>
4+
#include <set>
5+
#include <string>
56
#include <type_traits>
67
#include <vector>
7-
#include <set>
88

99

1010
class CommandLine;
@@ -44,10 +44,8 @@ class CommandLineTargetOptions {
4444
}
4545

4646
_shortMap.insert({std::string(shortName), std::string(name)});
47-
_args.insert({
48-
std::string(name),
49-
CommandLineOption{"", std::string(description), type, false}
50-
});
47+
_args.insert({std::string(name),
48+
CommandLineOption{"", std::string(description), type, false}});
5149
return *this;
5250
}
5351

@@ -124,4 +122,3 @@ class CommandLine {
124122
std::vector<std::string> _errors;
125123
std::string _usage;
126124
};
127-

include/Util/FileFinder.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#pragma once
22

3-
#include <string>
4-
#include <string_view>
53
#include <filesystem>
64
#include <set>
5+
#include <string>
6+
#include <string_view>
77
#include <vector>
88

99
class FileFinder {

include/Util/InfoTree/InfoKind.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#pragma once
2+
3+
enum class InfoKind {
4+
None,
5+
Object,
6+
Array,
7+
String,
8+
Bool,
9+
Number
10+
};

include/Util/InfoTree/InfoNode.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#pragma once
2+
3+
#include "InfoKind.h"
4+
#include <string>
5+
#include <unordered_map>
6+
7+
class InfoTree;
8+
9+
class InfoNode {
10+
public:
11+
InfoNode(std::size_t index, InfoTree *tree);
12+
13+
InfoKind GetKind() const;
14+
15+
bool IsNull() const;
16+
17+
bool IsObject() const;
18+
19+
bool IsArray() const;
20+
21+
bool IsNumber() const;
22+
23+
bool IsString() const;
24+
25+
bool IsBool() const;
26+
27+
void AddChild(InfoNode n);
28+
29+
void AddChild(std::string_view key, InfoNode n);
30+
31+
std::string AsString() const;
32+
33+
double AsDouble() const;
34+
35+
int AsInt() const;
36+
37+
bool AsBool() const;
38+
39+
std::vector<InfoNode> GetChildren() const;
40+
41+
InfoNode GetValue(std::string_view key) const;
42+
private:
43+
std::size_t _index;
44+
InfoTree *_tree;
45+
};

0 commit comments

Comments
 (0)