Browse Source

[Utils] Add more error checks to CompositeFormatStringParser.

newNRvisualizers
Simon Lindgren 13 years ago
parent
commit
185779b2b5
  1. 39
      ICSharpCode.NRefactory.Tests/Utils/CompositeFormatStringParser/CompositeFormatStringParserTests.cs
  2. 56
      ICSharpCode.NRefactory/Utils/CompositeFormatStringParser/CompositeFormatStringParser.cs
  3. 5
      ICSharpCode.NRefactory/Utils/CompositeFormatStringParser/IFormatStringError.cs

39
ICSharpCode.NRefactory.Tests/Utils/CompositeFormatStringParser/CompositeFormatStringParserTests.cs

@ -46,6 +46,9 @@ namespace ICSharpCode.NRefactory.Utils
Console.WriteLine("Actual format segments:"); Console.WriteLine("Actual format segments:");
foreach (var item in actualFormatSegments) { foreach (var item in actualFormatSegments) {
Console.WriteLine(item.ToString()); Console.WriteLine(item.ToString());
foreach (var error in item.Errors) {
Console.WriteLine("\t{0}", error);
}
} }
Assert.AreEqual(expectedFormatSegments, actualFormatSegments); Assert.AreEqual(expectedFormatSegments, actualFormatSegments);
@ -264,6 +267,42 @@ namespace ICSharpCode.NRefactory.Utils
ErrorTest(errors[1], "", "0", 2, 2); ErrorTest(errors[1], "", "0", 2, 2);
ErrorTest(errors[2], "", "}", 3, 3); ErrorTest(errors[2], "", "}", 3, 3);
} }
[Test]
public void InvalidNumberFormatInIndex()
{
var segments = ParseTest("{0 and then some invalid text}",
new FormatItem(0) { StartLocation = 0, EndLocation = 30 });
var errors = SegmentTest(1, segments.First());
ErrorTest(errors[0], "0 and then some invalid text", "0", 1, 29);
}
[Test]
public void InvalidNumberFormatTextBeforeDigitsInIndex()
{
var segments = ParseTest("{Some text 55}",
new FormatItem(0) { StartLocation = 0, EndLocation = 14 });
var errors = SegmentTest(1, segments.First());
ErrorTest(errors[0], "Some text 55", "0", 1, 13);
}
[Test]
public void InvalidNumberFormatInAlignment()
{
var segments = ParseTest("{0, 100 and then some invalid text}",
new FormatItem(0, 100) { StartLocation = 0, EndLocation = 35 });
var errors = SegmentTest(1, segments.First());
ErrorTest(errors[0], " 100 and then some invalid text", "100", 3, 34);
}
[Test]
public void InvalidNumberFormatTextBeforeDigitsInAlignment()
{
var segments = ParseTest("{0, Some text 55}",
new FormatItem(0, 0) { StartLocation = 0, EndLocation = 17 });
var errors = SegmentTest(1, segments.First());
ErrorTest(errors[0], " Some text 55", "0", 3, 16);
}
} }
} }

56
ICSharpCode.NRefactory/Utils/CompositeFormatStringParser/CompositeFormatStringParser.cs

@ -127,18 +127,18 @@ namespace ICSharpCode.NRefactory.Utils
int ParseIndex (string format, ref int i) int ParseIndex (string format, ref int i)
{ {
int? maybeIndex = GetNumber (format, ref i); int parsedCharacters;
if (maybeIndex.HasValue) { int? maybeIndex = GetAndCheckNumber (format, ",:}", ref i, i, out parsedCharacters);
return maybeIndex.Value; if (parsedCharacters == 0) {
AddError (new DefaultFormatStringError {
StartLocation = i,
EndLocation = i,
Message = "Missing index",
OriginalText = "",
SuggestedReplacementText = "0"
});
} }
AddError (new DefaultFormatStringError { return maybeIndex ?? 0;
StartLocation = i,
EndLocation = i,
Message = "Missing index",
OriginalText = "",
SuggestedReplacementText = "0"
});
return 0;
} }
int? ParseAlignment(string format, ref int i, int length) int? ParseAlignment(string format, ref int i, int length)
@ -153,13 +153,18 @@ namespace ICSharpCode.NRefactory.Utils
var message = string.Format ("Unexpected end of string: '{0}'", originalText); var message = string.Format ("Unexpected end of string: '{0}'", originalText);
AddMissingEndBraceError(alignmentBegin, i, message, originalText); AddMissingEndBraceError(alignmentBegin, i, message, originalText);
} else { } else {
int? number = GetNumber(format, ref i); int parsedCharacters;
if (number.HasValue) { var number = GetAndCheckNumber(format, ",:}", ref i, alignmentBegin + 1, out parsedCharacters);
return number; if (parsedCharacters == 0) {
} else { AddError (new DefaultFormatStringError {
AddInvalidNumberFormatError(i, "", "0"); StartLocation = i,
return 0; EndLocation = i,
Message = "Missing alignment",
OriginalText = "",
SuggestedReplacementText = "0"
});
} }
return number ?? 0;
} }
} }
return null; return null;
@ -211,6 +216,9 @@ namespace ICSharpCode.NRefactory.Utils
int? GetNumber (string format, ref int index) int? GetNumber (string format, ref int index)
{ {
if (format.Length == 0) {
return null;
}
int sum = 0; int sum = 0;
int i = index; int i = index;
bool positive = format [i] != '-'; bool positive = format [i] != '-';
@ -228,6 +236,20 @@ namespace ICSharpCode.NRefactory.Utils
return positive ? sum : -sum; return positive ? sum : -sum;
} }
int? GetAndCheckNumber(string format, string delimiters, ref int index, int numberFieldStart, out int parsedCharacters)
{
var numberText = GetUntil(format, delimiters, ref index);
parsedCharacters = numberText.Length;
int digitCount = 0;
int? number = GetNumber(numberText, ref digitCount);
if (digitCount != parsedCharacters) {
// Not the entire number field could be parsed
var suggestedNumber = (number ?? 0).ToString ();
AddInvalidNumberFormatError(numberFieldStart, format.Substring(numberFieldStart, index - numberFieldStart), suggestedNumber);
}
return number;
}
public static string UnEscape (string unEscaped) public static string UnEscape (string unEscaped)
{ {
return unEscaped.Replace ("{{", "{").Replace ("}}", "}"); return unEscaped.Replace ("{{", "{").Replace ("}}", "}");

5
ICSharpCode.NRefactory/Utils/CompositeFormatStringParser/IFormatStringError.cs

@ -59,5 +59,10 @@ namespace ICSharpCode.NRefactory.Utils
public string SuggestedReplacementText { get; set; } public string SuggestedReplacementText { get; set; }
#endregion #endregion
public override string ToString ()
{
return string.Format ("[DefaultFormatStringError: StartLocation={0}, EndLocation={1}, Message={2}, OriginalText={3}, SuggestedReplacementText={4}]", StartLocation, EndLocation, Message, OriginalText, SuggestedReplacementText);
}
} }
} }

Loading…
Cancel
Save