...
This commit is contained in:
@@ -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());
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user