forked from serilog/serilog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MessageTemplateParser.cs
301 lines (265 loc) · 10.3 KB
/
MessageTemplateParser.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
// Copyright 2013-2015 Serilog Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using System;
using System.Collections.Generic;
using System.Text;
using Serilog.Core;
using Serilog.Events;
namespace Serilog.Parsing
{
/// <summary>
/// Parses message template strings into sequences of text or property
/// tokens.
/// </summary>
public class MessageTemplateParser : IMessageTemplateParser
{
/// <summary>
/// Parse the supplied message template.
/// </summary>
/// <param name="messageTemplate">The message template to parse.</param>
/// <returns>A sequence of text or property tokens. Where the template
/// is not syntactically valid, text tokens will be returned. The parser
/// will make a best effort to extract valid property tokens even in the
/// presence of parsing issues.</returns>
public MessageTemplate Parse(string messageTemplate)
{
if (messageTemplate == null)
throw new ArgumentNullException(nameof(messageTemplate));
return new MessageTemplate(messageTemplate, Tokenize(messageTemplate));
}
static IEnumerable<MessageTemplateToken> Tokenize(string messageTemplate)
{
if (messageTemplate.Length == 0)
{
yield return new TextToken("", 0);
yield break;
}
var nextIndex = 0;
while (true)
{
var beforeText = nextIndex;
var tt = ParseTextToken(nextIndex, messageTemplate, out nextIndex);
if (nextIndex > beforeText)
yield return tt;
if (nextIndex == messageTemplate.Length)
yield break;
var beforeProp = nextIndex;
var pt = ParsePropertyToken(nextIndex, messageTemplate, out nextIndex);
if (beforeProp < nextIndex)
yield return pt;
if (nextIndex == messageTemplate.Length)
yield break;
}
}
static MessageTemplateToken ParsePropertyToken(int startAt, string messageTemplate, out int next)
{
var first = startAt;
startAt++;
while (startAt < messageTemplate.Length && IsValidInPropertyTag(messageTemplate[startAt]))
startAt++;
if (startAt == messageTemplate.Length || messageTemplate[startAt] != '}')
{
next = startAt;
return new TextToken(messageTemplate.Substring(first, next - first), first);
}
next = startAt + 1;
var rawText = messageTemplate.Substring(first, next - first);
var tagContent = rawText.Substring(1, next - (first + 2));
if (tagContent.Length == 0)
return new TextToken(rawText, first);
if (!TrySplitTagContent(tagContent, out var propertyNameAndDestructuring, out var format, out var alignment))
return new TextToken(rawText, first);
var propertyName = propertyNameAndDestructuring;
var destructuring = Destructuring.Default;
if (propertyName.Length != 0 && TryGetDestructuringHint(propertyName[0], out destructuring))
propertyName = propertyName.Substring(1);
if (propertyName.Length == 0)
return new TextToken(rawText, first);
for (var i = 0; i < propertyName.Length; ++i)
{
var c = propertyName[i];
if (!IsValidInPropertyName(c))
return new TextToken(rawText, first);
}
if (format != null)
{
for (var i = 0; i < format.Length; ++i)
{
var c = format[i];
if (!IsValidInFormat(c))
return new TextToken(rawText, first);
}
}
Alignment? alignmentValue = null;
if (alignment != null)
{
for (var i = 0; i < alignment.Length; ++i)
{
var c = alignment[i];
if (!IsValidInAlignment(c))
return new TextToken(rawText, first);
}
var lastDash = alignment.LastIndexOf('-');
if (lastDash > 0)
return new TextToken(rawText, first);
if (!int.TryParse(lastDash == -1 ? alignment : alignment.Substring(1), out var width) || width == 0)
return new TextToken(rawText, first);
var direction = lastDash == -1 ?
AlignmentDirection.Right :
AlignmentDirection.Left;
alignmentValue = new Alignment(direction, width);
}
return new PropertyToken(
propertyName,
rawText,
format,
alignmentValue,
destructuring,
first);
}
static bool TrySplitTagContent(string tagContent, out string propertyNameAndDestructuring, out string format, out string alignment)
{
var formatDelim = tagContent.IndexOf(':');
var alignmentDelim = tagContent.IndexOf(',');
if (formatDelim == -1 && alignmentDelim == -1)
{
propertyNameAndDestructuring = tagContent;
format = null;
alignment = null;
}
else
{
if (alignmentDelim == -1 || (formatDelim != -1 && alignmentDelim > formatDelim))
{
propertyNameAndDestructuring = tagContent.Substring(0, formatDelim);
format = formatDelim == tagContent.Length - 1 ?
null :
tagContent.Substring(formatDelim + 1);
alignment = null;
}
else
{
propertyNameAndDestructuring = tagContent.Substring(0, alignmentDelim);
if (formatDelim == -1)
{
if (alignmentDelim == tagContent.Length - 1)
{
alignment = format = null;
return false;
}
format = null;
alignment = tagContent.Substring(alignmentDelim + 1);
}
else
{
if (alignmentDelim == formatDelim - 1)
{
alignment = format = null;
return false;
}
alignment = tagContent.Substring(alignmentDelim + 1, formatDelim - alignmentDelim - 1);
format = formatDelim == tagContent.Length - 1 ?
null :
tagContent.Substring(formatDelim + 1);
}
}
}
return true;
}
static bool IsValidInPropertyTag(char c)
{
return IsValidInDestructuringHint(c) ||
IsValidInPropertyName(c) ||
IsValidInFormat(c) ||
c == ':';
}
static bool IsValidInPropertyName(char c) => char.IsLetterOrDigit(c) || c == '_';
static bool TryGetDestructuringHint(char c, out Destructuring destructuring)
{
switch (c)
{
case '@':
{
destructuring = Destructuring.Destructure;
return true;
}
case '$':
{
destructuring = Destructuring.Stringify;
return true;
}
default:
{
destructuring = Destructuring.Default;
return false;
}
}
}
static bool IsValidInDestructuringHint(char c)
{
return c == '@' ||
c == '$';
}
static bool IsValidInAlignment(char c)
{
return char.IsDigit(c) ||
c == '-';
}
static bool IsValidInFormat(char c)
{
return c != '}' &&
(char.IsLetterOrDigit(c) ||
char.IsPunctuation(c) ||
c == ' ' ||
c == '+');
}
static TextToken ParseTextToken(int startAt, string messageTemplate, out int next)
{
var first = startAt;
var accum = new StringBuilder();
do
{
var nc = messageTemplate[startAt];
if (nc == '{')
{
if (startAt + 1 < messageTemplate.Length &&
messageTemplate[startAt + 1] == '{')
{
accum.Append(nc);
startAt++;
}
else
{
break;
}
}
else
{
accum.Append(nc);
if (nc == '}')
{
if (startAt + 1 < messageTemplate.Length &&
messageTemplate[startAt + 1] == '}')
{
startAt++;
}
}
}
startAt++;
} while (startAt < messageTemplate.Length);
next = startAt;
return new TextToken(accum.ToString(), first);
}
}
}