Skip to content

Commit

Permalink
Added IBlockDocumentItem and BlockDocumentItemBase for Block documents
Browse files Browse the repository at this point in the history
Added Properties for ITokenOptions to allow custom name/value pairs to be stored from Tags & Blocks(Opening and Closing)
#16 Fixed
Fixed Logging formatter NREX
  • Loading branch information
Jean-Pierre Bachmann authored and Jean-Pierre Bachmann committed Jan 3, 2021
1 parent c1f9495 commit c792738
Show file tree
Hide file tree
Showing 63 changed files with 1,230 additions and 605 deletions.
9 changes: 6 additions & 3 deletions Morestachio.LessCompiler/CompileLessDocumentItem.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using dotless.Core;
Expand All @@ -11,6 +12,7 @@
using Morestachio.Framework;
using Morestachio.Framework.Context;
using Morestachio.Framework.IO;
using Morestachio.Framework.Tokenizing;
#if ValueTask
using ItemExecutionPromise = System.Threading.Tasks.ValueTask<System.Collections.Generic.IEnumerable<Morestachio.Document.Contracts.DocumentItemExecution>>;
using Promise = System.Threading.Tasks.ValueTask;
Expand All @@ -25,14 +27,15 @@ namespace Morestachio.LessCompiler
/// Wraps the dotless into an Document provider
/// </summary>
[Serializable]
public class CompileLessDocumentItem : DocumentItemBase, ToParsableStringDocumentVisitor.IStringVisitor
public class CompileLessDocumentItem : BlockDocumentItemBase, ToParsableStringDocumentVisitor.IStringVisitor
{
internal CompileLessDocumentItem() : base(CharacterLocation.Unknown)
internal CompileLessDocumentItem() : base(CharacterLocation.Unknown, null)
{

}

public CompileLessDocumentItem(CharacterLocation location) : base(location)
public CompileLessDocumentItem(CharacterLocation location, IEnumerable<ITokenOption> tagTokenOptions)
: base(location, tagTokenOptions)
{

}
Expand Down
7 changes: 4 additions & 3 deletions Morestachio.LessCompiler/LessCompilerDocumentItemProvider.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using JetBrains.Annotations;
using System.Collections.Generic;
using JetBrains.Annotations;
using Morestachio.Document.Contracts;
using Morestachio.Document.Custom;
using Morestachio.Framework.Tokenizing;
Expand All @@ -15,9 +16,9 @@ public LessCompilerDocumentItemProvider() : base("#Less", "/Less")
}

/// <inheritdoc />
public override IDocumentItem CreateDocumentItem(string tag, string value, TokenPair token, ParserOptions options)
public override IDocumentItem CreateDocumentItem(string tag, string value, TokenPair token, ParserOptions options, IEnumerable<ITokenOption> tagTokenOptions)
{
return new CompileLessDocumentItem(token.TokenLocation);
return new CompileLessDocumentItem(token.TokenLocation, tagTokenOptions);
}
}
}
55 changes: 32 additions & 23 deletions Morestachio.Tests/ParserFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using Morestachio.Document.Visitor;
using Morestachio.Formatter.Framework.Attributes;
using Morestachio.Framework;
using Morestachio.Framework.Context.Options;
using Morestachio.Framework.Error;
using Morestachio.Framework.Expression;
using Morestachio.Framework.Expression.Framework;
Expand Down Expand Up @@ -113,9 +114,18 @@ public static void TestLocationsInOrder(MorestachioDocumentInfo documentInfo)

while (api.Context.OperationStatus)
{
if (api.Context.CurrentNode.Item is TextEditDocumentItem || api.Context.CurrentNode.Item is RemoveAliasDocumentItem)
if (api.Context.CurrentNode.Item is TextEditDocumentItem txtEdit)
{
Assert.That(api.Context.CurrentNode.Item.ExpressionStart, Is.GreaterThan(lastLocation).Or.EqualTo(lastLocation));
if (txtEdit.EmbeddedInstructionOrigin != EmbeddedInstructionOrigin.Self)
{
api.SearchForward(f => true, false);
continue;
}
}

if (api.Context.CurrentNode.Item is RemoveAliasDocumentItem)
{
Assert.That(api.Context.CurrentNode.Item.ExpressionStart, Is.GreaterThan(lastLocation), () => "");
}
else
{
Expand Down Expand Up @@ -461,22 +471,22 @@ public async Task ParserCanVariableSetToOtherVariable()
public async Task ParserCanVariableScope()
{
var template =
@"{{#VAR global = data |-}}
{{#data |-}}
{{global |-}}
{{#LET global = 'Burns ' |-}}
{{global |-}}
{{#LET global = 'Likes ' |-}}
{{global |-}}
{{#LET local = 'Likes ' |-}}
{{#VAR global = 'Miss ' |-}}
{{/data |-}}
{{local |-}}
{{global |-}}
{{#VAR global = 'Money ' |-}}
{{global |-}}
{{#LET global = 'Alot' |-}}
{{global |-}}";
@"{{#VAR global = data |--}}
{{#data |--}}
{{global |--}}
{{#LET global = 'Burns ' |--}}
{{global |--}}
{{#LET global = 'Likes ' |--}}
{{global |--}}
{{#LET local = 'Likes ' |--}}
{{#VAR global = 'Miss ' |--}}
{{/data |--}}
{{local |--}}
{{global |--}}
{{#VAR global = 'Money ' |--}}
{{global |--}}
{{#LET global = 'Alot' |--}}
{{global |--}}";
var data = new Dictionary<string, object>
{
{ "data", "Mr " },
Expand Down Expand Up @@ -1745,8 +1755,8 @@ public async Task TestRepeatLoopContext()
[Test]
public async Task TestWhileLoopContext()
{
var template = @"{{#WHILE $index.SmallerAs(5) |-}}
{{$index}},
var template = @"{{#WHILE $index.SmallerAs(5)}}
{{-| $index}},
{{-| /WHILE}}";
var data = new Dictionary<string, object> { };

Expand Down Expand Up @@ -1793,10 +1803,10 @@ public async Task TestCanRemoveLineBreaks()
[Test]
public async Task TestCanRemoveLineBreaksWithinKeyword()
{
var template = @"{{data |-}}
var template = @"{{data |--}}
Static
{{-| data}}";
{{--| data}}";
var data = new
{
data = "World"
Expand All @@ -1815,7 +1825,6 @@ public async Task TestCanRemoveLineBreaksWithOption()
var template = @"{{#SET OPTION TrimTailing = true}}{{#SET OPTION TrimLeading = true}}
{{data}}
Static
{{data}}";
var data = new
{
Expand Down
3 changes: 2 additions & 1 deletion Morestachio.Tests/SerilalizerTests/SerializerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Morestachio.Document.Items;
using Morestachio.Document.Visitor;
using Morestachio.Framework;
using Morestachio.Framework.Tokenizing;
using Morestachio.Helper.Localization;
using Morestachio.TemplateContainers;
using Morestachio.Tests.SerilalizerTests.Strategies;
Expand Down Expand Up @@ -264,7 +265,7 @@ public void TestLocIsSerializable()
[Test]
public void Alias()
{
var alias = new AliasDocumentItem(CharacterLocation.Unknown, "Alias", 101);
var alias = new AliasDocumentItem(CharacterLocation.Unknown, "Alias", 101, null);
SerilalizeAndDeserialize(alias);
}
}
Expand Down
26 changes: 26 additions & 0 deletions Morestachio/Document/Contracts/IBlockDocumentItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Collections.Generic;
using Morestachio.Framework.Tokenizing;

namespace Morestachio.Document.Contracts
{
/// <summary>
/// Defines a part that can store itself and children
/// </summary>
public interface IBlockDocumentItem : IDocumentItem
{
/// <summary>
/// The list of Children that are children of this Document item
/// </summary>
IList<IDocumentItem> Children { get; }

/// <summary>
/// The token options set on a closing tag of an block
/// </summary>
IEnumerable<ITokenOption> BlockClosingOptions { get; set; }

/// <summary>
/// Adds the specified childs.
/// </summary>
void Add(params IDocumentItem[] documentChildren);
}
}
11 changes: 3 additions & 8 deletions Morestachio/Document/Contracts/IDocumentItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Morestachio.Framework;
using Morestachio.Framework.Context;
using Morestachio.Framework.IO;
using Morestachio.Framework.Tokenizing;
#if ValueTask
using ItemExecutionPromise = System.Threading.Tasks.ValueTask<System.Collections.Generic.IEnumerable<Morestachio.Document.Contracts.DocumentItemExecution>>;
using Promise = System.Threading.Tasks.ValueTask;
Expand All @@ -30,16 +31,10 @@ public interface IDocumentItem
ItemExecutionPromise Render(IByteCounterStream outputStream, ContextObject context,
ScopeData scopeData);


/// <summary>
/// The list of Children that are children of this Document item
/// Contains the list of TokenOptions used to construct this DocumentItem.
/// </summary>
IList<IDocumentItem> Children { get; }

/// <summary>
/// Adds the specified childs.
/// </summary>
void Add(params IDocumentItem[] documentChildren);
IEnumerable<ITokenOption> TagCreationOptions { get; }

/// <summary>
/// If this is a Natural Document item this defines the Position within the Template where the DocumentItem is parsed from
Expand Down
13 changes: 8 additions & 5 deletions Morestachio/Document/Custom/BlockDocumentItemProvider.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Morestachio.Document.Contracts;
using System.Collections.Generic;
using Morestachio.Document.Contracts;
using Morestachio.Document.Items.Base;
using Morestachio.Document.Visitor;
using Morestachio.Framework;
Expand Down Expand Up @@ -43,15 +44,16 @@ public class BlockDocumentItem : ValueDocumentItemBase
/// <summary>
///
/// </summary>
public BlockDocumentItem() : base(CharacterLocation.Unknown, null)
public BlockDocumentItem()
{

}

/// <inheritdoc />
public BlockDocumentItem(CharacterLocation location,
BlockDocumentProviderFunction action,
string value) : base(location, value)
string value,
IEnumerable<ITokenOption> tagCreationOptions) : base(location, value, tagCreationOptions)
{
_action = action;
}
Expand All @@ -71,9 +73,10 @@ public override void Accept(IDocumentItemVisitor visitor)
}

/// <inheritdoc />
public override IDocumentItem CreateDocumentItem(string tag, string value, TokenPair token, ParserOptions options)
public override IDocumentItem CreateDocumentItem(string tag, string value, TokenPair token,
ParserOptions options, IEnumerable<ITokenOption> tagCreationOptions)
{
return new BlockDocumentItem(token.TokenLocation, _action, value);
return new BlockDocumentItem(token.TokenLocation, _action, value, tagCreationOptions);
}
}
}
9 changes: 5 additions & 4 deletions Morestachio/Document/Custom/BlockDocumentItemProviderBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ protected BlockDocumentItemProviderBase(string tagOpen, string tagClose)
/// <summary>
/// Will be called to produce an Document item that must be executed
/// </summary>
public abstract IDocumentItem CreateDocumentItem(string tag, string value, TokenPair token, ParserOptions options);
public abstract IDocumentItem CreateDocumentItem(string tag, string value, TokenPair token,
ParserOptions options, IEnumerable<ITokenOption> tagCreationOptions);

/// <inheritdoc />
public override IEnumerable<TokenPair> Tokenize(TokenInfo token, ParserOptions options)
Expand All @@ -53,20 +54,20 @@ public override IEnumerable<TokenPair> Tokenize(TokenInfo token, ParserOptions o
}

/// <inheritdoc />
public override bool ShouldParse(TokenPair token, ParserOptions options)
public override bool ShouldParse(TokenPair token, ParserOptions options, IEnumerable<ITokenOption> tagCreationOptions)
{
return token.Type.Equals(TagOpen.Trim()) || token.Type.Equals(TagClose);
}

/// <inheritdoc />
public override IDocumentItem Parse(TokenPair token, ParserOptions options, Stack<DocumentScope> buildStack,
Func<int> getScope)
Func<int> getScope, IEnumerable<ITokenOption> tagCreationOptions)
{
if (Equals(token.Type, TagOpen.Trim()))
{
var tagDocumentItem = CreateDocumentItem(TagOpen,
token.Value?.Remove(0, TagOpen.Length).Trim(),
token, options);
token, options, tagCreationOptions);
buildStack.Push(new DocumentScope(tagDocumentItem, getScope));
return tagDocumentItem;
}
Expand Down
27 changes: 17 additions & 10 deletions Morestachio/Document/Custom/CustomDocumentItemProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ protected CustomDocumentItemProvider()
/// </summary>
public class TokenInfo
{

internal TokenInfo(string token,
TokenzierContext context,
Stack<Tokenizer.ScopeStackItem> scopeStack)
Stack<Tokenizer.ScopeStackItem> scopeStack, IList<ITokenOption> tokenOptions)
{
TokenizerContext = context;
ScopeStack = scopeStack;
TokenOptions = tokenOptions;
Token = token;
Errors = new List<IMorestachioError>();
}
Expand All @@ -52,6 +52,11 @@ internal TokenInfo(string token,
/// </summary>
public Stack<Tokenizer.ScopeStackItem> ScopeStack { get; }

/// <summary>
/// Contains the list of Options that are determend outside of the processing of this token
/// </summary>
public IList<ITokenOption> TokenOptions { get; }

/// <summary>
/// The obtained Token. This is the Full text token
/// </summary>
Expand All @@ -78,13 +83,14 @@ internal TokenInfo(string token,
/// <returns></returns>
public abstract IEnumerable<TokenPair> Tokenize(TokenInfo token, ParserOptions options);

/// <summary>
/// Should return True if the Token is produced by this provider and should be parsed with this provider
/// </summary>
/// <param name="token"></param>
/// <param name="options"></param>
/// <returns></returns>
public abstract bool ShouldParse(TokenPair token, ParserOptions options);
/// <summary>
/// Should return True if the Token is produced by this provider and should be parsed with this provider
/// </summary>
/// <param name="token"></param>
/// <param name="options"></param>
/// <param name="tokenOptions"></param>
/// <returns></returns>
public abstract bool ShouldParse(TokenPair token, ParserOptions options, IEnumerable<ITokenOption> tokenOptions);

/// <summary>
/// Should return an document item that will be invoked when parsing the Template
Expand All @@ -93,8 +99,9 @@ internal TokenInfo(string token,
/// <param name="options"></param>
/// <param name="buildStack"></param>
/// <param name="getScope"></param>
/// <param name="tokenOptions"></param>
/// <returns></returns>
public abstract IDocumentItem Parse(TokenPair token, ParserOptions options, Stack<DocumentScope> buildStack,
Func<int> getScope);
Func<int> getScope, IEnumerable<ITokenOption> tokenOptions);
}
}
11 changes: 7 additions & 4 deletions Morestachio/Document/Custom/TagDocumentItemProvider.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using Morestachio.Document.Contracts;
using Morestachio.Document.Items.Base;
using Morestachio.Document.Visitor;
Expand Down Expand Up @@ -40,15 +41,16 @@ public class TagDocumentItem : ValueDocumentItemBase
private readonly TagDocumentProviderFunction _action;

/// <inheritdoc />
public TagDocumentItem() : base(CharacterLocation.Unknown, null)
public TagDocumentItem()
{

}

/// <inheritdoc />
public TagDocumentItem(CharacterLocation location,
TagDocumentProviderFunction action,
string value) : base(location, value)
string value,
IEnumerable<ITokenOption> tagCreationOptions) : base(location, value, tagCreationOptions)
{
_action = action;
}
Expand All @@ -68,9 +70,10 @@ public override void Accept(IDocumentItemVisitor visitor)
}

/// <inheritdoc />
public override IDocumentItem CreateDocumentItem(string tag, string value, TokenPair token, ParserOptions options)
public override IDocumentItem CreateDocumentItem(string tag, string value, TokenPair token,
ParserOptions options, IEnumerable<ITokenOption> tagCreationOptions)
{
return new TagDocumentItem(token.TokenLocation, _action, value);
return new TagDocumentItem(token.TokenLocation, _action, value, tagCreationOptions);
}
}
}
Loading

0 comments on commit c792738

Please sign in to comment.