Implement FullSpan on tokens & nodes

This commit is contained in:
Alexander Luzgarev 2020-07-23 16:12:30 +02:00
parent 6f6bbfaa1c
commit 3584030768
4 changed files with 39 additions and 7 deletions

View File

@ -1,4 +1,5 @@
using Xunit;
using FluentAssertions;
namespace Parser.Tests
{
@ -95,7 +96,7 @@ namespace Parser.Tests
[Fact]
public void ProvidePosition()
{
var text = "2 + 3";
var text = "% Comment\n 2 + 3";
var sut = GetSut(text);
var actual = sut.Parse();
var statement = actual.Root.Body.Statements[0].AsNode() as ExpressionStatementSyntaxNode;
@ -104,8 +105,29 @@ namespace Parser.Tests
var operation = expression.Operation;
var rhs = expression.Rhs;
Assert.Equal(0, lhs.Position);
Assert.Equal(2, operation.Position);
Assert.Equal(4, rhs.Position);
Assert.Equal(14, operation.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);
}
}
}

View File

@ -6,6 +6,7 @@
<LangVersion>8.0</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.10.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.0.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">

View File

@ -1,4 +1,5 @@
using System.Collections.Generic;
using Parser.Internal;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
@ -13,6 +14,7 @@ namespace Parser
_parent = parent;
_green = green;
Position = position;
FullSpan = new TextSpan(Position, green.FullWidth);
}
private protected SyntaxNode(Internal.GreenNode green, int position)
@ -20,6 +22,7 @@ namespace Parser
_parent = this;
_green = green;
Position = position;
FullSpan = new TextSpan(Position, green.FullWidth);
}
public TokenKind Kind => _green.Kind;
@ -35,6 +38,10 @@ namespace Parser
public int Position { get; }
public TextSpan FullSpan { get; }
public int FullWidth => _green.FullWidth;
internal int GetChildPosition(int slot)
{
var result = Position;
@ -75,8 +82,6 @@ namespace Parser
public virtual string FullText => _green.FullText;
public int FullWidth => _green.FullWidth;
public virtual IReadOnlyList<SyntaxTrivia> LeadingTrivia
{
get

View File

@ -1,4 +1,5 @@
using System;
using Parser.Internal;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
@ -22,6 +23,7 @@ namespace Parser
_parent = parent;
_token = token ?? throw new ArgumentNullException(nameof(token));
Position = position;
FullSpan = new TextSpan(Position, token.FullWidth);
}
public SyntaxNode Parent => _parent;
@ -29,6 +31,8 @@ namespace Parser
public int Position { get; }
public TextSpan FullSpan { get; }
public object? Value => _token.GetValue();
public bool Equals(SyntaxToken other)