Skip to content

Commit 7dddc5c

Browse files
committed
Add parsing of type alias statements
1 parent 3ec64e1 commit 7dddc5c

9 files changed

+9103
-8306
lines changed

parser/build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ fn gen_phf(out_dir: &Path) {
154154
.entry("raise", "Tok::Raise")
155155
.entry("return", "Tok::Return")
156156
.entry("try", "Tok::Try")
157+
.entry("type", "Tok::Type")
157158
.entry("while", "Tok::While")
158159
.entry("with", "Tok::With")
159160
.entry("yield", "Tok::Yield")

parser/src/parser.rs

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,7 @@ class Foo(A, B):
636636
insta::assert_debug_snapshot!(ast::Suite::parse(source, "<test>").unwrap());
637637
}
638638

639+
639640
#[test]
640641
#[cfg(feature = "all-nodes-with-ranges")]
641642
fn test_parse_class_generic_types() {
@@ -867,11 +868,45 @@ except* OSError as e:
867868
assert!(parse(source, Mode::Interactive, "<embedded>").is_ok());
868869
}
869870

871+
872+
#[test]
873+
#[cfg(feature = "all-nodes-with-ranges")]
874+
fn test_parse_type_declaration() {
875+
let source = "\
876+
# A non-generic type alias
877+
type IntOrStr = int | str
878+
879+
# A generic type alias
880+
type ListOrSet[T] = list[T] | set[T]
881+
882+
# A type alias that includes a forward reference
883+
type AnimalOrVegetable = Animal | \"Vegetable\"
884+
885+
# A generic self-referential type alias
886+
type RecursiveList[T] = T | list[RecursiveList[T]]
887+
";
888+
insta::assert_debug_snapshot!(ast::Suite::parse(source, "<test>").unwrap());
889+
}
890+
891+
#[test]
892+
#[cfg(feature = "all-nodes-with-ranges")]
893+
fn test_type_as_identifier() {
894+
let source = r#"\
895+
type = lambda query: query == event
896+
print(type(12))
897+
"#;
898+
899+
use crate::lexer::lex;
900+
let lexer = lex(source, Mode::Module);
901+
println!("tokens {:#?}", lexer.map(|x| x.unwrap().0).collect::<Vec<_>>());
902+
903+
insta::assert_debug_snapshot!(ast::Suite::parse(source, "<test>").unwrap());
904+
}
905+
870906
#[test]
871907
#[cfg(feature = "all-nodes-with-ranges")]
872908
fn test_match_as_identifier() {
873-
let parse_ast = ast::Suite::parse(
874-
r#"
909+
let source = r#"\
875910
match *a + b, c # ((match * a) + b), c
876911
match *(a + b), c # (match * (a + b)), c
877912
match (*a + b, c) # match ((*(a + b)), c)
@@ -893,11 +928,8 @@ match match:
893928
pass
894929
match = lambda query: query == event
895930
print(match(12))
896-
"#,
897-
"<test>",
898-
)
899-
.unwrap();
900-
insta::assert_debug_snapshot!(parse_ast);
931+
"#;
932+
insta::assert_debug_snapshot!(ast::Suite::parse(source, "<test>").unwrap());
901933
}
902934

903935
#[test]

parser/src/python.lalrpop

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ SmallStatement: ast::Stmt = {
8686
GlobalStatement,
8787
NonlocalStatement,
8888
AssertStatement,
89+
TypeAliasStatement,
8990
};
9091

9192
PassStatement: ast::Stmt = {
@@ -979,6 +980,25 @@ FuncDef: ast::Stmt = {
979980
},
980981
};
981982

983+
TypeAliasName: ast::Expr = {
984+
<location:@L> <name:Identifier> <end_location:@R> => ast::Expr::Name(
985+
ast::ExprName { id: name, ctx: ast::ExprContext::Load, range: (location..end_location).into() },
986+
),
987+
}
988+
989+
TypeAliasStatement: ast::Stmt = {
990+
<location:@L> "type" <name:TypeAliasName> <type_params:TypeParamList?> "=" <value:Test<"all">> <end_location:@R> => {
991+
ast::Stmt::TypeAlias(
992+
ast::StmtTypeAlias {
993+
name: Box::new(name),
994+
value: Box::new(value),
995+
type_params: type_params.unwrap_or_default(),
996+
range: (location..end_location).into()
997+
},
998+
)
999+
},
1000+
};
1001+
9821002
Parameters: ast::Arguments = {
9831003
<location:@L> "(" <a: (ParameterList<TypedParameter, StarTypedParameter, DoubleStarTypedParameter>)?> ")" <end_location:@R> =>? {
9841004
a.as_ref().map(validate_arguments).transpose()?;
@@ -1751,6 +1771,7 @@ extern {
17511771
"raise" => token::Tok::Raise,
17521772
"return" => token::Tok::Return,
17531773
"try" => token::Tok::Try,
1774+
"type" => token::Tok::Type,
17541775
"while" => token::Tok::While,
17551776
"match" => token::Tok::Match,
17561777
"case" => token::Tok::Case,

parser/src/python.rs

Lines changed: 8515 additions & 8174 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)