This commit is contained in:
nub31
2025-10-26 22:28:48 +01:00
parent 27bc4da4fd
commit 560e6428ff
18 changed files with 663 additions and 483 deletions

View File

@@ -45,7 +45,7 @@ public sealed class Parser
Symbol.Func => ParseFunc(startIndex, exported, null),
Symbol.Struct => ParseStruct(startIndex, exported),
Symbol.Enum => ParseEnum(startIndex, exported),
_ => throw new ParseException(Diagnostic
_ => throw new CompileException(Diagnostic
.Error($"Expected 'func', 'struct', 'enum', 'import' or 'module' but found '{keyword.Symbol}'")
.WithHelp("Valid top level statements are 'func', 'struct', 'enum', 'import' and 'module'")
.At(keyword)
@@ -54,7 +54,7 @@ public sealed class Parser
topLevelSyntaxNodes.Add(definition);
}
catch (ParseException e)
catch (CompileException e)
{
Diagnostics.Add(e.Diagnostic);
while (HasToken)
@@ -180,7 +180,7 @@ public sealed class Parser
{
if (!TryExpectIntLiteral(out var intLiteralToken))
{
throw new ParseException(Diagnostic
throw new CompileException(Diagnostic
.Error("Value of enum field must be an integer literal")
.At(CurrentToken)
.Build());
@@ -451,13 +451,13 @@ public sealed class Parser
Symbol.OpenBrace => new StructInitializerSyntax(GetTokens(startIndex), null, ParseStructInitializerBody()),
Symbol.Struct => ParseStructInitializer(startIndex),
Symbol.At => ParseBuiltinFunction(startIndex),
_ => throw new ParseException(Diagnostic
_ => throw new CompileException(Diagnostic
.Error($"Unexpected symbol '{symbolToken.Symbol}' in expression")
.WithHelp("Expected '(', '-', '!', '[' or '{'")
.At(symbolToken)
.Build())
},
_ => throw new ParseException(Diagnostic
_ => throw new CompileException(Diagnostic
.Error($"Unexpected token '{token.GetType().Name}' in expression")
.WithHelp("Expected literal, identifier, or parenthesized expression")
.At(token)
@@ -488,7 +488,7 @@ public sealed class Parser
}
default:
{
throw new ParseException(Diagnostic.Error($"Unknown builtin {name.Value}").At(name).Build());
throw new CompileException(Diagnostic.Error($"Unknown builtin {name.Value}").At(name).Build());
}
}
}
@@ -628,7 +628,7 @@ public sealed class Parser
{
statements.Add(ParseStatement());
}
catch (ParseException ex)
catch (CompileException ex)
{
Diagnostics.Add(ex.Diagnostic);
if (HasToken)
@@ -654,7 +654,7 @@ public sealed class Parser
{
if (size is not 8 and not 16 and not 32 and not 64)
{
throw new ParseException(Diagnostic
throw new CompileException(Diagnostic
.Error("Arbitrary uint size is not supported")
.WithHelp("Use u8, u16, u32 or u64")
.At(name)
@@ -668,7 +668,7 @@ public sealed class Parser
{
if (size is not 8 and not 16 and not 32 and not 64)
{
throw new ParseException(Diagnostic
throw new CompileException(Diagnostic
.Error("Arbitrary int size is not supported")
.WithHelp("Use i8, i16, i32 or i64")
.At(name)
@@ -682,7 +682,7 @@ public sealed class Parser
{
if (size is not 32 and not 64)
{
throw new ParseException(Diagnostic
throw new CompileException(Diagnostic
.Error("Arbitrary float size is not supported")
.WithHelp("Use f32 or f64")
.At(name)
@@ -772,7 +772,7 @@ public sealed class Parser
}
}
throw new ParseException(Diagnostic
throw new CompileException(Diagnostic
.Error("Invalid type syntax")
.WithHelp("Expected type name, '^' for pointer, or '[]' for array")
.At(CurrentToken)
@@ -783,7 +783,7 @@ public sealed class Parser
{
if (!HasToken)
{
throw new ParseException(Diagnostic
throw new CompileException(Diagnostic
.Error("Unexpected end of file")
.WithHelp("Expected more tokens to complete the syntax")
.At(_tokens[^1])
@@ -800,7 +800,7 @@ public sealed class Parser
var token = ExpectToken();
if (token is not SymbolToken symbol)
{
throw new ParseException(Diagnostic
throw new CompileException(Diagnostic
.Error($"Expected symbol, but found {token.GetType().Name}")
.WithHelp("This position requires a symbol like '(', ')', '{', '}', etc.")
.At(token)
@@ -815,7 +815,7 @@ public sealed class Parser
var token = ExpectSymbol();
if (token.Symbol != expectedSymbol)
{
throw new ParseException(Diagnostic
throw new CompileException(Diagnostic
.Error($"Expected '{expectedSymbol}', but found '{token.Symbol}'")
.WithHelp($"Insert '{expectedSymbol}' here")
.At(token)
@@ -865,7 +865,7 @@ public sealed class Parser
var token = ExpectToken();
if (token is not IdentifierToken identifier)
{
throw new ParseException(Diagnostic
throw new CompileException(Diagnostic
.Error($"Expected identifier, but found {token.GetType().Name}")
.WithHelp("Provide a valid identifier name here")
.At(token)
@@ -893,7 +893,7 @@ public sealed class Parser
var token = ExpectToken();
if (token is not StringLiteralToken identifier)
{
throw new ParseException(Diagnostic
throw new CompileException(Diagnostic
.Error($"Expected string literal, but found {token.GetType().Name}")
.WithHelp("Provide a valid string literal")
.At(token)
@@ -914,14 +914,4 @@ public sealed class Parser
}
}
public record SyntaxTree(List<TopLevelSyntaxNode> TopLevelSyntaxNodes);
public class ParseException : Exception
{
public Diagnostic Diagnostic { get; }
public ParseException(Diagnostic diagnostic) : base(diagnostic.Message)
{
Diagnostic = diagnostic;
}
}
public record SyntaxTree(List<TopLevelSyntaxNode> TopLevelSyntaxNodes);