This commit is contained in:
nub31
2025-09-29 16:57:25 +02:00
parent 428d69d242
commit 0de84375d3
6 changed files with 127 additions and 120 deletions

View File

@@ -39,11 +39,11 @@ public sealed class Parser
{
while (TryExpectSymbol(Symbol.Import))
{
imports.Add(ExpectLiteral(LiteralKind.String).Value);
imports.Add(ExpectStringLiteral().Value);
}
ExpectSymbol(Symbol.Module);
_moduleName = ExpectLiteral(LiteralKind.String).Value;
_moduleName = ExpectStringLiteral().Value;
}
catch (ParseException e)
{
@@ -75,9 +75,9 @@ public sealed class Parser
if (TryExpectSymbol(Symbol.Extern))
{
var externSymbol = ExpectLiteral(LiteralKind.String).Value;
var externSymbol = ExpectStringLiteral();
ExpectSymbol(Symbol.Func);
definitions.Add(ParseFunc(startIndex, exported, externSymbol));
definitions.Add(ParseFunc(startIndex, exported, externSymbol.Value));
continue;
}
@@ -464,7 +464,10 @@ public sealed class Parser
var token = ExpectToken();
var expr = token switch
{
LiteralToken literal => new LiteralSyntax(GetTokens(startIndex), literal.Value, literal.Kind),
BoolLiteralToken boolLiteral => new BoolLiteralSyntax(GetTokens(startIndex), boolLiteral.Value),
StringLiteralToken stringLiteral => new StringLiteralSyntax(GetTokens(startIndex), stringLiteral.Value),
FloatLiteralToken floatLiteral => new FloatLiteralSyntax(GetTokens(startIndex), floatLiteral.Value),
IntLiteralToken intLiteral => new IntLiteralSyntax(GetTokens(startIndex), intLiteral.Value, intLiteral.Base),
IdentifierToken identifier => ParseIdentifier(startIndex, identifier),
SymbolToken symbolToken => symbolToken.Symbol switch
{
@@ -920,14 +923,14 @@ public sealed class Parser
return identifier;
}
private LiteralToken ExpectLiteral()
private IntLiteralToken ExpectIntLiteral()
{
var token = ExpectToken();
if (token is not LiteralToken identifier)
if (token is not IntLiteralToken identifier)
{
throw new ParseException(Diagnostic
.Error($"Expected literal, but found {token.GetType().Name}")
.WithHelp("Provide a valid literal name here")
.Error($"Expected int literal, but found {token.GetType().Name}")
.WithHelp("Provide a valid int literal")
.At(token)
.Build());
}
@@ -935,19 +938,49 @@ public sealed class Parser
return identifier;
}
private LiteralToken ExpectLiteral(LiteralKind kind)
private FloatLiteralToken ExpectFloatLiteral()
{
var literal = ExpectLiteral();
if (literal.Kind != kind)
var token = ExpectToken();
if (token is not FloatLiteralToken identifier)
{
throw new ParseException(Diagnostic
.Error($"Expected {kind} literal, but found {literal.Kind}")
.WithHelp($"Provide a {kind} literal name here")
.At(literal)
.Error($"Expected float literal, but found {token.GetType().Name}")
.WithHelp("Provide a valid float literal")
.At(token)
.Build());
}
return literal;
return identifier;
}
private BoolLiteralToken ExpectBoolLiteral()
{
var token = ExpectToken();
if (token is not BoolLiteralToken identifier)
{
throw new ParseException(Diagnostic
.Error($"Expected bool literal, but found {token.GetType().Name}")
.WithHelp("Provide a valid bool literal")
.At(token)
.Build());
}
return identifier;
}
private StringLiteralToken ExpectStringLiteral()
{
var token = ExpectToken();
if (token is not StringLiteralToken identifier)
{
throw new ParseException(Diagnostic
.Error($"Expected string literal, but found {token.GetType().Name}")
.WithHelp("Provide a valid string literal")
.At(token)
.Build());
}
return identifier;
}
private void Next()