-
Notifications
You must be signed in to change notification settings - Fork 2
/
YAMLite.cs
158 lines (138 loc) · 3.48 KB
/
YAMLite.cs
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
using System;
using System.Collections.Generic;
using System.Linq;
public class YAMLite {
public class Node : Dictionary<string, Node> {
public string leaf = null;
private bool initialized = false;
public Node parent;
public static implicit operator bool(Node n) {
return n != null && n.Count != 0 || n.leaf != null;
}
public static implicit operator string(Node n) {
if(n.leaf != null) {
return n.leaf;
} else if (n.Count == 0) {
//Null node
return "~";
} else {
//Object node
string yaml = "";
foreach(KeyValuePair<string, Node> entry in n) {
yaml += entry.Key + ":";
if(entry.Value.leaf != null) {
yaml += " " + entry.Value + "\n";
} else {
foreach(string line in entry.Value.ToString().Split(new char[]{'\n'}, System.StringSplitOptions.RemoveEmptyEntries).ToList ()) {
yaml += "\n " + line;
}
yaml += "\n";
}
}
return yaml;
}
}
public static implicit operator Node(string s) {
Node n = new Node();
n.leaf = s;
return n;
}
public Node merge(Node n){
if(this.leaf != null) {
return n;
}
foreach(KeyValuePair<string, Node> entry in n) {
if(! this.ContainsKey(entry.Key) ) {
this.Add(entry.Key, entry.Value);
} else {
//Leaf node on either
if(this[entry.Key].leaf != null || entry.Value.leaf != null) {
this[entry.Key] = entry.Value;
//Object node - recurse!
} else {
this[entry.Key].merge(entry.Value);
}
}
}
return this;
}
public new string ToString() {
return (string) this;
}
public Node this[int key] {
get {
return this[key.ToString()];
}
set {
this[key.ToString()] = value;
}
}
public new Node this[string key] {
get {
Node outNode = new Node();
TryGetValue(key, out outNode);
if(outNode == null) {
return new Node();
} else {
return outNode;
}
}
set {
initialized = true;
this.Remove(key);
this.Add(key, value);
}
}
}
public static Node parse(string yaml) {
yaml = yaml.TrimStart('\n');
if(yaml.StartsWith("---")) { yaml = yaml.Remove(0,3); }
yaml = yaml.TrimStart('\n');
return _parse(ref yaml, 0);
}
public static Node _parse(ref string yaml, int indentLevel) {
//Detect unindented array
bool unIndentedArray = false;
if(yaml[0] == '\n' && yaml.Remove(0,1).TrimStart(' ')[0] == '-') {
int _indentLevel = yaml.Remove(0,1).Length - yaml.Remove(0,1).TrimStart(' ').Length;
if(_indentLevel + 2 == indentLevel) {
yaml = yaml.Remove(0,1);
unIndentedArray = true;
}
}
int arrayCount = 0;
Node n = new Node();
while(yaml.Length != 0) {
char c = yaml[0];
switch(c) {
case ' ':
yaml = yaml.TrimStart(' ');
break;
case '\n':
yaml = yaml.Remove(0,1);
int _indentLevel = yaml.Length - yaml.TrimStart(' ').Length;
if(unIndentedArray) { _indentLevel += 2; }
if (_indentLevel != indentLevel) {
yaml = "\n" + yaml;
return n;
}
break;
case '-':
yaml = arrayCount++.ToString() + ": " + yaml.Remove(0,1);
break;
default:
string sym = String.Join("", yaml.TakeWhile( (_c) => _c != ':' && _c != '\n'));
yaml = yaml.Remove(0, sym.Length);
if(yaml.Length > 0 && yaml[0] == ':') {
yaml = yaml.Remove(0,1).TrimStart(' ');
n.Add(sym, _parse(ref yaml, indentLevel + (unIndentedArray ? 0 : 2)));
} else {
n.leaf = sym;
return n;
}
break;
}
}
return n;
}
}