Skip to content

Commit 871fe48

Browse files
committed
初步支持非标准符号
1 parent bed4965 commit 871fe48

File tree

15 files changed

+270
-33
lines changed

15 files changed

+270
-33
lines changed

CodeService/src/FormatElement/AlignmentLayoutElement.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ std::shared_ptr<FormatElement> FindLastValidChild(FormatElement::ChildIterator i
1616
return nullptr;
1717
}
1818

19-
AlignmentLayoutElement::AlignmentLayoutElement(std::string_view alignSign)
20-
: _alignSign(alignSign)
19+
AlignmentLayoutElement::AlignmentLayoutElement(LuaTokenType alignToken)
20+
: _alignToken(alignToken)
2121
{
2222
}
2323

@@ -144,7 +144,8 @@ int AlignmentLayoutElement::GetAlignOffsetWithWeakRule(FormatContext& ctx)
144144
if (textChild->GetType() == FormatElementType::TextElement)
145145
{
146146
const auto textElement = std::dynamic_pointer_cast<TextElement>(textChild);
147-
if (textElement->GetText() == _alignSign)
147+
const auto textNode = textElement->GetNode();
148+
if (textNode && textNode->GetTokenType() == _alignToken)
148149
{
149150
const auto signPosition = ctx.GetColumn(textElement->GetTextRange().StartOffset);
150151

@@ -185,7 +186,8 @@ void AlignmentLayoutElement::AlignmentSerialize(SerializeContext& ctx, ChildIter
185186
if (textChild->GetType() == FormatElementType::TextElement)
186187
{
187188
const auto textElement = std::dynamic_pointer_cast<TextElement>(textChild);
188-
if (textElement->GetText() == _alignSign && it != statChildren.begin())
189+
const auto textNode = textElement->GetNode();
190+
if (textNode && textNode->GetTokenType() == _alignToken && it != statChildren.begin())
189191
{
190192
auto lastIt = it;
191193
--lastIt;
@@ -214,7 +216,8 @@ void AlignmentLayoutElement::AlignmentDiagnosis(DiagnosisContext& ctx,
214216
if (textChild->GetType() == FormatElementType::TextElement)
215217
{
216218
const auto textElement = std::dynamic_pointer_cast<TextElement>(textChild);
217-
if (textElement->GetText() == _alignSign && it != statChildren.begin())
219+
const auto textNode = textElement->GetNode();
220+
if (textNode && textNode->GetTokenType() == _alignToken && it != statChildren.begin())
218221
{
219222
auto lastIt = it;
220223
--lastIt;

CodeService/src/FormatElement/TextElement.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@
33

44
TextElement::TextElement(std::string_view text, TextRange range)
55
: FormatElement(range),
6-
_text(text)
6+
_text(text),
7+
_node(nullptr)
78
{
89
}
910

1011
TextElement::TextElement(std::shared_ptr<LuaAstNode> node)
11-
: TextElement(node->GetText(), node->GetTextRange())
12+
: FormatElement(node->GetTextRange()),
13+
_node(node),
14+
_text(node->GetText())
15+
1216
{
1317
}
1418

@@ -38,3 +42,8 @@ std::string_view TextElement::GetText() const
3842
{
3943
return _text;
4044
}
45+
46+
std::shared_ptr<LuaAstNode> TextElement::GetNode() const
47+
{
48+
return _node;
49+
}

CodeService/src/LuaFormatter.cpp

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "CodeService/FormatElement/CallArgsListLayoutElement.h"
2525
#include "Util/StringUtil.h"
2626
#include "CodeService/AstUtil.h"
27+
#include "LuaParser/LuaTokenTypeDetail.h"
2728

2829
bool NextMatch(LuaAstNode::ChildIterator it, LuaAstNodeType type, const LuaAstNode::ChildrenContainer& container)
2930
{
@@ -411,7 +412,7 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatLocalStatement(std::shared_pt
411412
case LuaAstNodeType::GeneralOperator:
412413
{
413414
// 基于这样的考虑 可能local 语句没有等号所以nameDefList的空格移上来
414-
if (node->GetText() == "=")
415+
if (node->GetTokenType() == '=')
415416
{
416417
env->Add<KeepBlankElement>(1);
417418
}
@@ -1012,7 +1013,7 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatForBody(std::shared_ptr<LuaAs
10121013
{
10131014
case LuaAstNodeType::KeyWord:
10141015
{
1015-
if (child->GetText() == "do")
1016+
if (child->GetTokenType() == TK_DO)
10161017
{
10171018
bool singleLine = false;
10181019
env->AddChild(FormatNodeAndBlockOrEnd(it, singleLine, children));
@@ -1064,7 +1065,7 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatRepeatStatement(std::shared_p
10641065
{
10651066
case LuaAstNodeType::KeyWord:
10661067
{
1067-
if (child->GetText() == "repeat")
1068+
if (child->GetTokenType() == TK_REPEAT)
10681069
{
10691070
bool singleLine = false;
10701071
env->AddChild(FormatNodeAndBlockOrEnd(it, singleLine, children));
@@ -1110,13 +1111,13 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatIfStatement(std::shared_ptr<L
11101111
{
11111112
case LuaAstNodeType::KeyWord:
11121113
{
1113-
if (child->GetText() == "then" || child->GetText() == "else")
1114+
if (child->GetTokenType() == TK_THEN || child->GetTokenType() == TK_ELSE)
11141115
{
11151116
bool singleLine = false;
11161117
env->AddChild(FormatNodeAndBlockOrEnd(it, singleLine, children));
11171118
env->Add<KeepElement>(1, !singleLine);
11181119
}
1119-
else if (child->GetText() == "if" || child->GetText() == "elseif")
1120+
else if (child->GetTokenType() == TK_IF || child->GetTokenType() == TK_ELSEIF)
11201121
{
11211122
env->Add<TextElement>(child);
11221123
env->Add<KeepBlankElement>(1);
@@ -1183,7 +1184,7 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatExpressionStatement(std::shar
11831184
if (indexExpression)
11841185
{
11851186
auto indexOp = indexExpression->FindFirstOf(LuaAstNodeType::IndexOperator);
1186-
if (indexOp->GetText() == "." || indexOp->GetText() == ":")
1187+
if (indexOp->GetTokenType() == '.' || indexOp->GetTokenType() == ':')
11871188
{
11881189
auto continuationIndent = _parser->GetColumn(indexOp->GetTextRange().StartOffset)
11891190
- _parser->GetColumn(child->GetTextRange().StartOffset);
@@ -1366,17 +1367,17 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatParamList(std::shared_ptr<Lua
13661367
{
13671368
case LuaAstNodeType::GeneralOperator:
13681369
{
1369-
if (child->GetText() == ",")
1370+
if (child->GetTokenType() == ',')
13701371
{
1371-
if(_options.remove_expression_list_finish_comma
1372+
if (_options.remove_expression_list_finish_comma
13721373
&& NextMatch(it, LuaAstNodeType::GeneralOperator, children))
13731374
{
13741375
break;
13751376
}
13761377
paramListLayoutEnv->Add<TextElement>(child);
13771378
paramListLayoutEnv->Add<KeepElement>(1);
13781379
}
1379-
else if (child->GetText() == ")")
1380+
else if (child->GetTokenType() == ')')
13801381
{
13811382
env->AddChild(paramListLayoutEnv);
13821383
if (!paramListLayoutEnv->GetChildren().empty())
@@ -1541,12 +1542,12 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatTableExpression(std::shared_p
15411542
{
15421543
case LuaAstNodeType::GeneralOperator:
15431544
{
1544-
if (child->GetText() == "{")
1545+
if (child->GetTokenType() == '{')
15451546
{
15461547
env->Add<TextElement>(child);
15471548
leftBraceLine = _parser->GetLine(child->GetTextRange().EndOffset);
15481549
}
1549-
else if (child->GetText() == "}")
1550+
else if (child->GetTokenType() == '}')
15501551
{
15511552
if (tableFieldLayout->GetChildren().empty())
15521553
{
@@ -1591,7 +1592,7 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatTableField(std::shared_ptr<Lu
15911592
{
15921593
case LuaAstNodeType::GeneralOperator:
15931594
{
1594-
if (child->GetText() == "=")
1595+
if (child->GetTokenType() == '=')
15951596
{
15961597
eqSignFounded = true;
15971598
env->Add<KeepBlankElement>(1);
@@ -1672,7 +1673,7 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatAlignStatement(LuaAstNode::Ch
16721673
std::shared_ptr<FormatElement> env = nullptr;
16731674
if (_options.continuous_assign_statement_align_to_equal_sign)
16741675
{
1675-
env = std::make_shared<AlignmentLayoutElement>("=");
1676+
env = std::make_shared<AlignmentLayoutElement>('=');
16761677
}
16771678
else
16781679
{
@@ -1842,7 +1843,7 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatAlignTableField(LuaAstNode::C
18421843

18431844
if (canAlign && _options.continuous_assign_table_field_align_to_equal_sign && layout->GetChildren().size() > 1)
18441845
{
1845-
auto alignmentLayoutElement = std::make_shared<AlignmentLayoutElement>("=");
1846+
auto alignmentLayoutElement = std::make_shared<AlignmentLayoutElement>('=');
18461847
alignmentLayoutElement->CopyFrom(layout);
18471848
layout = alignmentLayoutElement;
18481849
}
@@ -1920,7 +1921,7 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatNodeAndBlockOrEnd(LuaAstNode:
19201921
if (NextMatch(it, LuaAstNodeType::KeyWord, children))
19211922
{
19221923
auto next = NextNode(it, children);
1923-
if (next->GetText() == "end")
1924+
if (next->GetTokenType() == TK_END)
19241925
{
19251926
env->Add<TextElement>(next);
19261927
++it;
@@ -1983,7 +1984,7 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatBlockFromParent(LuaAstNode::C
19831984
auto nextKey = NextNode(it, siblings);
19841985
if (_options.if_branch_comments_after_block_no_indent
19851986
&& nextKey && nextKey->GetType() == LuaAstNodeType::KeyWord
1986-
&& (nextKey->GetText() == "elseif" || nextKey->GetText() == "else"))
1987+
&& (nextKey->GetTokenType() == TK_ELSEIF || nextKey->GetTokenType() == TK_ELSE))
19871988
{
19881989
auto blockEnv = FormatBlock(copyBlock);
19891990
auto noIndentEnv = std::make_shared<NoIndentElement>();
@@ -2113,7 +2114,7 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatUnaryExpression(std::shared_p
21132114
case LuaAstNodeType::UnaryOperator:
21142115
{
21152116
env->Add<TextElement>(child);
2116-
if (child->GetText() == "not")
2117+
if (child->GetTokenType() == TK_NOT)
21172118
{
21182119
env->Add<KeepElement>(1);
21192120
}
@@ -2185,7 +2186,7 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatIndexExpression(std::shared_p
21852186
{
21862187
if (_options.table_append_expression_no_space)
21872188
{
2188-
if (child->GetText() == "[")
2189+
if (child->GetTokenType() == '[')
21892190
{
21902191
expressionAfterIndexOperator = true;
21912192
}
@@ -2194,7 +2195,7 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatIndexExpression(std::shared_p
21942195
env->Add<TextElement>(child);
21952196
if (_options.long_chain_expression_allow_one_space_after_colon)
21962197
{
2197-
if (child->GetText() == ":" && _parser->GetLuaFile()->OnlyEmptyCharBefore(
2198+
if (child->GetTokenType() == ':' && _parser->GetLuaFile()->OnlyEmptyCharBefore(
21982199
child->GetTextRange().StartOffset))
21992200
{
22002201
env->Add<MaxSpaceElement>(1);

LuaParser/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ target_sources(LuaParser PUBLIC
3131
${LuaParser_SOURCE_DIR}/src/LuaAstVisitor.cpp
3232
${LuaParser_SOURCE_DIR}/src/LuaFile.cpp
3333
${LuaParser_SOURCE_DIR}/src/LuaIdentify.cpp
34+
${LuaParser_SOURCE_DIR}/src/LuaCustomParser.cpp
3435
)
3536

3637
target_link_libraries(LuaParser Util)

LuaParser/src/LuaCustomParser.cpp

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
#include "LuaParser/LuaCustomParser.h"
2+
3+
#include "LuaDefine.h"
4+
#include "LuaParser/LuaTokenTypeDetail.h"
5+
6+
int GetCurrentChar(std::string_view source, std::size_t i)
7+
{
8+
if (i < source.size())
9+
{
10+
unsigned char ch = source[i];
11+
return ch;
12+
}
13+
}
14+
15+
LuaTokenType LuaCustomParser::Lex(std::string_view source, std::size_t start, std::size_t& consumeSize)
16+
{
17+
enum class ParseState
18+
{
19+
Unknown,
20+
Operator,
21+
Identify,
22+
} state = ParseState::Unknown;
23+
24+
for (; (start + consumeSize) < source.size(); consumeSize++)
25+
{
26+
int ch = GetCurrentChar(source, start + consumeSize);
27+
if (ch == EOZ || ch == std::isspace(ch))
28+
{
29+
break;
30+
}
31+
switch (ch)
32+
{
33+
case '/':
34+
case '-':
35+
case '*':
36+
case '^':
37+
case '%':
38+
case '+':
39+
case '&':
40+
case '<':
41+
case '>':
42+
case '~':
43+
case '=':
44+
{
45+
if (state == ParseState::Unknown)
46+
{
47+
state = ParseState::Operator;
48+
}
49+
else if (state == ParseState::Identify)
50+
{
51+
goto endLoop;
52+
}
53+
54+
break;
55+
}
56+
case '(':
57+
case ')':
58+
case '{':
59+
case '}':
60+
case '[':
61+
case ']':
62+
{
63+
goto endLoop;
64+
}
65+
default:
66+
{
67+
if (lislalpha(ch))
68+
{
69+
if (state == ParseState::Unknown)
70+
{
71+
state = ParseState::Identify;
72+
}
73+
else if (state == ParseState::Operator)
74+
{
75+
goto endLoop;
76+
}
77+
78+
break;
79+
}
80+
goto endLoop;
81+
}
82+
}
83+
}
84+
endLoop:
85+
86+
if (consumeSize == 0)
87+
{
88+
return TK_UNKNOWN;
89+
}
90+
91+
std::string_view tokenText = source.substr(start, consumeSize);
92+
auto it = _customTokens.find(tokenText);
93+
if (it == _customTokens.end())
94+
{
95+
return TK_UNKNOWN;
96+
}
97+
return it->second;
98+
}
99+
100+
void LuaCustomParser::SetEqToken(std::vector<std::string>& customTokens)
101+
{
102+
for (auto& token : customTokens)
103+
{
104+
_customTokens.insert({token, '='});
105+
}
106+
}

0 commit comments

Comments
 (0)