Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Qualified typenames #182

Merged
merged 3 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/CSnakes.Tests/GeneratedSignatureTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class GeneratedSignatureTests(TestEnvironment testEnv) : IClassFixture<Te
[InlineData("def hello(a: int = 0b10101010) -> None:\n ...\n", "void Hello(long a = 0b10101010)")]
[InlineData("def hello(a: int = 2147483648) -> None:\n ...\n", "void Hello(long a = 2147483648L)")]
[InlineData("def hello(a: Optional[int] = None) -> None:\n ...\n", "void Hello(long? a = null)")]
[InlineData("def hello(a: typing.List[int], b: typing.Dict[str, int]) -> typing.Tuple[str, int]:\n ...\n", "public (string, long) Hello(IReadOnlyList<long> a, IReadOnlyDictionary<string, long> b)")]
public void TestGeneratedSignature(string code, string expected)
{

Expand Down
22 changes: 22 additions & 0 deletions src/CSnakes.Tests/TokenizerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,28 @@ public void ParseFunctionParameterListDoubleGeneric()
Assert.Equal("list[int]", result.Value[0].Type.ToString());
}

[Fact]
public void ParseFunctionParameterListQualifiedGenericType()
{
var code = "(a: typing.List[int], b)";
var tokens = PythonTokenizer.Instance.Tokenize(code);
var result = PythonParser.PythonParameterListTokenizer.TryParse(tokens);
Assert.True(result.HasValue);
Assert.Equal("a", result.Value[0].Name);
Assert.Equal("typing.List[int]", result.Value[0].Type.ToString());
}

[Fact]
public void ParseFunctionParameterListQualifiedBasicType()
{
var code = "(a: np.ndarray, b)";
var tokens = PythonTokenizer.Instance.Tokenize(code);
var result = PythonParser.PythonParameterListTokenizer.TryParse(tokens);
Assert.True(result.HasValue);
Assert.Equal("a", result.Value[0].Name);
Assert.Equal("np.ndarray", result.Value[0].Type.ToString());
}

[Fact]
public void ParseFunctionParameterListEasy()
{
Expand Down
8 changes: 8 additions & 0 deletions src/CSnakes/Parser/PythonParser.TypeDef.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
using CSnakes.Parser.Types;
using Superpower;
using Superpower.Model;
using Superpower.Parsers;

namespace CSnakes.Parser;
public static partial class PythonParser
{
public static TextParser<TextSpan> QualifiedName { get; } =
Span.MatchedBy(
Character.Letter.Or(Character.EqualTo('_'))
.IgnoreThen(Character.LetterOrDigit.Or(Character.EqualTo('_')).Many())
.AtLeastOnceDelimitedBy(Character.EqualTo('.'))
);

public static TokenListParser<PythonToken, PythonTypeSpec?> PythonTypeDefinitionTokenizer { get; } =
(from name in Token.EqualTo(PythonToken.Identifier).Or(Token.EqualTo(PythonToken.None))
#pragma warning disable CS8620
Expand Down
2 changes: 1 addition & 1 deletion src/CSnakes/Parser/PythonTokenizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static class PythonTokenizer
.Match(Span.EqualTo("None"), PythonToken.None, requireDelimiters: true)
.Match(Span.EqualTo("True"), PythonToken.True, requireDelimiters: true)
.Match(Span.EqualTo("False"), PythonToken.False, requireDelimiters: true)
.Match(Identifier.CStyle, PythonToken.Identifier, requireDelimiters: true) // TODO: (track) Does this require delimiters?
.Match(PythonParser.QualifiedName, PythonToken.Identifier, requireDelimiters: true)
.Match(PythonParser.IntegerConstantToken, PythonToken.Integer, requireDelimiters: true)
.Match(PythonParser.DecimalConstantToken, PythonToken.Decimal, requireDelimiters: true)
.Match(PythonParser.HexidecimalConstantToken, PythonToken.HexidecimalInteger, requireDelimiters: true)
Expand Down
1 change: 1 addition & 0 deletions src/CSnakes/Parser/PythonTokens.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public enum PythonToken
DoubleAsterisk,

Identifier,
QualifiedIdentifier,

[Token(Example = "->")]
Arrow,
Expand Down
2 changes: 1 addition & 1 deletion src/CSnakes/Reflection/TypeReflection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static TypeSyntax AsPredefinedType(PythonTypeSpec pythonType)
if (pythonType.HasArguments())
{
// Get last occurrence of ] in pythonType
return pythonType.Name switch
return pythonType.Name.Replace("typing.", "") switch
{
"list" => CreateListType(pythonType.Arguments[0]),
"List" => CreateListType(pythonType.Arguments[0]),
Expand Down
Loading