wip: enums
This commit is contained in:
@@ -8,10 +8,10 @@ namespace NubLang.Parsing;
|
||||
public sealed class Parser
|
||||
{
|
||||
private readonly List<Diagnostic> _diagnostics = [];
|
||||
private readonly HashSet<string> _templateArguments = [];
|
||||
private List<Token> _tokens = [];
|
||||
private int _tokenIndex;
|
||||
private string _moduleName = string.Empty;
|
||||
private HashSet<string> _templateArguments = [];
|
||||
|
||||
private Token? CurrentToken => _tokenIndex < _tokens.Count ? _tokens[_tokenIndex] : null;
|
||||
private bool HasToken => CurrentToken != null;
|
||||
@@ -86,10 +86,11 @@ public sealed class Parser
|
||||
}
|
||||
|
||||
var keyword = ExpectSymbol();
|
||||
DefinitionSyntax definition = keyword.Symbol switch
|
||||
var definition = keyword.Symbol switch
|
||||
{
|
||||
Symbol.Func => ParseFunc(startIndex, exported, null),
|
||||
Symbol.Struct => ParseStruct(startIndex, exported),
|
||||
Symbol.Enum => ParseEnum(startIndex, exported),
|
||||
_ => throw new ParseException(Diagnostic
|
||||
.Error($"Expected 'func' or 'struct' but found '{keyword.Symbol}'")
|
||||
.WithHelp("Valid definition keywords are 'func' and 'struct'")
|
||||
@@ -228,6 +229,36 @@ public sealed class Parser
|
||||
return new StructSyntax(GetTokens(startIndex), name.Value, exported, fields, funcs);
|
||||
}
|
||||
|
||||
private EnumSyntax ParseEnum(int startIndex, bool exported)
|
||||
{
|
||||
var name = ExpectIdentifier();
|
||||
|
||||
TypeSyntax? type = null;
|
||||
if (TryExpectSymbol(Symbol.Colon))
|
||||
{
|
||||
type = ParseType();
|
||||
}
|
||||
|
||||
var values = new List<EnumValueSyntax>();
|
||||
|
||||
ExpectSymbol(Symbol.OpenBrace);
|
||||
while (!TryExpectSymbol(Symbol.CloseBrace))
|
||||
{
|
||||
var valueStartIndex = _tokenIndex;
|
||||
var valueName = ExpectIdentifier().Value;
|
||||
|
||||
ExpressionSyntax? valueValue = null;
|
||||
if (TryExpectSymbol(Symbol.Assign))
|
||||
{
|
||||
valueValue = ParseExpression();
|
||||
}
|
||||
|
||||
values.Add(new EnumValueSyntax(GetTokens(valueStartIndex), valueName, valueValue));
|
||||
}
|
||||
|
||||
return new EnumSyntax(GetTokens(startIndex), name.Value, exported, type, values);
|
||||
}
|
||||
|
||||
private StatementSyntax ParseStatement()
|
||||
{
|
||||
var startIndex = _tokenIndex;
|
||||
|
||||
Reference in New Issue
Block a user