Базовые классы для codegen-tools
This commit is contained in:
@@ -0,0 +1,27 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace mROA.CodegenTools
|
||||||
|
{
|
||||||
|
public class DefineTemplatePart : ITemplatePart, ITagged
|
||||||
|
{
|
||||||
|
public TemplateDocument Context { get; set; }
|
||||||
|
public string StoredText { get; }
|
||||||
|
private string _tag;
|
||||||
|
|
||||||
|
public DefineTemplatePart(string storedText)
|
||||||
|
{
|
||||||
|
StoredText = storedText;
|
||||||
|
}
|
||||||
|
|
||||||
|
public string Tag => _tag;
|
||||||
|
public string Bake()
|
||||||
|
{
|
||||||
|
return String.Empty;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
return StoredText;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
namespace mROA.CodegenTools
|
||||||
|
{
|
||||||
|
public interface ITemplatePart
|
||||||
|
{
|
||||||
|
TemplateDocument Context { get; set; }
|
||||||
|
string Bake();
|
||||||
|
}
|
||||||
|
|
||||||
|
public interface ITagged : ITemplatePart
|
||||||
|
{
|
||||||
|
string Tag { get; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,67 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace mROA.CodegenTools
|
||||||
|
{
|
||||||
|
public class InsertTemplatePart : ITemplatePart, ITagged
|
||||||
|
{
|
||||||
|
private string _insertedText = null;
|
||||||
|
private ITemplatePart _insertedPart = null;
|
||||||
|
private string _tag;
|
||||||
|
public string Tag => _tag;
|
||||||
|
public TemplateDocument Context { get; set; }
|
||||||
|
public List<string> Parameters;
|
||||||
|
|
||||||
|
private bool IsInserted(object value)
|
||||||
|
{
|
||||||
|
if (!(value is string) && !(value is ITemplatePart) )
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (Parameters.IndexOf("R") == 0) return false;
|
||||||
|
|
||||||
|
ProduceInserted(value);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ProduceInserted(object value)
|
||||||
|
{
|
||||||
|
var currentIndex = Context.Parts.IndexOf(this);
|
||||||
|
var clone = (InsertTemplatePart)MemberwiseClone();
|
||||||
|
clone._tag += "+";
|
||||||
|
|
||||||
|
if (value is string s)
|
||||||
|
{
|
||||||
|
clone.Setup(s);
|
||||||
|
}else
|
||||||
|
clone.Setup((ITemplatePart)value);
|
||||||
|
|
||||||
|
Context.Parts.Insert(currentIndex, clone);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void Setup(string inside)
|
||||||
|
{
|
||||||
|
if (IsInserted(inside))
|
||||||
|
return;
|
||||||
|
_insertedText = inside;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Setup(ITemplatePart part)
|
||||||
|
{
|
||||||
|
if (IsInserted(part))
|
||||||
|
return;
|
||||||
|
_insertedPart = part;
|
||||||
|
}
|
||||||
|
|
||||||
|
public string Bake()
|
||||||
|
{
|
||||||
|
if (_insertedText != null)
|
||||||
|
{
|
||||||
|
return _insertedText;
|
||||||
|
}
|
||||||
|
|
||||||
|
return _insertedPart.Bake();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
using System.Linq;
|
||||||
|
|
||||||
|
namespace mROA.CodegenTools
|
||||||
|
{
|
||||||
|
public class LinkTemplatePart : ITemplatePart
|
||||||
|
{
|
||||||
|
private readonly string _linkedTag;
|
||||||
|
public TemplateDocument Context { get; set; }
|
||||||
|
|
||||||
|
public string LinkedTag => _linkedTag;
|
||||||
|
|
||||||
|
public string Bake()
|
||||||
|
{
|
||||||
|
return Context.Parts.OfType<ITagged>().FirstOrDefault(i => i.Tag == _linkedTag)?.Bake();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
namespace mROA.CodegenTools
|
||||||
|
{
|
||||||
|
public class LiteralTemplatePart : ITemplatePart
|
||||||
|
{
|
||||||
|
private string _internalText;
|
||||||
|
public TemplateDocument Context { get; set; }
|
||||||
|
public string Bake()
|
||||||
|
{
|
||||||
|
return _internalText;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
return _internalText;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace mROA.CodegenTools
|
||||||
|
{
|
||||||
|
public class TemplateDocument
|
||||||
|
{
|
||||||
|
public List<ITemplatePart> Parts { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user