-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix source generator to support nested arguments class (#174)
Fixes #173.
- Loading branch information
Showing
8 changed files
with
346 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
...on/SourceGeneratorTests/Generate_with_nested_args_class/MyConsoleApp.Program-Arguments.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
#nullable enable annotations | ||
|
||
using System.Collections; | ||
using System.Collections.Generic; | ||
using DocoptNet; | ||
using DocoptNet.Internals; | ||
using Leaves = DocoptNet.Internals.ReadOnlyList<DocoptNet.Internals.LeafPattern>; | ||
|
||
namespace MyConsoleApp | ||
{ | ||
partial class Program | ||
{ | ||
partial class Arguments : IEnumerable<KeyValuePair<string, object?>> | ||
{ | ||
public const string Usage = "Usage: program"; | ||
|
||
static readonly IBaselineParser<Arguments> Parser = GeneratedSourceModule.CreateParser(Help, Parse); | ||
|
||
public static IBaselineParser<Arguments> CreateParser() => Parser; | ||
|
||
static IParser<Arguments>.IResult Parse(IEnumerable<string> args, ParseFlags flags, string? version) | ||
{ | ||
var options = new List<Option> | ||
{ | ||
}; | ||
|
||
return GeneratedSourceModule.Parse(Help, Usage, args, options, flags, version, Parse); | ||
|
||
static IParser<Arguments>.IResult Parse(Leaves left) | ||
{ | ||
var required = new RequiredMatcher(1, left, new Leaves()); | ||
Match(ref required); | ||
if (!required.Result || required.Left.Count > 0) | ||
{ | ||
return GeneratedSourceModule.CreateInputErrorResult<Arguments>(string.Empty, Usage); | ||
} | ||
var collected = required.Collected; | ||
var result = new Arguments(); | ||
|
||
return GeneratedSourceModule.CreateArgumentsResult(result); | ||
} | ||
|
||
static void Match(ref RequiredMatcher required) | ||
{ | ||
// Required(Required()) | ||
var a = new RequiredMatcher(1, required.Left, required.Collected); | ||
while (a.Next()) | ||
{ | ||
// Required() | ||
var b = new RequiredMatcher(0, a.Left, a.Collected); | ||
while (b.Next()) | ||
{ | ||
if (!b.LastMatched) | ||
{ | ||
break; | ||
} | ||
} | ||
a.Fold(b.Result); | ||
if (!a.LastMatched) | ||
{ | ||
break; | ||
} | ||
} | ||
required.Fold(a.Result); | ||
} | ||
} | ||
|
||
IEnumerator<KeyValuePair<string, object?>> GetEnumerator() | ||
{ | ||
yield break; | ||
} | ||
|
||
IEnumerator<KeyValuePair<string, object?>> IEnumerable<KeyValuePair<string, object?>>.GetEnumerator() => GetEnumerator(); | ||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); | ||
} | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
.../CodeGeneration/SourceGeneratorTests/Generate_with_nested_args_class/Program-Arguments.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
#nullable enable annotations | ||
|
||
using System.Collections; | ||
using System.Collections.Generic; | ||
using DocoptNet; | ||
using DocoptNet.Internals; | ||
using Leaves = DocoptNet.Internals.ReadOnlyList<DocoptNet.Internals.LeafPattern>; | ||
|
||
partial class Program | ||
{ | ||
partial class Arguments : IEnumerable<KeyValuePair<string, object?>> | ||
{ | ||
public const string Usage = "Usage: program"; | ||
|
||
static readonly IBaselineParser<Arguments> Parser = GeneratedSourceModule.CreateParser(Help, Parse); | ||
|
||
public static IBaselineParser<Arguments> CreateParser() => Parser; | ||
|
||
static IParser<Arguments>.IResult Parse(IEnumerable<string> args, ParseFlags flags, string? version) | ||
{ | ||
var options = new List<Option> | ||
{ | ||
}; | ||
|
||
return GeneratedSourceModule.Parse(Help, Usage, args, options, flags, version, Parse); | ||
|
||
static IParser<Arguments>.IResult Parse(Leaves left) | ||
{ | ||
var required = new RequiredMatcher(1, left, new Leaves()); | ||
Match(ref required); | ||
if (!required.Result || required.Left.Count > 0) | ||
{ | ||
return GeneratedSourceModule.CreateInputErrorResult<Arguments>(string.Empty, Usage); | ||
} | ||
var collected = required.Collected; | ||
var result = new Arguments(); | ||
|
||
return GeneratedSourceModule.CreateArgumentsResult(result); | ||
} | ||
|
||
static void Match(ref RequiredMatcher required) | ||
{ | ||
// Required(Required()) | ||
var a = new RequiredMatcher(1, required.Left, required.Collected); | ||
while (a.Next()) | ||
{ | ||
// Required() | ||
var b = new RequiredMatcher(0, a.Left, a.Collected); | ||
while (b.Next()) | ||
{ | ||
if (!b.LastMatched) | ||
{ | ||
break; | ||
} | ||
} | ||
a.Fold(b.Result); | ||
if (!a.LastMatched) | ||
{ | ||
break; | ||
} | ||
} | ||
required.Fold(a.Result); | ||
} | ||
} | ||
|
||
IEnumerator<KeyValuePair<string, object?>> GetEnumerator() | ||
{ | ||
yield break; | ||
} | ||
|
||
IEnumerator<KeyValuePair<string, object?>> IEnumerable<KeyValuePair<string, object?>>.GetEnumerator() => GetEnumerator(); | ||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); | ||
} | ||
} |
Oops, something went wrong.