Skip to content

Commit 64550cb

Browse files
committed
Rewrite some extensions as blocks
1 parent ad1b9d5 commit 64550cb

File tree

5 files changed

+124
-103
lines changed

5 files changed

+124
-103
lines changed

eg/SourceGenerator/Extensions.cs

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,38 @@
66

77
static class ParseResultExtensions
88
{
9-
public static int Run<T>(this IHelpFeaturingParser<T> parser, IEnumerable<string> args, Func<T, int> runner) =>
10-
parser.Run(args, null, null, 0, runner);
9+
extension<T>(IHelpFeaturingParser<T> parser)
10+
{
11+
public int Run(IEnumerable<string> args, Func<T, int> runner) =>
12+
parser.Run(args, null, null, 0, runner);
1113

12-
public static int Run<T>(this IHelpFeaturingParser<T> parser, IEnumerable<string> args,
13-
TextWriter? stdout, TextWriter? stderr, int errorExitCode,
14-
Func<T, int> runner) =>
15-
Run(parser.Parse(args), stdout, stderr, errorExitCode, runner);
14+
public int Run(IEnumerable<string> args,
15+
TextWriter? stdout, TextWriter? stderr, int errorExitCode,
16+
Func<T, int> runner) =>
17+
Run(parser.Parse(args), stdout, stderr, errorExitCode, runner);
18+
}
1619

17-
public static int Run<T>(this IParser<T> parser, IEnumerable<string> args, Func<T, int> runner) =>
18-
parser.Run(args, null, null, 0, runner);
20+
extension<T>(IParser<T> parser)
21+
{
22+
public int Run(IEnumerable<string> args, Func<T, int> runner) =>
23+
parser.Run(args, null, null, 0, runner);
1924

20-
public static int Run<T>(this IParser<T> parser, IEnumerable<string> args,
21-
TextWriter? stdout, TextWriter? stderr, int errorExitCode,
22-
Func<T, int> runner) =>
23-
Run(parser.Parse(args), stdout, stderr, errorExitCode, runner);
25+
public int Run(IEnumerable<string> args,
26+
TextWriter? stdout, TextWriter? stderr, int errorExitCode,
27+
Func<T, int> runner) =>
28+
Run(parser.Parse(args), stdout, stderr, errorExitCode, runner);
29+
}
2430

25-
public static int Run<T>(this IBaselineParser<T> parser, IEnumerable<string> args, Func<T, int> runner) =>
26-
parser.Run(args, null, 0, runner);
31+
extension<T>(IBaselineParser<T> parser)
32+
{
33+
public int Run(IEnumerable<string> args, Func<T, int> runner) =>
34+
parser.Run(args, null, 0, runner);
2735

28-
public static int Run<T>(this IBaselineParser<T> parser, IEnumerable<string> args,
29-
TextWriter? stderr, int errorExitCode,
30-
Func<T, int> runner) =>
31-
Run(parser.Parse(args), null, stderr, errorExitCode, runner);
36+
public int Run(IEnumerable<string> args,
37+
TextWriter? stderr, int errorExitCode,
38+
Func<T, int> runner) =>
39+
Run(parser.Parse(args), null, stderr, errorExitCode, runner);
40+
}
3241

3342
static int Run<T>(object result,
3443
TextWriter? stdout, TextWriter? stderr, int errorExitCode,

src/DocoptNet/ApplicationResult.cs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,23 @@ public TResult Accumulate<TState, TResult>(TState initialState,
3434

3535
static class ApplicationResultExtensions
3636
{
37-
internal static IDictionary<string, ValueObject> ToValueObjectDictionary(this ApplicationResult result)
37+
extension(ApplicationResult result)
3838
{
39-
if (result is null) throw new ArgumentNullException(nameof(result));
39+
internal IDictionary<string, ValueObject> ToValueObjectDictionary()
40+
{
41+
if (result is null) throw new ArgumentNullException(nameof(result));
4042

41-
return result.Accumulate(new Dictionary<string, ValueObject>(),
42-
ApplicationResultAccumulators.ValueObjectDictionary);
43-
}
43+
return result.Accumulate(new Dictionary<string, ValueObject>(),
44+
ApplicationResultAccumulators.ValueObjectDictionary);
45+
}
4446

45-
internal static IDictionary<string, ArgValue> ToValueDictionary(this ApplicationResult result)
46-
{
47-
if (result is null) throw new ArgumentNullException(nameof(result));
47+
internal IDictionary<string, ArgValue> ToValueDictionary()
48+
{
49+
if (result is null) throw new ArgumentNullException(nameof(result));
4850

49-
return result.Accumulate(new Dictionary<string, ArgValue>(),
50-
ApplicationResultAccumulators.ValueDictionary);
51+
return result.Accumulate(new Dictionary<string, ArgValue>(),
52+
ApplicationResultAccumulators.ValueDictionary);
53+
}
5154
}
5255
}
5356
}

src/DocoptNet/CodeGeneration/Extensions.cs

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,20 @@ public static void LaunchDebuggerIfFlagged(this GeneratorExecutionContext contex
4747
string generatorName) =>
4848
context.AnalyzerConfigOptions.GlobalOptions.LaunchDebuggerIfFlagged(generatorName);
4949

50-
public static void LaunchDebuggerIfFlagged(this AnalyzerConfigOptions options,
51-
string generatorName)
50+
extension(AnalyzerConfigOptions options)
5251
{
53-
if (options.IsFlagged("build_property.DebugSourceGenerators") ||
54-
options.IsFlagged("build_property.Debug" + generatorName))
52+
public void LaunchDebuggerIfFlagged(string generatorName)
5553
{
56-
Debugger.Launch();
54+
if (options.IsFlagged("build_property.DebugSourceGenerators") ||
55+
options.IsFlagged("build_property.Debug" + generatorName))
56+
{
57+
Debugger.Launch();
58+
}
5759
}
58-
}
5960

60-
public static bool IsFlagged(this AnalyzerConfigOptions options, string name) =>
61-
options.TryGetValue(name, out var s) && bool.TryParse(s, out var flag) && flag;
61+
public bool IsFlagged(string name) =>
62+
options.TryGetValue(name, out var s) && bool.TryParse(s, out var flag) && flag;
63+
}
6264
}
6365
}
6466

@@ -99,35 +101,38 @@ namespace DocoptNet.CodeGeneration
99101

100102
partial class Extensions
101103
{
102-
public static string EncodeXmlText(this string str) => str.EncodeXmlText(attribute: false);
103-
public static string EncodeXmlAttributeValue(this string str) => str.EncodeXmlText(attribute: true);
104-
105-
static string EncodeXmlText(this string str, bool attribute)
104+
extension(string str)
106105
{
107-
StringBuilder? sb = null;
106+
public string EncodeXmlText() => str.EncodeXmlText(attribute: false);
107+
public string EncodeXmlAttributeValue() => str.EncodeXmlText(attribute: true);
108108

109-
StringBuilder StringBuilder() => sb ??= new StringBuilder(str.Length);
110-
void Append(string s) => StringBuilder().Append(s);
111-
void AppendChar(char ch) => StringBuilder().Append(ch);
112-
113-
foreach (var ch in str)
109+
string EncodeXmlText(bool attribute)
114110
{
115-
switch (attribute, ch)
111+
StringBuilder? sb = null;
112+
113+
StringBuilder StringBuilder() => sb ??= new StringBuilder(str.Length);
114+
void Append(string s) => StringBuilder().Append(s);
115+
void AppendChar(char ch) => StringBuilder().Append(ch);
116+
117+
foreach (var ch in str)
116118
{
117-
case (_, '<') : Append("&lt;"); break;
118-
case (_, '>') : Append("&gt;"); break;
119-
case (_, '&') : Append("&amp;"); break;
120-
case (true, '"') : Append("&quot;"); break;
121-
case (true, '\''): Append("&apos;"); break;
122-
case (true, '\n'): Append("&#xA;"); break;
123-
case (true, '\r'): Append("&#xD;"); break;
124-
case (true, '\t'): Append("&#x9;"); break;
125-
case (_, < ' ') : throw new ArgumentException($"Character U+{(int)ch:X4} is forbidden in XML.", nameof(str));
126-
default : AppendChar(ch); break;
119+
switch (attribute, ch)
120+
{
121+
case (_, '<') : Append("&lt;"); break;
122+
case (_, '>') : Append("&gt;"); break;
123+
case (_, '&') : Append("&amp;"); break;
124+
case (true, '"') : Append("&quot;"); break;
125+
case (true, '\''): Append("&apos;"); break;
126+
case (true, '\n'): Append("&#xA;"); break;
127+
case (true, '\r'): Append("&#xD;"); break;
128+
case (true, '\t'): Append("&#x9;"); break;
129+
case (_, < ' ') : throw new ArgumentException($"Character U+{(int)ch:X4} is forbidden in XML.", nameof(str));
130+
default : AppendChar(ch); break;
131+
}
127132
}
128-
}
129133

130-
return sb?.ToString() ?? str;
134+
return sb?.ToString() ?? str;
135+
}
131136
}
132137
}
133138
}

src/DocoptNet/Internals/PatternMatcher.cs

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -207,43 +207,46 @@ public bool Fold(MatchResult match)
207207

208208
static partial class PatternMatcher
209209
{
210-
public static MatchResult Match(this Pattern pattern, Leaves left)
210+
extension(Pattern pattern)
211211
{
212-
return pattern.Match(left, new Leaves());
213-
}
212+
public MatchResult Match(Leaves left)
213+
{
214+
return pattern.Match(left, new Leaves());
215+
}
214216

215-
public static MatchResult Match(this Pattern pattern, Leaves left, Leaves collected)
216-
{
217-
switch (pattern)
217+
public MatchResult Match(Leaves left, Leaves collected)
218218
{
219-
case Required { Children: { Count: var count } children }: return MatchBranch(children, new RequiredMatcher(count, left, collected));
220-
case Either { Children: { Count: var count } children }: return MatchBranch(children, new EitherMatcher(count, left, collected));
221-
case Optional { Children: { Count: var count } children }: return MatchBranch(children, new OptionalMatcher(count, left, collected));
222-
case OneOrMore { Children: var children }: return MatchBranch(children, new OneOrMoreMatcher(1, left, collected));
223-
case LeafPattern leaf:
219+
switch (pattern)
224220
{
225-
var matcher = leaf switch
221+
case Required { Children: { Count: var count } children }: return MatchBranch(children, new RequiredMatcher(count, left, collected));
222+
case Either { Children: { Count: var count } children }: return MatchBranch(children, new EitherMatcher(count, left, collected));
223+
case Optional { Children: { Count: var count } children }: return MatchBranch(children, new OptionalMatcher(count, left, collected));
224+
case OneOrMore { Children: var children }: return MatchBranch(children, new OneOrMoreMatcher(1, left, collected));
225+
case LeafPattern leaf:
226226
{
227-
Command => CommandMatcher,
228-
Argument => ArgumentMatcher,
229-
Option => OptionMatcher,
230-
_ => throw new NotSupportedException()
231-
};
232-
233-
return matcher.Match(left, collected, leaf.Name, leaf.Value.Kind);
227+
var matcher = leaf switch
228+
{
229+
Command => CommandMatcher,
230+
Argument => ArgumentMatcher,
231+
Option => OptionMatcher,
232+
_ => throw new NotSupportedException()
233+
};
234+
235+
return matcher.Match(left, collected, leaf.Name, leaf.Value.Kind);
236+
}
237+
default:
238+
throw new ArgumentException(nameof(pattern));
234239
}
235-
default:
236-
throw new ArgumentException(nameof(pattern));
237-
}
238240

239-
static MatchResult MatchBranch<T>(IList<Pattern> children, T matcher) where T : IBranchPatternMatcher
240-
{
241-
while (matcher.Next())
241+
static MatchResult MatchBranch<T>(IList<Pattern> children, T matcher) where T : IBranchPatternMatcher
242242
{
243-
if (!matcher.Match(children[matcher.Index]))
244-
break;
243+
while (matcher.Next())
244+
{
245+
if (!matcher.Match(children[matcher.Index]))
246+
break;
247+
}
248+
return matcher.Result;
245249
}
246-
return matcher.Result;
247250
}
248251
}
249252

tests/DocoptNet.Tests/Extensions.cs

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,25 @@ public static MatchResult Match(this Pattern pattern, params LeafPattern[] left)
1212
return PatternMatcher.Match(pattern, new ReadOnlyList<LeafPattern>(left));
1313
}
1414

15-
public static IDictionary<string, ArgValue>? Apply(this Docopt docopt,
16-
string doc, string cmdLine,
17-
bool help = true,
18-
object? version = null,
19-
bool optionsFirst = false,
20-
bool exit = false)
15+
extension(Docopt docopt)
2116
{
22-
return docopt.Apply(doc, Args.Parse(cmdLine), help, version, optionsFirst, exit);
23-
}
17+
public IDictionary<string, ArgValue>? Apply(string doc, string cmdLine,
18+
bool help = true,
19+
object? version = null,
20+
bool optionsFirst = false,
21+
bool exit = false)
22+
{
23+
return docopt.Apply(doc, Args.Parse(cmdLine), help, version, optionsFirst, exit);
24+
}
2425

25-
public static IDictionary<string, ArgValue>? Apply(this Docopt docopt,
26-
string doc, Args argv,
27-
bool help = true,
28-
object? version = null,
29-
bool optionsFirst = false,
30-
bool exit = false)
31-
{
32-
return Docopt.Internal.Apply(docopt, doc, argv.List.AsEnumerable(), help, version, optionsFirst, exit);
26+
public IDictionary<string, ArgValue>? Apply(string doc, Args argv,
27+
bool help = true,
28+
object? version = null,
29+
bool optionsFirst = false,
30+
bool exit = false)
31+
{
32+
return Docopt.Internal.Apply(docopt, doc, argv.List.AsEnumerable(), help, version, optionsFirst, exit);
33+
}
3334
}
3435
}
3536

0 commit comments

Comments
 (0)