Implement FullSpan on tokens & nodes
This commit is contained in:
parent
6f6bbfaa1c
commit
3584030768
@ -1,4 +1,5 @@
|
|||||||
using Xunit;
|
using Xunit;
|
||||||
|
using FluentAssertions;
|
||||||
|
|
||||||
namespace Parser.Tests
|
namespace Parser.Tests
|
||||||
{
|
{
|
||||||
@ -95,7 +96,7 @@ namespace Parser.Tests
|
|||||||
[Fact]
|
[Fact]
|
||||||
public void ProvidePosition()
|
public void ProvidePosition()
|
||||||
{
|
{
|
||||||
var text = "2 + 3";
|
var text = "% Comment\n 2 + 3";
|
||||||
var sut = GetSut(text);
|
var sut = GetSut(text);
|
||||||
var actual = sut.Parse();
|
var actual = sut.Parse();
|
||||||
var statement = actual.Root.Body.Statements[0].AsNode() as ExpressionStatementSyntaxNode;
|
var statement = actual.Root.Body.Statements[0].AsNode() as ExpressionStatementSyntaxNode;
|
||||||
@ -104,8 +105,29 @@ namespace Parser.Tests
|
|||||||
var operation = expression.Operation;
|
var operation = expression.Operation;
|
||||||
var rhs = expression.Rhs;
|
var rhs = expression.Rhs;
|
||||||
Assert.Equal(0, lhs.Position);
|
Assert.Equal(0, lhs.Position);
|
||||||
Assert.Equal(2, operation.Position);
|
Assert.Equal(14, operation.Position);
|
||||||
Assert.Equal(4, rhs.Position);
|
Assert.Equal(16, rhs.Position);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void ProvideFullSpan()
|
||||||
|
{
|
||||||
|
var text = "% Comment\n 2 + 3";
|
||||||
|
var sut = GetSut(text);
|
||||||
|
var actual = sut.Parse();
|
||||||
|
var statement = actual.Root.Body.Statements[0].AsNode() as ExpressionStatementSyntaxNode;
|
||||||
|
var expression = statement!.Expression as BinaryOperationExpressionSyntaxNode;
|
||||||
|
var lhs = expression!.Lhs;
|
||||||
|
var operation = expression.Operation;
|
||||||
|
var rhs = expression.Rhs;
|
||||||
|
expression.FullSpan.Start.Should().Be(0);
|
||||||
|
expression.FullSpan.End.Should().Be(17);
|
||||||
|
lhs.FullSpan.Start.Should().Be(0);
|
||||||
|
lhs.FullSpan.End.Should().Be(14);
|
||||||
|
operation.FullSpan.Start.Should().Be(14);
|
||||||
|
operation.FullSpan.End.Should().Be(16);
|
||||||
|
rhs.FullSpan.Start.Should().Be(16);
|
||||||
|
rhs.FullSpan.End.Should().Be(17);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -6,6 +6,7 @@
|
|||||||
<LangVersion>8.0</LangVersion>
|
<LangVersion>8.0</LangVersion>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="FluentAssertions" Version="5.10.3" />
|
||||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.0.1" />
|
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.0.1" />
|
||||||
<PackageReference Include="xunit" Version="2.4.1" />
|
<PackageReference Include="xunit" Version="2.4.1" />
|
||||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
|
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
using System.Collections.Generic;
|
using Parser.Internal;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Collections.Immutable;
|
using System.Collections.Immutable;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
|
||||||
@ -13,6 +14,7 @@ namespace Parser
|
|||||||
_parent = parent;
|
_parent = parent;
|
||||||
_green = green;
|
_green = green;
|
||||||
Position = position;
|
Position = position;
|
||||||
|
FullSpan = new TextSpan(Position, green.FullWidth);
|
||||||
}
|
}
|
||||||
|
|
||||||
private protected SyntaxNode(Internal.GreenNode green, int position)
|
private protected SyntaxNode(Internal.GreenNode green, int position)
|
||||||
@ -20,6 +22,7 @@ namespace Parser
|
|||||||
_parent = this;
|
_parent = this;
|
||||||
_green = green;
|
_green = green;
|
||||||
Position = position;
|
Position = position;
|
||||||
|
FullSpan = new TextSpan(Position, green.FullWidth);
|
||||||
}
|
}
|
||||||
|
|
||||||
public TokenKind Kind => _green.Kind;
|
public TokenKind Kind => _green.Kind;
|
||||||
@ -35,6 +38,10 @@ namespace Parser
|
|||||||
|
|
||||||
public int Position { get; }
|
public int Position { get; }
|
||||||
|
|
||||||
|
public TextSpan FullSpan { get; }
|
||||||
|
|
||||||
|
public int FullWidth => _green.FullWidth;
|
||||||
|
|
||||||
internal int GetChildPosition(int slot)
|
internal int GetChildPosition(int slot)
|
||||||
{
|
{
|
||||||
var result = Position;
|
var result = Position;
|
||||||
@ -75,8 +82,6 @@ namespace Parser
|
|||||||
|
|
||||||
public virtual string FullText => _green.FullText;
|
public virtual string FullText => _green.FullText;
|
||||||
|
|
||||||
public int FullWidth => _green.FullWidth;
|
|
||||||
|
|
||||||
public virtual IReadOnlyList<SyntaxTrivia> LeadingTrivia
|
public virtual IReadOnlyList<SyntaxTrivia> LeadingTrivia
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
using System;
|
using Parser.Internal;
|
||||||
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Collections.Immutable;
|
using System.Collections.Immutable;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
@ -22,6 +23,7 @@ namespace Parser
|
|||||||
_parent = parent;
|
_parent = parent;
|
||||||
_token = token ?? throw new ArgumentNullException(nameof(token));
|
_token = token ?? throw new ArgumentNullException(nameof(token));
|
||||||
Position = position;
|
Position = position;
|
||||||
|
FullSpan = new TextSpan(Position, token.FullWidth);
|
||||||
}
|
}
|
||||||
|
|
||||||
public SyntaxNode Parent => _parent;
|
public SyntaxNode Parent => _parent;
|
||||||
@ -29,6 +31,8 @@ namespace Parser
|
|||||||
|
|
||||||
public int Position { get; }
|
public int Position { get; }
|
||||||
|
|
||||||
|
public TextSpan FullSpan { get; }
|
||||||
|
|
||||||
public object? Value => _token.GetValue();
|
public object? Value => _token.GetValue();
|
||||||
|
|
||||||
public bool Equals(SyntaxToken other)
|
public bool Equals(SyntaxToken other)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user