...
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
namespace Nub.Lang.Frontend.Parsing;
|
||||
|
||||
public class ExternFuncDefinitionNode(string name, List<FuncParameter> parameters, Optional<NubType> returnType) : DefinitionNode
|
||||
public class ExternFuncDefinitionNode(string name, List<FuncParameter> parameters, Optional<NubType> returnType, Optional<string> documentation) : DefinitionNode
|
||||
{
|
||||
public string Name { get; } = name;
|
||||
public List<FuncParameter> Parameters { get; } = parameters;
|
||||
public Optional<NubType> ReturnType { get; } = returnType;
|
||||
public Optional<string> Documentation { get; set; } = documentation;
|
||||
|
||||
public override string ToString() => $"{Name}({string.Join(", ", Parameters.Select(p => p.ToString()))}){(ReturnType.HasValue ? ": " + ReturnType.Value : "")}";
|
||||
}
|
||||
@@ -1,12 +1,13 @@
|
||||
namespace Nub.Lang.Frontend.Parsing;
|
||||
|
||||
public class LocalFuncDefinitionNode(string name, List<FuncParameter> parameters, BlockNode body, Optional<NubType> returnType, bool global) : DefinitionNode
|
||||
public class LocalFuncDefinitionNode(string name, List<FuncParameter> parameters, BlockNode body, Optional<NubType> returnType, bool global, Optional<string> documentation) : DefinitionNode
|
||||
{
|
||||
public string Name { get; } = name;
|
||||
public List<FuncParameter> Parameters { get; } = parameters;
|
||||
public BlockNode Body { get; } = body;
|
||||
public Optional<NubType> ReturnType { get; } = returnType;
|
||||
public bool Global { get; } = global;
|
||||
public Optional<string> Documentation { get; set; } = documentation;
|
||||
|
||||
public override string ToString() => $"{Name}({string.Join(", ", Parameters.Select(p => p.ToString()))}){(ReturnType.HasValue ? ": " + ReturnType.Value : "")}";
|
||||
}
|
||||
@@ -36,6 +36,15 @@ public class Parser
|
||||
{
|
||||
List<Modifier> modifiers = [];
|
||||
|
||||
List<string> documentationParts = [];
|
||||
while (_index < _tokens.Count && _tokens[_index] is CommentToken commentToken)
|
||||
{
|
||||
documentationParts.Add(commentToken.Comment);
|
||||
_index++;
|
||||
}
|
||||
|
||||
var documentation = documentationParts.Count == 0 ? null : string.Join('\n', documentationParts);
|
||||
|
||||
while (TryExpectModifier(out var modifier))
|
||||
{
|
||||
modifiers.Add(modifier);
|
||||
@@ -44,13 +53,13 @@ public class Parser
|
||||
var keyword = ExpectSymbol();
|
||||
return keyword.Symbol switch
|
||||
{
|
||||
Symbol.Func => ParseFuncDefinition(modifiers),
|
||||
Symbol.Struct => ParseStruct(modifiers),
|
||||
Symbol.Func => ParseFuncDefinition(modifiers, Optional.OfNullable(documentation)),
|
||||
Symbol.Struct => ParseStruct(modifiers, Optional.OfNullable(documentation)),
|
||||
_ => throw new Exception("Unexpected symbol: " + keyword.Symbol)
|
||||
};
|
||||
}
|
||||
|
||||
private DefinitionNode ParseFuncDefinition(List<Modifier> modifiers)
|
||||
private DefinitionNode ParseFuncDefinition(List<Modifier> modifiers, Optional<string> documentation)
|
||||
{
|
||||
var name = ExpectIdentifier();
|
||||
List<FuncParameter> parameters = [];
|
||||
@@ -77,7 +86,7 @@ public class Parser
|
||||
throw new Exception($"Modifiers: {string.Join(", ", modifiers)} is not valid for an extern function");
|
||||
}
|
||||
|
||||
return new ExternFuncDefinitionNode(name.Value, parameters, returnType);
|
||||
return new ExternFuncDefinitionNode(name.Value, parameters, returnType, documentation);
|
||||
}
|
||||
|
||||
var body = ParseBlock();
|
||||
@@ -88,10 +97,10 @@ public class Parser
|
||||
throw new Exception($"Modifiers: {string.Join(", ", modifiers)} is not valid for a local function");
|
||||
}
|
||||
|
||||
return new LocalFuncDefinitionNode(name.Value, parameters, body, returnType, global);
|
||||
return new LocalFuncDefinitionNode(name.Value, parameters, body, returnType, global, documentation);
|
||||
}
|
||||
|
||||
private StructDefinitionNode ParseStruct(List<Modifier> _)
|
||||
private StructDefinitionNode ParseStruct(List<Modifier> _, Optional<string> documentation)
|
||||
{
|
||||
var name = ExpectIdentifier().Value;
|
||||
|
||||
@@ -115,7 +124,7 @@ public class Parser
|
||||
variables.Add(new StructField(variableName, variableType, variableValue));
|
||||
}
|
||||
|
||||
return new StructDefinitionNode(name, variables);
|
||||
return new StructDefinitionNode(name, variables, documentation);
|
||||
}
|
||||
|
||||
private FuncParameter ParseFuncParameter()
|
||||
@@ -230,7 +239,7 @@ public class Parser
|
||||
private ExpressionNode ParseExpression(int precedence = 0)
|
||||
{
|
||||
var left = ParsePrimaryExpression();
|
||||
|
||||
|
||||
while (true)
|
||||
{
|
||||
var token = Peek();
|
||||
@@ -308,7 +317,7 @@ public class Parser
|
||||
private ExpressionNode ParsePrimaryExpression()
|
||||
{
|
||||
ExpressionNode expr;
|
||||
|
||||
|
||||
var token = ExpectToken();
|
||||
switch (token)
|
||||
{
|
||||
@@ -341,6 +350,7 @@ public class Parser
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
case SymbolToken symbolToken:
|
||||
@@ -403,6 +413,7 @@ public class Parser
|
||||
throw new Exception($"Unknown symbol: {symbolToken.Symbol}");
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
default:
|
||||
@@ -463,7 +474,7 @@ public class Parser
|
||||
{
|
||||
return NubType.Parse(name);
|
||||
}
|
||||
|
||||
|
||||
if (TryExpectSymbol(Symbol.Caret))
|
||||
{
|
||||
var baseType = ParseType();
|
||||
@@ -558,14 +569,15 @@ public class Parser
|
||||
|
||||
private Optional<Token> Peek()
|
||||
{
|
||||
while (_index < _tokens.Count && _tokens.ElementAt(_index) is SymbolToken { Symbol: Symbol.Whitespace })
|
||||
var peekIndex = _index;
|
||||
while (peekIndex < _tokens.Count && _tokens[peekIndex] is CommentToken)
|
||||
{
|
||||
Next();
|
||||
peekIndex++;
|
||||
}
|
||||
|
||||
if (_index < _tokens.Count)
|
||||
|
||||
if (peekIndex < _tokens.Count)
|
||||
{
|
||||
return _tokens.ElementAt(_index);
|
||||
return _tokens[peekIndex];
|
||||
}
|
||||
|
||||
return Optional<Token>.Empty();
|
||||
@@ -573,6 +585,11 @@ public class Parser
|
||||
|
||||
private void Next()
|
||||
{
|
||||
while (_index < _tokens.Count && _tokens[_index] is CommentToken)
|
||||
{
|
||||
_index++;
|
||||
}
|
||||
|
||||
_index++;
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,8 @@
|
||||
namespace Nub.Lang.Frontend.Parsing;
|
||||
|
||||
public class StructDefinitionNode(string name, List<StructField> fields) : DefinitionNode
|
||||
public class StructDefinitionNode(string name, List<StructField> fields, Optional<string> documentation) : DefinitionNode
|
||||
{
|
||||
public string Name { get; } = name;
|
||||
public List<StructField> Fields { get; } = fields;
|
||||
public Optional<string> Documentation { get; set; } = documentation;
|
||||
}
|
||||
Reference in New Issue
Block a user