forked from SharpGenTools/SharpGenTools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStatementSyntaxList.cs
More file actions
110 lines (97 loc) · 4.44 KB
/
Copy pathStatementSyntaxList.cs
File metadata and controls
110 lines (97 loc) · 4.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#nullable enable
#if !SHARPGEN_ROSLYN
using SharpGen.Model;
#endif
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
namespace SharpGen.Generator;
public sealed class StatementSyntaxList : SyntaxListBase<StatementSyntax, StatementSyntaxList>
{
#if !SHARPGEN_ROSLYN
public StatementSyntaxList(Ioc? ioc = null) : base(ioc)
{
}
protected override StatementSyntaxList New => new(ioc);
protected override IEnumerable<StatementSyntax> GetPlatformSpecificValue<TResult>(
IEnumerable<PlatformDetectionType> types, Func<PlatformDetectionType, TResult> syntaxBuilder
) where TResult : class => new[]
{
GeneratorHelpers.GetPlatformSpecificStatements(
GlobalNamespace, Config, types,
platform => Coerce(syntaxBuilder(platform))
)
};
protected override IEnumerable<StatementSyntax> GetPlatformSpecificValue<TResult>(
IEnumerable<PlatformDetectionType> types, Func<PlatformDetectionType, IEnumerable<TResult>> syntaxBuilder
) where TResult : class => new[]
{
GeneratorHelpers.GetPlatformSpecificStatements(
GlobalNamespace, Config, types,
platform => Coerce(syntaxBuilder(platform))
)
};
public void Add<T>(T source, IStatementCodeGenerator<T> generator) where T : CsBase
{
if (TryAdd<T, StatementSyntax, IStatementCodeGenerator<T>>(source, generator))
return;
if (TryAdd<T, ExpressionSyntax, IStatementCodeGenerator<T>>(source, generator))
return;
throw new ArgumentOutOfRangeException(nameof(generator));
}
[SuppressMessage("ReSharper", "PossibleMultipleEnumeration")]
public void AddRange<T>(IEnumerable<T> source, IStatementCodeGenerator<T> generator) where T : CsBase
{
if (TryAdd<T, StatementSyntax, IStatementCodeGenerator<T>>(source, generator))
return;
if (TryAdd<T, ExpressionSyntax, IStatementCodeGenerator<T>>(source, generator))
return;
throw new ArgumentOutOfRangeException(nameof(generator));
}
public void Add<T>(T source, PlatformDetectionType platform, IStatementCodeGenerator<T> generator) where T : CsBase
{
if (TryAddPlatformFixed<T, StatementSyntax, IStatementCodeGenerator<T>>(source, platform, generator))
return;
if (TryAddGeneric<T, StatementSyntax, IStatementCodeGenerator<T>>(source, generator))
return;
if (TryAddPlatformFixed<T, ExpressionSyntax, IStatementCodeGenerator<T>>(source, platform, generator))
return;
if (TryAddGeneric<T, ExpressionSyntax, IStatementCodeGenerator<T>>(source, generator))
return;
throw new ArgumentOutOfRangeException(nameof(generator));
}
[SuppressMessage("ReSharper", "PossibleMultipleEnumeration")]
public void AddRange<T>(IEnumerable<T> source, PlatformDetectionType platform, IStatementCodeGenerator<T> generator) where T : CsBase
{
if (TryAddPlatformFixed<T, StatementSyntax, IStatementCodeGenerator<T>>(source, platform, generator))
return;
if (TryAddGeneric<T, StatementSyntax, IStatementCodeGenerator<T>>(source, generator))
return;
if (TryAddPlatformFixed<T, ExpressionSyntax, IStatementCodeGenerator<T>>(source, platform, generator))
return;
if (TryAddGeneric<T, ExpressionSyntax, IStatementCodeGenerator<T>>(source, generator))
return;
throw new ArgumentOutOfRangeException(nameof(generator));
}
#else
protected override StatementSyntaxList New => new();
#endif
protected override StatementSyntax? Coerce<T>(T? value) where T : class => value switch
{
null => null,
StatementSyntax statement => statement,
ExpressionSyntax expression => ExpressionStatement(expression),
StatementSyntaxList statementList => statementList.ToStatement(),
IEnumerable<StatementSyntax> statementList => From(statementList).ToStatement(),
IEnumerable<ExpressionSyntax> statementList => From(statementList).ToStatement(),
_ => throw new ArgumentOutOfRangeException(nameof(value), value, null)
};
public BlockSyntax ToBlock()
{
var statement = ToStatement();
return statement as BlockSyntax ?? Block(statement);
}
public StatementSyntax ToStatement() => Count == 1 ? this[0] : Block(this);
}