This commit is contained in:
nub31
2025-05-26 19:16:56 +02:00
parent bc040c3fef
commit cfd7a6ebef
18 changed files with 124 additions and 156 deletions

View File

@@ -1,10 +0,0 @@
using Nub.Lang.Frontend.Lexing;
namespace Nub.Lang.Frontend.Parsing;
public class ModuleNode(IReadOnlyList<Token> tokens, string path, List<string> imports, List<DefinitionNode> definitions) : Node(tokens)
{
public string Path { get; } = path;
public List<string> Imports { get; } = imports;
public List<DefinitionNode> Definitions { get; } = definitions;
}

View File

@@ -6,41 +6,44 @@ namespace Nub.Lang.Frontend.Parsing;
public class Parser
{
private List<Diagnostic> _diagnostics = [];
private List<Token> _tokens = [];
private int _index;
private List<Diagnostic> _diagnostics = [];
public DiagnosticsResult<ModuleNode> ParseModule(List<Token> tokens, string rootFilePath)
public DiagnosticsResult<SourceFile?> ParseModule(List<Token> tokens)
{
_index = 0;
_tokens = tokens;
_diagnostics = [];
_tokens = tokens;
_index = 0;
List<DefinitionNode> definitions = [];
List<string> imports = [];
while (Peek().HasValue)
try
{
try
List<string> imports = [];
while (TryExpectSymbol(Symbol.Import))
{
if (TryExpectSymbol(Symbol.Import))
{
var name = ExpectIdentifier();
imports.Add(name.Value);
}
else
{
definitions.Add(ParseDefinition());
}
var name = ExpectIdentifier();
imports.Add(name.Value);
}
catch (ParseException ex)
ExpectSymbol(Symbol.Module);
var module = ExpectIdentifier().Value;
List<DefinitionNode> definitions = [];
while (Peek().HasValue)
{
_diagnostics.Add(ex.Diagnostic);
RecoverToNextDefinition();
definitions.Add(ParseDefinition());
}
return new DiagnosticsResult<SourceFile?>(_diagnostics, new SourceFile(module, imports, definitions));
}
catch (ParseException ex)
{
_diagnostics.Add(ex.Diagnostic);
RecoverToNextDefinition();
}
return new DiagnosticsResult<ModuleNode>(_diagnostics, new ModuleNode(GetTokensForNode(0), rootFilePath, imports, definitions));
return new DiagnosticsResult<SourceFile?>(_diagnostics, null);
}
private DefinitionNode ParseDefinition()
@@ -567,7 +570,7 @@ public class Parser
throw new ParseException(Diagnostic
.Error("Unexpected end of file while parsing type")
.WithHelp("Expected a type name")
.At(_tokens.Last().SourceFile, SourceLocationCalculator.GetSpan(_tokens.Last()))
.At(_tokens.Last().SourceText, SourceLocationCalculator.GetSpan(_tokens.Last()))
.Build());
}
@@ -585,7 +588,7 @@ public class Parser
throw new ParseException(Diagnostic
.Error("Unexpected end of file")
.WithHelp("Expected more tokens to complete the syntax")
.At(_tokens.Last().SourceFile, SourceLocationCalculator.GetSpan(_tokens.Last()))
.At(_tokens.Last().SourceText, SourceLocationCalculator.GetSpan(_tokens.Last()))
.Build());
}

View File

@@ -0,0 +1,8 @@
namespace Nub.Lang.Frontend.Parsing;
public class SourceFile(string module, List<string> imports, List<DefinitionNode> definitions)
{
public string Module { get; } = module;
public List<string> Imports { get; } = imports;
public List<DefinitionNode> Definitions { get; } = definitions;
}