900 lines
30 KiB
C#
900 lines
30 KiB
C#
using System.Diagnostics;
|
|
using System.Diagnostics.CodeAnalysis;
|
|
using Nub.Lang.Diagnostics;
|
|
using Nub.Lang.Frontend.Lexing;
|
|
using Nub.Lang.Frontend.Parsing.Definitions;
|
|
using Nub.Lang.Frontend.Parsing.Expressions;
|
|
using Nub.Lang.Frontend.Parsing.Statements;
|
|
using Nub.Lang.Frontend.Typing;
|
|
|
|
namespace Nub.Lang.Frontend.Parsing;
|
|
|
|
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
|
|
{
|
|
ExpectSymbol(Symbol.Namespace);
|
|
_namespace = ExpectIdentifier().Value;
|
|
|
|
List<DefinitionNode> definitions = [];
|
|
|
|
while (Peek().HasValue)
|
|
{
|
|
definitions.Add(ParseDefinition());
|
|
}
|
|
|
|
return new DiagnosticsResult<SourceFile?>(_diagnostics, new SourceFile(_namespace, definitions));
|
|
}
|
|
catch (ParseException ex)
|
|
{
|
|
_diagnostics.Add(ex.Diagnostic);
|
|
RecoverToNextDefinition();
|
|
}
|
|
|
|
return new DiagnosticsResult<SourceFile?>(_diagnostics, null);
|
|
}
|
|
|
|
private DefinitionNode ParseDefinition()
|
|
{
|
|
var startIndex = _index;
|
|
List<ModifierToken> modifiers = [];
|
|
|
|
List<string> documentationParts = [];
|
|
while (_index < _tokens.Count && _tokens[_index] is DocumentationToken commentToken)
|
|
{
|
|
documentationParts.Add(commentToken.Documentation);
|
|
_index++;
|
|
}
|
|
|
|
var documentation = documentationParts.Count == 0 ? null : string.Join('\n', documentationParts);
|
|
|
|
while (TryExpectModifier(out var modifier))
|
|
{
|
|
modifiers.Add(modifier);
|
|
}
|
|
|
|
var keyword = ExpectSymbol();
|
|
return keyword.Symbol switch
|
|
{
|
|
Symbol.Func => ParseFuncDefinition(startIndex, modifiers, Optional.OfNullable(documentation)),
|
|
Symbol.Struct => ParseStruct(startIndex, modifiers, Optional.OfNullable(documentation)),
|
|
_ => throw new ParseException(Diagnostic
|
|
.Error($"Expected 'func' or 'struct', but found '{keyword.Symbol}'")
|
|
.WithHelp("Valid definition keywords are 'func' and 'struct'")
|
|
.At(keyword)
|
|
.Build())
|
|
};
|
|
}
|
|
|
|
private DefinitionNode ParseFuncDefinition(int startIndex, List<ModifierToken> modifiers, Optional<string> documentation)
|
|
{
|
|
var name = ExpectIdentifier();
|
|
List<FuncParameter> parameters = [];
|
|
|
|
ExpectSymbol(Symbol.OpenParen);
|
|
|
|
while (!TryExpectSymbol(Symbol.CloseParen))
|
|
{
|
|
parameters.Add(ParseFuncParameter());
|
|
|
|
if (!TryExpectSymbol(Symbol.Comma) && Peek().TryGetValue(out var token) && token is not SymbolToken { Symbol: Symbol.CloseParen })
|
|
{
|
|
_diagnostics.Add(Diagnostic
|
|
.Warning("Missing comma between function parameters")
|
|
.WithHelp("Add a ',' to separate parameters")
|
|
.At(token)
|
|
.Build());
|
|
}
|
|
}
|
|
|
|
var returnType = TryExpectSymbol(Symbol.Colon) ? ParseType() : new NubVoidType();
|
|
|
|
var isExtern = modifiers.RemoveAll(x => x.Modifier == Modifier.Extern) > 0;
|
|
if (isExtern)
|
|
{
|
|
if (modifiers.Count != 0)
|
|
{
|
|
throw new ParseException(Diagnostic
|
|
.Error($"Invalid modifier for extern function: {modifiers[0].Modifier}")
|
|
.WithHelp($"Extern functions cannot use the '{modifiers[0].Modifier}' modifier")
|
|
.At(modifiers[0])
|
|
.Build());
|
|
}
|
|
|
|
var callName = name.Value;
|
|
|
|
if (TryExpectSymbol(Symbol.Calls))
|
|
{
|
|
callName = ExpectIdentifier().Value;
|
|
}
|
|
|
|
return new ExternFuncDefinitionNode(GetTokensForNode(startIndex), documentation, name.Value, _namespace, callName, parameters, returnType);
|
|
}
|
|
|
|
var body = ParseBlock();
|
|
var exported = modifiers.RemoveAll(x => x.Modifier == Modifier.Export) > 0;
|
|
|
|
if (modifiers.Count != 0)
|
|
{
|
|
throw new ParseException(Diagnostic
|
|
.Error($"Invalid modifiers for function: {modifiers[0].Modifier}")
|
|
.WithHelp($"Functions cannot use the '{modifiers[0].Modifier}' modifier")
|
|
.At(modifiers[0])
|
|
.Build());
|
|
}
|
|
|
|
return new LocalFuncDefinitionNode(GetTokensForNode(startIndex), documentation, name.Value, _namespace, parameters, body, returnType, exported);
|
|
}
|
|
|
|
private StructDefinitionNode ParseStruct(int startIndex, List<ModifierToken> _, Optional<string> documentation)
|
|
{
|
|
var name = ExpectIdentifier().Value;
|
|
|
|
ExpectSymbol(Symbol.OpenBrace);
|
|
|
|
List<StructField> variables = [];
|
|
|
|
while (!TryExpectSymbol(Symbol.CloseBrace))
|
|
{
|
|
var variableName = ExpectIdentifier().Value;
|
|
ExpectSymbol(Symbol.Colon);
|
|
var variableType = ParseType();
|
|
|
|
var variableValue = Optional<ExpressionNode>.Empty();
|
|
|
|
if (TryExpectSymbol(Symbol.Assign))
|
|
{
|
|
variableValue = ParseExpression();
|
|
}
|
|
|
|
variables.Add(new StructField(variableName, variableType, variableValue));
|
|
}
|
|
|
|
return new StructDefinitionNode(GetTokensForNode(startIndex), documentation, name, _namespace, variables);
|
|
}
|
|
|
|
private FuncParameter ParseFuncParameter()
|
|
{
|
|
var variadic = false;
|
|
if (TryExpectSymbol(Symbol.Period))
|
|
{
|
|
ExpectSymbol(Symbol.Period);
|
|
ExpectSymbol(Symbol.Period);
|
|
variadic = true;
|
|
}
|
|
|
|
var name = ExpectIdentifier();
|
|
ExpectSymbol(Symbol.Colon);
|
|
var type = ParseType();
|
|
|
|
return new FuncParameter(name.Value, type, variadic);
|
|
}
|
|
|
|
private StatementNode ParseStatement()
|
|
{
|
|
var startIndex = _index;
|
|
if (!Peek().TryGetValue(out var token))
|
|
{
|
|
throw new ParseException(Diagnostic
|
|
.Error("Unexpected end of file while parsing statement")
|
|
.At(_tokens.Last())
|
|
.Build());
|
|
}
|
|
|
|
if (token is SymbolToken symbol)
|
|
{
|
|
switch (symbol.Symbol)
|
|
{
|
|
case Symbol.Return:
|
|
return ParseReturn(startIndex);
|
|
case Symbol.If:
|
|
return ParseIf(startIndex);
|
|
case Symbol.While:
|
|
return ParseWhile(startIndex);
|
|
case Symbol.Let:
|
|
return ParseVariableDeclaration(startIndex);
|
|
case Symbol.Break:
|
|
return ParseBreak(startIndex);
|
|
case Symbol.Continue:
|
|
return ParseContinue(startIndex);
|
|
}
|
|
}
|
|
|
|
return ParseStatementExpression(startIndex);
|
|
}
|
|
|
|
private StatementNode ParseStatementExpression(int startIndex)
|
|
{
|
|
var expr = ParseExpression();
|
|
|
|
if (Peek().TryGetValue(out var token))
|
|
{
|
|
if (token is SymbolToken symbol)
|
|
{
|
|
switch (symbol.Symbol)
|
|
{
|
|
case Symbol.Assign:
|
|
{
|
|
switch (expr)
|
|
{
|
|
case MemberAccessNode memberAccess:
|
|
{
|
|
Next();
|
|
var value = ParseExpression();
|
|
return new MemberAssignmentNode(GetTokensForNode(startIndex), memberAccess, value);
|
|
}
|
|
case ArrayIndexAccessNode arrayIndexAccess:
|
|
{
|
|
Next();
|
|
var value = ParseExpression();
|
|
return new ArrayIndexAssignmentNode(GetTokensForNode(startIndex), arrayIndexAccess, value);
|
|
}
|
|
case IdentifierNode identifier:
|
|
{
|
|
Next();
|
|
var value = ParseExpression();
|
|
return new VariableAssignmentNode(GetTokensForNode(startIndex), identifier, value);
|
|
}
|
|
case DereferenceNode dereference:
|
|
{
|
|
Next();
|
|
var value = ParseExpression();
|
|
return new DereferenceAssignmentNode(GetTokensForNode(startIndex), dereference, value);
|
|
}
|
|
}
|
|
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return new StatementExpressionNode(GetTokensForNode(startIndex), expr);
|
|
}
|
|
|
|
private VariableDeclarationNode ParseVariableDeclaration(int startIndex)
|
|
{
|
|
ExpectSymbol(Symbol.Let);
|
|
var name = ExpectIdentifier().Value;
|
|
var type = Optional<NubType>.Empty();
|
|
if (TryExpectSymbol(Symbol.Colon))
|
|
{
|
|
type = ParseType();
|
|
}
|
|
|
|
var value = Optional<ExpressionNode>.Empty();
|
|
if (TryExpectSymbol(Symbol.Assign))
|
|
{
|
|
value = ParseExpression();
|
|
}
|
|
|
|
return new VariableDeclarationNode(GetTokensForNode(startIndex), name, type, value);
|
|
}
|
|
|
|
private StatementNode ParseBreak(int startIndex)
|
|
{
|
|
ExpectSymbol(Symbol.Break);
|
|
Next();
|
|
return new BreakNode(GetTokensForNode(startIndex));
|
|
}
|
|
|
|
private StatementNode ParseContinue(int startIndex)
|
|
{
|
|
ExpectSymbol(Symbol.Continue);
|
|
return new ContinueNode(GetTokensForNode(startIndex));
|
|
}
|
|
|
|
private ReturnNode ParseReturn(int startIndex)
|
|
{
|
|
ExpectSymbol(Symbol.Return);
|
|
var value = Optional<ExpressionNode>.Empty();
|
|
if (!TryExpectSymbol(Symbol.Semicolon))
|
|
{
|
|
value = ParseExpression();
|
|
}
|
|
|
|
return new ReturnNode(GetTokensForNode(startIndex), value);
|
|
}
|
|
|
|
private IfNode ParseIf(int startIndex)
|
|
{
|
|
ExpectSymbol(Symbol.If);
|
|
var condition = ParseExpression();
|
|
var body = ParseBlock();
|
|
|
|
var elseStatement = Optional<Variant<IfNode, BlockNode>>.Empty();
|
|
if (TryExpectSymbol(Symbol.Else))
|
|
{
|
|
var newStartIndex = _index;
|
|
elseStatement = TryExpectSymbol(Symbol.If)
|
|
? (Variant<IfNode, BlockNode>)ParseIf(newStartIndex)
|
|
: (Variant<IfNode, BlockNode>)ParseBlock();
|
|
}
|
|
|
|
return new IfNode(GetTokensForNode(startIndex), condition, body, elseStatement);
|
|
}
|
|
|
|
private WhileNode ParseWhile(int startIndex)
|
|
{
|
|
ExpectSymbol(Symbol.While);
|
|
var condition = ParseExpression();
|
|
var body = ParseBlock();
|
|
return new WhileNode(GetTokensForNode(startIndex), condition, body);
|
|
}
|
|
|
|
private ExpressionNode ParseExpression(int precedence = 0)
|
|
{
|
|
var startIndex = _index;
|
|
var left = ParsePrimaryExpression();
|
|
|
|
while (true)
|
|
{
|
|
var token = Peek();
|
|
if (!token.HasValue || token.Value is not SymbolToken symbolToken || !TryGetBinaryOperator(symbolToken.Symbol, out var op) ||
|
|
GetBinaryOperatorPrecedence(op.Value) < precedence)
|
|
{
|
|
break;
|
|
}
|
|
|
|
Next();
|
|
var right = ParseExpression(GetBinaryOperatorPrecedence(op.Value) + 1);
|
|
|
|
left = new BinaryExpressionNode(GetTokensForNode(startIndex), left, op.Value, right);
|
|
}
|
|
|
|
return left;
|
|
}
|
|
|
|
private static int GetBinaryOperatorPrecedence(BinaryExpressionOperator binaryExpressionOperator)
|
|
{
|
|
return binaryExpressionOperator switch
|
|
{
|
|
BinaryExpressionOperator.Multiply => 3,
|
|
BinaryExpressionOperator.Divide => 3,
|
|
BinaryExpressionOperator.Plus => 2,
|
|
BinaryExpressionOperator.Minus => 2,
|
|
BinaryExpressionOperator.GreaterThan => 1,
|
|
BinaryExpressionOperator.GreaterThanOrEqual => 1,
|
|
BinaryExpressionOperator.LessThan => 1,
|
|
BinaryExpressionOperator.LessThanOrEqual => 1,
|
|
BinaryExpressionOperator.Equal => 0,
|
|
BinaryExpressionOperator.NotEqual => 0,
|
|
_ => throw new ArgumentOutOfRangeException(nameof(binaryExpressionOperator), binaryExpressionOperator, null)
|
|
};
|
|
}
|
|
|
|
private static bool TryGetBinaryOperator(Symbol symbol, [NotNullWhen(true)] out BinaryExpressionOperator? binaryExpressionOperator)
|
|
{
|
|
switch (symbol)
|
|
{
|
|
case Symbol.Equal:
|
|
binaryExpressionOperator = BinaryExpressionOperator.Equal;
|
|
return true;
|
|
case Symbol.NotEqual:
|
|
binaryExpressionOperator = BinaryExpressionOperator.NotEqual;
|
|
return true;
|
|
case Symbol.LessThan:
|
|
binaryExpressionOperator = BinaryExpressionOperator.LessThan;
|
|
return true;
|
|
case Symbol.LessThanOrEqual:
|
|
binaryExpressionOperator = BinaryExpressionOperator.LessThanOrEqual;
|
|
return true;
|
|
case Symbol.GreaterThan:
|
|
binaryExpressionOperator = BinaryExpressionOperator.GreaterThan;
|
|
return true;
|
|
case Symbol.GreaterThanOrEqual:
|
|
binaryExpressionOperator = BinaryExpressionOperator.GreaterThanOrEqual;
|
|
return true;
|
|
case Symbol.Plus:
|
|
binaryExpressionOperator = BinaryExpressionOperator.Plus;
|
|
return true;
|
|
case Symbol.Minus:
|
|
binaryExpressionOperator = BinaryExpressionOperator.Minus;
|
|
return true;
|
|
case Symbol.Star:
|
|
binaryExpressionOperator = BinaryExpressionOperator.Multiply;
|
|
return true;
|
|
case Symbol.ForwardSlash:
|
|
binaryExpressionOperator = BinaryExpressionOperator.Divide;
|
|
return true;
|
|
default:
|
|
binaryExpressionOperator = null;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
private ExpressionNode ParsePrimaryExpression()
|
|
{
|
|
var startIndex = _index;
|
|
ExpressionNode expr;
|
|
|
|
var token = ExpectToken();
|
|
switch (token)
|
|
{
|
|
case LiteralToken literal:
|
|
{
|
|
expr = new LiteralNode(GetTokensForNode(startIndex), literal.Value, literal.Kind);
|
|
break;
|
|
}
|
|
case IdentifierToken identifier:
|
|
{
|
|
var next = Peek();
|
|
switch (next.Value)
|
|
{
|
|
case SymbolToken { Symbol: Symbol.DoubleColon }:
|
|
{
|
|
Next();
|
|
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());
|
|
}
|
|
}
|
|
|
|
expr = new FuncCallNode(GetTokensForNode(startIndex), identifier.Value, name.Value, parameters);
|
|
break;
|
|
}
|
|
case SymbolToken { Symbol: Symbol.OpenParen }:
|
|
{
|
|
Next();
|
|
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 FuncCallNode(GetTokensForNode(startIndex), _namespace, identifier.Value, parameters);
|
|
break;
|
|
}
|
|
default:
|
|
{
|
|
expr = new IdentifierNode(GetTokensForNode(startIndex), identifier.Value);
|
|
break;
|
|
}
|
|
}
|
|
|
|
break;
|
|
}
|
|
case SymbolToken symbolToken:
|
|
{
|
|
switch (symbolToken.Symbol)
|
|
{
|
|
case Symbol.OpenParen:
|
|
{
|
|
var expression = ParseExpression();
|
|
ExpectSymbol(Symbol.CloseParen);
|
|
expr = expression;
|
|
break;
|
|
}
|
|
case Symbol.Ampersand:
|
|
{
|
|
var expression = ParsePrimaryExpression();
|
|
if (expression is not LValueNode lValue)
|
|
{
|
|
throw new ParseException(Diagnostic
|
|
.Error("& symbol can only be used on lvalues")
|
|
.At(expression)
|
|
.Build());
|
|
}
|
|
|
|
expr = new AddressOfNode(GetTokensForNode(startIndex), lValue);
|
|
break;
|
|
}
|
|
case Symbol.Minus:
|
|
{
|
|
var expression = ParsePrimaryExpression();
|
|
expr = new UnaryExpressionNode(GetTokensForNode(startIndex), UnaryExpressionOperator.Negate, expression);
|
|
break;
|
|
}
|
|
case Symbol.Bang:
|
|
{
|
|
var expression = ParsePrimaryExpression();
|
|
expr = new UnaryExpressionNode(GetTokensForNode(startIndex), UnaryExpressionOperator.Invert, expression);
|
|
break;
|
|
}
|
|
case Symbol.OpenBracket:
|
|
{
|
|
if (Peek().TryGetValue(out var capacityToken) && capacityToken is LiteralToken { Kind: LiteralKind.Integer } literalToken)
|
|
{
|
|
var capacity = int.Parse(literalToken.Value);
|
|
Next();
|
|
ExpectSymbol(Symbol.CloseBracket);
|
|
var elementType = ParseType();
|
|
|
|
if (capacity > 0)
|
|
{
|
|
expr = new FixedArrayInitializerNode(GetTokensForNode(startIndex), elementType, capacity);
|
|
}
|
|
else
|
|
{
|
|
throw new ParseException(Diagnostic
|
|
.Error("Fixed array size must be a positive integer")
|
|
.WithHelp("Use a positive integer literal for the array size")
|
|
.At(literalToken)
|
|
.Build());
|
|
}
|
|
}
|
|
else
|
|
{
|
|
var capacity = ParseExpression();
|
|
ExpectSymbol(Symbol.CloseBracket);
|
|
var type = ParseType();
|
|
|
|
expr = new ArrayInitializerNode(GetTokensForNode(startIndex), capacity, type);
|
|
}
|
|
|
|
break;
|
|
}
|
|
case Symbol.Alloc:
|
|
{
|
|
var type = ParseType();
|
|
Dictionary<string, ExpressionNode> initializers = [];
|
|
ExpectSymbol(Symbol.OpenBrace);
|
|
while (!TryExpectSymbol(Symbol.CloseBrace))
|
|
{
|
|
var name = ExpectIdentifier().Value;
|
|
ExpectSymbol(Symbol.Assign);
|
|
var value = ParseExpression();
|
|
initializers.Add(name, value);
|
|
}
|
|
|
|
if (type is not NubStructType structType)
|
|
{
|
|
throw new ParseException(Diagnostic
|
|
.Error($"Cannot alloc type '{type}'")
|
|
.At(symbolToken)
|
|
.Build());
|
|
}
|
|
|
|
expr = new StructInitializerNode(GetTokensForNode(startIndex), structType, initializers);
|
|
break;
|
|
}
|
|
default:
|
|
{
|
|
throw new ParseException(Diagnostic
|
|
.Error($"Unexpected symbol '{symbolToken.Symbol}' in expression")
|
|
.WithHelp("Expected literal, identifier, or '(' to start expression")
|
|
.At(symbolToken)
|
|
.Build());
|
|
}
|
|
}
|
|
|
|
break;
|
|
}
|
|
default:
|
|
{
|
|
throw new ParseException(Diagnostic
|
|
.Error($"Unexpected token '{token.GetType().Name}' in expression")
|
|
.WithHelp("Expected literal, identifier, or parenthesized expression")
|
|
.At(token)
|
|
.Build());
|
|
}
|
|
}
|
|
|
|
return ParsePostfixOperators(startIndex, expr);
|
|
}
|
|
|
|
private ExpressionNode ParsePostfixOperators(int startIndex, ExpressionNode expr)
|
|
{
|
|
while (true)
|
|
{
|
|
if (TryExpectSymbol(Symbol.Caret))
|
|
{
|
|
expr = new DereferenceNode(GetTokensForNode(startIndex), expr);
|
|
continue;
|
|
}
|
|
|
|
if (TryExpectSymbol(Symbol.Period))
|
|
{
|
|
var structMember = ExpectIdentifier().Value;
|
|
expr = new MemberAccessNode(GetTokensForNode(startIndex), expr, structMember);
|
|
continue;
|
|
}
|
|
|
|
if (TryExpectSymbol(Symbol.OpenBracket))
|
|
{
|
|
var index = ParseExpression();
|
|
ExpectSymbol(Symbol.CloseBracket);
|
|
expr = new ArrayIndexAccessNode(GetTokensForNode(startIndex), expr, index);
|
|
continue;
|
|
}
|
|
|
|
break;
|
|
}
|
|
|
|
return expr;
|
|
}
|
|
|
|
private BlockNode ParseBlock()
|
|
{
|
|
var startIndex = _index;
|
|
ExpectSymbol(Symbol.OpenBrace);
|
|
List<StatementNode> statements = [];
|
|
while (Peek().HasValue && !TryExpectSymbol(Symbol.CloseBrace))
|
|
{
|
|
try
|
|
{
|
|
statements.Add(ParseStatement());
|
|
}
|
|
catch (ParseException ex)
|
|
{
|
|
_diagnostics.Add(ex.Diagnostic);
|
|
RecoverToNextStatement();
|
|
}
|
|
}
|
|
|
|
return new BlockNode(GetTokensForNode(startIndex), statements);
|
|
}
|
|
|
|
private NubType ParseType()
|
|
{
|
|
if (TryExpectIdentifier(out var name))
|
|
{
|
|
if (name == "any")
|
|
{
|
|
return new NubAnyType();
|
|
}
|
|
|
|
if (name == "void")
|
|
{
|
|
return new NubVoidType();
|
|
}
|
|
|
|
if (NubPrimitiveType.TryParse(name, out var primitiveTypeKind))
|
|
{
|
|
return new NubPrimitiveType(primitiveTypeKind.Value);
|
|
}
|
|
|
|
var @namespace = _namespace;
|
|
if (TryExpectSymbol(Symbol.DoubleColon))
|
|
{
|
|
@namespace = ExpectIdentifier().Value;
|
|
}
|
|
|
|
return new NubStructType(@namespace, name);
|
|
}
|
|
|
|
if (TryExpectSymbol(Symbol.Caret))
|
|
{
|
|
var baseType = ParseType();
|
|
return new NubPointerType(baseType);
|
|
}
|
|
|
|
if (TryExpectSymbol(Symbol.OpenBracket))
|
|
{
|
|
if (Peek().TryGetValue(out var token) && token is LiteralToken { Kind: LiteralKind.Integer, Value: var sizeValue })
|
|
{
|
|
Next();
|
|
ExpectSymbol(Symbol.CloseBracket);
|
|
var baseType = ParseType();
|
|
|
|
var size = int.Parse(sizeValue);
|
|
|
|
if (size > 0)
|
|
{
|
|
return new NubFixedArrayType(baseType, size);
|
|
}
|
|
else
|
|
{
|
|
throw new UnreachableException();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
ExpectSymbol(Symbol.CloseBracket);
|
|
var baseType = ParseType();
|
|
return new NubArrayType(baseType);
|
|
}
|
|
}
|
|
|
|
if (!Peek().TryGetValue(out var peekToken))
|
|
{
|
|
throw new ParseException(Diagnostic
|
|
.Error("Unexpected end of file while parsing type")
|
|
.WithHelp("Expected a type name")
|
|
.At(_tokens.Last())
|
|
.Build());
|
|
}
|
|
|
|
throw new ParseException(Diagnostic
|
|
.Error("Invalid type syntax")
|
|
.WithHelp("Expected type name, '^' for pointer, or '[]' for array")
|
|
.At(peekToken)
|
|
.Build());
|
|
}
|
|
|
|
private Token ExpectToken()
|
|
{
|
|
if (!Peek().TryGetValue(out var token))
|
|
{
|
|
throw new ParseException(Diagnostic
|
|
.Error("Unexpected end of file")
|
|
.WithHelp("Expected more tokens to complete the syntax")
|
|
.At(_tokens.Last())
|
|
.Build());
|
|
}
|
|
|
|
Next();
|
|
return token;
|
|
}
|
|
|
|
private SymbolToken ExpectSymbol()
|
|
{
|
|
var token = ExpectToken();
|
|
if (token is not SymbolToken symbol)
|
|
{
|
|
throw new ParseException(Diagnostic
|
|
.Error($"Expected symbol, but found {token.GetType().Name}")
|
|
.WithHelp("This position requires a symbol like '(', ')', '{', '}', etc.")
|
|
.At(token)
|
|
.Build());
|
|
}
|
|
|
|
return symbol;
|
|
}
|
|
|
|
private void ExpectSymbol(Symbol expectedSymbol)
|
|
{
|
|
var token = ExpectSymbol();
|
|
if (token.Symbol != expectedSymbol)
|
|
{
|
|
throw new ParseException(Diagnostic
|
|
.Error($"Expected '{expectedSymbol}', but found '{token.Symbol}'")
|
|
.WithHelp($"Insert '{expectedSymbol}' here")
|
|
.At(token)
|
|
.Build());
|
|
}
|
|
}
|
|
|
|
private bool TryExpectSymbol(Symbol symbol)
|
|
{
|
|
if (Peek() is { Value: SymbolToken symbolToken } && symbolToken.Symbol == symbol)
|
|
{
|
|
Next();
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
private bool TryExpectModifier([NotNullWhen(true)] out ModifierToken? modifier)
|
|
{
|
|
if (Peek() is { Value: ModifierToken modifierToken })
|
|
{
|
|
modifier = modifierToken;
|
|
Next();
|
|
return true;
|
|
}
|
|
|
|
modifier = null;
|
|
return false;
|
|
}
|
|
|
|
private bool TryExpectIdentifier([NotNullWhen(true)] out string? identifier)
|
|
{
|
|
if (Peek() is { Value: IdentifierToken identifierToken })
|
|
{
|
|
identifier = identifierToken.Value;
|
|
Next();
|
|
return true;
|
|
}
|
|
|
|
identifier = null;
|
|
return false;
|
|
}
|
|
|
|
private IdentifierToken ExpectIdentifier()
|
|
{
|
|
var token = ExpectToken();
|
|
if (token is not IdentifierToken identifier)
|
|
{
|
|
throw new ParseException(Diagnostic
|
|
.Error($"Expected identifier, but found {token.GetType().Name}")
|
|
.WithHelp("Provide a valid identifier name here")
|
|
.At(token)
|
|
.Build());
|
|
}
|
|
|
|
return identifier;
|
|
}
|
|
|
|
private void RecoverToNextDefinition()
|
|
{
|
|
while (Peek().HasValue)
|
|
{
|
|
var token = Peek().Value;
|
|
if (token is SymbolToken { Symbol: Symbol.Func or Symbol.Struct })
|
|
{
|
|
break;
|
|
}
|
|
|
|
Next();
|
|
}
|
|
}
|
|
|
|
private void RecoverToNextStatement()
|
|
{
|
|
while (Peek().TryGetValue(out var token))
|
|
{
|
|
if (token is SymbolToken { Symbol: Symbol.CloseBrace } or IdentifierToken or SymbolToken
|
|
{
|
|
Symbol: Symbol.Return or Symbol.If or Symbol.While or Symbol.Let or Symbol.Break or Symbol.Continue
|
|
})
|
|
{
|
|
break;
|
|
}
|
|
|
|
Next();
|
|
}
|
|
}
|
|
|
|
private Optional<Token> Peek(int offset = 0)
|
|
{
|
|
var peekIndex = _index + offset;
|
|
while (peekIndex < _tokens.Count && _tokens[peekIndex] is DocumentationToken)
|
|
{
|
|
peekIndex++;
|
|
}
|
|
|
|
if (peekIndex < _tokens.Count)
|
|
{
|
|
return _tokens[peekIndex];
|
|
}
|
|
|
|
return Optional<Token>.Empty();
|
|
}
|
|
|
|
private void Next()
|
|
{
|
|
while (_index < _tokens.Count && _tokens[_index] is DocumentationToken)
|
|
{
|
|
_index++;
|
|
}
|
|
|
|
_index++;
|
|
}
|
|
|
|
private IReadOnlyList<Token> GetTokensForNode(int startIndex)
|
|
{
|
|
return _tokens[startIndex..Math.Min(_index, _tokens.Count - 1)];
|
|
}
|
|
}
|
|
|
|
public class ParseException : Exception
|
|
{
|
|
public Diagnostic Diagnostic { get; }
|
|
|
|
public ParseException(Diagnostic diagnostic) : base(diagnostic.Message)
|
|
{
|
|
Diagnostic = diagnostic;
|
|
}
|
|
} |