feat: temporary grammar

This commit is contained in:
Mattia Belletti
2025-03-01 12:30:36 +01:00
parent aa3baa780d
commit eed28168ad
31 changed files with 1409 additions and 479 deletions

View File

@@ -17,11 +17,12 @@ public class ContentTextTest
public void TestBaseContentSuccess(string inkInput, string result, int numErrors)
{
// parse the story
var fileReader = new PreMadeFileReader([
var fileReader = new InMemoryFileReader([
("main.ink", inkInput)
]);
var parser = new InkBlotParser();
var (story, diagnostics) = parser.Parse(fileReader, "main.ink");
using var loggerFactory = Helpers.GetLoggerFactory();
var (story, diagnostics) = InkBlotParser.Parse(fileReader, loggerFactory, "main.ink");
// check the diagnostic counts match
diagnostics.Count().ShouldBe(numErrors);

View File

@@ -1,4 +1,5 @@
using System.Text;
using Microsoft.Extensions.Logging;
namespace InkBlot.Tests;
@@ -6,10 +7,19 @@ namespace InkBlot.Tests;
/// A file reader where the contents are directly provided as strings.
/// </summary>
/// <param name="filesToContents">A map between file names and their contents.</param>
internal class PreMadeFileReader((string, string)[] filesToContents) : IFileReader
internal class InMemoryFileReader((string, string)[] filesToContents) : IFileReader
{
public Stream GetContents(string filename)
{
return new MemoryStream(Encoding.UTF8.GetBytes(filesToContents.First(e => e.Item1 == filename).Item2));
}
}
internal static class Helpers
{
public static ILoggerFactory GetLoggerFactory()
{
var loggerFactory = LoggerFactory.Create(builder => builder.AddConsole());
return loggerFactory;
}
}

View File

@@ -9,6 +9,7 @@
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.2"/>
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="9.0.2"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0"/>
<PackageReference Include="Shouldly" Version="4.3.0"/>
<PackageReference Include="xunit" Version="2.9.2"/>

View File

@@ -0,0 +1,34 @@
using Antlr4.Runtime;
using Shouldly;
namespace InkBlot.Tests;
public class LexerTest
{
[Fact]
public void TestDivertForLexer()
{
var divertTokens = GetTokens("<- threadName");
divertTokens.ShouldSatisfyAllConditions(
() => divertTokens[0].Text.ShouldBe("<-"),
() => divertTokens[0].Type.ShouldBe(InkBlotAntlrGrammarLexer.THREAD_ARROW),
() => divertTokens[1].Text.ShouldBe(" "),
() => divertTokens[1].Type.ShouldBe(InkBlotAntlrGrammarLexer.WS),
() => divertTokens[2].Text.ShouldBe("threadName"),
() => divertTokens[2].Type.ShouldBe(InkBlotAntlrGrammarLexer.IDENTIFIER),
() => divertTokens[3].Text.ShouldBe("<EOF>"),
() => divertTokens[3].Type.ShouldBe(InkBlotAntlrGrammarLexer.Eof)
);
}
private IList<IToken> GetTokens(string text)
{
var fileReader = new InMemoryFileReader([
("main.ink", text)
]);
var tokensStream = InkBlotParser.GetTokenStream(fileReader, "main.ink");
tokensStream.Fill();
var tokens = tokensStream.GetTokens();
return tokens;
}
}

View File

@@ -0,0 +1,24 @@
using InkBlot.ParseHierarchy;
using Shouldly;
namespace InkBlot.Tests;
public class MultiDivertTest
{
[Fact]
public void TestThreadDivert()
{
var fileReader = new InMemoryFileReader([
("main.ink", "<- threadName")
]);
var tokensStream = InkBlotParser.GetTokenStream(fileReader, "main.ink");
tokensStream.Fill();
var tokens = tokensStream.GetTokens();
using var loggerFactory = Helpers.GetLoggerFactory();
var (story, diagnostics) = InkBlotParser.Parse(fileReader, loggerFactory, "main.ink");
diagnostics.ShouldBe([]);
var storyNodes = story.StoryNodes.ToArray();
storyNodes.ShouldBe([new MultiDivert(new ThreadDivert(new Identifier(["threadName"])))]);
}
}