Клинап

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
@@ -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;