1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Diagnostics;
using System.Web.Razor.Parser.SyntaxTree;
using System.Web.Razor.Text;
namespace System.Web.Razor.Parser
{
internal abstract class MarkupRewriter : ParserVisitor, ISyntaxTreeRewriter
{
private Stack<BlockBuilder> _blocks = new Stack<BlockBuilder>();
private Action<SpanBuilder, SourceLocation, string> _markupSpanFactory;
protected MarkupRewriter(Action<SpanBuilder, SourceLocation, string> markupSpanFactory)
{
if (markupSpanFactory == null)
{
throw new ArgumentNullException("markupSpanFactory");
}
_markupSpanFactory = markupSpanFactory;
}
protected BlockBuilder Parent
{
get { return _blocks.Count > 0 ? _blocks.Peek() : null; }
}
public virtual Block Rewrite(Block input)
{
input.Accept(this);
Debug.Assert(_blocks.Count == 1);
return _blocks.Pop().Build();
}
public override void VisitBlock(Block block)
{
if (CanRewrite(block))
{
SyntaxTreeNode newNode = RewriteBlock(_blocks.Peek(), block);
if (newNode != null)
{
_blocks.Peek().Children.Add(newNode);
}
}
else
{
// Not rewritable.
BlockBuilder builder = new BlockBuilder(block);
builder.Children.Clear();
_blocks.Push(builder);
base.VisitBlock(block);
Debug.Assert(ReferenceEquals(builder, _blocks.Peek()));
if (_blocks.Count > 1)
{
_blocks.Pop();
_blocks.Peek().Children.Add(builder.Build());
}
}
}
public override void VisitSpan(Span span)
{
if (CanRewrite(span))
{
SyntaxTreeNode newNode = RewriteSpan(_blocks.Peek(), span);
if (newNode != null)
{
_blocks.Peek().Children.Add(newNode);
}
}
else
{
_blocks.Peek().Children.Add(span);
}
}
protected virtual bool CanRewrite(Block block)
{
return false;
}
protected virtual bool CanRewrite(Span span)
{
return false;
}
protected virtual SyntaxTreeNode RewriteBlock(BlockBuilder parent, Block block)
{
throw new NotImplementedException();
}
protected virtual SyntaxTreeNode RewriteSpan(BlockBuilder parent, Span span)
{
throw new NotImplementedException();
}
protected void FillSpan(SpanBuilder builder, SourceLocation start, string content)
{
_markupSpanFactory(builder, start, content);
}
}
}
|