namespace
This commit is contained in:
@@ -9,24 +9,19 @@ public class Parser
|
||||
private List<Diagnostic> _diagnostics = [];
|
||||
private List<Token> _tokens = [];
|
||||
private int _index;
|
||||
private string _namespace = string.Empty;
|
||||
|
||||
public DiagnosticsResult<SourceFile?> ParseModule(List<Token> tokens)
|
||||
{
|
||||
_diagnostics = [];
|
||||
_tokens = tokens;
|
||||
_index = 0;
|
||||
_namespace = string.Empty;
|
||||
|
||||
try
|
||||
{
|
||||
List<string> imports = [];
|
||||
while (TryExpectSymbol(Symbol.Import))
|
||||
{
|
||||
var name = ExpectIdentifier();
|
||||
imports.Add(name.Value);
|
||||
}
|
||||
|
||||
ExpectSymbol(Symbol.Module);
|
||||
var module = ExpectIdentifier().Value;
|
||||
ExpectSymbol(Symbol.Namespace);
|
||||
_namespace = ExpectIdentifier().Value;
|
||||
|
||||
List<DefinitionNode> definitions = [];
|
||||
|
||||
@@ -34,8 +29,8 @@ public class Parser
|
||||
{
|
||||
definitions.Add(ParseDefinition());
|
||||
}
|
||||
|
||||
return new DiagnosticsResult<SourceFile?>(_diagnostics, new SourceFile(module, imports, definitions));
|
||||
|
||||
return new DiagnosticsResult<SourceFile?>(_diagnostics, new SourceFile(_namespace, definitions));
|
||||
}
|
||||
catch (ParseException ex)
|
||||
{
|
||||
@@ -187,41 +182,7 @@ public class Parser
|
||||
{
|
||||
case IdentifierToken identifier:
|
||||
{
|
||||
var symbol = ExpectSymbol();
|
||||
switch (symbol.Symbol)
|
||||
{
|
||||
case Symbol.OpenParen:
|
||||
{
|
||||
var parameters = new List<ExpressionNode>();
|
||||
while (!TryExpectSymbol(Symbol.CloseParen))
|
||||
{
|
||||
parameters.Add(ParseExpression());
|
||||
TryExpectSymbol(Symbol.Comma);
|
||||
}
|
||||
|
||||
return new FuncCallStatementNode(GetTokensForNode(startIndex), new FuncCall(identifier.Value, parameters));
|
||||
}
|
||||
case Symbol.Assign:
|
||||
{
|
||||
var value = ParseExpression();
|
||||
return new VariableAssignmentNode(GetTokensForNode(startIndex), identifier.Value, Optional<NubType>.Empty(), value);
|
||||
}
|
||||
case Symbol.Colon:
|
||||
{
|
||||
var type = ParseType();
|
||||
ExpectSymbol(Symbol.Assign);
|
||||
var value = ParseExpression();
|
||||
return new VariableAssignmentNode(GetTokensForNode(startIndex), identifier.Value, type, value);
|
||||
}
|
||||
default:
|
||||
{
|
||||
throw new ParseException(Diagnostic
|
||||
.Error($"Unexpected symbol '{symbol.Symbol}' after identifier")
|
||||
.WithHelp("Expected '(', '=', or ':' after identifier")
|
||||
.At(symbol)
|
||||
.Build());
|
||||
}
|
||||
}
|
||||
return ParseStatementIdentifier(startIndex, identifier);
|
||||
}
|
||||
case SymbolToken symbol:
|
||||
{
|
||||
@@ -250,6 +211,72 @@ public class Parser
|
||||
}
|
||||
}
|
||||
|
||||
private StatementNode ParseStatementIdentifier(int startIndex, IdentifierToken identifier)
|
||||
{
|
||||
var symbol = ExpectSymbol();
|
||||
switch (symbol.Symbol)
|
||||
{
|
||||
case Symbol.DoubleColon:
|
||||
{
|
||||
var name = ExpectIdentifier();
|
||||
ExpectSymbol(Symbol.OpenParen);
|
||||
var parameters = new List<ExpressionNode>();
|
||||
while (!TryExpectSymbol(Symbol.CloseParen))
|
||||
{
|
||||
parameters.Add(ParseExpression());
|
||||
if (!TryExpectSymbol(Symbol.Comma) && Peek().TryGetValue(out var nextToken) && nextToken is not SymbolToken { Symbol: Symbol.CloseParen })
|
||||
{
|
||||
_diagnostics.Add(Diagnostic
|
||||
.Warning("Missing comma between function arguments")
|
||||
.WithHelp("Add a ',' to separate arguments")
|
||||
.At(nextToken)
|
||||
.Build());
|
||||
}
|
||||
}
|
||||
|
||||
return new FuncCallStatementNode(GetTokensForNode(startIndex), new FuncCall(identifier.Value, name.Value, parameters));
|
||||
}
|
||||
case Symbol.OpenParen:
|
||||
{
|
||||
var parameters = new List<ExpressionNode>();
|
||||
while (!TryExpectSymbol(Symbol.CloseParen))
|
||||
{
|
||||
parameters.Add(ParseExpression());
|
||||
if (!TryExpectSymbol(Symbol.Comma) && Peek().TryGetValue(out var nextToken) && nextToken is not SymbolToken { Symbol: Symbol.CloseParen })
|
||||
{
|
||||
_diagnostics.Add(Diagnostic
|
||||
.Warning("Missing comma between function arguments")
|
||||
.WithHelp("Add a ',' to separate arguments")
|
||||
.At(nextToken)
|
||||
.Build());
|
||||
}
|
||||
}
|
||||
|
||||
return new FuncCallStatementNode(GetTokensForNode(startIndex), new FuncCall(_namespace, identifier.Value, parameters));
|
||||
}
|
||||
case Symbol.Assign:
|
||||
{
|
||||
var value = ParseExpression();
|
||||
return new VariableAssignmentNode(GetTokensForNode(startIndex), identifier.Value, Optional<NubType>.Empty(), value);
|
||||
}
|
||||
case Symbol.Colon:
|
||||
{
|
||||
var type = ParseType();
|
||||
ExpectSymbol(Symbol.Assign);
|
||||
var value = ParseExpression();
|
||||
return new VariableAssignmentNode(GetTokensForNode(startIndex), identifier.Value, type, value);
|
||||
}
|
||||
default:
|
||||
{
|
||||
throw new ParseException(Diagnostic
|
||||
.Error($"Unexpected symbol '{symbol.Symbol}' after identifier")
|
||||
.WithHelp("Expected '(', '=', or ':' after identifier")
|
||||
.At(symbol)
|
||||
.Build());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private ReturnNode ParseReturn(int startIndex)
|
||||
{
|
||||
var value = Optional<ExpressionNode>.Empty();
|
||||
@@ -384,10 +411,11 @@ public class Parser
|
||||
var next = Peek();
|
||||
switch (next.Value)
|
||||
{
|
||||
case SymbolToken { Symbol: Symbol.OpenParen }:
|
||||
case SymbolToken { Symbol: Symbol.DoubleColon }:
|
||||
{
|
||||
Next();
|
||||
List<ExpressionNode> parameters = [];
|
||||
var name = ExpectIdentifier();
|
||||
ExpectSymbol(Symbol.OpenParen);
|
||||
var parameters = new List<ExpressionNode>();
|
||||
while (!TryExpectSymbol(Symbol.CloseParen))
|
||||
{
|
||||
parameters.Add(ParseExpression());
|
||||
@@ -401,7 +429,26 @@ public class Parser
|
||||
}
|
||||
}
|
||||
|
||||
expr = new FuncCallExpressionNode(GetTokensForNode(startIndex), new FuncCall(identifier.Value, parameters));
|
||||
expr = new FuncCallExpressionNode(GetTokensForNode(startIndex), new FuncCall(identifier.Value, name.Value, parameters));
|
||||
break;
|
||||
}
|
||||
case SymbolToken { Symbol: Symbol.OpenParen }:
|
||||
{
|
||||
var parameters = new List<ExpressionNode>();
|
||||
while (!TryExpectSymbol(Symbol.CloseParen))
|
||||
{
|
||||
parameters.Add(ParseExpression());
|
||||
if (!TryExpectSymbol(Symbol.Comma) && Peek().TryGetValue(out var nextToken) && nextToken is not SymbolToken { Symbol: Symbol.CloseParen })
|
||||
{
|
||||
_diagnostics.Add(Diagnostic
|
||||
.Warning("Missing comma between function arguments")
|
||||
.WithHelp("Add a ',' to separate arguments")
|
||||
.At(nextToken)
|
||||
.Build());
|
||||
}
|
||||
}
|
||||
|
||||
expr = new FuncCallExpressionNode(GetTokensForNode(startIndex), new FuncCall(_namespace, identifier.Value, parameters));
|
||||
break;
|
||||
}
|
||||
default:
|
||||
|
||||
Reference in New Issue
Block a user