File: CompositeFormatStringParser.cs

package info (click to toggle)
monodevelop 4.0.12%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 219,596 kB
  • ctags: 253,200
  • sloc: cs: 1,486,058; xml: 952,347; java: 60,981; makefile: 4,213; sh: 1,727; ansic: 867; objc: 302; sql: 111
file content (343 lines) | stat: -rw-r--r-- 10,070 bytes parent folder | download | duplicates (5)
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
//
// CompositeFormatStringParser.cs
//
// Authors:
//   Simon Lindgren <simon.n.lindgren@gmail.com>
//
// Copyright (c) 2012 Simon Lindgren
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.Collections.Generic;
using System.Linq;

namespace ICSharpCode.NRefactory.Utils
{
	/// <summary>
	/// Composite format string parser.
	/// </summary>
	/// <remarks>
	/// Implements a complete parser for valid strings as well as
	/// error reporting and best-effort parsing for invalid strings.
	/// </remarks>		
	public class CompositeFormatStringParser
	{

		public CompositeFormatStringParser ()
		{
			errors = new List<IFormatStringError> ();
		}

		/// <summary>
		/// Parse the specified format string.
		/// </summary>
		/// <param name='format'>
		/// The format string.
		/// </param>
		public FormatStringParseResult Parse (string format)
		{
			if (format == null)
				throw new ArgumentNullException ("format");

			var result = new FormatStringParseResult();

			// Format string syntax: http://msdn.microsoft.com/en-us/library/txafckwd.aspx
			int textStart = 0;
			var length = format.Length;
			for (int i = 0; i < length; i++) {
				// Get fixed text
				GetText (format, ref i);

				if (i < format.Length && format [i] == '{') {
					int formatItemStart = i;
					int index;
					int? alignment;
					string argumentFormat;
					var textSegmentErrors = new List<IFormatStringError>(GetErrors());

					// Try to parse the parts of the format item
					++i;
					index = ParseIndex (format, ref i);
					CheckForMissingEndBrace (format, i, length);

					alignment = ParseAlignment (format, ref i, length);
					CheckForMissingEndBrace (format, i, length);

					argumentFormat = ParseSubFormatString (format, ref i, length);
					CheckForMissingEndBrace (format, i, length);

					// Check what we parsed
					if (i == formatItemStart + 1 && (i == length || (i < length && format[i] != '}'))) {
						// There were no format item after all, this was just an
						// unescaped left brace, or the initial brace of an escape sequence
						SetErrors(textSegmentErrors);
						if (i >= length || format[i] != '{') {
							AddError (new DefaultFormatStringError {
								Message = "Unescaped '{'",
								StartLocation = formatItemStart,
								EndLocation = formatItemStart + 1,
								OriginalText = "{",
								SuggestedReplacementText = "{{"
							});
						}
						continue;
					}

					if (formatItemStart - textStart > 0) {
						// We have parsed a format item, end the text segment
						var textSegment = new TextSegment (UnEscape (format.Substring (textStart, formatItemStart - textStart)));
						textSegment.Errors = textSegmentErrors;
						result.Segments.Add (textSegment);
					}
					
					// Unclosed format items in fixed text advances i one step too far
					if (i < length && format [i] != '}')
						--i;

					// i may actually point outside of format if there is a syntactical error
					// if that happens, we want the last position
					var endLocation = Math.Min (length, i + 1);
					result.Segments.Add (new FormatItem (index, alignment, argumentFormat) {
						StartLocation = formatItemStart,
						EndLocation = endLocation,
						Errors = GetErrors ()
					});
					ClearErrors ();

					// The next potential text segment starts after this format item
					textStart = i + 1;
				}
			}
			// Handle remaining text
			if (textStart < length) {
				var textSegment = new TextSegment (UnEscape (format.Substring (textStart)), textStart);
				textSegment.Errors = GetErrors();
				result.Segments.Add (textSegment);

			}
			return result;
		}

		int ParseIndex (string format, ref int i)
		{
			int parsedCharacters;
			int? maybeIndex = GetAndCheckNumber (format, ",:}", ref i, i, out parsedCharacters);
			if (parsedCharacters == 0) {
				AddError (new DefaultFormatStringError {
					StartLocation = i,
					EndLocation = i,
					Message = "Missing index",
					OriginalText = "",
					SuggestedReplacementText = "0"
				});
			}
			return maybeIndex ?? 0;
		}

		int? ParseAlignment(string format, ref int i, int length)
		{
			if (i < length && format [i] == ',') {
				int alignmentBegin = i;
				++i;
				while (i < length && char.IsWhiteSpace(format [i]))
					++i;

				int parsedCharacters;
				var number = GetAndCheckNumber (format, ",:}", ref i, alignmentBegin + 1, out parsedCharacters);
				if (parsedCharacters == 0) {
					AddError (new DefaultFormatStringError {
						StartLocation = i,
						EndLocation = i,
						Message = "Missing alignment",
						OriginalText = "",
						SuggestedReplacementText = "0"
					});
				}
				return number ?? 0;
			}
			return null;
		}

		string ParseSubFormatString(string format, ref int i, int length)
		{
			if (i < length && format [i] == ':') {
				++i;
				int begin = i;
				GetText(format, ref i, "", true);
				var escaped = format.Substring (begin, i - begin);
				return UnEscape (escaped);
			}
			return null;
		}

		void CheckForMissingEndBrace (string format, int i, int length)
		{
			if (i == length) {
				int j;
				for (j = i - 1; format[j] == '}'; j--);
				var oddEndBraceCount = (i - j) % 2 == 1;
				if (oddEndBraceCount) {
					AddMissingEndBraceError(i, i, "Missing '}'", "");
				}
				return;
			}
			return;
		}
		
		void GetText (string format, ref int index, string delimiters = "", bool allowEscape = false)
		{
			while (index < format.Length) {
				if (format [index] == '{' || format[index] == '}') {
					if (index + 1 < format.Length && format [index + 1] == format[index] && allowEscape)
						++index;
					else
						break;
				} else if (delimiters.Contains(format[index].ToString())) {
					break;
				}
				++index;
			};
		}
		
		int? GetNumber (string format, ref int index)
		{
			if (format.Length == 0) {
				return null;
			}
			int sum = 0;
			int i = index;
			bool positive = format [i] != '-';
			if (!positive)
				++i;
			int numberStartIndex = i;
			while (i < format.Length && format[i] >= '0' && format[i] <= '9') {
				sum = 10 * sum + format [i] - '0';
				++i;
			}
			if (i == numberStartIndex)
				return null;

			index = i;
			return positive ? sum : -sum;
		}

		int? GetAndCheckNumber (string format, string delimiters, ref int index, int numberFieldStart, out int parsedCharacters)
		{
			int fieldIndex = index;
			GetText (format, ref fieldIndex, delimiters);
			int fieldEnd = fieldIndex;
			var numberText = format.Substring(index, fieldEnd - index);
			parsedCharacters = numberText.Length;
			int numberLength = 0;
			int? number = GetNumber (numberText, ref numberLength);
			if (numberLength != parsedCharacters && fieldEnd < format.Length && delimiters.Contains (format [fieldEnd])) {
				// Not the entire number field could be parsed
				// The field actually ended as intended, so set the index to the end of the field
				index = fieldEnd;
				var suggestedNumber = (number ?? 0).ToString ();
				AddInvalidNumberFormatError (numberFieldStart, format.Substring (numberFieldStart, index - numberFieldStart), suggestedNumber);
			} else {
				var endingChar = index + numberLength;
				if (numberLength != parsedCharacters) {
					// Not the entire number field could be parsed
					// The field didn't end, it was cut off so we are missing an ending brace
					index = endingChar;
					AddMissingEndBraceError (index, index, "Missing ending '}'", "");
				} else {
					index = endingChar;
				}
			}
			return number;
		}

		public static string UnEscape (string unEscaped)
		{
			return unEscaped.Replace ("{{", "{").Replace ("}}", "}");
		}

		IList<IFormatStringError> errors;
		
		bool hasMissingEndBrace = false;

		void AddError (IFormatStringError error)
		{
			errors.Add (error);
		}

		void AddMissingEndBraceError(int start, int end, string message, string originalText)
		{
			// Only add a single missing end brace per format item
			if (hasMissingEndBrace)
				return;
			AddError (new DefaultFormatStringError {
				StartLocation = start,
				EndLocation = end,
				Message = message,
				OriginalText = originalText,
				SuggestedReplacementText = "}"
			});
			hasMissingEndBrace = true;
		}

		void AddInvalidNumberFormatError (int i, string number, string replacementText)
		{
			AddError (new DefaultFormatStringError {
				StartLocation = i,
				EndLocation = i + number.Length,
				Message = string.Format ("Invalid number '{0}'", number),
				OriginalText = number,
				SuggestedReplacementText = replacementText
			});
		}

		IList<IFormatStringError> GetErrors ()
		{
			return errors;
		}
		
		void SetErrors (IList<IFormatStringError> errors)
		{
			this.errors = errors;
		}

		void ClearErrors ()
		{
			hasMissingEndBrace = false;
			errors = new List<IFormatStringError> ();
		}
	}

	public class FormatStringParseResult
	{
		public FormatStringParseResult()
		{
			Segments = new List<IFormatStringSegment>();
		}

		public IList<IFormatStringSegment> Segments { get; private set; }

		public bool HasErrors
		{
			get {
				return Segments.SelectMany(segment => segment.Errors).Any();
			}
		}
	}
}