refact: 合乎代码规范

This commit is contained in:
jackfiled 2024-03-11 11:36:39 +08:00
parent 9984cee920
commit 46c4a5e5ea

View File

@ -1,62 +1,60 @@
 namespace Canon.Core.LexicalParser;
namespace Canon.Core.LexicalParser public class Lexer
{ {
public class Lexer private readonly LinkedList<char> _source;
{ private LinkedListNode<char>? _currentNode;
private readonly LinkedList<char> source; private uint _line = 1;
private LinkedListNode<char>? currentNode; private uint _charPosition;
private uint line = 1; private readonly List<SemanticToken> _tokens = [];
private uint charPosition = 0;
private List<SemanticToken> tokens = new List<SemanticToken>();
public Lexer(string source) public Lexer(string source)
{ {
// 将字符串转换为LinkedList<char> // 将字符串转换为LinkedList<char>
this.source = new LinkedList<char>(source); _source = new LinkedList<char>(source);
currentNode = this.source.First; _currentNode = _source.First;
} }
public List<SemanticToken> Tokenize() public List<SemanticToken> Tokenize()
{ {
while (currentNode != null) while (_currentNode != null)
{ {
charPosition = 0; // 重置字符位置 _charPosition = 0; // 重置字符位置
SkipWhitespace(); SkipWhitespace();
if (currentNode == null) break; // 如果跳过空格后到达了末尾,则退出循环 if (_currentNode == null) break; // 如果跳过空格后到达了末尾,则退出循环
SemanticToken? token = null; SemanticToken? token = null;
// 尝试解析各种类型的词法单元 // 尝试解析各种类型的词法单元
if (DelimiterSemanticToken.TryParse(line, charPosition, currentNode, out var delimiterToken)) if (DelimiterSemanticToken.TryParse(_line, _charPosition, _currentNode, out var delimiterToken))
{ {
token = delimiterToken; token = delimiterToken;
} }
else if (CharacterSemanticToken.TryParse(line, charPosition, currentNode, out var characterToken)) else if (CharacterSemanticToken.TryParse(_line, _charPosition, _currentNode, out var characterToken))
{ {
token = characterToken; token = characterToken;
} }
else if (KeywordSemanticToken.TryParse(line, charPosition, currentNode, out var keywordToken)) else if (KeywordSemanticToken.TryParse(_line, _charPosition, _currentNode, out var keywordToken))
{ {
token = keywordToken; token = keywordToken;
} }
else if (OperatorSemanticToken.TryParse(line, charPosition, currentNode, out var operatorToken)) else if (OperatorSemanticToken.TryParse(_line, _charPosition, _currentNode, out var operatorToken))
{ {
token = operatorToken; token = operatorToken;
} }
else if (NumberSemanticToken.TryParse(line, charPosition, currentNode, out var numberToken)) else if (NumberSemanticToken.TryParse(_line, _charPosition, _currentNode, out var numberToken))
{ {
token = numberToken; token = numberToken;
} }
else if (IdentifierSemanticToken.TryParse(line, charPosition, currentNode, out var identifierToken)) else if (IdentifierSemanticToken.TryParse(_line, _charPosition, _currentNode, out var identifierToken))
{ {
token = identifierToken; token = identifierToken;
} }
if (token != null) if (token != null)
{ {
tokens.Add(token); _tokens.Add(token);
// 根据词法单元的长度移动currentNode // 根据词法单元的长度移动currentNode
MoveCurrentNode(token.LiteralValue.Length); MoveCurrentNode(token.LiteralValue.Length);
} }
@ -68,28 +66,27 @@ namespace Canon.Core.LexicalParser
} }
// tokens.Add(new EOFToken(line, charPosition)); // 添加EOF标记 // tokens.Add(new EOFToken(line, charPosition)); // 添加EOF标记
return tokens; return _tokens;
} }
private void SkipWhitespace() private void SkipWhitespace()
{ {
while (currentNode != null && char.IsWhiteSpace(currentNode.Value)) while (_currentNode != null && char.IsWhiteSpace(_currentNode.Value))
{ {
if (currentNode.Value == '\n') if (_currentNode.Value == '\n')
{ {
line++; _line++;
charPosition = 0; _charPosition = 0;
} }
currentNode = currentNode.Next; _currentNode = _currentNode.Next;
} }
} }
private void MoveCurrentNode(int steps) private void MoveCurrentNode(int steps)
{ {
for (int i = 0; i < steps && currentNode != null; i++) for (int i = 0; i < steps && _currentNode != null; i++)
{ {
currentNode = currentNode.Next; _currentNode = _currentNode.Next;
}
} }
} }
} }