feat: parse content text (base case).

This commit is contained in:
mattia
2025-02-16 18:26:28 +01:00
parent b7aae9a04f
commit d386c50499
32 changed files with 940 additions and 6 deletions

View File

@@ -0,0 +1,5 @@
namespace InkBlot.Visitor;
public partial class Listener : InkBlotAntlrGrammarBaseListener
{
}

View File

@@ -0,0 +1,34 @@
using System.Text.RegularExpressions;
using Antlr4.Runtime.Tree;
namespace InkBlot.Visitor;
public partial class Listener
{
private readonly ParseTreeProperty<string> _contentTextValue = new();
private readonly Regex _escapeRegex = MyRegex();
[GeneratedRegex(@"\\(.)")]
private static partial Regex MyRegex();
private string GetContentText(InkBlotAntlrGrammarParser.ContentTextContext context)
{
return _contentTextValue.Get(context);
}
public override void ExitContentText(InkBlotAntlrGrammarParser.ContentTextContext context)
{
// escape sequences are captured by this node, but not interpreted
var contentWithEscapes = context.CONTENT_TEXT_NO_ESCAPE_SIMPLE().ToString();
if (contentWithEscapes == null)
// when does this happen? :?
throw new InvalidOperationException();
// replace \X with just X
var content = _escapeRegex.Replace(contentWithEscapes, match => match.Groups[1].Value);
// save the result
_contentTextValue.Put(context, content);
}
}

View File

@@ -0,0 +1,20 @@
using InkBlot.ParseHierarchy;
namespace InkBlot.Visitor;
public partial class Listener
{
private Story? _story;
public Story Story => _story ?? throw new InvalidOperationException("No story found yet.");
public override void ExitStory(InkBlotAntlrGrammarParser.StoryContext context)
{
var storyNodes = context.children.Select(child => child switch
{
InkBlotAntlrGrammarParser.ContentTextContext contentText => new Content(GetContentText(contentText)),
_ => throw new InvalidOperationException($"unknown context of type {child.GetType()}")
});
_story = new Story(storyNodes);
}
}