Support for storing comments about specific variables
This commit is contained in:
parent
a66b1abbf5
commit
963245c9e7
|
@ -1,6 +1,7 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Collections.Immutable;
|
using System.Collections.Immutable;
|
||||||
|
using System.Text;
|
||||||
using Upsilon.BaseTypes;
|
using Upsilon.BaseTypes;
|
||||||
using Upsilon.BaseTypes.Number;
|
using Upsilon.BaseTypes.Number;
|
||||||
using Upsilon.Parser;
|
using Upsilon.Parser;
|
||||||
|
@ -423,12 +424,23 @@ namespace Upsilon.Binder
|
||||||
parameters.Add(vari);
|
parameters.Add(vari);
|
||||||
innerScope.DefineLocalVariable(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))
|
if (!Scope.TryGetVariable(name, !isLocal, out var variable))
|
||||||
{
|
{
|
||||||
variable = new FunctionVariableSymbol(name, Type.Function, isLocal, parameters.ToImmutable());
|
var functionVariable = new FunctionVariableSymbol(name, Type.Function, isLocal, parameters.ToImmutable())
|
||||||
((FunctionVariableSymbol) variable).IsBound = !(func is UnboundFunctionExpression);
|
{
|
||||||
|
CommentValue = commentData.ToArray()
|
||||||
|
};
|
||||||
|
variable = functionVariable;
|
||||||
|
functionVariable.IsBound = !(func is UnboundFunctionExpression);
|
||||||
if (isLocal)
|
if (isLocal)
|
||||||
Scope.DefineLocalVariable(variable);
|
Scope.DefineLocalVariable(variable);
|
||||||
else
|
else
|
||||||
|
@ -451,6 +463,7 @@ namespace Upsilon.Binder
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
return new BoundFunctionAssignmentStatement(variable, func, e.Span);
|
return new BoundFunctionAssignmentStatement(variable, func, e.Span);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -11,6 +11,7 @@ namespace Upsilon.Binder
|
||||||
}
|
}
|
||||||
|
|
||||||
public VariableSymbol VariableSymbol { get; }
|
public VariableSymbol VariableSymbol { get; }
|
||||||
|
|
||||||
public override BoundKind Kind => BoundKind.BoundVariableSymbol;
|
public override BoundKind Kind => BoundKind.BoundVariableSymbol;
|
||||||
public override BoundNode GetNodeAtPosition(int characterPosition)
|
public override BoundNode GetNodeAtPosition(int characterPosition)
|
||||||
{
|
{
|
||||||
|
|
|
@ -15,6 +15,8 @@ namespace Upsilon.Binder
|
||||||
public Type Type { get; set; }
|
public Type Type { get; set; }
|
||||||
public bool Local { get; }
|
public bool Local { get; }
|
||||||
public string Name { get; }
|
public string Name { get; }
|
||||||
|
public string[] CommentValue { get; set; }
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public class FunctionVariableSymbol : VariableSymbol
|
public class FunctionVariableSymbol : VariableSymbol
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
using System;
|
using System.Collections.Generic;
|
||||||
using System.Collections.Immutable;
|
using System.Collections.Immutable;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using Upsilon.Text;
|
using Upsilon.Text;
|
||||||
|
@ -26,7 +26,7 @@ namespace Upsilon.Parser
|
||||||
private char Current => _position >= _text.Length ? '\0' : _text[_position];
|
private char Current => _position >= _text.Length ? '\0' : _text[_position];
|
||||||
private char Next => _position + 1 >= _text.Length ? '\0' : _text[_position + 1];
|
private char Next => _position + 1 >= _text.Length ? '\0' : _text[_position + 1];
|
||||||
|
|
||||||
|
private readonly List<string> _activeComments = new List<string>();
|
||||||
private ImmutableArray<SyntaxToken> Lex()
|
private ImmutableArray<SyntaxToken> Lex()
|
||||||
{
|
{
|
||||||
var array = ImmutableArray.CreateBuilder<SyntaxToken>();
|
var array = ImmutableArray.CreateBuilder<SyntaxToken>();
|
||||||
|
@ -35,8 +35,19 @@ namespace Upsilon.Parser
|
||||||
var next = LexNext();
|
var next = LexNext();
|
||||||
if (next.Kind != SyntaxKind.WhiteSpace)
|
if (next.Kind != SyntaxKind.WhiteSpace)
|
||||||
{
|
{
|
||||||
if (next.Kind == SyntaxKind.Comment)
|
switch (next.Kind)
|
||||||
continue;
|
{
|
||||||
|
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);
|
array.Add(next);
|
||||||
if (next.Kind == SyntaxKind.EndOfFile)
|
if (next.Kind == SyntaxKind.EndOfFile)
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -228,11 +228,17 @@ namespace Upsilon.Parser
|
||||||
private StatementSyntax ParseFunctionAssignmentStatement()
|
private StatementSyntax ParseFunctionAssignmentStatement()
|
||||||
{
|
{
|
||||||
SyntaxToken localToken = null;
|
SyntaxToken localToken = null;
|
||||||
|
string[] commentData = null;
|
||||||
if (Current.Kind == SyntaxKind.LocalKeyword)
|
if (Current.Kind == SyntaxKind.LocalKeyword)
|
||||||
{
|
{
|
||||||
localToken = NextToken();
|
localToken = NextToken();
|
||||||
|
commentData = localToken.CommentData;
|
||||||
}
|
}
|
||||||
var functionToken = MatchToken(SyntaxKind.FunctionKeyword);
|
var functionToken = MatchToken(SyntaxKind.FunctionKeyword);
|
||||||
|
if (commentData == null)
|
||||||
|
{
|
||||||
|
commentData = functionToken.CommentData;
|
||||||
|
}
|
||||||
var identifier = MatchToken(SyntaxKind.Identifier);
|
var identifier = MatchToken(SyntaxKind.Identifier);
|
||||||
var openParenthesis = MatchToken(SyntaxKind.OpenParenthesis);
|
var openParenthesis = MatchToken(SyntaxKind.OpenParenthesis);
|
||||||
var variableBuilder = ImmutableArray.CreateBuilder<IdentifierToken>();
|
var variableBuilder = ImmutableArray.CreateBuilder<IdentifierToken>();
|
||||||
|
@ -254,7 +260,10 @@ namespace Upsilon.Parser
|
||||||
var endToken = MatchToken(SyntaxKind.EndKeyword);
|
var endToken = MatchToken(SyntaxKind.EndKeyword);
|
||||||
var functionExpression = new FunctionExpressionSyntax(functionToken, openParenthesis,
|
var functionExpression = new FunctionExpressionSyntax(functionToken, openParenthesis,
|
||||||
variableBuilder.ToImmutable(), closeParenthesis, (BlockStatementSyntax) block, endToken);
|
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()
|
private StatementSyntax ParseExpressionStatement()
|
||||||
|
|
|
@ -18,6 +18,7 @@ namespace Upsilon.Parser
|
||||||
public SyntaxToken LocalToken { get; }
|
public SyntaxToken LocalToken { get; }
|
||||||
public IdentifierToken Identifier { get; }
|
public IdentifierToken Identifier { get; }
|
||||||
public FunctionExpressionSyntax FunctionExpression { get; }
|
public FunctionExpressionSyntax FunctionExpression { get; }
|
||||||
|
public string[] CommentData { get; set; }
|
||||||
|
|
||||||
public override SyntaxKind Kind => SyntaxKind.FunctionAssignmentStatement;
|
public override SyntaxKind Kind => SyntaxKind.FunctionAssignmentStatement;
|
||||||
public override IEnumerable<SyntaxNode> ChildNodes()
|
public override IEnumerable<SyntaxNode> ChildNodes()
|
||||||
|
|
|
@ -14,6 +14,7 @@ namespace Upsilon.Parser
|
||||||
|
|
||||||
public override SyntaxKind Kind { get; }
|
public override SyntaxKind Kind { get; }
|
||||||
public object Value { get; }
|
public object Value { get; }
|
||||||
|
public string[] CommentData { get; set; }
|
||||||
|
|
||||||
public override IEnumerable<SyntaxNode> ChildNodes()
|
public override IEnumerable<SyntaxNode> ChildNodes()
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue