-
Notifications
You must be signed in to change notification settings - Fork 47
/
Parser.cs
192 lines (169 loc) · 6.1 KB
/
Parser.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace DotNetEnv
{
internal class Parser
{
private static Regex ExportRegex = new Regex("^\\s*export\\s+");
private static Regex VariableRegex = new Regex("\\$[0-9a-zA-Z_]+");
private static bool IsComment(string line)
{
return line.Trim().StartsWith("#");
}
private static string RemoveInlineComment(string line)
{
int pos = line.IndexOf('#');
return pos >= 0 ? line.Substring(0, pos) : line;
}
private static string RemoveExportKeyword(string line)
{
Match match = ExportRegex.Match(line);
return match.Success ? line.Substring(match.Length) : line;
}
private static string ParseVariables(Vars vars, string line)
{
var result = line;
while (true)
{
var match = VariableRegex.Match(result);
if (match.Success)
{
var key = match.Groups[0].Value.Substring(1);
var value = vars.ContainsKey(key) ? vars[key] : Environment.GetEnvironmentVariable(key);
result = Replace(result, match.Index, match.Length, value);
}
else
{
break;
}
}
return result;
}
private static string Replace(string s, int index, int length, string replacement)
{
var builder = new StringBuilder();
builder.Append(s.Substring(0, index));
builder.Append(replacement);
builder.Append(s.Substring(index + length));
return builder.ToString();
}
public static Vars Parse(
string[] lines,
bool trimWhitespace = true,
bool isEmbeddedHashComment = true,
bool unescapeQuotedValues = true,
bool parseVariables = true
)
{
Vars vars = new Vars();
for (int i = 0; i < lines.Length; i++)
{
string line = lines[i];
// skip comments
if (IsComment(line))
continue;
if (isEmbeddedHashComment)
{
line = RemoveInlineComment(line);
}
line = RemoveExportKeyword(line);
string[] keyValuePair = line.Split(new char[] { '=' }, 2);
// skip malformed lines
if (keyValuePair.Length != 2)
continue;
if (trimWhitespace)
{
keyValuePair[0] = keyValuePair[0].Trim();
keyValuePair[1] = keyValuePair[1].Trim();
}
if (unescapeQuotedValues && IsQuoted(keyValuePair[1]))
{
keyValuePair[1] = Unescape(
keyValuePair[1].Substring(1, keyValuePair[1].Length - 2)
);
}
if (parseVariables)
{
keyValuePair[1] = ParseVariables(vars, keyValuePair[1]);
}
vars.Add(
keyValuePair[0],
keyValuePair[1]
);
}
return vars;
}
private static bool IsQuoted(string s)
{
return s.Length > 1 && (
(s[0] == '"' && s[s.Length - 1] == '"')
|| (s[0] == '\'' && s[s.Length - 1] == '\'')
);
}
// copied from https://stackoverflow.com/questions/6629020/evaluate-escaped-string/25471811#25471811
private static string Unescape(string s)
{
StringBuilder sb = new StringBuilder();
Regex r = new Regex("\\\\[abfnrtv?\"'\\\\]|\\\\[0-3]?[0-7]{1,2}|\\\\u[0-9a-fA-F]{4}|\\\\U[0-9a-fA-F]{8}|.");
MatchCollection mc = r.Matches(s, 0);
foreach (Match m in mc)
{
if (m.Length == 1)
{
sb.Append(m.Value);
}
else
{
if (m.Value[1] >= '0' && m.Value[1] <= '7')
{
int i = Convert.ToInt32(m.Value.Substring(1), 8);
sb.Append((char)i);
}
else if (m.Value[1] == 'u')
{
int i = Convert.ToInt32(m.Value.Substring(2), 16);
sb.Append((char)i);
}
else if (m.Value[1] == 'U')
{
int i = Convert.ToInt32(m.Value.Substring(2), 16);
sb.Append(char.ConvertFromUtf32(i));
}
else
{
switch (m.Value[1])
{
case 'a':
sb.Append('\a');
break;
case 'b':
sb.Append('\b');
break;
case 'f':
sb.Append('\f');
break;
case 'n':
sb.Append('\n');
break;
case 'r':
sb.Append('\r');
break;
case 't':
sb.Append('\t');
break;
case 'v':
sb.Append('\v');
break;
default:
sb.Append(m.Value[1]);
break;
}
}
}
}
return sb.ToString();
}
}
}