Skip to content

Add parsing of type alias statements i.e. the type keyword #97

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

Merged
merged 19 commits into from
Jul 17, 2023
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 parser/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ fn gen_phf(out_dir: &Path) {
.entry("raise", "Tok::Raise")
.entry("return", "Tok::Return")
.entry("try", "Tok::Try")
.entry("type", "Tok::Type")
.entry("while", "Tok::While")
.entry("with", "Tok::With")
.entry("yield", "Tok::Yield")
Expand Down
92 changes: 85 additions & 7 deletions parser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -894,11 +894,92 @@ except* OSError as e:
assert!(parse(source, Mode::Interactive, "<embedded>").is_ok());
}

#[test]
#[cfg(feature = "all-nodes-with-ranges")]
fn test_parse_type_declaration() {
let source = r#"
type X = int
type X = int | str
type X = int | "ForwardRefY"
type X[T] = T | list[X[T]] # recursive
type X[T] = int
type X[T] = list[T] | set[T]
type X[T, *Ts, **P] = (T, Ts, P)
type X[T: int, *Ts, **P] = (T, Ts, P)
type X[T: (int, str), *Ts, **P] = (T, Ts, P)

# soft keyword as alias name
type type = int
type match = int
type case = int

# soft keyword as value
type foo = type
type foo = match
type foo = case

# multine definitions
type \
X = int
type X \
= int
type X = \
int
type X = (
int
)
type \
X[T] = T
type X \
[T] = T
type X[T] \
= T
"#;
insta::assert_debug_snapshot!(ast::Suite::parse(source, "<test>").unwrap());
}

#[test]
#[cfg(feature = "all-nodes-with-ranges")]
fn test_type_as_identifier() {
let source = r#"\
type *a + b, c # ((type * a) + b), c
type *(a + b), c # (type * (a + b)), c
type (*a + b, c) # type ((*(a + b)), c)
type -a * b + c # (type - (a * b)) + c
type -(a * b) + c # (type - (a * b)) + c
type (-a) * b + c # (type (-(a * b))) + c
type ().a # (type()).a
type (()).a # (type(())).a
type ((),).a # (type(())).a
type [a].b # (type[a]).b
type [a,].b # (type[(a,)]).b (not (type[a]).b)
type [(a,)].b # (type[(a,)]).b
type()[a:
b] # (type())[a: b]
if type := 1: pass
type = lambda query: query == event
print(type(12))
type(type)
a = (
type in C
)
a = (
type(b)
)
type (
X = int
)
type = 1
type = x = 1
x = type = 1
"#;
insta::assert_debug_snapshot!(ast::Suite::parse(source, "<test>").unwrap());
}

#[test]
#[cfg(feature = "all-nodes-with-ranges")]
fn test_match_as_identifier() {
let parse_ast = ast::Suite::parse(
r#"
let source = r#"\
match *a + b, c # ((match * a) + b), c
match *(a + b), c # (match * (a + b)), c
match (*a + b, c) # match ((*(a + b)), c)
Expand All @@ -920,11 +1001,8 @@ match match:
pass
match = lambda query: query == event
print(match(12))
"#,
"<test>",
)
.unwrap();
insta::assert_debug_snapshot!(parse_ast);
"#;
insta::assert_debug_snapshot!(ast::Suite::parse(source, "<test>").unwrap());
}

#[test]
Expand Down
21 changes: 21 additions & 0 deletions parser/src/python.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ SmallStatement: ast::Stmt = {
GlobalStatement,
NonlocalStatement,
AssertStatement,
TypeAliasStatement,
};

PassStatement: ast::Stmt = {
Expand Down Expand Up @@ -978,6 +979,25 @@ FuncDef: ast::Stmt = {
},
};

TypeAliasName: ast::Expr = {
<location:@L> <name:Identifier> <end_location:@R> => ast::Expr::Name(
ast::ExprName { id: name, ctx: ast::ExprContext::Load, range: (location..end_location).into() },
),
}

TypeAliasStatement: ast::Stmt = {
<location:@L> "type" <name:TypeAliasName> <type_params:TypeParamList?> "=" <value:Test<"all">> <end_location:@R> => {
ast::Stmt::TypeAlias(
ast::StmtTypeAlias {
name: Box::new(name),
value: Box::new(value),
type_params: type_params.unwrap_or_default(),
range: (location..end_location).into()
},
)
},
};

Parameters: ast::Arguments = {
<location:@L> "(" <a: (ParameterList<TypedParameter, StarTypedParameter, DoubleStarTypedParameter>)?> ")" <end_location:@R> =>? {
a.as_ref().map(validate_arguments).transpose()?;
Expand Down Expand Up @@ -1750,6 +1770,7 @@ extern {
"raise" => token::Tok::Raise,
"return" => token::Tok::Return,
"try" => token::Tok::Try,
"type" => token::Tok::Type,
"while" => token::Tok::While,
"match" => token::Tok::Match,
"case" => token::Tok::Case,
Expand Down
16,919 changes: 8,630 additions & 8,289 deletions parser/src/python.rs

Large diffs are not rendered by default.

Loading