feat: parse content text (base case).
This commit is contained in:
5
InkBlot/Visitor/Listener.cs
Normal file
5
InkBlot/Visitor/Listener.cs
Normal file
@@ -0,0 +1,5 @@
|
||||
namespace InkBlot.Visitor;
|
||||
|
||||
public partial class Listener : InkBlotAntlrGrammarBaseListener
|
||||
{
|
||||
}
|
||||
34
InkBlot/Visitor/ListenerContentText.cs
Normal file
34
InkBlot/Visitor/ListenerContentText.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
20
InkBlot/Visitor/ListenerStory.cs
Normal file
20
InkBlot/Visitor/ListenerStory.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user