forked from fdorg/flashdevelop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBottomUpParser.cs
More file actions
118 lines (96 loc) · 2.45 KB
/
Copy pathBottomUpParser.cs
File metadata and controls
118 lines (96 loc) · 2.45 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
using System;
using System.Collections.Generic;
using System.Text;
namespace PluginCore.BBCode
{
public class BottomUpParser
{
public BottomUpParser()
{
}
public IPairTagMatcher pairTagMatcher;
public IPairTagMatchHandler pairTagHandler;
public String input;
public IndexTree lastTree;
public IndexTree parse()
{
lastTree = null;
if (pairTagMatcher == null || this.input == null || this.input.Length < 1)
return null;
lastTree = _parse();
return lastTree;
}
private IndexTree _parse()
{
pairTagMatcher.input = input;
List<IPairTagMatch> openers = _findAllOpeners();
IndexTree tree = _buildTree(openers);
return tree;
}
private List<IPairTagMatch> _findAllOpeners()
{
List<IPairTagMatch> openers = new List<IPairTagMatch>();
IPairTagMatch m;
int prevI = 0;
int prevL = 0;
while (true)
{
m = pairTagMatcher.searchOpener((uint)(prevI + prevL));
if (m == null)
break;
prevI = m.tagIndex;
prevL = (int)m.tagLength;
if (pairTagHandler != null
&& (!pairTagHandler.isHandleable(m)
|| !pairTagHandler.handleTag(m)))
continue;
openers.Add(m);
}
return openers;
}
private IndexTree _buildTree(List<IPairTagMatch> openers)
{
uint inputL = (uint)input.Length;
Boolean closerOutOfBounds;
int closerStartAt;
Dictionary<int, IPairTagMatch> closerIndices = new Dictionary<int, IPairTagMatch>();
IPairTagMatch mOp;
IPairTagMatch mCl;
IndexTree rootTree = new IndexTree(0, (int)inputL, 0, 0, null, null);
int i = openers.Count;
while (i-- > 0)
{
mOp = openers[i];
closerStartAt = (int)(mOp.tagIndex + mOp.tagLength);
closerOutOfBounds = false;
while (true)
{
mCl = pairTagMatcher.searchCloserFor(mOp, (uint)closerStartAt);
if (mCl == null)
{
mCl = new VoidCloserTagMatch((int)inputL);
closerOutOfBounds = true;
}
if (!closerIndices.ContainsKey(mCl.tagIndex))
{
if (mCl.tagIndex < inputL)
closerIndices[mCl.tagIndex] = mCl;
IndexTree.insertLeaf(rootTree,
new IndexTree(mOp.tagIndex,
(int)(mCl.tagIndex + mCl.tagLength),
(int)(mOp.tagLength),
(int)(-mCl.tagLength),
new PairTag(mOp, mCl),
null));
break;
}
if (closerOutOfBounds)
break;
else if (mCl != null)
closerStartAt = mCl.tagIndex + 1;
}
}
return rootTree;
}
}
}