Browse Source

[Utils] Make CompositeFormatStringParser.Parse() wrap the segments in a class instead of just returning an IEnumerable.

newNRvisualizers
Simon Lindgren 13 years ago
parent
commit
d2474e5305
  1. 2
      ICSharpCode.NRefactory.Tests/Utils/CompositeFormatStringParser/CompositeFormatStringParserTests.cs
  2. 35
      ICSharpCode.NRefactory/Utils/CompositeFormatStringParser/CompositeFormatStringParser.cs
  3. 2
      ICSharpCode.NRefactory/Utils/CompositeFormatStringParser/IFormatStringError.cs

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

@ -37,7 +37,7 @@ namespace ICSharpCode.NRefactory.Utils @@ -37,7 +37,7 @@ namespace ICSharpCode.NRefactory.Utils
IList<IFormatStringSegment> ParseTest(string format, params IFormatStringSegment[] expectedFormatSegments)
{
var parser = new CompositeFormatStringParser();
var actualFormatSegments = parser.Parse(format).ToList();
var actualFormatSegments = parser.Parse(format).Segments;
Console.WriteLine("Expected format segments:");
foreach (var item in expectedFormatSegments) {

35
ICSharpCode.NRefactory/Utils/CompositeFormatStringParser/CompositeFormatStringParser.cs

@ -26,6 +26,7 @@ @@ -26,6 +26,7 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
namespace ICSharpCode.NRefactory.Utils
{
@ -50,11 +51,13 @@ namespace ICSharpCode.NRefactory.Utils @@ -50,11 +51,13 @@ namespace ICSharpCode.NRefactory.Utils
/// <param name='format'>
/// The format string.
/// </param>
public IEnumerable<IFormatStringSegment> Parse (string format)
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 start = 0;
var length = format.Length;
@ -73,8 +76,8 @@ namespace ICSharpCode.NRefactory.Utils @@ -73,8 +76,8 @@ namespace ICSharpCode.NRefactory.Utils
}
}
};
yield return textSegment;
yield break;
result.Segments.Add(textSegment);
return result;
} else if (format [i + 1] == '{') {
// Escape sequence; we're still in a text segment
// Skip ahead to the char after the escape sequence
@ -83,7 +86,7 @@ namespace ICSharpCode.NRefactory.Utils @@ -83,7 +86,7 @@ namespace ICSharpCode.NRefactory.Utils
} else {
// This is the end of the text segment and the start of a FormatItem
if (i - start > 0) {
yield return new TextSegment (UnEscape (format.Substring (start, i - start)));
result.Segments.Add(new TextSegment (UnEscape (format.Substring (start, i - start))));
start = i;
}
}
@ -108,11 +111,11 @@ namespace ICSharpCode.NRefactory.Utils @@ -108,11 +111,11 @@ namespace ICSharpCode.NRefactory.Utils
// i may actually point outside of format; if that happens, we want the last position
var endLocation = Math.Min (length, i + 1);
var errors = GetErrors ();
yield return new FormatItem (index, alignment, argumentFormat) {
result.Segments.Add(new FormatItem (index, alignment, argumentFormat) {
StartLocation = start,
EndLocation = endLocation,
Errors = errors
};
});
ClearErrors ();
// The next potential text segment starts after this format item
@ -121,8 +124,9 @@ namespace ICSharpCode.NRefactory.Utils @@ -121,8 +124,9 @@ namespace ICSharpCode.NRefactory.Utils
}
// Handle remaining text
if (start < length) {
yield return new TextSegment (UnEscape (format.Substring (start)), start);
result.Segments.Add(new TextSegment (UnEscape (format.Substring (start)), start));
}
return result;
}
int ParseIndex (string format, ref int i)
@ -301,5 +305,22 @@ namespace ICSharpCode.NRefactory.Utils @@ -301,5 +305,22 @@ namespace ICSharpCode.NRefactory.Utils
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();
}
}
}
}

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

@ -39,7 +39,7 @@ namespace ICSharpCode.NRefactory.Utils @@ -39,7 +39,7 @@ namespace ICSharpCode.NRefactory.Utils
string SuggestedReplacementText { get; }
}
class DefaultFormatStringError : IFormatStringError
public class DefaultFormatStringError : IFormatStringError
{
public DefaultFormatStringError()
{

Loading…
Cancel
Save