Make module nullable in custom type

This commit is contained in:
nub31
2025-10-22 14:37:40 +02:00
parent 03bab1fba5
commit 174c8aea1b
3 changed files with 96 additions and 95 deletions

View File

@@ -7,7 +7,6 @@ public sealed class Parser
{
private List<Token> _tokens = [];
private int _tokenIndex;
private string _moduleName = string.Empty;
private Token? CurrentToken => _tokenIndex < _tokens.Count ? _tokens[_tokenIndex] : null;
private bool HasToken => CurrentToken != null;
@@ -19,34 +18,9 @@ public sealed class Parser
Diagnostics.Clear();
_tokens = tokens;
_tokenIndex = 0;
_moduleName = string.Empty;
string? moduleName = null;
var imports = new List<string>();
try
{
while (TryExpectSymbol(Symbol.Import))
{
imports.Add(ExpectStringLiteral().Value);
}
ExpectSymbol(Symbol.Module);
_moduleName = ExpectStringLiteral().Value;
}
catch (ParseException e)
{
Diagnostics.Add(e.Diagnostic);
while (HasToken)
{
if (CurrentToken is SymbolToken { Symbol: Symbol.Module or Symbol.Import })
{
break;
}
Next();
}
}
var definitions = new List<DefinitionSyntax>();
while (HasToken)
@@ -54,6 +28,41 @@ public sealed class Parser
try
{
var startIndex = _tokenIndex;
if (TryExpectSymbol(Symbol.Import))
{
var name = ExpectStringLiteral();
if (imports.Contains(name.Value))
{
Diagnostics.Add(Diagnostic
.Warning($"Module {name.Value} is imported twice")
.At(name)
.WithHelp($"Remove duplicate import \"{name.Value}\"")
.Build());
}
else
{
imports.Add(name.Value);
}
continue;
}
if (TryExpectSymbol(Symbol.Module))
{
if (moduleName != null)
{
throw new ParseException(Diagnostic
.Error("Module is declared more than once")
.At(CurrentToken)
.WithHelp("Remove duplicate module declaration")
.Build());
}
moduleName = ExpectStringLiteral().Value;
continue;
}
var exported = TryExpectSymbol(Symbol.Export);
if (TryExpectSymbol(Symbol.Extern))
@@ -93,7 +102,7 @@ public sealed class Parser
}
}
return new SyntaxTree(definitions, _moduleName, imports);
return new SyntaxTree(definitions, moduleName ?? "default", imports);
}
private FuncParameterSyntax ParseFuncParameter()
@@ -659,7 +668,7 @@ public sealed class Parser
return new BoolTypeSyntax(GetTokens(startIndex));
default:
{
var module = _moduleName;
string? module = null;
if (TryExpectSymbol(Symbol.DoubleColon))
{