Browse Source

[Utils] More error handling in CompositeFormatStringParser.

newNRvisualizers
Simon Lindgren 13 years ago
parent
commit
7edf902fea
  1. 20
      ICSharpCode.NRefactory.Tests/Utils/CompositeFormatStringParser/CompositeFormatStringParserTests.cs
  2. 24
      ICSharpCode.NRefactory/Utils/CompositeFormatStringParser/CompositeFormatStringParser.cs

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

@ -244,6 +244,26 @@ namespace ICSharpCode.NRefactory.Utils @@ -244,6 +244,26 @@ namespace ICSharpCode.NRefactory.Utils
var errors = SegmentTest(1, segments.Skip(1).First());
ErrorTest(errors[0], "", "0", 11, 11);
}
[Test]
public void MissingAlignment()
{
var segments = ParseTest("Some text {0,}",
new TextSegment("Some text "),
new FormatItem(0, 0) { StartLocation = 10, EndLocation = 14 });
var errors = SegmentTest(1, segments.Skip(1).First());
ErrorTest(errors[0], "", "0", 13, 13);
}
[Test]
public void MissingEveryThing()
{
var segments = ParseTest("{,:", new FormatItem(0, 0, "") { StartLocation = 0, EndLocation = 3 });
var errors = SegmentTest(3, segments.First());
ErrorTest(errors[0], "", "0", 1, 1);
ErrorTest(errors[1], "", "0", 2, 2);
ErrorTest(errors[2], "", "}", 3, 3);
}
}
}

24
ICSharpCode.NRefactory/Utils/CompositeFormatStringParser/CompositeFormatStringParser.cs

@ -25,6 +25,7 @@ @@ -25,6 +25,7 @@
// THE SOFTWARE.
using System;
using System.Collections.Generic;
using System.Globalization;
namespace ICSharpCode.NRefactory.Utils
{
@ -151,11 +152,15 @@ namespace ICSharpCode.NRefactory.Utils @@ -151,11 +152,15 @@ namespace ICSharpCode.NRefactory.Utils
var originalText = format.Substring (alignmentBegin);
var message = string.Format ("Unexpected end of string: '{0}'", originalText);
AddMissingEndBraceError(alignmentBegin, i, message, originalText);
} else if (format [i] == '-') {
++i;
return -int.Parse (GetUntil (format, ":}", ref i));
} else {
return int.Parse (GetUntil (format, ":}", ref i));
var number = GetUntil (format, ":}", ref i);
int value;
if (int.TryParse(number, NumberStyles.Integer, CultureInfo.InvariantCulture, out value)) {
return value;
} else {
AddInvalidNumberFormatError(i, number, "0");
return 0;
}
}
}
return null;
@ -269,6 +274,17 @@ namespace ICSharpCode.NRefactory.Utils @@ -269,6 +274,17 @@ namespace ICSharpCode.NRefactory.Utils
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;

Loading…
Cancel
Save