FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
EmmyLuaCodeStyle/CodeFormat/src/LuaCheck.cpp at master · InfResearch/EmmyLuaCodeStyle · GitHub
InfResearch
EmmyLuaCodeStyle
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
EmmyLuaCodeStyle
/
CodeFormat
/
src
/
LuaCheck.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
148 lines (131 loc) · 5.1 KB
Breadcrumbs
EmmyLuaCodeStyle
/
CodeFormat
/
src
/
LuaCheck.cpp
Copy path
File metadata and controls
148 lines (131 loc) · 5.1 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#
include
"
LuaCheck.h
"
#
include
"
nlohmann/json.hpp
"
#
include
<
CodeFormatCore/Diagnostic/DiagnosticBuilder.h
>
#
include
<
LuaParser/File/LuaSource.h
>
#
include
<
LuaParser/Lexer/LuaLexer.h
>
#
include
<
LuaParser/Parse/LuaParser.h
>
#
include
<
Util/FileFinder.h
>
#
include
<
Util/format.h
>
#
include
<
iostream
>
bool
LuaCheck::Check
(
const
FormatContext &context) {
switch
(context.
GetWorkMode
()) {
case
WorkMode::File:
case
WorkMode::Stdin: {
auto
inputPath = context.
GetInputPath
();
if
(
CheckSingleFile
(context, inputPath,
std::move
(context.
GetInputText
()), context.
GetStyle
(inputPath))) {
std::cerr <<
util::format
(
"
Check {} ... ok
"
, inputPath) << std::endl;
return
true
;
}
break
;
}
case
WorkMode::Workspace: {
return
CheckWorkspace
(context);
}
}
return
false
;
}
void
DiagnosticInspection
(std::string_view message, TextRange range, LuaSource &file,
std::string_view path) {
auto
startLine = file.
GetLine
(range.
StartOffset
);
auto
startChar = file.
GetColumn
(range.
StartOffset
);
auto
endLine = file.
GetLine
(range.
GetEndOffset
());
auto
endChar = file.
GetColumn
(range.
GetEndOffset
());
std::cerr <<
util::format
(
"
\t
{}({}:{} to {}:{}): {}
"
, path, startLine +
1
, startChar, endLine +
1
, endChar,
message)
<< std::endl;
}
nlohmann::json
DiagnosticToJson
(std::string_view message, TextRange range, LuaSource &file) {
auto
startLine = file.
GetLine
(range.
StartOffset
);
auto
startChar = file.
GetColumn
(range.
StartOffset
);
auto
endLine = file.
GetLine
(range.
GetEndOffset
());
auto
endChar = file.
GetColumn
(range.
GetEndOffset
());
auto
obj =
nlohmann::json::object
();
obj[
"
message
"
] = message;
obj[
"
range
"
] = {
{
"
start
"
, {{
"
line
"
, startLine}, {
"
character
"
, startChar}}},
{
"
end
"
, {{
"
line
"
, endLine}, {
"
character
"
, endChar}} }
};
return
obj;
}
bool
LuaCheck::CheckSingleFile
(
const
FormatContext &context, std::string_view path, std::string &&sourceText, LuaStyle style) {
auto
file = std::make_shared<LuaSource>(
std::move
(sourceText));
LuaLexer
luaLexer
(file);
if
(context.
IsNonStandardLua
()) {
luaLexer.
SupportNonStandardSymbol
();
}
if
(context.
IsCLikeCommentsSupport
()) {
luaLexer.
SupportCLikeComments
();
}
luaLexer.
Parse
();
LuaParser
p
(file,
std::move
(luaLexer.
GetTokens
()));
p.
Parse
();
if
(p.
HasError
()) {
auto
&errors = p.
GetErrors
();
std::cout <<
util::format
(
"
Check {} ...
\t
{} error
"
, path, errors.
size
()) << std::endl;
for
(
auto
&error: errors) {
DiagnosticInspection
(error.
ErrorMessage
, error.
ErrorRange
, *file, path);
}
return
false
;
}
LuaSyntaxTree t;
t.
BuildTree
(p);
auto
diagnosticStyle = context.
GetDiagnosticStyle
();
DiagnosticBuilder
diagnosticBuilder
(style, diagnosticStyle);
diagnosticBuilder.
CodeStyleCheck
(t);
diagnosticBuilder.
NameStyleCheck
(t);
auto
diagnostics = diagnosticBuilder.
GetDiagnosticResults
(t);
if
(diagnostics.
empty
()) {
return
true
;
}
if
(context.
IsDumpJson
()) {
std::cerr <<
util::format
(
"
Check {}
\t
{} warning
"
, path, diagnostics.
size
()) << std::endl;
auto
arr =
nlohmann::json::array
();
for
(
auto
&d: diagnostics) {
auto
obj =
DiagnosticToJson
(d.
Message
, d.
Range
, *file);
arr.
push_back
(obj);
}
std::cout << arr.
dump
() << std::endl;
return
false
;
}
else
{
std::cerr <<
util::format
(
"
Check {}
\t
{} warning
"
, path, diagnostics.
size
()) << std::endl;
for
(
auto
&d: diagnostics) {
DiagnosticInspection
(d.
Message
, d.
Range
, *file, path);
}
return
false
;
}
}
bool
LuaCheck::CheckWorkspace
(
const
FormatContext &context) {
auto
workspace = context.
GetWorkspace
();
bool
diagnosis =
false
;
FileFinder
finder
(workspace);
finder.
AddFindExtension
(
"
.lua
"
);
finder.
AddFindExtension
(
"
.lua.txt
"
);
finder.
AddIgnoreDirectory
(
"
.git
"
);
finder.
AddIgnoreDirectory
(
"
.github
"
);
finder.
AddIgnoreDirectory
(
"
.svn
"
);
finder.
AddIgnoreDirectory
(
"
.idea
"
);
finder.
AddIgnoreDirectory
(
"
.vs
"
);
finder.
AddIgnoreDirectory
(
"
.vscode
"
);
for
(
const
auto
&pattern: context.
GetIgnorePattern
()) {
finder.
AddIgnorePatterns
(pattern);
}
auto
files = finder.
FindFiles
();
for
(
auto
&filePath: files) {
auto
opText =
ReadFile
(filePath);
std::string relativePath = filePath;
if
(!workspace.
empty
()) {
relativePath =
string_util::GetFileRelativePath
(workspace, filePath);
}
if
(opText.
has_value
()) {
auto
style = context.
GetStyle
(relativePath);
if
(
CheckSingleFile
(context, relativePath,
std::move
(opText.
value
()), style)) {
std::cerr <<
util::format
(
"
Check {} ok.
"
, relativePath) << std::endl;
}
else
{
diagnosis =
true
;
}
}
else
{
std::cerr <<
util::format
(
"
Can not read file {}
"
, relativePath) << std::endl;
}
}
return
!diagnosis;
}
Back
|
FazBrowse Home
|
New Git URL