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

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

Merged
merged 19 commits into from
Jul 17, 2023

Conversation

zanieb
Copy link
Contributor

@zanieb zanieb commented Jul 13, 2023

Extends #95
Closes #82

Adds parsing of new type soft keyword for defining type aliases.

Supports type alias statements as defined in PEP 695 e.g.

# A non-generic type alias
type IntOrStr = int | str

# A generic type alias
type ListOrSet[T] = list[T] | set[T]

# A type alias that includes a forward reference
type AnimalOrVegetable = Animal | "Vegetable"

# A generic self-referential type alias
type RecursiveList[T] = T | list[RecursiveList[T]]

All type parameter kinds are supported as in #95.

Builds on soft keyword abstractions introduced in RustPython/RustPython#4519

@zanieb zanieb force-pushed the parse/typealias branch 2 times, most recently from d04c741 to 52d1a41 Compare July 14, 2023 15:18
@zanieb zanieb marked this pull request as ready for review July 14, 2023 15:18
parser/src/soft_keywords.rs Outdated Show resolved Hide resolved
parser/src/parser.rs Show resolved Hide resolved
parser/src/parser.rs Show resolved Hide resolved
parser/src/python.lalrpop Show resolved Hide resolved
parser/src/soft_keywords.rs Outdated Show resolved Hide resolved
parser/src/parser.rs Show resolved Hide resolved
// 2. The type token is followed by a name token.
// 3. The name token is followed by an equality token.
else if matches!(tok, Tok::Type) {
if !self.start_of_line {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll look at this on Monday. I need a fresh mind to process this

parser/src/soft_keywords.rs Outdated Show resolved Hide resolved
parser/src/soft_keywords.rs Outdated Show resolved Hide resolved
@charliermarsh
Copy link
Contributor

Nice job figuring this out. A couple questions to confirm my understanding of the soft-keyword heuristics.

parser/src/soft_keywords.rs Outdated Show resolved Hide resolved
Copy link
Member

@youknowone youknowone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, thank you so much.

@zanieb zanieb changed the title Add parsing of type alias statements Add parsing of type alias statements i.e. the type keyword Jul 15, 2023
Comment on lines 116 to 117
Tok::Lpar | Tok::Lsqb | Tok::Lbrace => nesting += 1,
Tok::Rpar | Tok::Rsqb | Tok::Rbrace => nesting -= 1,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to support parentheses for the type case? I didn't see any test case that uses a parenthesized name.

I think we can be more strict in the type implementation and only skip over NonLogicalNewline, Continuation, and Comment. Whatever token comes after must be the = or it isn't a type assignment

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this is valid: type Foo[T] = ....

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I cannot find a case where parens are allowed, although we do need to support brackets for the generic definition

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. I guess we then still need to support skipping over arbitrary tokens.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are generally syntax errors and won't result in an identifier at runtime so I'm actually not sure if we want to change the type token to an identifier.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

b2d67c7
af508c2

We can add more complex parsing in a follow-up that includes tests that assert syntax errors if we want.

Copy link
Contributor

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ready to merge when you are

@charliermarsh charliermarsh merged commit 704eb40 into RustPython:main Jul 17, 2023
zanieb added a commit to astral-sh/RustPython-Parser that referenced this pull request Jul 17, 2023
…hon#97)

Extends RustPython#95
Closes RustPython#82

Adds parsing of new `type` soft keyword for defining type aliases.

Supports type alias statements as defined in PEP 695 e.g.

```python
type IntOrStr = int | str

type ListOrSet[T] = list[T] | set[T]

type AnimalOrVegetable = Animal | "Vegetable"

type RecursiveList[T] = T | list[RecursiveList[T]]
```

All type parameter kinds are supported as in RustPython#95.

Builds on soft keyword abstractions introduced in RustPython/RustPython#4519
zanieb added a commit to astral-sh/RustPython-Parser that referenced this pull request Jul 17, 2023
…hon#97)

Extends RustPython#95
Closes RustPython#82

Adds parsing of new `type` soft keyword for defining type aliases.

Supports type alias statements as defined in PEP 695 e.g.

```python
type IntOrStr = int | str

type ListOrSet[T] = list[T] | set[T]

type AnimalOrVegetable = Animal | "Vegetable"

type RecursiveList[T] = T | list[RecursiveList[T]]
```

All type parameter kinds are supported as in RustPython#95.

Builds on soft keyword abstractions introduced in RustPython/RustPython#4519
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

PEP 695 – Type Parameter Syntax
4 participants