Support for storing comments about specific variables

This commit is contained in:
Deukhoofd 2018-11-26 14:50:46 +01:00
parent a66b1abbf5
commit 963245c9e7
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
7 changed files with 46 additions and 8 deletions

View File

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Text;
using Upsilon.BaseTypes;
using Upsilon.BaseTypes.Number;
using Upsilon.Parser;
@ -423,12 +424,23 @@ namespace Upsilon.Binder
parameters.Add(vari);
innerScope.DefineLocalVariable(vari);
}
var commentData = new List<string>();
if (e.CommentData != null)
{
foreach (var comment in e.CommentData)
{
commentData.Add(comment);
}
}
if (!Scope.TryGetVariable(name, !isLocal, out var variable))
{
variable = new FunctionVariableSymbol(name, Type.Function, isLocal, parameters.ToImmutable());
((FunctionVariableSymbol) variable).IsBound = !(func is UnboundFunctionExpression);
var functionVariable = new FunctionVariableSymbol(name, Type.Function, isLocal, parameters.ToImmutable())
{
CommentValue = commentData.ToArray()
};
variable = functionVariable;
functionVariable.IsBound = !(func is UnboundFunctionExpression);
if (isLocal)
Scope.DefineLocalVariable(variable);
else
@ -451,6 +463,7 @@ namespace Upsilon.Binder
}
}
return new BoundFunctionAssignmentStatement(variable, func, e.Span);
}

View File

@ -11,6 +11,7 @@ namespace Upsilon.Binder
}
public VariableSymbol VariableSymbol { get; }
public override BoundKind Kind => BoundKind.BoundVariableSymbol;
public override BoundNode GetNodeAtPosition(int characterPosition)
{

View File

@ -15,6 +15,8 @@ namespace Upsilon.Binder
public Type Type { get; set; }
public bool Local { get; }
public string Name { get; }
public string[] CommentValue { get; set; }
}
public class FunctionVariableSymbol : VariableSymbol

View File

@ -1,4 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Text;
using Upsilon.Text;
@ -26,7 +26,7 @@ namespace Upsilon.Parser
private char Current => _position >= _text.Length ? '\0' : _text[_position];
private char Next => _position + 1 >= _text.Length ? '\0' : _text[_position + 1];
private readonly List<string> _activeComments = new List<string>();
private ImmutableArray<SyntaxToken> Lex()
{
var array = ImmutableArray.CreateBuilder<SyntaxToken>();
@ -35,8 +35,19 @@ namespace Upsilon.Parser
var next = LexNext();
if (next.Kind != SyntaxKind.WhiteSpace)
{
if (next.Kind == SyntaxKind.Comment)
switch (next.Kind)
{
case SyntaxKind.Comment:
_activeComments.Add(next.Value.ToString());
continue;
case SyntaxKind.FunctionKeyword:
case SyntaxKind.LocalKeyword:
case SyntaxKind.Identifier:
next.CommentData = _activeComments.ToArray();
break;
}
_activeComments.Clear();
array.Add(next);
if (next.Kind == SyntaxKind.EndOfFile)
break;

View File

@ -228,11 +228,17 @@ namespace Upsilon.Parser
private StatementSyntax ParseFunctionAssignmentStatement()
{
SyntaxToken localToken = null;
string[] commentData = null;
if (Current.Kind == SyntaxKind.LocalKeyword)
{
localToken = NextToken();
commentData = localToken.CommentData;
}
var functionToken = MatchToken(SyntaxKind.FunctionKeyword);
if (commentData == null)
{
commentData = functionToken.CommentData;
}
var identifier = MatchToken(SyntaxKind.Identifier);
var openParenthesis = MatchToken(SyntaxKind.OpenParenthesis);
var variableBuilder = ImmutableArray.CreateBuilder<IdentifierToken>();
@ -254,7 +260,10 @@ namespace Upsilon.Parser
var endToken = MatchToken(SyntaxKind.EndKeyword);
var functionExpression = new FunctionExpressionSyntax(functionToken, openParenthesis,
variableBuilder.ToImmutable(), closeParenthesis, (BlockStatementSyntax) block, endToken);
return new FunctionAssignmentStatementSyntax(localToken, (IdentifierToken) identifier, functionExpression);
return new FunctionAssignmentStatementSyntax(localToken, (IdentifierToken) identifier, functionExpression)
{
CommentData = commentData
};
}
private StatementSyntax ParseExpressionStatement()

View File

@ -18,6 +18,7 @@ namespace Upsilon.Parser
public SyntaxToken LocalToken { get; }
public IdentifierToken Identifier { get; }
public FunctionExpressionSyntax FunctionExpression { get; }
public string[] CommentData { get; set; }
public override SyntaxKind Kind => SyntaxKind.FunctionAssignmentStatement;
public override IEnumerable<SyntaxNode> ChildNodes()

View File

@ -14,6 +14,7 @@ namespace Upsilon.Parser
public override SyntaxKind Kind { get; }
public object Value { get; }
public string[] CommentData { get; set; }
public override IEnumerable<SyntaxNode> ChildNodes()
{