-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathTypeReflection.cs
93 lines (82 loc) · 4.87 KB
/
TypeReflection.cs
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
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using CSnakes.Parser.Types;
using System.ComponentModel;
namespace CSnakes.Reflection;
public static class TypeReflection
{
public enum ConversionDirection
{
ToPython,
FromPython
}
public static TypeSyntax AsPredefinedType(PythonTypeSpec pythonType, ConversionDirection direction)
{
// If type is an alias, e.g. "list[int]", "list[float]", etc.
if (pythonType.HasArguments())
{
// Get last occurrence of ] in pythonType
return pythonType.Name.Replace("typing.", "") switch
{
"list" => CreateListType(pythonType.Arguments[0], direction),
"List" => CreateListType(pythonType.Arguments[0], direction),
"Tuple" => CreateTupleType(pythonType.Arguments, direction),
"tuple" => CreateTupleType(pythonType.Arguments, direction),
"dict" => CreateDictionaryType(pythonType.Arguments[0], pythonType.Arguments[1], direction),
"Dict" => CreateDictionaryType(pythonType.Arguments[0], pythonType.Arguments[1], direction),
"Mapping" => CreateDictionaryType(pythonType.Arguments[0], pythonType.Arguments[1], direction),
"Sequence" => CreateListType(pythonType.Arguments[0], direction),
"Optional" => AsPredefinedType(pythonType.Arguments[0], direction),
"Generator" => CreateGeneratorType(pythonType.Arguments[0], pythonType.Arguments[1], pythonType.Arguments[2], direction),
// Todo more types... see https://docs.python.org/3/library/stdtypes.html#standard-generic-classes
_ => SyntaxFactory.ParseTypeName("PyObject"),
};
}
return (pythonType.Name, direction) switch
{
("int", _) => SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.LongKeyword)),
("str", _) => SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.StringKeyword)),
("float", _) => SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.DoubleKeyword)),
("bool", _) => SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.BoolKeyword)),
("bytes", _) => SyntaxFactory.ParseTypeName("byte[]"),
("Buffer", ConversionDirection.FromPython) => SyntaxFactory.ParseTypeName("IPyBuffer"),
("typing.Buffer", ConversionDirection.FromPython) => SyntaxFactory.ParseTypeName("IPyBuffer"),
("collections.abc.Buffer", ConversionDirection.FromPython) => SyntaxFactory.ParseTypeName("IPyBuffer"),
_ => SyntaxFactory.ParseTypeName("PyObject"),
};
}
private static TypeSyntax CreateDictionaryType(PythonTypeSpec keyType, PythonTypeSpec valueType, ConversionDirection direction) => CreateGenericType("IReadOnlyDictionary", [
AsPredefinedType(keyType, direction),
AsPredefinedType(valueType, direction)
]);
private static TypeSyntax CreateGeneratorType(PythonTypeSpec yieldType, PythonTypeSpec sendType, PythonTypeSpec returnType, ConversionDirection direction) => CreateGenericType("IGeneratorIterator", [
AsPredefinedType(yieldType, direction),
AsPredefinedType(sendType, direction),
AsPredefinedType(returnType, direction)
]);
private static TypeSyntax CreateTupleType(PythonTypeSpec[] tupleTypes, ConversionDirection direction)
{
if (tupleTypes.Length == 1)
{
return CreateGenericType("ValueTuple", tupleTypes.Select(t => AsPredefinedType(t, direction)));
}
IEnumerable<TupleElementSyntax> tupleTypeSyntaxGroups = tupleTypes.Select((x, i) => new { Index = i, Value = x })
.GroupBy(x => x.Index / 7)
.Select(x => x.Select(v => v.Value))
.Select(typeSpecs => typeSpecs.Select(t => AsPredefinedType(t, direction)))
.SelectMany(item => item.Select(SyntaxFactory.TupleElement));
return SyntaxFactory.TupleType(
SyntaxFactory.Token(SyntaxKind.OpenParenToken),
SyntaxFactory.SeparatedList(tupleTypeSyntaxGroups),
SyntaxFactory.Token(SyntaxKind.CloseParenToken));
}
private static TypeSyntax CreateListType(PythonTypeSpec genericOf, ConversionDirection direction) => CreateGenericType("IReadOnlyList", [AsPredefinedType(genericOf, direction)]);
internal static TypeSyntax CreateGenericType(string typeName, IEnumerable<TypeSyntax> genericArguments) =>
SyntaxFactory.GenericName(
SyntaxFactory.Identifier(typeName))
.WithTypeArgumentList(
SyntaxFactory.TypeArgumentList(
SyntaxFactory.SeparatedList(
genericArguments)));
}