-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRegexNodeJson.cs
More file actions
128 lines (116 loc) · 3.45 KB
/
Copy pathRegexNodeJson.cs
File metadata and controls
128 lines (116 loc) · 3.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
119
120
121
122
123
124
125
126
127
128
using RegexDebug.RegexDev;
using System;
using System.Buffers;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Threading.Tasks;
namespace RegexDebug
{
internal class RegexNodeJson
{
internal string GetJsonObject(IRegexNode node)
{
var options = new JsonWriterOptions
{
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
//Indented = true
};
var buffer = new ArrayBufferWriter<byte>();
var writer = new Utf8JsonWriter(buffer, options);
GetJsonObject(node, writer);
writer.Flush();
return Encoding.UTF8.GetString(buffer.WrittenSpan);
}
private void GetJsonObject(IRegexNode node, Utf8JsonWriter writer)
{
writer.WriteStartObject();
if (node is RePanel panel)
{
var type = "sequence";
if (panel.addBracket)
type = "constructure";
else if (panel.GroupingConstruct.Count > 0)
type = "constructure";
else if (!string.IsNullOrEmpty(panel.Quantifier))
type = "repeat";
writer.WriteString("type", type);
if (type == "sequence")
{
writer.WriteStartArray("children");
foreach (var child in panel.SequenceNodes)
{
GetJsonObject(child, writer);
}
writer.WriteEndArray();
}
else
{
writer.WritePropertyName("child");
GetJsonObject(panel.SequenceNodes[0], writer);
}
if (panel.addBracket)
{
writer.WriteString("pattern", "(");
if (panel.GroupingNumber > 0)
writer.WriteNumber("GroupingNumber", panel.GroupingNumber);
}
else if (panel.GroupingConstruct.Count > 0)
{
writer.WriteString("pattern", panel.GroupingConstruct[0]);
if (panel.GroupingNumber > 0)
writer.WriteNumber("GroupingNumber", panel.GroupingNumber);
if (panel.CaptureGroup2rdName != "")
writer.WriteString("CaptureGroup2rdName", panel.CaptureGroup2rdName);
if (panel.BalancingGroup2rdName != "")
writer.WriteString("BalancingGroup2rdName", panel.BalancingGroup2rdName);
}
else if (panel.Quantifier.Length > 0)
writer.WriteString("kind", panel.Quantifier);
}
else if (node is Reline line)
{
writer.WriteString("type", "alt");
writer.WriteStartArray("children");
foreach (var child in line.AlternationNodes)
{
GetJsonObject(child, writer);
}
writer.WriteEndArray();
}
else if (node is ReSingle single)
{
writer.WriteString("type", "single");
writer.WriteString("text", single.pattern);
if (single.IsReference != null)
writer.WriteBoolean("IsReference", single.IsReference == true);
if (single.singleType == SingleType.EndofLineComment) writer.WriteNumber("EndofLineComment", 1);
}
else if (node is ReCondition condition)
{
writer.WriteString("type", "condition");
if (!condition.HaveNoBanch)
writer.WriteNumber("notHaveNoBanch", 1);
if (condition.condition1 != null)
{
writer.WritePropertyName("c1");
GetJsonObject(condition.condition1, writer);
}
else
{
writer.WriteString("group", condition.conditionGroup);
if (condition.conditionGroup2RdName.Length > 0)
writer.WriteString("group2rdName", condition.conditionGroup2RdName);
}
writer.WritePropertyName("c2");
GetJsonObject(condition.condition2, writer);
writer.WritePropertyName("c3");
GetJsonObject(condition.condition3, writer);
}
else throw new Exception("unkonw regex node");
writer.WriteEndObject();
}
}
}