Skip to content

Generate token spec sets #1741

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 2 commits into from
Jun 16, 2023
Merged

Conversation

kimdv
Copy link
Contributor

@kimdv kimdv commented Jun 7, 2023

Context: #1727 (comment)

Copy link
Contributor Author

@kimdv kimdv left a comment

Choose a reason for hiding this comment

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

@ahoppen opened this PR to start a discussion if I'm on the right way. Added someinline comments.

I would also like a first round of some initial toughts of yours

Comment on lines +199 to +211
case message
case renamed
case introduced
case obsoleted
case deprecated
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I found that this doesn't contain the same as in AvailabilityArgumentKind

It have also a special handling

https://github.com/apple/swift-syntax/blob/5474478ccf6bd3319c5c3564d2eed15dc3e45eac/Sources/SwiftParser/Availability.swift#L57-L96

Copy link
Member

Choose a reason for hiding this comment

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

These are exactly the interesting cases. A short-term solution would probably be to define the case from AvailabilityArgumentKind that aren’t in AvailabilityLabeledArgumentLabel in a separate TokenSpecSet and then use self.at(anyIn:or:) in Availability.swift:104. In general for these cases, we might need to consider if there is a bug in the parser or if we can model the syntax tree better so that the generated TokenSpecSet matches the parser.

Copy link
Member

@ahoppen ahoppen left a comment

Choose a reason for hiding this comment

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

Thank you. This is great. I wonder how many previously handwritten TokenSpecSet we can replace by the generated ones.

I also wonder whether we can use the generated TokenSpecSet in RawSyntaxValidation.swift.

Comment on lines +199 to +211
case message
case renamed
case introduced
case obsoleted
case deprecated
Copy link
Member

Choose a reason for hiding this comment

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

These are exactly the interesting cases. A short-term solution would probably be to define the case from AvailabilityArgumentKind that aren’t in AvailabilityLabeledArgumentLabel in a separate TokenSpecSet and then use self.at(anyIn:or:) in Availability.swift:104. In general for these cases, we might need to consider if there is a bug in the parser or if we can model the syntax tree better so that the generated TokenSpecSet matches the parser.

@kimdv kimdv force-pushed the kimdv/generate-token-spec-set branch from b9b176f to efaf78d Compare June 9, 2023 06:35
Copy link
Member

@ahoppen ahoppen left a comment

Choose a reason for hiding this comment

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

Looks good to me. Only one comment inline.

CC @bnbarham because we talked about this at some point as well.

@kimdv kimdv force-pushed the kimdv/generate-token-spec-set branch from efaf78d to 86cfd7f Compare June 12, 2023 06:07
@kimdv
Copy link
Contributor Author

kimdv commented Jun 12, 2023

@ahoppen we get this TokenSpecSet

extension SimpleTypeIdentifierSyntax {
  enum NameOptions: TokenSpecSet {
    case identifier
    case `self`
    case `Self`
    case `Any`
    case keyword
    case wildcard
    
    init?(lexeme: Lexer.Lexeme) {
      switch PrepareForKeywordMatch(lexeme) {
      case TokenSpec(.identifier):
        self = .identifier
      case TokenSpec(.`self`):
        self = .`self`
      case TokenSpec(.`Self`):
        self = .`Self`
      case TokenSpec(.`Any`):
        self = .`Any`
      case TokenSpec(.keyword):
        self = .keyword
      case TokenSpec(.wildcard):
        self = .wildcard
      default:
        return nil
      }
    }
    
    var spec: TokenSpec {
      switch self {
      case .identifier:
        return .identifier
      case .`self`:
        return .keyword(.`self`)
      case .`Self`:
        return .keyword(.`Self`)
      case .`Any`:
        return .keyword(.`Any`)
      case .keyword:
        return .keyword
      case .wildcard:
        return .wildcard
      }
    }
  }
}

The . keyword is not valid TokenSpec.
What do you think we should do?
Omit it?

This is the node

Node(
    kind: .simpleTypeIdentifier,
    base: .type,
    nameForDiagnostics: "type",
    children: [
      Child(
        name: "Name",
        kind: .token(choices: [
          .token(tokenKind: "IdentifierToken"),
          .keyword(text: "self"),
          .keyword(text: "Self"),
          .keyword(text: "Any"),
          .token(tokenKind: "KeywordToken"),
          .token(tokenKind: "WildcardToken"),
        ]),
        classification: "TypeIdentifier"
      ),
      Child(
        name: "GenericArgumentClause",
        kind: .node(kind: .genericArgumentClause),
        isOptional: true
      ),
    ]
  )

@kimdv kimdv force-pushed the kimdv/generate-token-spec-set branch from 86cfd7f to c18e339 Compare June 12, 2023 06:40
@kimdv kimdv requested a review from ahoppen June 12, 2023 18:12
@kimdv kimdv force-pushed the kimdv/generate-token-spec-set branch from c18e339 to 9c4f2e3 Compare June 12, 2023 18:37
@ahoppen
Copy link
Member

ahoppen commented Jun 12, 2023

This is the solution: #1769

@kimdv kimdv marked this pull request as ready for review June 13, 2023 10:20
@kimdv kimdv force-pushed the kimdv/generate-token-spec-set branch 2 times, most recently from a527707 to 826f4d8 Compare June 14, 2023 08:39
Copy link
Contributor Author

@kimdv kimdv left a comment

Choose a reason for hiding this comment

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

I found very few usage of the new generated spec sets that matched 1-1.
I can try to find more.

And second, I found also some cases where there was a mismatch between the generated ones and the handwritten ones.
Should I try to apply your suggestion above, so we can see how many cases there are so we need what to do?

@kimdv kimdv force-pushed the kimdv/generate-token-spec-set branch 2 times, most recently from 4b61ca8 to c12f1d4 Compare June 14, 2023 09:47
Copy link
Member

@ahoppen ahoppen left a comment

Choose a reason for hiding this comment

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

I found very few usage of the new generated spec sets that matched 1-1.

🤩 Nice. Those were more than I expected, honestly. I like it.

And second, I found also some cases where there was a mismatch between the generated ones and the handwritten ones.
Should I try to apply your suggestion above, so we can see how many cases there are so we need what to do?

Trying to move more parser calls to the generated TokenSpecSet would be great. I’d just like to do it in a follow-up PR do make it easier to review.

))) { (arena, _) in
))) {(arena, _) in
Copy link
Member

Choose a reason for hiding this comment

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

Do you know where these changes come from?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm... maybe my local CodeGen did not resolve the HEAD correctly. Will try tomorrow again

I run into this as well @kimdv but apparently @ahoppen does not 🤷. I just delete .build and Package.resolved each time.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I normally also run swift package update and swift package resolve each time.
But forgot it 😅

But thanks for the tip!

Should we file a bug in spm repo?
It would be nice if it checked that automatically.

Copy link
Member

Choose a reason for hiding this comment

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

Does swift package update and swift package resolve always fix the issue for you? Recently I didn’t have the feeling but I think I only ever ran one of the two commands, not both.

I think it’s expected that you need to run swift package update. That matches the SwiftPM behavior when you depend on branches where you also only want to update your dependencies (and thus Package.resolved) when you explicitly request it. We should add that to our documentation.

If swift package resolve is also necessary, I would say that’s a but in SwiftPM. If you could file an issue for that, that’d be great.

Copy link
Contributor

Choose a reason for hiding this comment

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

Hmmm. Just had a look at the docs, maybe swift package resolve is what we should be running? swift package update will update Package.resolved with the new hash and then swift package resolve would update the packages to match that version:

The swift package resolve command resolves the dependencies, taking into account the current version restrictions in the Package.swift manifest and Package.resolved resolved versions file, and issuing an error if the graph cannot be resolved. For packages which have previously resolved versions recorded in the Package.resolved file, the resolve command will resolve to those versions as long as they are still eligible. If the resolved version's file changes (e.g., because a teammate pushed a new version of the file) the next resolve command will update packages to match that file. After a successful resolve command, the checked out versions of all dependencies and the versions recorded in the resolved versions file will match. In most cases the resolve command will perform no changes unless the Package.swift manifest or Package.resolved file have changed.

But having said that:

Most SwiftPM commands will implicitly invoke the swift package resolve functionality before running, and will cancel with an error if dependencies cannot be resolved.

So maybe it's a bug if we have to run it, unsure.

@kimdv kimdv force-pushed the kimdv/generate-token-spec-set branch from c12f1d4 to 30eade9 Compare June 15, 2023 10:28
@kimdv
Copy link
Contributor Author

kimdv commented Jun 15, 2023

@swift-ci please test

@kimdv kimdv enabled auto-merge June 15, 2023 10:30
@kimdv
Copy link
Contributor Author

kimdv commented Jun 15, 2023

@swift-ci please test windows

@kimdv kimdv force-pushed the kimdv/generate-token-spec-set branch from 30eade9 to d5f4a79 Compare June 15, 2023 12:19
@kimdv
Copy link
Contributor Author

kimdv commented Jun 15, 2023

@swift-ci please test

Comment on lines 660 to 668
kind: .node(kind: .token),
kind: .token(choices: [
.keyword(text: "target"),
.keyword(text: "availability"),
.keyword(text: "exported"),
.keyword(text: "kind"),
.keyword(text: "spi"),
.keyword(text: "spiModule"),
.keyword(text: "available"),
]),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will remove this @ahoppen as the parser fails in some test cases.
We are now allowed to provide e as a label.

Will be added in #1727

@kimdv kimdv force-pushed the kimdv/generate-token-spec-set branch from d5f4a79 to 35f61b0 Compare June 16, 2023 07:01
@kimdv kimdv force-pushed the kimdv/generate-token-spec-set branch from 35f61b0 to 34724b5 Compare June 16, 2023 07:06
@kimdv
Copy link
Contributor Author

kimdv commented Jun 16, 2023

@swift-ci please test

@kimdv
Copy link
Contributor Author

kimdv commented Jun 16, 2023

@swift-ci please test windows

@kimdv kimdv merged commit 80cf536 into swiftlang:main Jun 16, 2023
@kimdv kimdv deleted the kimdv/generate-token-spec-set branch June 16, 2023 10:22
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.

3 participants