Клинап

This commit is contained in:
2025-03-29 19:31:10 +03:00
parent 361b4cd3f6
commit 5cc53d72fd
18 changed files with 359 additions and 390 deletions
+12 -14
View File
@@ -1,32 +1,30 @@
using System;
namespace mROA.CodegenTools
namespace mROA.Codegen.Tools
{
public class DefineTemplateSection : ITagged
{
public TemplateDocument Context { get; set; }
public string StoredText { get; }
private readonly string _tag;
public DefineTemplateSection(string storedText, string tag, TemplateDocument context)
{
StoredText = storedText;
_tag = tag;
Tag = tag;
Context = context;
}
public string Tag => _tag;
public string StoredText { get; }
public TemplateDocument Context { get; set; }
public string Tag { get; }
public int TargetLength => StoredText.Length;
public object Clone()
{
return new DefineTemplateSection(StoredText, Tag, Context);
}
public override string ToString()
{
return StoredText;
}
public object Clone()
{
return new DefineTemplateSection(StoredText, _tag, Context);
}
}
}
+2 -3
View File
@@ -1,6 +1,6 @@
using System;
namespace mROA.CodegenTools
namespace mROA.Codegen.Tools
{
public interface ITemplateSection : ICloneable
{
@@ -12,8 +12,7 @@ namespace mROA.CodegenTools
{
string Bake();
}
public interface ITagged : ITemplateSection
{
string Tag { get; }
+10 -11
View File
@@ -1,25 +1,24 @@
namespace mROA.CodegenTools
namespace mROA.Codegen.Tools
{
public class InnerTemplateSection : ITemplateSection, ITagged
{
private readonly string _tag;
public TemplateDocument Context { get; set; }
public int TargetLength => 0;
public string Tag => _tag;
public TemplateDocument InnerTemplate { get; }
public InnerTemplateSection(string tag, TemplateDocument innerTemplate, TemplateDocument context)
{
_tag = tag;
Tag = tag;
Context = context;
InnerTemplate = innerTemplate;
}
public TemplateDocument InnerTemplate { get; }
public string Tag { get; }
public TemplateDocument Context { get; set; }
public int TargetLength => 0;
public object Clone()
{
return new InnerTemplateSection(_tag, InnerTemplate, Context);
return new InnerTemplateSection(Tag, InnerTemplate, Context);
}
}
}
+54 -54
View File
@@ -1,70 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace mROA.CodegenTools
namespace mROA.Codegen.Tools
{
public class InsertTemplateSection : ITagged, IBaking
{
private object _insertedValue;
private string _tag;
public string Tag => _tag;
public TemplateDocument Context { get; set; }
public readonly List<string> Parameters;
private object _insertedValue;
public InsertTemplateSection(string tag, TemplateDocument context, params string[] parameters)
{
_tag = tag;
Tag = tag;
Parameters = parameters.ToList();
Context = context;
}
private bool IsInserted(object value)
{
if (Parameters.IndexOf("r") == -1) return false;
ProduceInserted(value);
return true;
}
private void ProduceInserted(object value)
{
var currentIndex = Context.Parts.IndexOf(this);
var clone = (InsertTemplateSection)MemberwiseClone();
clone._tag += "+";
clone._insertedValue = value;
Context.Parts.Insert(currentIndex, clone);
InsertSeparator(currentIndex + 1);
}
private void InsertSeparator(int currentIndex)
{
var sepIndex = Parameters.IndexOf("sep");
if (sepIndex == -1)
return;
Context.Parts.Insert(currentIndex, new LiteralTemplateSection(Context[Parameters[sepIndex + 1]].ToString(), Context));
}
public void Setup(object value)
{
if (!(value is string) && !(value is ITemplateSection) )
return;
if (IsInserted(value))
return;
_insertedValue = value;
}
public int TargetLength => 0;
public string Bake()
{
switch (_insertedValue)
@@ -78,9 +28,59 @@ namespace mROA.CodegenTools
}
}
public string Tag { get; private set; }
public TemplateDocument Context { get; set; }
public int TargetLength => 0;
public object Clone()
{
return new InsertTemplateSection(_tag, Context, Parameters.ToArray());
return new InsertTemplateSection(Tag, Context, Parameters.ToArray());
}
private bool IsInserted(object value)
{
if (Parameters.IndexOf("r") == -1) return false;
ProduceInserted(value);
return true;
}
private void ProduceInserted(object value)
{
var currentIndex = Context.Parts.IndexOf(this);
var clone = (InsertTemplateSection)MemberwiseClone();
clone.Tag += "+";
clone._insertedValue = value;
Context.Parts.Insert(currentIndex, clone);
InsertSeparator(currentIndex + 1);
}
private void InsertSeparator(int currentIndex)
{
var sepIndex = Parameters.IndexOf("sep");
if (sepIndex == -1)
return;
Context.Parts.Insert(currentIndex,
new LiteralTemplateSection(Context[Parameters[sepIndex + 1]].ToString(), Context));
}
public void Setup(object value)
{
if (!(value is string) && !(value is ITemplateSection))
return;
if (IsInserted(value))
return;
_insertedValue = value;
}
}
}
+13 -17
View File
@@ -1,43 +1,39 @@
using System.Linq;
namespace mROA.CodegenTools
namespace mROA.Codegen.Tools
{
public class LinkTemplateSection : ITemplateSection, IBaking
{
private readonly string _linkedTag;
public LinkTemplateSection(string linkedTag, TemplateDocument context)
{
_linkedTag = linkedTag;
LinkedTag = linkedTag;
Context = context;
}
public TemplateDocument Context { get; set; }
public string LinkedTag { get; }
public string LinkedTag => _linkedTag;
public string Bake()
{
return Context[LinkedTag]?.ToString();
}
public TemplateDocument Context { get; set; }
public int TargetLength
{
get
{
var attached = Context[_linkedTag];
var attached = Context[LinkedTag];
return attached?.TargetLength ?? 0;
}
}
public string Bake()
public object Clone()
{
return Context[_linkedTag]?.ToString();
return new LinkTemplateSection(LinkedTag, Context);
}
public override string ToString()
{
return $"{nameof(LinkedTag)}: {_linkedTag}";
}
public object Clone()
{
return new LinkTemplateSection(_linkedTag, Context);
return $"{nameof(LinkedTag)}: {LinkedTag}";
}
}
}
+10 -10
View File
@@ -1,8 +1,8 @@
namespace mROA.CodegenTools
namespace mROA.Codegen.Tools
{
public class LiteralTemplateSection : ITemplateSection, IBaking
{
private string _internalText;
private readonly string _internalText;
public LiteralTemplateSection(string internalText, TemplateDocument context)
{
@@ -10,22 +10,22 @@ namespace mROA.CodegenTools
Context = context;
}
public TemplateDocument Context { get; set; }
public int TargetLength => _internalText.Length;
public string Bake()
{
return ToString();
}
public TemplateDocument Context { get; set; }
public int TargetLength => _internalText.Length;
public object Clone()
{
return new LiteralTemplateSection(_internalText, Context);
}
public override string ToString()
{
return _internalText;
}
public object Clone()
{
return new LiteralTemplateSection(_internalText, Context);
}
}
}
@@ -1,13 +1,10 @@
using System;
namespace mROA.CodegenTools
namespace mROA.Codegen.Tools
{
public class ProgrammableTextSection : ITemplateSection, IBaking
{
public TemplateDocument Context { get; set; }
public int TargetLength => 0;
private Func<object, string> _bakeFunc;
private readonly Func<object, string> _bakeFunc;
public ProgrammableTextSection(Func<object, string> bakeFunc, TemplateDocument context)
{
@@ -21,6 +18,9 @@ namespace mROA.CodegenTools
return _bakeFunc(Context.AdditionalContext);
}
public TemplateDocument Context { get; set; }
public int TargetLength => 0;
public object Clone()
{
return new ProgrammableTextSection(_bakeFunc, Context);
@@ -1,6 +1,6 @@
using System;
namespace mROA.CodegenTools
namespace mROA.Codegen.Tools.Reading
{
public class DefineSectionReader : LeadingTextSectionReader
{
+1 -3
View File
@@ -1,6 +1,4 @@
using System.Collections.Generic;
namespace mROA.CodegenTools
namespace mROA.Codegen.Tools.Reading
{
public interface ISectionReader
{
@@ -1,10 +1,10 @@
using System;
namespace mROA.CodegenTools
namespace mROA.Codegen.Tools.Reading
{
public class InnerTemplateSectionReader : LeadingTextSectionReader
{
public InnerTemplateSectionReader()
public InnerTemplateSectionReader()
: base("<!T")
{
}
@@ -15,10 +15,10 @@ namespace mROA.CodegenTools
var end = FindEnd(index + 4);
var from = index;
index = PassCaretToCloseSymbol();
var to = index - 1;
var to = index - 1;
var tagName = GetTagValue(from, to);
var innerDocText = TemplateText.Substring(index, end - index);
var innerDoc = TemplateReader.Parse(innerDocText);
_currentCaretPosition = end + 4;
@@ -1,6 +1,6 @@
using System.Linq;
namespace mROA.CodegenTools
namespace mROA.Codegen.Tools.Reading
{
public class InsertSectionReader : LeadingTextSectionReader
{
@@ -12,12 +12,12 @@ namespace mROA.CodegenTools
public override ITemplateSection ExtractSection(ref int index, TemplateDocument document)
{
var from = index;
index = PassCaretToCloseSymbol();
var to = index - 1;
index = PassCaretToCloseSymbol();
var to = index - 1;
var tagText = GetTagValue(from, to);
var parts = tagText.Split(' ');
return new InsertTemplateSection(parts[0], document, parts.Skip(1).ToArray());
}
}
@@ -1,23 +1,17 @@
using System;
namespace mROA.CodegenTools
namespace mROA.Codegen.Tools.Reading
{
public abstract class LeadingTextSectionReader : ISectionReader
{
private readonly string _tagLeading;
protected int _currentCaretPosition;
private string _tagLeading;
protected LeadingTextSectionReader(string tagLeading)
{
_tagLeading = tagLeading + " ";
}
protected int PassCaretToCloseSymbol()
{
_currentCaretPosition = TemplateText.IndexOf(">", _currentCaretPosition, StringComparison.Ordinal) + 1;
return _currentCaretPosition;
}
public string TemplateText { get; set; }
public int GetNextSectionIndex(int currentIndex)
@@ -30,6 +24,12 @@ namespace mROA.CodegenTools
public abstract ITemplateSection ExtractSection(ref int index, TemplateDocument document);
protected int PassCaretToCloseSymbol()
{
_currentCaretPosition = TemplateText.IndexOf(">", _currentCaretPosition, StringComparison.Ordinal) + 1;
return _currentCaretPosition;
}
protected string GetTagValue(int from, int to)
{
var realStart = from + 4;
@@ -1,4 +1,4 @@
namespace mROA.CodegenTools
namespace mROA.Codegen.Tools.Reading
{
public class LinkSectionReader : LeadingTextSectionReader
{
@@ -11,7 +11,7 @@ namespace mROA.CodegenTools
{
var from = index;
index = PassCaretToCloseSymbol();
var to = index - 1;
var to = index - 1;
var tagText = GetTagValue(from, to);
return new LinkTemplateSection(tagText, document);
+11 -15
View File
@@ -4,11 +4,11 @@ using System.IO;
using System.Linq;
using System.Reflection;
namespace mROA.CodegenTools
namespace mROA.Codegen.Tools.Reading
{
public static class TemplateReader
{
private static readonly List<Type> ReaderTypes = new List<Type>
private static readonly List<Type> ReaderTypes = new()
{
typeof(DefineSectionReader),
typeof(InsertSectionReader),
@@ -18,8 +18,7 @@ namespace mROA.CodegenTools
public static TemplateDocument Parse(string templateText)
{
int currentIndex = 0;
var currentIndex = 0;
var activeReaders = new List<ISectionReader>();
foreach (var readerType in ReaderTypes)
@@ -28,26 +27,25 @@ namespace mROA.CodegenTools
reader.TemplateText = templateText;
activeReaders.Add(reader);
}
var doc = new TemplateDocument();
while (currentIndex < templateText.Length)
{
List<(ISectionReader reader, int index)> indices = activeReaders.Select(i => ((ISectionReader Readers, int index))(i, i.GetNextSectionIndex(currentIndex))).Where(i => i.index > -1).ToList();
int minIndex = templateText.Length;
List<(ISectionReader reader, int index)> indices = activeReaders
.Select(i => ((ISectionReader Readers, int index))(i, i.GetNextSectionIndex(currentIndex)))
.Where(i => i.index > -1).ToList();
var minIndex = templateText.Length;
ISectionReader minReader = null;
if (indices.Count != 0)
{
minIndex = indices.Min(i => i.index);
minReader = indices.FirstOrDefault(i => i.index == minIndex).reader;
if (indices.Count != activeReaders.Count)
{
activeReaders = indices.Select(i => i.reader).ToList();
}
if (indices.Count != activeReaders.Count) activeReaders = indices.Select(i => i.reader).ToList();
}
if (currentIndex != minIndex)
{
var literalText = templateText.Substring(currentIndex, minIndex - currentIndex);
@@ -62,8 +60,6 @@ namespace mROA.CodegenTools
var section = minReader.ExtractSection(ref currentIndex, doc);
doc.Parts.Add(section);
}
}
return doc;
+24 -24
View File
@@ -3,21 +3,35 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace mROA.CodegenTools
namespace mROA.Codegen.Tools
{
public class TemplateDocument : ICloneable
{
public List<ITemplateSection> Parts { get; set; } = new List<ITemplateSection>();
public List<ITemplateSection> Parts { get; set; } = new();
public object AdditionalContext { get; set; }
public ITagged? this[string tag] => Parts.OfType<ITagged>().FirstOrDefault(i => i.Tag == tag);
public object Clone()
{
var cloneDoc = new TemplateDocument
{
AdditionalContext = AdditionalContext is ICloneable c ? c.Clone() : AdditionalContext
};
cloneDoc.Parts = Parts.Select(i =>
{
var clone = i.Clone() as ITemplateSection;
clone.Context = cloneDoc;
return clone;
}).ToList();
return cloneDoc;
}
public void Insert(string tag, object value)
{
var selectedPart = this[tag];
if (selectedPart is InsertTemplateSection itp)
{
itp.Setup(value);
}
if (selectedPart is InsertTemplateSection itp) itp.Setup(value);
}
public string Compile()
@@ -34,28 +48,14 @@ namespace mROA.CodegenTools
return stringBuilder.ToString();
}
public object Clone()
{
var cloneDoc = new TemplateDocument
{
AdditionalContext = AdditionalContext is ICloneable c ? c.Clone() : AdditionalContext
};
cloneDoc.Parts = Parts.Select(i =>
{
var clone = i.Clone() as ITemplateSection;
clone.Context = cloneDoc;
return clone;
}).ToList();
return cloneDoc;
}
public void AddDefine(string tag, string text)
{
Parts.Add(new DefineTemplateSection(text, tag, this));
}
public void AddDefine(string tag, object value) => AddDefine(tag, value.ToString());
public void AddDefine(string tag, object value)
{
AddDefine(tag, value.ToString());
}
}
}