This repository has been archived by the owner on Dec 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 224
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add extensible directive abstractions
- Based generic directive implementation off of descriptors. - Added parsing logic to consume descriptors and parse content that's expected. - Added parsing errors to automagically detect unexpected directive pieces. - Updated visitor implementations to understand the directive bits. - Added a builder abstraction to easily create descriptors. Had to maintain the ability to manually construct a descriptor to enable convenient serialization/deserialization. #853
- Loading branch information
1 parent
47769c1
commit 30388a5
Showing
16 changed files
with
572 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
src/Microsoft.AspNetCore.Razor.Evolution/DirectiveDescriptor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// Copyright(c) .NET Foundation.All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System.Collections.Generic; | ||
|
||
namespace Microsoft.AspNetCore.Razor.Evolution | ||
{ | ||
public class DirectiveDescriptor | ||
{ | ||
public static IDirectiveDescriptorBuilder Create(string name) | ||
{ | ||
return new DefaultDirectiveDescriptorBuilder(name, DirectiveDescriptorType.SingleLine); | ||
} | ||
|
||
public static IDirectiveDescriptorBuilder CreateRazorBlock(string name) | ||
{ | ||
return new DefaultDirectiveDescriptorBuilder(name, DirectiveDescriptorType.RazorBlock); | ||
} | ||
|
||
public static IDirectiveDescriptorBuilder CreateCodeBlock(string name) | ||
{ | ||
return new DefaultDirectiveDescriptorBuilder(name, DirectiveDescriptorType.CodeBlock); | ||
} | ||
|
||
public string Name { get; set; } | ||
|
||
public DirectiveDescriptorType Type { get; set; } | ||
|
||
public IList<DirectiveTokenDescriptor> Tokens { get; set; } | ||
|
||
private class DefaultDirectiveDescriptorBuilder : IDirectiveDescriptorBuilder | ||
{ | ||
private readonly List<DirectiveTokenDescriptor> _tokenDescriptors; | ||
private readonly string _name; | ||
private readonly DirectiveDescriptorType _type; | ||
|
||
public DefaultDirectiveDescriptorBuilder(string name, DirectiveDescriptorType type) | ||
{ | ||
_name = name; | ||
_type = type; | ||
_tokenDescriptors = new List<DirectiveTokenDescriptor>(); | ||
} | ||
|
||
public IDirectiveDescriptorBuilder AddType() | ||
{ | ||
var descriptor = new DirectiveTokenDescriptor() | ||
{ | ||
Type = DirectiveTokenType.Type | ||
}; | ||
_tokenDescriptors.Add(descriptor); | ||
|
||
return this; | ||
} | ||
|
||
public IDirectiveDescriptorBuilder AddMember() | ||
{ | ||
var descriptor = new DirectiveTokenDescriptor() | ||
{ | ||
Type = DirectiveTokenType.Member | ||
}; | ||
_tokenDescriptors.Add(descriptor); | ||
|
||
return this; | ||
} | ||
|
||
public IDirectiveDescriptorBuilder AddString() | ||
{ | ||
var descriptor = new DirectiveTokenDescriptor() | ||
{ | ||
Type = DirectiveTokenType.String | ||
}; | ||
_tokenDescriptors.Add(descriptor); | ||
|
||
return this; | ||
} | ||
|
||
public IDirectiveDescriptorBuilder AddLiteral(string literal, bool optional) | ||
{ | ||
var descriptor = new DirectiveTokenDescriptor() | ||
{ | ||
Type = DirectiveTokenType.Literal, | ||
Value = literal, | ||
Optional = optional, | ||
}; | ||
_tokenDescriptors.Add(descriptor); | ||
|
||
return this; | ||
} | ||
|
||
public DirectiveDescriptor Build() | ||
{ | ||
var descriptor = new DirectiveDescriptor | ||
{ | ||
Name = _name, | ||
Type = _type, | ||
Tokens = _tokenDescriptors, | ||
}; | ||
|
||
return descriptor; | ||
} | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/Microsoft.AspNetCore.Razor.Evolution/DirectiveDescriptorType.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Copyright(c) .NET Foundation.All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
namespace Microsoft.AspNetCore.Razor.Evolution | ||
{ | ||
public enum DirectiveDescriptorType | ||
{ | ||
SingleLine, | ||
RazorBlock, | ||
CodeBlock | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/Microsoft.AspNetCore.Razor.Evolution/DirectiveTokenDescriptor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Copyright(c) .NET Foundation.All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
namespace Microsoft.AspNetCore.Razor.Evolution | ||
{ | ||
public class DirectiveTokenDescriptor | ||
{ | ||
public DirectiveTokenType Type { get; set; } | ||
|
||
public string Value { get; set; } | ||
|
||
public bool Optional { get; set; } | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/Microsoft.AspNetCore.Razor.Evolution/DirectiveTokenType.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Copyright(c) .NET Foundation.All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
namespace Microsoft.AspNetCore.Razor.Evolution | ||
{ | ||
public enum DirectiveTokenType | ||
{ | ||
Type, | ||
Member, | ||
String, | ||
Literal | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/Microsoft.AspNetCore.Razor.Evolution/IDirectiveDescriptorBuilder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Copyright(c) .NET Foundation.All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
namespace Microsoft.AspNetCore.Razor.Evolution | ||
{ | ||
public interface IDirectiveDescriptorBuilder | ||
{ | ||
IDirectiveDescriptorBuilder AddType(); | ||
|
||
IDirectiveDescriptorBuilder AddMember(); | ||
|
||
IDirectiveDescriptorBuilder AddString(); | ||
|
||
IDirectiveDescriptorBuilder AddLiteral(string literal, bool optional); | ||
|
||
DirectiveDescriptor Build(); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/Microsoft.AspNetCore.Razor.Evolution/Intermediate/DirectiveIRNode.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright(c) .NET Foundation.All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System.Collections.Generic; | ||
using Microsoft.AspNetCore.Razor.Evolution.Legacy; | ||
|
||
namespace Microsoft.AspNetCore.Razor.Evolution.Intermediate | ||
{ | ||
public class DirectiveIRNode : RazorIRNode | ||
{ | ||
public override IList<RazorIRNode> Children { get; } = new List<RazorIRNode>(); | ||
|
||
public override RazorIRNode Parent { get; set; } | ||
|
||
internal override SourceLocation SourceLocation { get; set; } | ||
|
||
public string Name { get; set; } | ||
|
||
public IList<DirectiveTokenIRNode> Tokens { get; } = new List<DirectiveTokenIRNode>(); | ||
|
||
public DirectiveDescriptor Descriptor { get; set; } | ||
|
||
public override void Accept(RazorIRNodeVisitor visitor) | ||
{ | ||
visitor.VisitDirective(this); | ||
} | ||
|
||
public override TResult Accept<TResult>(RazorIRNodeVisitor<TResult> visitor) | ||
{ | ||
return visitor.VisitDirective(this); | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/Microsoft.AspNetCore.Razor.Evolution/Intermediate/DirectiveTokenIRNode.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright(c) .NET Foundation.All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System.Collections.Generic; | ||
using Microsoft.AspNetCore.Razor.Evolution.Legacy; | ||
|
||
namespace Microsoft.AspNetCore.Razor.Evolution.Intermediate | ||
{ | ||
public class DirectiveTokenIRNode : RazorIRNode | ||
{ | ||
public override IList<RazorIRNode> Children { get; } = EmptyArray; | ||
|
||
public override RazorIRNode Parent { get; set; } | ||
|
||
internal override SourceLocation SourceLocation { get; set; } | ||
|
||
public string Content { get; set; } | ||
|
||
public DirectiveTokenDescriptor Descriptor { get; set; } | ||
|
||
public override void Accept(RazorIRNodeVisitor visitor) | ||
{ | ||
visitor.VisitDirectiveToken(this); | ||
} | ||
|
||
public override TResult Accept<TResult>(RazorIRNodeVisitor<TResult> visitor) | ||
{ | ||
return visitor.VisitDirectiveToken(this); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.