2
// CompositeFormatStringParser.cs
5
// Simon Lindgren <simon.n.lindgren@gmail.com>
7
// Copyright (c) 2012 Simon Lindgren
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
10
// of this software and associated documentation files (the "Software"), to deal
11
// in the Software without restriction, including without limitation the rights
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
// copies of the Software, and to permit persons to whom the Software is
14
// furnished to do so, subject to the following conditions:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27
using System.Collections.Generic;
30
namespace ICSharpCode.NRefactory.Utils
33
/// Composite format string parser.
36
/// Implements a complete parser for valid strings as well as
37
/// error reporting and best-effort parsing for invalid strings.
39
public class CompositeFormatStringParser
42
public CompositeFormatStringParser ()
44
errors = new List<IFormatStringError> ();
48
/// Parse the specified format string.
50
/// <param name='format'>
51
/// The format string.
53
public FormatStringParseResult Parse (string format)
56
throw new ArgumentNullException ("format");
58
var result = new FormatStringParseResult();
60
// Format string syntax: http://msdn.microsoft.com/en-us/library/txafckwd.aspx
62
var length = format.Length;
63
for (int i = 0; i < length; i++) {
65
GetText (format, ref i);
67
if (i < format.Length && format [i] == '{') {
68
int formatItemStart = i;
71
string argumentFormat;
72
var textSegmentErrors = new List<IFormatStringError>(GetErrors());
74
// Try to parse the parts of the format item
76
index = ParseIndex (format, ref i);
77
CheckForMissingEndBrace (format, i, length);
79
alignment = ParseAlignment (format, ref i, length);
80
CheckForMissingEndBrace (format, i, length);
82
argumentFormat = ParseSubFormatString (format, ref i, length);
83
CheckForMissingEndBrace (format, i, length);
85
// Check what we parsed
86
if (i == formatItemStart + 1 && (i == length || (i < length && format[i] != '}'))) {
87
// There were no format item after all, this was just an
88
// unescaped left brace, or the initial brace of an escape sequence
89
SetErrors(textSegmentErrors);
90
if (i >= length || format[i] != '{') {
91
AddError (new DefaultFormatStringError {
92
Message = "Unescaped '{'",
93
StartLocation = formatItemStart,
94
EndLocation = formatItemStart + 1,
96
SuggestedReplacementText = "{{"
102
if (formatItemStart - textStart > 0) {
103
// We have parsed a format item, end the text segment
104
var textSegment = new TextSegment (UnEscape (format.Substring (textStart, formatItemStart - textStart)));
105
textSegment.Errors = textSegmentErrors;
106
result.Segments.Add (textSegment);
109
// Unclosed format items in fixed text advances i one step too far
110
if (i < length && format [i] != '}')
113
// i may actually point outside of format if there is a syntactical error
114
// if that happens, we want the last position
115
var endLocation = Math.Min (length, i + 1);
116
result.Segments.Add (new FormatItem (index, alignment, argumentFormat) {
117
StartLocation = formatItemStart,
118
EndLocation = endLocation,
119
Errors = GetErrors ()
123
// The next potential text segment starts after this format item
127
// Handle remaining text
128
if (textStart < length) {
129
var textSegment = new TextSegment (UnEscape (format.Substring (textStart)), textStart);
130
textSegment.Errors = GetErrors();
131
result.Segments.Add (textSegment);
137
int ParseIndex (string format, ref int i)
139
int parsedCharacters;
140
int? maybeIndex = GetAndCheckNumber (format, ",:}", ref i, i, out parsedCharacters);
141
if (parsedCharacters == 0) {
142
AddError (new DefaultFormatStringError {
145
Message = "Missing index",
147
SuggestedReplacementText = "0"
150
return maybeIndex ?? 0;
153
int? ParseAlignment(string format, ref int i, int length)
155
if (i < length && format [i] == ',') {
156
int alignmentBegin = i;
158
while (i < length && char.IsWhiteSpace(format [i]))
161
int parsedCharacters;
162
var number = GetAndCheckNumber (format, ",:}", ref i, alignmentBegin + 1, out parsedCharacters);
163
if (parsedCharacters == 0) {
164
AddError (new DefaultFormatStringError {
167
Message = "Missing alignment",
169
SuggestedReplacementText = "0"
177
string ParseSubFormatString(string format, ref int i, int length)
179
if (i < length && format [i] == ':') {
182
GetText(format, ref i, "", true);
183
var escaped = format.Substring (begin, i - begin);
184
return UnEscape (escaped);
189
void CheckForMissingEndBrace (string format, int i, int length)
193
for (j = i - 1; format[j] == '}'; j--);
194
var oddEndBraceCount = (i - j) % 2 == 1;
195
if (oddEndBraceCount) {
196
AddMissingEndBraceError(i, i, "Missing '}'", "");
203
void GetText (string format, ref int index, string delimiters = "", bool allowEscape = false)
205
while (index < format.Length) {
206
if (format [index] == '{' || format[index] == '}') {
207
if (index + 1 < format.Length && format [index + 1] == format[index] && allowEscape)
211
} else if (delimiters.Contains(format[index].ToString())) {
218
int? GetNumber (string format, ref int index)
220
if (format.Length == 0) {
225
bool positive = format [i] != '-';
228
int numberStartIndex = i;
229
while (i < format.Length && format[i] >= '0' && format[i] <= '9') {
230
sum = 10 * sum + format [i] - '0';
233
if (i == numberStartIndex)
237
return positive ? sum : -sum;
240
int? GetAndCheckNumber (string format, string delimiters, ref int index, int numberFieldStart, out int parsedCharacters)
242
int fieldIndex = index;
243
GetText (format, ref fieldIndex, delimiters);
244
int fieldEnd = fieldIndex;
245
var numberText = format.Substring(index, fieldEnd - index);
246
parsedCharacters = numberText.Length;
247
int numberLength = 0;
248
int? number = GetNumber (numberText, ref numberLength);
249
if (numberLength != parsedCharacters && fieldEnd < format.Length && delimiters.Contains (format [fieldEnd])) {
250
// Not the entire number field could be parsed
251
// The field actually ended as intended, so set the index to the end of the field
253
var suggestedNumber = (number ?? 0).ToString ();
254
AddInvalidNumberFormatError (numberFieldStart, format.Substring (numberFieldStart, index - numberFieldStart), suggestedNumber);
256
var endingChar = index + numberLength;
257
if (numberLength != parsedCharacters) {
258
// Not the entire number field could be parsed
259
// The field didn't end, it was cut off so we are missing an ending brace
261
AddMissingEndBraceError (index, index, "Missing ending '}'", "");
269
public static string UnEscape (string unEscaped)
271
return unEscaped.Replace ("{{", "{").Replace ("}}", "}");
274
IList<IFormatStringError> errors;
276
bool hasMissingEndBrace = false;
278
void AddError (IFormatStringError error)
283
void AddMissingEndBraceError(int start, int end, string message, string originalText)
285
// Only add a single missing end brace per format item
286
if (hasMissingEndBrace)
288
AddError (new DefaultFormatStringError {
289
StartLocation = start,
292
OriginalText = originalText,
293
SuggestedReplacementText = "}"
295
hasMissingEndBrace = true;
298
void AddInvalidNumberFormatError (int i, string number, string replacementText)
300
AddError (new DefaultFormatStringError {
302
EndLocation = i + number.Length,
303
Message = string.Format ("Invalid number '{0}'", number),
304
OriginalText = number,
305
SuggestedReplacementText = replacementText
309
IList<IFormatStringError> GetErrors ()
314
void SetErrors (IList<IFormatStringError> errors)
316
this.errors = errors;
321
hasMissingEndBrace = false;
322
errors = new List<IFormatStringError> ();
326
public class FormatStringParseResult
328
public FormatStringParseResult()
330
Segments = new List<IFormatStringSegment>();
333
public IList<IFormatStringSegment> Segments { get; private set; }
335
public bool HasErrors
338
return Segments.SelectMany(segment => segment.Errors).Any();