Skip to content

Commit 05886a2

Browse files
committed
Support type parameters
FIX: Implement PEP 695 type parameter syntax. Closes #18 Closes #19
1 parent 954098f commit 05886a2

File tree

3 files changed

+42
-5
lines changed

3 files changed

+42
-5
lines changed

src/python.grammar

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,21 @@
2222
Decorator { At dottedName ArgList? newline }
2323

2424
FunctionDefinition {
25-
kw<"async">? kw<"def"> VariableName
25+
kw<"async">? kw<"def"> VariableName TypeParamList?
2626
ParamList
2727
TypeDef { "->" test }?
2828
Body
2929
}
3030

3131
ParamList { "(" commaSep<param>? ")" }
3232

33+
TypeParamList { "[" commaSep<TypeParam> "]" }
34+
35+
TypeParam {
36+
("*" | "**") VariableName |
37+
VariableName TypeDef?
38+
}
39+
3340
MatchStatement {
3441
skw<"match"> "*"? expression MatchBody { ":" newline indent MatchClause+ (dedent | eof) }
3542
}
@@ -65,7 +72,7 @@ SequencePattern { "[" commaSep<pattern> "]" | "(" commaSep<pattern> ")" }
6572

6673
MappingPattern { "{" commaSep<"**" pattern | (VariableName | LiteralPattern) ":" pattern> "}" }
6774

68-
ClassDefinition { kw<"class"> VariableName ArgList? Body }
75+
ClassDefinition { kw<"class"> VariableName TypeParamList? ArgList? Body }
6976

7077
param { VariableName TypeDef? (AssignOp{"="} test)? | "*" VariableName? | "**" VariableName | "/" }
7178

@@ -92,7 +99,8 @@ smallStatement {
9299
RaiseStatement { kw<"raise"> (test (kw<"from"> test | ("," test ("," test)?))?)? } |
93100
ImportStatement |
94101
ScopeStatement { (kw<"global"> | kw<"nonlocal">) commaSep<VariableName> } |
95-
AssertStatement { kw<"assert"> commaSep<test> }
102+
AssertStatement { kw<"assert"> commaSep<test> } |
103+
TypeDefinition { skw<"type"> VariableName TypeParamList? "=" test }
96104
}
97105

98106
expressions { commaSep<"*" expression | test> }

src/tokens.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {ExternalTokenizer, ContextTracker} from "@lezer/lr"
22
import {
33
newline as newlineToken, eof, newlineBracketed, blankLineStart, indent, dedent, printKeyword,
44
ParenthesizedExpression, TupleExpression, ComprehensionExpression,
5-
PatternArgList, SequencePattern, MappingPattern, FormatString,
5+
PatternArgList, SequencePattern, MappingPattern, FormatString, TypeParamList,
66
ArrayExpression, ArrayComprehensionExpression, ArgList, ParamList, importList, subscript,
77
DictionaryExpression, DictionaryComprehensionExpression, SetExpression, SetComprehensionExpression,
88
formatString1Content, formatString1Brace, formatString1End,
@@ -20,7 +20,7 @@ const bracketed = new Set([
2020
ArrayExpression, ArrayComprehensionExpression, subscript,
2121
SetExpression, SetComprehensionExpression, FormatString,
2222
DictionaryExpression, DictionaryComprehensionExpression,
23-
SequencePattern, MappingPattern, PatternArgList
23+
SequencePattern, MappingPattern, PatternArgList, TypeParamList
2424
])
2525

2626
function isLineBreak(ch) {

test/statement.txt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,3 +383,32 @@ Script(MatchStatement(match,VariableName,MatchBody(
383383
LogicOp,
384384
SequencePattern(CapturePattern(VariableName),CapturePattern(VariableName))),
385385
Body(PassStatement(pass))))))
386+
387+
# Type Params
388+
389+
class ClassA[T: str]:
390+
def method1(self) -> T:
391+
pass
392+
393+
def func[**T](a: T, b: T) -> T:
394+
pass
395+
396+
==>
397+
398+
Script(
399+
ClassDefinition(class,VariableName,
400+
TypeParamList(TypeParam(VariableName,TypeDef(":",VariableName))),
401+
Body(FunctionDefinition(def,VariableName,ParamList(VariableName),TypeDef(VariableName),
402+
Body(PassStatement(pass))))),
403+
FunctionDefinition(def,VariableName,TypeParamList(TypeParam(VariableName)),
404+
ParamList(VariableName,TypeDef(VariableName),VariableName,TypeDef(VariableName)),
405+
TypeDef(VariableName),
406+
Body(PassStatement(pass))))
407+
408+
# Type Definition
409+
410+
type Point = tuple[float, float]
411+
412+
==>
413+
414+
Script(TypeDefinition(type,VariableName,MemberExpression(VariableName,VariableName,",",VariableName)))

0 commit comments

Comments
 (0)