refeat: ILexer接口适配 (#38)
Co-authored-by: Huaps <1183155719@qq.com> Co-authored-by: duqoo <92306417+duqoo@users.noreply.github.com> Reviewed-on: PostGuard/Canon#38
This commit is contained in:
83
Canon.Core/LexicalParser/LexRules.cs
Normal file
83
Canon.Core/LexicalParser/LexRules.cs
Normal file
@@ -0,0 +1,83 @@
|
||||
namespace Canon.Core.LexicalParser;
|
||||
|
||||
public static class LexRules
|
||||
{
|
||||
// 保留关键字
|
||||
private static readonly string[] _keywords =
|
||||
[
|
||||
"Program", "Const", "Var", "Procedure",
|
||||
"Function", "Begin", "End", "Array",
|
||||
"Of", "If", "Then", "Else",
|
||||
"For", "To", "Do", "Integer",
|
||||
"Real", "Boolean", "Character", "Divide",
|
||||
"Not", "Mod", "And", "Or"
|
||||
];
|
||||
|
||||
private static readonly string[] _delimiter = [";", ",", ":", ".", "(", ")", "[", "]", "'", "\"", ".."];
|
||||
|
||||
private static readonly string[] _operator = ["=", "<>", "<", "<=", ">", ">=", "+", "-", "*", "/", ":="];
|
||||
|
||||
// 判断字符
|
||||
public static bool IsDigit(char _ch) {
|
||||
if (_ch >= '0' && _ch <= '9') return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
public static bool IsHexDigit(char _ch)
|
||||
{
|
||||
if ((_ch >= '0' && _ch <= '9') || (_ch<= 'F' && _ch >= 'A')) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
public static bool IsLetter(char _ch) {
|
||||
if ((_ch >= 'A' && _ch <= 'Z') || (_ch >= 'a' && _ch <= 'z' || _ch == '_')) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static bool IsKeyword(string tokenString)
|
||||
{
|
||||
|
||||
foreach (var t in _keywords)
|
||||
{
|
||||
if (string.Equals(tokenString, t, StringComparison.OrdinalIgnoreCase)) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public static bool IsDelimiter(char ch)
|
||||
{
|
||||
foreach (var delimiter in _delimiter)
|
||||
{
|
||||
if (delimiter.Contains(ch))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static bool IsOperator(char ch)
|
||||
{
|
||||
foreach (var o in _operator)
|
||||
{
|
||||
if (o.Contains(ch))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static bool IsBreakPoint(char ch)
|
||||
{
|
||||
if (ch == ' ' || ch == '\n' || ch == '\t' || ch == '\r')
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
95
Canon.Core/LexicalParser/LexemeFactory.cs
Normal file
95
Canon.Core/LexicalParser/LexemeFactory.cs
Normal file
@@ -0,0 +1,95 @@
|
||||
using Canon.Core.Enums;
|
||||
|
||||
namespace Canon.Core.LexicalParser;
|
||||
|
||||
public static class LexemeFactory
|
||||
{
|
||||
|
||||
public static SemanticToken MakeToken(SemanticTokenType tokenType,string literal,uint _line,uint _chPos)
|
||||
{
|
||||
SemanticToken? token;
|
||||
switch (tokenType)
|
||||
{
|
||||
case SemanticTokenType.Character:
|
||||
CharacterSemanticToken characterSemanticToken = new CharacterSemanticToken()
|
||||
{
|
||||
LinePos = _line, CharacterPos = _chPos, LiteralValue = literal,
|
||||
};
|
||||
token = characterSemanticToken;
|
||||
break;
|
||||
case SemanticTokenType.Identifier:
|
||||
IdentifierSemanticToken identifierSemanticToken = new IdentifierSemanticToken()
|
||||
{
|
||||
LinePos = _line, CharacterPos = _chPos, LiteralValue = literal,
|
||||
};
|
||||
token = identifierSemanticToken;
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException(nameof(tokenType), tokenType, null);
|
||||
}
|
||||
|
||||
return token;
|
||||
|
||||
|
||||
}
|
||||
|
||||
public static KeywordSemanticToken MakeToken(KeywordType keywordType,string literal,uint _line,uint _chPos)
|
||||
{
|
||||
KeywordSemanticToken keywordSemanticToken = new KeywordSemanticToken
|
||||
{
|
||||
LinePos = _line,
|
||||
CharacterPos = _chPos,
|
||||
LiteralValue = literal,
|
||||
KeywordType = keywordType
|
||||
};
|
||||
return keywordSemanticToken;
|
||||
}
|
||||
|
||||
public static DelimiterSemanticToken MakeToken(DelimiterType delimiterType,string literal,uint _line,uint _chPos)
|
||||
{
|
||||
DelimiterSemanticToken delimiterSemanticToken = new DelimiterSemanticToken()
|
||||
{
|
||||
LinePos = _line,
|
||||
CharacterPos = _chPos,
|
||||
LiteralValue = literal,
|
||||
DelimiterType = delimiterType
|
||||
};
|
||||
return delimiterSemanticToken;
|
||||
}
|
||||
|
||||
public static NumberSemanticToken MakeToken(NumberType numberType,string literal,uint _line,uint _chPos)
|
||||
{
|
||||
string temp = literal;
|
||||
string result;
|
||||
if (numberType == NumberType.Hex)
|
||||
{
|
||||
result = string.Concat("0x", temp.AsSpan(1, temp.Length - 1));
|
||||
}
|
||||
else
|
||||
{
|
||||
result = temp;
|
||||
}
|
||||
|
||||
NumberSemanticToken numberSemanticToken = new NumberSemanticToken()
|
||||
{
|
||||
LinePos = _line,
|
||||
CharacterPos = _chPos,
|
||||
LiteralValue = result,
|
||||
NumberType = numberType
|
||||
};
|
||||
return numberSemanticToken;
|
||||
|
||||
}
|
||||
|
||||
public static OperatorSemanticToken MakeToken(OperatorType operatorType,string literal,uint _line,uint _chPos)
|
||||
{
|
||||
OperatorSemanticToken operatorSemanticToken = new OperatorSemanticToken()
|
||||
{
|
||||
LinePos = _line,
|
||||
CharacterPos = _chPos,
|
||||
LiteralValue = literal,
|
||||
OperatorType = operatorType
|
||||
};
|
||||
return operatorSemanticToken;
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
@@ -339,18 +339,4 @@ public class EndSemanticToken : SemanticToken
|
||||
public override SemanticTokenType TokenType => SemanticTokenType.End;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 错误类型记号
|
||||
/// </summary>
|
||||
public class ErrorSemanticToken : SemanticToken
|
||||
{
|
||||
public override SemanticTokenType TokenType => SemanticTokenType.Error;
|
||||
|
||||
public static bool TryParse(uint linePos, uint characterPos, LinkedListNode<char> now,
|
||||
out IdentifierSemanticToken? token)
|
||||
{
|
||||
token = null;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user