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
IList<IFormatStringSegment> ParseTest(string format, params IFormatStringSegment[] expectedFormatSegments) IList<IFormatStringSegment> ParseTest(string format, params IFormatStringSegment[] expectedFormatSegments)
{ {
var parser = new CompositeFormatStringParser(); var parser = new CompositeFormatStringParser();
var actualFormatSegments = parser.Parse(format).ToList(); var actualFormatSegments = parser.Parse(format).Segments;
Console.WriteLine("Expected format segments:"); Console.WriteLine("Expected format segments:");
foreach (var item in expectedFormatSegments) { foreach (var item in expectedFormatSegments) {

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

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

Loading…
Cancel
Save