2024-04-18 16:34:32 +08:00
|
|
|
|
using Canon.Core.Enums;
|
|
|
|
|
|
|
|
|
|
namespace Canon.Core.LexicalParser;
|
|
|
|
|
|
|
|
|
|
public static class LexemeFactory
|
|
|
|
|
{
|
2024-04-20 11:48:05 +08:00
|
|
|
|
public static SemanticToken MakeToken(SemanticTokenType tokenType,string literal,uint line,uint chPos)
|
2024-04-18 16:34:32 +08:00
|
|
|
|
{
|
|
|
|
|
SemanticToken? token;
|
|
|
|
|
switch (tokenType)
|
|
|
|
|
{
|
|
|
|
|
case SemanticTokenType.Character:
|
2024-04-20 11:48:05 +08:00
|
|
|
|
CharacterSemanticToken characterSemanticToken = new()
|
2024-04-18 16:34:32 +08:00
|
|
|
|
{
|
2024-04-20 11:48:05 +08:00
|
|
|
|
LinePos = line, CharacterPos = chPos, LiteralValue = literal,
|
2024-04-18 16:34:32 +08:00
|
|
|
|
};
|
|
|
|
|
token = characterSemanticToken;
|
|
|
|
|
break;
|
|
|
|
|
case SemanticTokenType.Identifier:
|
2024-04-20 11:48:05 +08:00
|
|
|
|
IdentifierSemanticToken identifierSemanticToken = new()
|
2024-04-18 16:34:32 +08:00
|
|
|
|
{
|
2024-04-20 11:48:05 +08:00
|
|
|
|
LinePos = line, CharacterPos = chPos, LiteralValue = literal,
|
2024-04-18 16:34:32 +08:00
|
|
|
|
};
|
|
|
|
|
token = identifierSemanticToken;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
2024-04-20 11:48:05 +08:00
|
|
|
|
throw new InvalidOperationException("Can only create Character or Identifier SemanticToken.");
|
2024-04-18 16:34:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return token;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-20 11:48:05 +08:00
|
|
|
|
public static KeywordSemanticToken MakeToken(KeywordType keywordType,string literal,uint line,uint chPos)
|
2024-04-18 16:34:32 +08:00
|
|
|
|
{
|
2024-04-20 11:48:05 +08:00
|
|
|
|
KeywordSemanticToken keywordSemanticToken = new()
|
2024-04-18 16:34:32 +08:00
|
|
|
|
{
|
2024-04-20 11:48:05 +08:00
|
|
|
|
LinePos = line,
|
|
|
|
|
CharacterPos = chPos,
|
2024-04-18 16:34:32 +08:00
|
|
|
|
LiteralValue = literal,
|
|
|
|
|
KeywordType = keywordType
|
|
|
|
|
};
|
|
|
|
|
return keywordSemanticToken;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-20 11:48:05 +08:00
|
|
|
|
public static DelimiterSemanticToken MakeToken(DelimiterType delimiterType,string literal,uint line,uint chPos)
|
2024-04-18 16:34:32 +08:00
|
|
|
|
{
|
2024-04-20 11:48:05 +08:00
|
|
|
|
DelimiterSemanticToken delimiterSemanticToken = new()
|
2024-04-18 16:34:32 +08:00
|
|
|
|
{
|
2024-04-20 11:48:05 +08:00
|
|
|
|
LinePos = line,
|
|
|
|
|
CharacterPos = chPos,
|
2024-04-18 16:34:32 +08:00
|
|
|
|
LiteralValue = literal,
|
|
|
|
|
DelimiterType = delimiterType
|
|
|
|
|
};
|
|
|
|
|
return delimiterSemanticToken;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-20 11:48:05 +08:00
|
|
|
|
public static NumberSemanticToken MakeToken(NumberType numberType,string literal,uint line,uint chPos)
|
2024-04-18 16:34:32 +08:00
|
|
|
|
{
|
|
|
|
|
string temp = literal;
|
|
|
|
|
string result;
|
|
|
|
|
if (numberType == NumberType.Hex)
|
|
|
|
|
{
|
|
|
|
|
result = string.Concat("0x", temp.AsSpan(1, temp.Length - 1));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
result = temp;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-20 11:48:05 +08:00
|
|
|
|
NumberSemanticToken numberSemanticToken = new()
|
2024-04-18 16:34:32 +08:00
|
|
|
|
{
|
2024-04-20 11:48:05 +08:00
|
|
|
|
LinePos = line,
|
|
|
|
|
CharacterPos = chPos,
|
2024-04-18 16:34:32 +08:00
|
|
|
|
LiteralValue = result,
|
|
|
|
|
NumberType = numberType
|
|
|
|
|
};
|
|
|
|
|
return numberSemanticToken;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-20 11:48:05 +08:00
|
|
|
|
public static OperatorSemanticToken MakeToken(OperatorType operatorType,string literal,uint line,uint chPos)
|
2024-04-18 16:34:32 +08:00
|
|
|
|
{
|
2024-04-20 11:48:05 +08:00
|
|
|
|
OperatorSemanticToken operatorSemanticToken = new()
|
2024-04-18 16:34:32 +08:00
|
|
|
|
{
|
2024-04-20 11:48:05 +08:00
|
|
|
|
LinePos = line,
|
|
|
|
|
CharacterPos = chPos,
|
2024-04-18 16:34:32 +08:00
|
|
|
|
LiteralValue = literal,
|
|
|
|
|
OperatorType = operatorType
|
|
|
|
|
};
|
|
|
|
|
return operatorSemanticToken;
|
|
|
|
|
}
|
|
|
|
|
}
|