Skip to content

Commit 5248117

Browse files
committed
format doc基础
1 parent 0616805 commit 5248117

File tree

8 files changed

+173
-3
lines changed

8 files changed

+173
-3
lines changed

CodeFormat/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.11)
22

33
project(CodeFormat)
44

5-
add_executable(CodeFormat)
5+
add_executable(CodeFormat ../include/CodeService/Format/Analyzer/FormatDocAnalyze.h)
66

77
add_dependencies(CodeFormat CodeService Util)
88

CodeService/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
project(CodeService)
44

5-
add_library(CodeService STATIC)
5+
add_library(CodeService STATIC src/Format/Analyzer/FormatDocAnalyze.cpp)
66

77
add_dependencies(CodeService LuaParser Util)
88

@@ -28,6 +28,7 @@ target_sources(CodeService
2828
${CodeService_SOURCE_DIR}/src/Format/Analyzer/SpaceAnalyzer.cpp
2929
${CodeService_SOURCE_DIR}/src/Format/Analyzer/IndentationAnalyzer.cpp
3030
${CodeService_SOURCE_DIR}/src/Format/Analyzer/LineBreakAnalyzer.cpp
31+
${CodeService_SOURCE_DIR}/src/Format/Analyzer/FormatDocAnalyze.cpp
3132
${CodeService_SOURCE_DIR}/src/Format/Analyzer/AlignAnalyzer.cpp
3233
${CodeService_SOURCE_DIR}/src/Format/Analyzer/TokenAnalyzer.cpp
3334
${CodeService_SOURCE_DIR}/src/Format/Analyzer/FormatResolve.cpp
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
#include "CodeService/Format/Analyzer/FormatDocAnalyze.h"
2+
#include "LuaParser/Lexer/LuaTokenTypeDetail.h"
3+
#include "Util/StringUtil.h"
4+
#include "LuaParser/Lexer/TextReader.h"
5+
6+
FormatDocAnalyze::FormatDocAnalyze() {
7+
8+
}
9+
10+
void FormatDocAnalyze::Analyze(FormatState &f, const LuaSyntaxTree &t) {
11+
// for (auto syntaxNode: t.GetSyntaxNodes()) {
12+
// switch (syntaxNode.GetTokenKind(t)) {
13+
// case TK_SHORT_COMMENT: {
14+
// if (syntaxNode.GetParent(t).GetSyntaxKind(t) == LuaSyntaxNodeKind::Block) {
15+
//
16+
//
17+
// break;
18+
// }
19+
// }
20+
// default: {
21+
//
22+
// }
23+
// }
24+
// }
25+
}
26+
27+
void FormatDocAnalyze::ComplexAnalyze(FormatState &f, const LuaSyntaxTree &t) {
28+
FormatAnalyzer::ComplexAnalyze(f, t);
29+
}
30+
31+
void
32+
FormatDocAnalyze::Query(FormatState &f, LuaSyntaxNode &syntaxNode, const LuaSyntaxTree &t, FormatResolve &resolve) {
33+
34+
}
35+
36+
bool IsWhiteSpaces(int c) {
37+
return c > 0 && std::isspace(c);
38+
}
39+
40+
bool ActionStart(int c) {
41+
return c > 0 && std::isalpha(c);
42+
}
43+
44+
bool ActionContinue(int c) {
45+
return c > 0 && (std::isalnum(c) || c == '-');
46+
}
47+
48+
void FormatDocAnalyze::AnalyzeDocFormat(LuaSyntaxNode n, FormatState &f, const LuaSyntaxTree &t) {
49+
auto text = n.GetText(t);
50+
TextReader textReader(text);
51+
52+
enum class ParseState {
53+
Init,
54+
TagFormat,
55+
Action,
56+
List
57+
} state = ParseState::Init;
58+
59+
while (!textReader.IsEof()) {
60+
textReader.ResetBuffer();
61+
62+
if (IsWhiteSpaces(textReader.GetCurrentChar())) {
63+
textReader.EatWhile(IsWhiteSpaces);
64+
continue;
65+
}
66+
67+
switch (state) {
68+
case ParseState::Init: {
69+
if (textReader.GetCurrentChar() == '-') {
70+
auto dashNum = textReader.EatWhen('-');
71+
if (dashNum == 3) {
72+
state = ParseState::TagFormat;
73+
break;
74+
}
75+
}
76+
return;
77+
}
78+
case ParseState::TagFormat: {
79+
if (textReader.GetCurrentChar() == '@') {
80+
textReader.NextChar();
81+
textReader.EatWhile([](int c) { return c > 0 && std::isalpha(c); });
82+
if (textReader.GetSaveText() == "format") {
83+
state = ParseState::Action;
84+
break;
85+
}
86+
}
87+
return;
88+
}
89+
case ParseState::Action: {
90+
if (ActionStart(textReader.GetCurrentChar())) {
91+
textReader.EatWhile(ActionContinue);
92+
auto action = textReader.GetSaveText();
93+
if (action == "disable") {
94+
95+
} else if (action == "disable-next") {
96+
97+
} else if (action == "on") {
98+
state = ParseState::List;
99+
// todo
100+
break;
101+
} else if (action == "on-next") {
102+
state = ParseState::List;
103+
// todo
104+
105+
break;
106+
}
107+
}
108+
109+
return;
110+
}
111+
case ParseState::List : {
112+
return;
113+
}
114+
}
115+
}
116+
}

CodeService/src/Format/FormatState.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "CodeService/Format/Analyzer/LineBreakAnalyzer.h"
55
#include "CodeService/Format/Analyzer/AlignAnalyzer.h"
66
#include "CodeService/Format/Analyzer/TokenAnalyzer.h"
7+
#include "CodeService/Format/Analyzer/FormatDocAnalyze.h"
78

89
FormatState::FormatState()
910
: _currentWidth(0) {
@@ -44,6 +45,7 @@ void FormatState::Analyze(const LuaSyntaxTree &t) {
4445
AddAnalyzer<LineBreakAnalyzer>();
4546
AddAnalyzer<AlignAnalyzer>();
4647
AddAnalyzer<TokenAnalyzer>();
48+
AddAnalyzer<FormatDocAnalyze>();
4749

4850
_fileEndOfLine = t.GetFile().GetEndOfLine();
4951
for (const auto &analyzer: _analyzers) {

LuaParser/src/Lexer/TextReader.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ void TextReader::ResetBuffer() {
7878
}
7979

8080
std::string_view TextReader::GetSaveText() const {
81-
if(_hasSaveText) {
81+
if (_hasSaveText) {
8282
return _text.substr(_buffStart, _buffIndex - _buffStart + 1);
8383
}
8484
return _text.substr(_buffStart, 0);
@@ -87,3 +87,21 @@ std::string_view TextReader::GetSaveText() const {
8787
bool TextReader::IsEof() const {
8888
return _isEof;
8989
}
90+
91+
std::size_t TextReader::EatWhen(int ch) {
92+
std::size_t count = 0;
93+
while (!IsEof() && GetCurrentChar() == ch) {
94+
SaveAndNext();
95+
count++;
96+
}
97+
return count;
98+
}
99+
100+
std::size_t TextReader::EatWhile(const std::function<bool(int)> &fn) {
101+
std::size_t count = 0;
102+
while (!IsEof() && fn(GetCurrentChar())) {
103+
SaveAndNext();
104+
count++;
105+
}
106+
return count;
107+
}

include/CodeService/Format/Analyzer/FormatAnalyzerType.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ enum class FormatAnalyzerType {
66
IndentationAnalyzer,
77
AlignAnalyzer,
88
TokenAnalyzer,
9+
FormatDocAnalyze,
910

1011
Count,
1112
};
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#pragma once
2+
3+
#include "FormatAnalyzer.h"
4+
5+
class FormatDocAnalyze : public FormatAnalyzer {
6+
public:
7+
DECLARE_FORMAT_ANALYZER(FormatDocAnalyze)
8+
9+
enum class FormatType {
10+
DisableNext,
11+
Disable,
12+
Set
13+
};
14+
15+
FormatDocAnalyze();
16+
17+
void Analyze(FormatState &f, const LuaSyntaxTree &t) override;
18+
19+
void ComplexAnalyze(FormatState &f, const LuaSyntaxTree &t) override;
20+
21+
void Query(FormatState &f, LuaSyntaxNode &syntaxNode, const LuaSyntaxTree &t, FormatResolve &resolve) override;
22+
23+
private:
24+
void AnalyzeDocFormat(LuaSyntaxNode n, FormatState &f, const LuaSyntaxTree &t);
25+
26+
// std::unordered_map<std::size_t, >
27+
};

include/LuaParser/Lexer/TextReader.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22
#include <string_view>
3+
#include <functional>
34
#include "LuaParser/Types/TextRange.h"
45

56
class TextReader {
@@ -26,6 +27,10 @@ class TextReader {
2627

2728
void ResetBuffer();
2829

30+
std::size_t EatWhen(int ch);
31+
32+
std::size_t EatWhile(const std::function<bool(int ch)>& fn);
33+
2934
bool IsEof() const;
3035
private:
3136
std::string_view _text;

0 commit comments

Comments
 (0)