This commit is contained in:
nub31
2026-02-25 19:39:31 +01:00
parent 282dd0502a
commit 7b1e202424
2 changed files with 55 additions and 40 deletions

View File

@@ -81,7 +81,7 @@ public class Parser
foreach (var modifier in modifiers)
// todo(nub31): Add to diagnostics instead of throwing
throw new CompileException(Diagnostic.Error("Invalid modifier for function").At(fileName, modifier.Value).Build());
throw BasicError("Invalid modifier for function", modifier.Value);
var name = ExpectIdent();
var parameters = new List<NodeDefinitionFunc.Param>();
@@ -111,7 +111,7 @@ public class Parser
foreach (var modifier in modifiers)
// todo(nub31): Add to diagnostics instead of throwing
throw new CompileException(Diagnostic.Error("Invalid modifier for struct").At(fileName, modifier.Value).Build());
throw BasicError("Invalid modifier for struct", modifier.Value);
var name = ExpectIdent();
var fields = new List<NodeDefinitionStruct.Field>();
@@ -135,7 +135,7 @@ public class Parser
foreach (var modifier in modifiers)
// todo(nub31): Add to diagnostics instead of throwing
throw new CompileException(Diagnostic.Error("Invalid modifier for struct").At(fileName, modifier.Value).Build());
throw BasicError("Invalid modifier for struct", modifier.Value);
var name = ExpectIdent();
var variants = new List<NodeDefinitionEnum.Variant>();
@@ -174,7 +174,7 @@ public class Parser
foreach (var modifier in modifiers)
// todo(nub31): Add to diagnostics instead of throwing
throw new CompileException(Diagnostic.Error("Invalid modifier for global variable").At(fileName, modifier.Value).Build());
throw BasicError("Invalid modifier for global variable", modifier.Value);
var name = ExpectIdent();
ExpectSymbol(Symbol.Colon);
@@ -183,7 +183,7 @@ public class Parser
return new NodeDefinitionGlobalVariable(TokensFrom(startIndex), exported, name, type);
}
throw new CompileException(Diagnostic.Error("Not a valid definition").At(fileName, Peek()).Build());
throw BasicError("Not a valid definition", Peek());
}
private NodeStatement ParseStatement()
@@ -403,7 +403,7 @@ public class Parser
}
else
{
throw new CompileException(Diagnostic.Error("Expected start of expression").At(fileName, Peek()).Build());
throw BasicError("Expected start of expression", Peek());
}
while (true)
@@ -495,7 +495,7 @@ public class Parser
}
}
throw new CompileException(Diagnostic.Error("Expected type").At(fileName, Peek()).Build());
throw BasicError("Expected type", Peek());
}
private List<Token> TokensFrom(int startIndex)
@@ -511,7 +511,7 @@ public class Parser
return;
}
throw new CompileException(Diagnostic.Error($"Expected '{keyword.AsString()}'").At(fileName, Peek()).Build());
throw BasicError($"Expected '{keyword.AsString()}'", Peek());
}
private bool TryExpectKeyword(Keyword keyword)
@@ -533,7 +533,7 @@ public class Parser
return;
}
throw new CompileException(Diagnostic.Error($"Expected '{symbol.AsString()}'").At(fileName, Peek()).Build());
throw BasicError($"Expected '{symbol.AsString()}'", Peek());
}
private bool TryExpectSymbol(Symbol symbol)
@@ -555,7 +555,7 @@ public class Parser
return token;
}
throw new CompileException(Diagnostic.Error("Expected identifier").At(fileName, Peek()).Build());
throw BasicError("Expected identifier", Peek());
}
private bool TryExpectIdent([NotNullWhen(true)] out TokenIdent? ident)
@@ -613,7 +613,7 @@ public class Parser
private void Next()
{
if (index >= tokens.Count)
throw new CompileException(Diagnostic.Error("Unexpected end of tokens").At(fileName, Peek()).Build());
throw BasicError("Unexpected end of tokens", Peek());
index += 1;
}
@@ -686,6 +686,16 @@ public class Parser
return false;
}
}
private CompileException BasicError(string message, Token? ident)
{
return new CompileException(Diagnostic.Error(message).At(fileName, ident).Build());
}
private CompileException BasicError(string message, Node node)
{
return new CompileException(Diagnostic.Error(message).At(fileName, node).Build());
}
}
public class Ast(string fileName, TokenIdent moduleName, List<NodeDefinition> definitions)