Skip to content

Remove XXXRawSyntax.withYYY() methods #1048

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 1 commit into from
Nov 3, 2022
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
17 changes: 15 additions & 2 deletions Sources/SwiftParser/Declarations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,13 @@ extension Parser {
arena: parser.arena)
} addSemicolonIfNeeded: { lastElement, newItemAtStartOfLine, parser in
if lastElement.semicolon == nil && !newItemAtStartOfLine {
return lastElement.withSemicolon(parser.missingToken(.semicolon, text: nil), arena: parser.arena)
Copy link
Contributor

@bnbarham bnbarham Nov 3, 2022

Choose a reason for hiding this comment

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

Totally fine with this change for now. But one thing I was thinking of... What if we instead had just a single .with that takes the previous node + optional params + leading/trailing trivia? For both the red + green tree. Then this would be eg.

return RawMemberDeclListItemSyntax(lastElement, semicolon: parser.missingToken(.semicolon, text: nil)))

EDIT: Actually, thinking about this more - it's probably not worth it for the green tree. We really shouldn't be modifying them very much (ideally not at all but... 🤷). So for the green tree we'd just have the initializers (no with's) and for the red tree each would have a single .with(...).

return RawMemberDeclListItemSyntax(
lastElement.unexpectedBeforeDecl,
decl: lastElement.decl,
lastElement.unexpectedBetweenDeclAndSemicolon,
semicolon: parser.missingToken(.semicolon, text: nil),
lastElement.unexpectedAfterSemicolon,
arena: parser.arena)
} else {
return nil
}
Expand Down Expand Up @@ -681,7 +687,14 @@ extension Parser {
break
}
if let lastItem = elements.last, lastItem.semicolon == nil && !newItemAtStartOfLine {
elements[elements.count - 1] = lastItem.withSemicolon(missingToken(.semicolon, text: nil), arena: self.arena)
elements[elements.count - 1] = RawMemberDeclListItemSyntax(
lastItem.unexpectedBeforeDecl,
decl: lastItem.decl,
lastItem.unexpectedBetweenDeclAndSemicolon,
semicolon: self.missingToken(.semicolon, text: nil),
lastItem.unexpectedAfterSemicolon,
arena: self.arena)

}
elements.append(newElement)
}
Expand Down
20 changes: 18 additions & 2 deletions Sources/SwiftParser/TopLevel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,15 @@ extension Parser {
break
}
if let lastItem = elements.last, lastItem.semicolon == nil && !newItemAtStartOfLine {
elements[elements.count - 1] = lastItem.withSemicolon(missingToken(.semicolon, text: nil), arena: self.arena)
elements[elements.count - 1] = RawCodeBlockItemSyntax(
lastItem.unexpectedBeforeItem,
item: .init(lastItem.item)!,
lastItem.unexpectedBetweenItemAndSemicolon,
semicolon: self.missingToken(.semicolon, text: nil),
lastItem.unexpectedBetweenSemicolonAndErrorTokens,
errorTokens: lastItem.errorTokens,
lastItem.unexpectedAfterErrorTokens,
arena: self.arena)
}
elements.append(newElement)
}
Expand Down Expand Up @@ -195,7 +203,15 @@ extension Parser {
$0.parseCodeBlockItem()
} addSemicolonIfNeeded: { lastElement, newItemAtStartOfLine, parser in
if lastElement.semicolon == nil && !newItemAtStartOfLine {
return lastElement.withSemicolon(parser.missingToken(.semicolon, text: nil), arena: parser.arena)
return RawCodeBlockItemSyntax(
lastElement.unexpectedBeforeItem,
item: .init(lastElement.item)!,
lastElement.unexpectedBetweenItemAndSemicolon,
semicolon: parser.missingToken(.semicolon, text: nil),
lastElement.unexpectedBetweenSemicolonAndErrorTokens,
errorTokens: lastElement.errorTokens,
lastElement.unexpectedAfterErrorTokens,
arena: parser.arena)
} else {
return nil
}
Expand Down
7 changes: 0 additions & 7 deletions Sources/SwiftSyntax/Raw/RawSyntaxNodes.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,6 @@ public struct Raw${node.name}: Raw${node.name if node.is_base() else node.base_t
layoutView.children[${idx}]${iuo_mark}
% else:
layoutView.children[${idx}].map(Raw${child.type_name}.init(raw:))${iuo_mark}
% end
}
public func with${child.name}(_ ${child.swift_name}: Raw${child.type_name + optional_mark}, arena: SyntaxArena) -> Raw${node.name} {
% if child.is_optional:
return layoutView.replacingChild(at: ${idx}, with: ${child.swift_name}.map(RawSyntax.init), arena: arena).as(Raw${node.name}.self)!
% else:
return layoutView.replacingChild(at: ${idx}, with: RawSyntax(${child.swift_name}), arena: arena).as(Raw${node.name}.self)!
% end
}
% end
Expand Down
Loading