Skip to content

Commit

Permalink
Update Parlot (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienros authored Jul 23, 2024
1 parent 9fd86dd commit a837fa8
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 47 deletions.
4 changes: 2 additions & 2 deletions Shortcodes.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30107.140
# Visual Studio Version 17
VisualStudioVersion = 17.11.35111.106
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Shortcodes.Tests", "tests\Shortcodes.Tests\Shortcodes.Tests.csproj", "{7ECF5D62-FDDF-4A6E-A6DD-8B4EDFB5CB4E}"
EndProject
Expand Down
17 changes: 9 additions & 8 deletions src/Shortcodes/RawText.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
namespace Shortcodes
using Parlot;
using System;

namespace Shortcodes
{
public class RawText : Node
{
public RawText(string buffer, int offset, int count)
private readonly TextSpan _textSpan;

public RawText(TextSpan textSpan)
{
Buffer = buffer;
Offset = offset;
Count = count;
_textSpan = textSpan;
}

public string Buffer { get; }
public int Offset { get; }
public int Count { get; }
public ReadOnlySpan<char> Span => _textSpan.Span;
}
}
4 changes: 2 additions & 2 deletions src/Shortcodes/Shortcodes.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="all" />
<PackageReference Include="Parlot" Version="0.0.24" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="all" />
<PackageReference Include="Parlot" Version="1.0.2" />
</ItemGroup>

</Project>
27 changes: 14 additions & 13 deletions src/Shortcodes/ShortcodesParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ private List<Node> ParseNodes()

private RawText ParseRawText()
{
if (_scanner.ReadRawText(out var _result))
if (_scanner.ReadRawText(out var result))
{
return new RawText(_scanner.Buffer, _result.Start, _result.Length);
return new RawText(result);
}

return null;
Expand Down Expand Up @@ -110,7 +110,7 @@ private Shortcode ParseShortcode()
return null;
}

var identifier = _result.GetText();
var identifier = _result.ToString();

_scanner.SkipWhiteSpace();

Expand All @@ -128,16 +128,17 @@ private Shortcode ParseShortcode()
{
arguments ??= CreateArgumentsDictionary();

arguments[argumentIndex.ToString()] = Character.DecodeString(new TextSpan(_scanner.Buffer, _result.Start + 1, _result.Length - 2)).ToString();
arguments[argumentIndex.ToString()] = Character.DecodeString(_result)[1..^1].ToString();

argumentIndex += 1;
}
else if (_scanner.ReadIdentifier(out _result))
{
_scanner.SkipWhiteSpace();

var argumentName = _result.GetText();

var argumentName = _result.ToString();
var valueStart = _scanner.Cursor.Offset;

// It might just be a value
if (_scanner.ReadChar('='))
{
Expand All @@ -147,13 +148,13 @@ private Shortcode ParseShortcode()
{
arguments ??= CreateArgumentsDictionary();

arguments[argumentName] = Character.DecodeString(new TextSpan(_scanner.Buffer, _result.Start + 1, _result.Length - 2)).ToString();
arguments[argumentName] = Character.DecodeString(_result)[1..^1].ToString();
}
else if (_scanner.ReadValue(out _result))
else if (_scanner.ReadValue(out var textSpan))
{
arguments ??= CreateArgumentsDictionary();

arguments[argumentName] = _result.GetText();
arguments[argumentName] = textSpan.ToString();
}
else
{
Expand All @@ -168,11 +169,11 @@ private Shortcode ParseShortcode()

_scanner.Cursor.ResetPosition(argumentStart);

if (_scanner.ReadValue(out _result))
if (_scanner.ReadValue(out var textSpan))
{
arguments ??= CreateArgumentsDictionary();

arguments[argumentIndex.ToString()] = _result.GetText();
arguments[argumentIndex.ToString()] = textSpan.ToString();

argumentIndex += 1;
}
Expand All @@ -184,11 +185,11 @@ private Shortcode ParseShortcode()
}
}
}
else if (_scanner.ReadValue(out _result))
else if (_scanner.ReadValue(out var textSpan))
{
arguments ??= CreateArgumentsDictionary();

arguments[argumentIndex.ToString()] = _result.GetText();
arguments[argumentIndex.ToString()] = textSpan.ToString();

argumentIndex += 1;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Shortcodes/ShortcodesProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ private async ValueTask<string> FoldClosingTagsAsync(string input, List<Node> no
{
var text = node as RawText;

sb.Builder.Append(text.Buffer, text.Offset, text.Count);
sb.Builder.Append(text.Span);
}

cursor += 1;
Expand Down Expand Up @@ -271,7 +271,7 @@ private async Task AppendAsync(StringBuilder builder, string source, Node start,
switch (start)
{
case RawText raw:
builder.Append(raw.Buffer, raw.Offset, raw.Count);
builder.Append(raw.Span);
return;

case Shortcode code:
Expand Down
17 changes: 9 additions & 8 deletions src/Shortcodes/ShortcodesScanner.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Parlot;
using System;

namespace Shortcodes
{
Expand All @@ -9,7 +10,7 @@ public ShortcodesScanner(string buffer) : base(buffer)

}

public bool ReadRawText(out TokenResult result)
public bool ReadRawText(out TextSpan result)
{
var start = Cursor.Offset;

Expand All @@ -27,26 +28,26 @@ public bool ReadRawText(out TokenResult result)

if (length == 0)
{
result = TokenResult.Fail();
result = null;
return false;
}

result = TokenResult.Succeed(Buffer, start, Cursor.Offset);
result = new TextSpan(Buffer, start, length);

return true;
}

public bool ReadValue(out TokenResult result)
public bool ReadValue(out TextSpan result)
{
if (Cursor.Match(']') || Cursor.Match('\'') || Cursor.Match('"') || Character.IsWhiteSpaceOrNewLine(Cursor.Current))
{
result = TokenResult.Fail();
result = null;
return false;
}

if (Cursor.Match("/]"))
{
result = TokenResult.Fail();
result = null;
return false;
}

Expand All @@ -56,14 +57,14 @@ public bool ReadValue(out TokenResult result)
{
if (Cursor.Eof)
{
result = TokenResult.Fail();
result = null;
return false;
}

Cursor.Advance();
}

result = TokenResult.Succeed(Buffer, start, Cursor.Offset);
result = new TextSpan(Buffer, start, Cursor.Offset - start);

return true;
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Shortcodes.Benchmarks/Shortcodes.Benchmarks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<IsPackable>false</IsPackable>

<DebugSymbols>true</DebugSymbols>
Expand All @@ -14,7 +14,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.12.1" />
<PackageReference Include="BenchmarkDotNet" Version="0.13.12" />
</ItemGroup>

</Project>
18 changes: 12 additions & 6 deletions tests/Shortcodes.Tests/Shortcodes.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net5.0</TargetFrameworks>
<TargetFrameworks>net8.0</TargetFrameworks>
<IsPackable>false</IsPackable>

<DebugSymbols>true</DebugSymbols>
<DebugType>pdbonly</DebugType>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
<PackageReference Include="coverlet.collector" Version="1.2.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="xunit" Version="2.9.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
Expand Down
9 changes: 5 additions & 4 deletions tests/Shortcodes.Tests/ShortcodesParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ private string EncodeNodes(IEnumerable<Node> nodes)
break;

case RawText raw:
_builder.Append($"R({raw.Count})");
_builder.Append($"R({raw.Span.Length})");
break;
}
}
Expand Down Expand Up @@ -118,7 +118,8 @@ public void ShouldScanArguments(string input, string encoded)

[Theory]
[InlineData("[hello a='b]", "R(12)")]
[InlineData("[hello '\\a']", "R(12)")]
// \z is not a valid escape sequence
[InlineData("[hello '\\z']", "R(12)")]
public void ShouldIgnoreMalformedArguments(string input, string encoded)
{
var nodes = new ShortcodesParser().Parse(input);
Expand All @@ -131,8 +132,8 @@ public void ShouldIgnoreMalformedArguments(string input, string encoded)
[InlineData("[h a='\\u03A9']", "[h a=Ω]")]
[InlineData("[h a='\\xe9']", "[h a=é]")]
[InlineData("[h a='\\xE9']", "[h a=é]")]
// This is not a valid string (invalid escape sequence), and not a valid value as it start with '
[InlineData("[h a='\\a']", "R(10)")]
// This is not a valid string (invalid escape sequence), and not a valid value as it starts with '
[InlineData("[h a='\\z']", "R(10)")]
[InlineData("[h a='\\\\']", "[h a=\\]")]
[InlineData("[h a='\\\"']", "[h a=\"]")]
[InlineData("[h a='\\\'']", "[h a=']")]
Expand Down

0 comments on commit a837fa8

Please sign in to comment.