-
Notifications
You must be signed in to change notification settings - Fork 441
Add *Syntax.parse
methods for the nodes that we support parsing
#1076
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
Conversation
@swift-ci please test |
21c450b
to
7186e5a
Compare
@swift-ci please test |
2dd7a11
to
d06c953
Compare
@swift-ci please test |
// TODO: Consider removing `HasTrailingCodeBlock` and friends | ||
// It'd be much nicer if we didn't have any builder APIs taking strings. We | ||
// should just make convenvience inits where needed such that we could write: | ||
// `IfStmt("if foo")` -> `IfStmt(conditions: [.expression("foo")])` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is what we had before I introduced HasTrailingCodeBlock
and it was super verbose and I didn't like it. If you want, you can take a look at old versions of how classes or Strucks were being constructed in CodeGeneration.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll take a look, but if it's too verbose we should fix that. IfStmt("if foo")
seems really weird to me, we've said it's an if
so why are we writing if
again 😅? In general I'd just really like to steer away from methods that take a String
and parse it, without it being obvious that's what they're doing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume you're referring to 2c04f34? To take some examples from there -
Old:
SwitchCase(label: SwitchCaseLabel(caseItems: [CaseItem(pattern: ExpressionPattern(expression: MemberAccessExpr(name: "contextualKeyword")))]))
New:
SwitchCase("case .contextualKeyword:")
Suggested:
SwitchCase(pattern: ".contextualKeyword") { }
Old:
ExtensionDecl(
leadingTrivia: node.documentation.isEmpty
? []
: .docLineComment("/// \(node.documentation)") + .newline,
extendedType: Type(node.type.shorthandName),
inheritanceClause: TypeInheritanceClause { InheritedType(typeName: Type("ExpressibleByArrayLiteral")) }
)
New:
ExtensionDecl("\(docComment)extension \(node.type.shorthandName): ExpressibleByArrayLiteral")
Suggested:
ExtensionDecl(
leadingTrivia: docComment,
extendedType: "\(node.type.shorthandName)",
inheritances: ["ExpressibleByArrayLiteral"])
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the function declarations were one of the worst in the old API but maybe you're right that we can switch if every child node of these is passable from a string literal. Your examples definitely look good to me.
_ markedSource: String, | ||
_ parseSyntax: (inout Parser) -> Node, | ||
_ parse: (String) -> S, | ||
substructure expectedSubstructure: Syntax? = nil, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seeing this, I'm wondering whether we can get rid of the custom entry point here altogether. It didn't really turn out to be super useful IMO.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Which one? I was going to change these methods to a struct + check
or similar after this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking that parse
here could just always be SourceFileSyntax.parse
. I'm still not 100% sure about it though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh I see. We currently expose a few parse methods (eg. decl/statement/expr/pattern/memberdecl), so IMO it probably makes sense to keep these still.
@swift-ci please test |
eb8bf94
to
10f5bdd
Compare
@swift-ci please test |
10f5bdd
to
f1751a6
Compare
@swift-ci please test |
f1751a6
to
6f462d7
Compare
@swift-ci please test |
6f462d7
to
0d72ebf
Compare
@swift-ci please test |
Adds a new `SyntaxParseable` protocol with a single `parse(from:)` method. Any nodes with `parser_function` defined implement this protocol. Other updates: - `SyntaxExpressibleByStringInterpolation` nodes to use these new methods - Parser tests to check against `*Syntax` rather than `Raw*Syntax` nodes - Builder methods to to take nodes (which are expressible by string literals) rather than `String`
0d72ebf
to
d44fe39
Compare
@swift-ci please test |
Adds a new
SyntaxParseable
protocol with a singleparse(from:)
method. Any nodes withparser_function
defined implement this protocol.Other updates:
SyntaxExpressibleByStringInterpolation
nodes to use these new methods*Syntax
rather thanRaw*Syntax
nodesString