Skip to content

Commit

Permalink
support clike comments
Browse files Browse the repository at this point in the history
  • Loading branch information
CppCXY committed Dec 11, 2023
1 parent 1b5763c commit ebcd76a
Show file tree
Hide file tree
Showing 13 changed files with 127 additions and 17 deletions.
13 changes: 12 additions & 1 deletion CodeFormat/src/CodeFormat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ int main(int argc, char **argv) {
"Use file wildcards to specify how to ignore files\n"
"\t\tseparated by ';'")
.Add<bool>("non-standard", "", "Enable non-standard formatting")
.Add<bool>("c-like-comments", "", "Enable c-like-comments formatting")
.EnableKeyValueArgs();
cmd.AddTarget("rangeformat")
.Add<std::string>("file", "f", "Specify the input file")
Expand All @@ -74,6 +75,7 @@ int main(int argc, char **argv) {
.Add<std::string>("range-line", "", "the format is startline:endline, for eg: 1:10")
.Add<std::string>("range-offset", "", "the format is startOffset:endOffset, for eg: 0:256")
.Add<bool>("non-standard", "", "Enable non-standard rangeformatting")
.Add<bool>("c-like-comments", "", "Enable c-like-comments formatting")
.EnableKeyValueArgs();
cmd.AddTarget("check")
.Add<std::string>("file", "f", "Specify the input file")
Expand All @@ -93,6 +95,7 @@ int main(int argc, char **argv) {
"\t\tseparated by ';'")
.Add<bool>("name-style", "ns", "Enable name-style check")
.Add<bool>("non-standard", "", "Enable non-standard checking")
.Add<bool>("c-like-comments", "", "Enable c-like-comments formatting")
.Add<bool>("dump-json", "", "Dump json format diagnosis info")
.EnableKeyValueArgs();

Expand Down Expand Up @@ -176,6 +179,10 @@ bool InitFormat(CommandLine &cmd, FormatContext &formatContext) {
formatContext.EnableNonStandardLuaSupport();
}

if (cmd.Get<bool>("c-like-comments")) {
formatContext.EnableCLikeCommentsSupport();
}

formatContext.SetDefaultStyleOptions(cmd.GetKeyValueOptions());
return true;
}
Expand Down Expand Up @@ -234,10 +241,14 @@ bool InitCheck(CommandLine &cmd, FormatContext &formatContext) {
formatContext.EnableNameStyleCheckSupport();
}

if(cmd.Get<bool>("dump-json")) {
if (cmd.Get<bool>("dump-json")) {
formatContext.EnableJsonDump();
}

if (cmd.Get<bool>("c-like-comments")) {
formatContext.EnableCLikeCommentsSupport();
}

formatContext.SetDefaultStyleOptions(cmd.GetKeyValueOptions());
return true;
}
Expand Down
8 changes: 8 additions & 0 deletions CodeFormat/src/FormatContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ void FormatContext::EnableJsonDump() {
_dumpJson = true;
}

void FormatContext::EnableCLikeCommentsSupport() {
_isSupportCLikeComments = false;
}

LuaStyle FormatContext::GetStyle(std::string_view path) const {
std::shared_ptr<LuaEditorConfig> editorConfig = nullptr;
std::size_t matchProcess = 0;
Expand Down Expand Up @@ -232,3 +236,7 @@ std::string FormatContext::GetInputPath() const {
const LuaDiagnosticStyle &FormatContext::GetDiagnosticStyle() const {
return _diagnosticStyle;
}

bool FormatContext::IsCLikeCommentsSupport() const {
return _isSupportCLikeComments;
}
4 changes: 4 additions & 0 deletions CodeFormat/src/FormatContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ class FormatContext {

void EnableJsonDump();

void EnableCLikeCommentsSupport();

[[nodiscard]] LuaStyle GetStyle(std::string_view path) const;

[[nodiscard]] WorkMode GetWorkMode() const;
Expand All @@ -67,6 +69,7 @@ class FormatContext {

[[nodiscard]] const LuaDiagnosticStyle &GetDiagnosticStyle() const;

[[nodiscard]] bool IsCLikeCommentsSupport() const;
private:
WorkMode _workMode = WorkMode::File;
std::string _inputPath;
Expand All @@ -81,5 +84,6 @@ class FormatContext {
bool _isRangeLine = false;
std::string _rangeStr;
bool _isSupportNonStandardLua = false;
bool _isSupportCLikeComments = false;
bool _dumpJson = false;
};
4 changes: 4 additions & 0 deletions CodeFormat/src/LuaCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ bool LuaCheck::CheckSingleFile(const FormatContext &context, std::string_view pa
if (context.IsNonStandardLua()) {
luaLexer.SupportNonStandardSymbol();
}
if (context.IsCLikeCommentsSupport()) {
luaLexer.SupportCLikeComments();
}

luaLexer.Parse();

LuaParser p(file, std::move(luaLexer.GetTokens()));
Expand Down
8 changes: 7 additions & 1 deletion CodeFormat/src/LuaFormat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#include "Util/format.h"
#include <fstream>
#include <iostream>
#include <sstream>

bool LuaFormat::Reformat(const FormatContext &context) {
switch (context.GetWorkMode()) {
Expand All @@ -37,6 +36,10 @@ bool LuaFormat::ReformatSingleFile(const FormatContext &context, std::string_vie
if (context.IsNonStandardLua()) {
luaLexer.SupportNonStandardSymbol();
}
if (context.IsCLikeCommentsSupport()) {
luaLexer.SupportCLikeComments();
}

luaLexer.Parse();

LuaParser p(file, std::move(luaLexer.GetTokens()));
Expand Down Expand Up @@ -75,6 +78,9 @@ bool LuaFormat::RangeReformat(const FormatContext &context) {
if (context.IsNonStandardLua()) {
luaLexer.SupportNonStandardSymbol();
}
if (context.IsCLikeCommentsSupport()) {
luaLexer.SupportCLikeComments();
}

luaLexer.Parse();

Expand Down
21 changes: 19 additions & 2 deletions CodeFormatLib/src/CodeFormatLib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,6 @@ int range_format(lua_State *L) {
return 0;
}


int type_format(lua_State *L) {
int top = lua_gettop(L);

Expand Down Expand Up @@ -293,7 +292,6 @@ int type_format(lua_State *L) {
return 0;
}


int update_config(lua_State *L) {
int top = lua_gettop(L);

Expand Down Expand Up @@ -487,6 +485,24 @@ int set_nonstandard_symbol(lua_State *L) {
return 0;
}

int set_clike_comments_symbol(lua_State *L) {
int top = lua_gettop(L);

try {
LuaCodeFormat::GetInstance().SupportCLikeComments();
lua_pushboolean(L, true);
return 1;
} catch (std::exception &e) {
std::string err = e.what();
lua_settop(L, top);
lua_pushboolean(L, false);
lua_pushlstring(L, err.c_str(), err.size());
return 2;
}

return 0;
}

int spell_load_dictionary_from_path(lua_State *L) {
int top = lua_gettop(L);

Expand Down Expand Up @@ -739,6 +755,7 @@ static const luaL_Reg lib[] = {
{"spell_analysis", spell_analysis },
{"spell_suggest", spell_suggest },
{"set_nonstandard_symbol", set_nonstandard_symbol },
{"set_clike_comments_symbol", set_clike_comments_symbol },
{"name_style_analysis", name_style_analysis },
{"update_name_style_config", update_name_style_config },
{nullptr, nullptr }
Expand Down
31 changes: 30 additions & 1 deletion CodeFormatLib/src/LuaCodeFormat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ LuaCodeFormat &LuaCodeFormat::GetInstance() {
}

LuaCodeFormat::LuaCodeFormat()
: _supportNonStandardSymbol(false) {
: _supportNonStandardSymbol(false),
_supportCLikeComments(false) {
}

void LuaCodeFormat::UpdateCodeStyle(const std::string &workspaceUri, const std::string &configPath) {
Expand Down Expand Up @@ -58,6 +59,10 @@ void LuaCodeFormat::SupportNonStandardSymbol() {
_supportNonStandardSymbol = true;
}

void LuaCodeFormat::SupportCLikeComments() {
_supportCLikeComments = true;
}

void LuaCodeFormat::LoadSpellDictionary(const std::string &path) {
_spellChecker.LoadDictionary(path);
}
Expand All @@ -72,6 +77,10 @@ Result<std::string> LuaCodeFormat::Reformat(const std::string &uri, std::string
if (_supportNonStandardSymbol) {
luaLexer.SupportNonStandardSymbol();
}
if (_supportCLikeComments) {
luaLexer.SupportCLikeComments();
}

luaLexer.Parse();

LuaParser p(file, std::move(luaLexer.GetTokens()));
Expand Down Expand Up @@ -100,6 +109,10 @@ Result<std::string> LuaCodeFormat::RangeFormat(const std::string &uri, FormatRan
if (_supportNonStandardSymbol) {
luaLexer.SupportNonStandardSymbol();
}
if (_supportCLikeComments) {
luaLexer.SupportCLikeComments();
}

luaLexer.Parse();

LuaParser p(file, std::move(luaLexer.GetTokens()));
Expand Down Expand Up @@ -130,6 +143,10 @@ LuaCodeFormat::TypeFormat(const std::string &uri, std::size_t line, std::size_t
if (_supportNonStandardSymbol) {
luaLexer.SupportNonStandardSymbol();
}
if (_supportCLikeComments) {
luaLexer.SupportCLikeComments();
}

luaLexer.Parse();

LuaParser p(file, std::move(luaLexer.GetTokens()));
Expand Down Expand Up @@ -158,6 +175,10 @@ Result<std::vector<LuaDiagnosticInfo>> LuaCodeFormat::Diagnostic(const std::stri
if (_supportNonStandardSymbol) {
luaLexer.SupportNonStandardSymbol();
}
if (_supportCLikeComments) {
luaLexer.SupportCLikeComments();
}

luaLexer.Parse();

LuaParser p(file, std::move(luaLexer.GetTokens()));
Expand All @@ -184,6 +205,10 @@ Result<std::vector<LuaDiagnosticInfo>> LuaCodeFormat::SpellCheck(const std::stri
if (_supportNonStandardSymbol) {
luaLexer.SupportNonStandardSymbol();
}
if (_supportCLikeComments) {
luaLexer.SupportCLikeComments();
}

luaLexer.Parse();

LuaParser p(file, std::move(luaLexer.GetTokens()));
Expand All @@ -210,6 +235,10 @@ Result<std::vector<LuaDiagnosticInfo>> LuaCodeFormat::NameStyleCheck(const std::
if (_supportNonStandardSymbol) {
luaLexer.SupportNonStandardSymbol();
}
if (_supportCLikeComments) {
luaLexer.SupportCLikeComments();
}

luaLexer.Parse();

LuaParser p(file, std::move(luaLexer.GetTokens()));
Expand Down
3 changes: 3 additions & 0 deletions CodeFormatLib/src/LuaCodeFormat.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class LuaCodeFormat {

void SupportNonStandardSymbol();

void SupportCLikeComments();

void LoadSpellDictionary(const std::string &path);

void LoadSpellDictionaryFromBuffer(const std::string &buffer);
Expand Down Expand Up @@ -65,4 +67,5 @@ class LuaCodeFormat {
CodeSpellChecker _spellChecker;
LuaDiagnosticStyle _diagnosticStyle;
bool _supportNonStandardSymbol;
bool _supportCLikeComments;
};
7 changes: 4 additions & 3 deletions LuaParser/include/LuaParser/Lexer/LuaLexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
class LuaLexer {
public:
explicit LuaLexer(std::shared_ptr<LuaSource> file);
explicit LuaLexer(std::shared_ptr<LuaSource> source);

bool Parse();

Expand All @@ -32,7 +32,7 @@ class LuaLexer {

void SupportNonStandardSymbol();

// void SetCustomParser(std::shared_ptr<LuaCustomParser> parser);
void SupportCLikeComments();
private:
static std::map<std::string, LuaTokenKind, std::less<>> LuaReserved;

Expand All @@ -52,12 +52,13 @@ class LuaLexer {

bool CurrentIsNewLine();

bool IsReserved(std::string_view text);
static bool IsReserved(std::string_view text);

void TokenError(std::string_view message, TextRange range);

int _linenumber;
bool _supportNonStandardSymbol;
bool _supportCLikeComments;
TextReader _reader;
std::vector<LuaToken> _tokens;
std::vector<LuaTokenError> _errors;
Expand Down
27 changes: 20 additions & 7 deletions LuaParser/src/Lexer/LuaLexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,12 @@ std::map<std::string, LuaTokenKind, std::less<>> LuaLexer::LuaReserved = {
{"::", TK_DBCOLON }
};

LuaLexer::LuaLexer(std::shared_ptr<LuaSource> file)
LuaLexer::LuaLexer(std::shared_ptr<LuaSource> source)
: _linenumber(0),
_supportNonStandardSymbol(false),
_reader(file->GetSource()),
_file(file) {
_supportCLikeComments(false),
_reader(source->GetSource()),
_file(source) {
}

bool LuaLexer::Parse() {
Expand Down Expand Up @@ -85,6 +86,13 @@ std::vector<LuaToken> &LuaLexer::GetTokens() {
return _tokens;
}

void LuaLexer::SupportNonStandardSymbol() {
_supportNonStandardSymbol = true;
}
void LuaLexer::SupportCLikeComments(){
_supportCLikeComments = true;
}

LuaTokenKind LuaLexer::Lex() {
_reader.ResetBuffer();

Expand Down Expand Up @@ -204,9 +212,18 @@ LuaTokenKind LuaLexer::Lex() {
}
case '/': {
_reader.SaveAndNext();

if(_supportCLikeComments) {
while (!CurrentIsNewLine() && _reader.GetCurrentChar() != EOZ) {
_reader.SaveAndNext();
}
return TK_SHORT_COMMENT;
}

if (_reader.CheckNext1('=')) {
return '=';
}

return TK_IDIV;
}
case '*': {
Expand Down Expand Up @@ -607,7 +624,3 @@ bool LuaLexer::IsReserved(std::string_view text) {
void LuaLexer::TokenError(std::string_view message, TextRange range) {
_errors.emplace_back(message, range, 0);
}

void LuaLexer::SupportNonStandardSymbol() {
_supportNonStandardSymbol = true;
}
11 changes: 11 additions & 0 deletions Test/src/Grammar_unitest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,15 @@ local t = disableControl?.disableMovement
local t2 = ddd?:ffffff()
local t3 = ddd?["hello"]
)", true).HasError()) << "extend grammar nullable operator test fail";
EXPECT_FALSE(TestHelper::GetParser(R"(
local t = a // yes it is a comment
)", true).HasError()) << "extend grammar comment test fail";
EXPECT_FALSE(TestHelper::GetParser(R"(
-- yes it is a comment
// it is a comemnt
local t = a /*
a new comments
*/
)", true, true).HasError()) << "extend grammar comment test fail";
}
Loading

0 comments on commit ebcd76a

Please sign in to comment.