fix: 区分字符和字符串 (#74)

Co-authored-by: Huaps <1183155719@qq.com>
Reviewed-on: PostGuard/Canon#74
This commit is contained in:
2024-05-04 13:57:14 +08:00
parent 8da24523c9
commit 6e8e3885ac
7 changed files with 74 additions and 30 deletions

View File

@@ -16,6 +16,13 @@ public static class LexemeFactory
};
token = characterSemanticToken;
break;
case SemanticTokenType.String:
StringSemanticToken stringSemanticToken = new()
{
LinePos = line, CharacterPos = chPos, LiteralValue = literal,
};
token = stringSemanticToken;
break;
case SemanticTokenType.Identifier:
IdentifierSemanticToken identifierSemanticToken = new()
{

View File

@@ -80,6 +80,7 @@ public class Lexer : ILexer
}
_tokens.Add(SemanticToken.End);
return _tokens;
}
@@ -447,7 +448,6 @@ public class Lexer : ILexer
}
break;
case '\'':
case '\"':
{
// 重置_token准备收集字符串内容
ResetTokenBuilder();
@@ -464,8 +464,18 @@ public class Lexer : ILexer
}
}
_semanticToken = LexemeFactory.MakeToken(SemanticTokenType.Character,
GetCurrentTokenString(), _line, _chPos);
string currentString = GetCurrentTokenString();
if (currentString.Length > 1)
{
_semanticToken = LexemeFactory.MakeToken(SemanticTokenType.String,
currentString, _line, _chPos);
}
else
{
_semanticToken = LexemeFactory.MakeToken(SemanticTokenType.Character,
currentString, _line, _chPos);
}
ResetTokenBuilder();

View File

@@ -104,6 +104,32 @@ public abstract class SemanticToken : IEquatable<SemanticToken>
public class CharacterSemanticToken : SemanticToken
{
public override SemanticTokenType TokenType => SemanticTokenType.Character;
/// <summary>
/// 获得令牌代表的字符
/// </summary>
/// <returns>字符</returns>
public char ParseAsCharacter()
{
return char.Parse(LiteralValue);
}
}
/// <summary>
/// 字符串类型记号
/// </summary>
public class StringSemanticToken : SemanticToken
{
public override SemanticTokenType TokenType => SemanticTokenType.String;
/// <summary>
/// 获得令牌代表的字符串
/// </summary>
/// <returns>字符串</returns>
public string ParseAsString()
{
return LiteralValue;
}
}
/// <summary>