Skip to content

Commit 668b88d

Browse files
committed
support nullable operator
1 parent 73e3ccc commit 668b88d

File tree

5 files changed

+25
-71
lines changed

5 files changed

+25
-71
lines changed

LuaParser/src/Ast/LuaSyntaxTree.cpp

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
#include "LuaParser/Ast/LuaSyntaxTree.h"
2-
#include <algorithm>
3-
#include "LuaParser/Parse/LuaParser.h"
42
#include "LuaParser/Lexer/LuaTokenTypeDetail.h"
3+
#include "LuaParser/Parse/LuaParser.h"
54
#include "Util/format.h"
5+
#include <algorithm>
66

77
LuaSyntaxTree::LuaSyntaxTree()
8-
: _file(),
9-
_tokenIndex(0) {
10-
8+
: _file(),
9+
_tokenIndex(0) {
1110
}
1211

1312
void LuaSyntaxTree::BuildTree(LuaParser &p) {
@@ -107,8 +106,7 @@ void LuaSyntaxTree::EatInlineComment(LuaParser &p) {
107106
case TK_LONG_COMMENT:
108107
case TK_SHEBANG: {
109108
auto prevToken = tokens[index - 1];
110-
if (_file->GetLine(prevToken.Range.GetEndOffset())
111-
== _file->GetLine(tokens[index].Range.StartOffset)) {
109+
if (_file->GetLine(prevToken.Range.GetEndOffset()) == _file->GetLine(tokens[index].Range.StartOffset)) {
112110
EatToken(p);
113111
}
114112
break;
@@ -263,7 +261,7 @@ std::size_t LuaSyntaxTree::GetEndOffset(std::size_t index) const {
263261
TextRange LuaSyntaxTree::GetTokenRange(std::size_t index) const {
264262
if (index < _nodeOrTokens.size()) {
265263
auto &n = _nodeOrTokens[index];
266-
if(n.Type == NodeOrTokenType::Token) {
264+
if (n.Type == NodeOrTokenType::Token) {
267265
auto &token = _tokens[n.Data.TokenIndex];
268266
return TextRange(token.Start, token.Length);
269267
}
@@ -343,7 +341,7 @@ std::size_t LuaSyntaxTree::GetPrevToken(std::size_t index) const {
343341
if (tokenIndex != 0) {
344342
return _tokens[tokenIndex - 1].NodeIndex;
345343
}
346-
} else { // Node, 可能存在无元素节点
344+
} else {// Node, 可能存在无元素节点
347345
for (auto nodeIndex = index - 1; nodeIndex > 0; nodeIndex--) {
348346
if (IsToken(nodeIndex)) {
349347
return nodeIndex;
@@ -364,7 +362,7 @@ std::size_t LuaSyntaxTree::GetNextToken(std::size_t index) const {
364362
auto &n = _nodeOrTokens[index];
365363
if (n.Type == NodeOrTokenType::Token) {
366364
tokenNodeIndex = index;
367-
} else { // Node, 可能存在无元素节点
365+
} else {// Node, 可能存在无元素节点
368366
auto lastTokenIndex = GetLastToken(index);
369367
if (lastTokenIndex != 0) {
370368
tokenNodeIndex = lastTokenIndex;
@@ -541,13 +539,9 @@ bool LuaSyntaxTree::HasError() const {
541539
}
542540

543541
bool LuaSyntaxTree::IsEatAllComment(LuaSyntaxNodeKind kind) const {
544-
return kind == LuaSyntaxNodeKind::Block
545-
|| kind == LuaSyntaxNodeKind::TableFieldList
546-
|| kind == LuaSyntaxNodeKind::File;
542+
return kind == LuaSyntaxNodeKind::Block || kind == LuaSyntaxNodeKind::TableFieldList || kind == LuaSyntaxNodeKind::File;
547543
}
548544

549545
const std::vector<LuaParseError> &LuaSyntaxTree::GetErrors() const {
550546
return _errors;
551547
}
552-
553-

LuaParser/src/Lexer/LuaLexer.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,14 @@ LuaTokenKind LuaLexer::Lex() {
354354
}
355355
return '{';
356356
}
357+
case '?': {
358+
_reader.SaveAndNext();
359+
if(_supportNonStandardSymbol && _reader.CheckNext1('.')) {
360+
return '.';
361+
}
362+
363+
return '?';
364+
}
357365
default: {
358366
if (lislalpha(_reader.GetCurrentChar())) /* identifier or reserved word? */
359367
{

LuaParser/src/Parse/LuaParser.cpp

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -463,40 +463,6 @@ void LuaParser::ExpressionStatement() {
463463
}
464464
}
465465

466-
//
467-
//void LuaParser::DocStatement(std::shared_ptr<LuaSyntaxNode> comment, LuaToken &docComment) {
468-
// LuaDocTokenParser docTokenParser(docComment.Text, docComment.Range);
469-
// docTokenParser.Next();
470-
//
471-
// switch (docTokenParser.Current().TokenType) {
472-
// case TK_DOC_TAG_FORMAT: {
473-
// DocTagFormatStatement(comment, docTokenParser);
474-
// break;
475-
// }
476-
// default: {
477-
// break;
478-
// }
479-
// }
480-
//}
481-
//
482-
//void LuaParser::DocTagFormatStatement(std::shared_ptr<LuaSyntaxNode> comment, LuaDocTokenParser &docTokenParser) {
483-
// auto docFormat = CreateAstNodeFromToken(LuaSyntaxNodeKind::DocTagFormat, docTokenParser.Current());
484-
//
485-
// docTokenParser.Next();
486-
// switch (docTokenParser.Current().TokenType) {
487-
// case TK_DOC_DISABLE:
488-
// case TK_DOC_DISABLE_NEXT: {
489-
// docFormat->AddChild(CreateAstNodeFromToken(LuaSyntaxNodeKind::KeyWord, docTokenParser.Current()));
490-
// break;
491-
// }
492-
// default: {
493-
// return;
494-
// }
495-
// }
496-
//
497-
// comment->AddChild(docFormat);
498-
//}
499-
500466
void LuaParser::Condition() {
501467
Expression();
502468
}
@@ -1013,15 +979,3 @@ void LuaParser::NameDefList() {
1013979
std::vector<LuaToken> &LuaParser::GetTokens() {
1014980
return _tokens;
1015981
}
1016-
1017-
1018-
1019-
1020-
1021-
1022-
1023-
1024-
1025-
1026-
1027-

Test/src/Grammar_unitest.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,7 @@ local t = 123/*1233*/
5757
EXPECT_FALSE(TestHelper::GetParser(R"(
5858
local t = {@abaa}
5959
)", true).HasError()) << "extend grammar issue 119 test fail";
60+
EXPECT_FALSE(TestHelper::GetParser(R"(
61+
local t = disableControl?.disableMovement
62+
)", true).HasError()) << "extend grammar nullable operator test fail";
6063
}

Test2/src/FormatTest2.cpp

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,11 @@
88

99
int main() {
1010
std::string buffer = R"(
11-
t+=123
12-
t-=123
13-
t/=123
14-
t//=123
15-
t*=123
16-
t<<=123
17-
t>>=123
18-
t%=123
19-
t|=123
20-
t&=123
11+
local disable = {
12+
move= disableControls?.disableMovement,
13+
oko = disableControls?.disableLook,
14+
combat = disableControls?.disableCombat,
15+
}
2116
)";
2217

2318
auto file = std::make_shared<LuaFile>(std::move(buffer));

0 commit comments

Comments
 (0)