Skip to content

[Parse/Syntax] Refactoring to decouple the parser from syntax tree creation #21368

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 12 commits into from
Jan 8, 2019

Conversation

akyrtzi
Copy link
Contributor

@akyrtzi akyrtzi commented Dec 17, 2018

Instead of creating syntax nodes directly, modify the parser to invoke an abstract interface 'SyntaxParseActions' while it is parsing the source code.
This decouples the act of parsing from the act of forming a syntax tree representation.
'SyntaxTreeCreator' is an implementation of SyntaxParseActions that handles the logic of creating a syntax tree.
To enforce the layering separation of parsing and syntax tree creation, a static library swiftSyntaxParse is introduced to compose the two.

This decoupling is important for introducing a syntax parser library for SwiftSyntax to directly access parsing.

@akyrtzi akyrtzi requested review from rintaro and nkcsgexi December 17, 2018 04:56
@rintaro
Copy link
Member

rintaro commented Dec 17, 2018

I will review this tomorrow.

@akyrtzi
Copy link
Contributor Author

akyrtzi commented Dec 18, 2018

I just realized that doing a direct ParsedSyntaxRecorder::record[some syntax] call from the parser is not a good idea due to possibility of being in a backtracking context when the call is made. I'm going to push a change to change the calls to ParsedSyntaxRecorder::make[some syntax] which will implicitly check for backtracking and create a recorded or deferred node accordingly.

@akyrtzi akyrtzi force-pushed the syntax-parser-decoupling branch from 6e0453b to 150b4cb Compare December 20, 2018 03:50
@rintaro
Copy link
Member

rintaro commented Dec 20, 2018

@swift-ci Please ASAN test

@jrose-apple
Copy link
Contributor

What's the impact on parse time?

@akyrtzi
Copy link
Contributor Author

akyrtzi commented Dec 20, 2018

What's the impact on parse time?

There's no impact on parse time for the compiler because the libSyntax parsing code path is completely disabled (and I've made no changes to the Lexer).

For libSyntax C++ tree creation itself I didn't bother doing measurements because the architectural change for SwiftSyntax will obviate the need to create the C++ syntax tree for SwiftSyntax and make SwiftSyntax multiple times faster.

However there are some performance optimizations to make to speed-up the libSyntax parsing path, which I will push for after we adopt the new model for SwiftSyntax.

@akyrtzi
Copy link
Contributor Author

akyrtzi commented Dec 20, 2018

@swift-ci Please ASAN test

@jrose-apple
Copy link
Contributor

What's the impact on parse time?

There's no impact on parse time for the compiler because the libSyntax parsing code path is completely disabled (and I've made no changes to the Lexer).

We enable it for tests, at least, don't we?

@akyrtzi
Copy link
Contributor Author

akyrtzi commented Dec 20, 2018

We enable it for tests, at least, don't we?

Good point. I measured and it's ~15% regression in parse time for C++ libSyntax. In absolute terms this is +6.5 ms for a 4,120 LOC file.

@jrose-apple
Copy link
Contributor

That is non-wonderful but probably livable, given that parse time remains a small percentage of overall time for fully-compiled tests.

@akyrtzi akyrtzi force-pushed the syntax-parser-decoupling branch from 0fb29ff to cc04c28 Compare December 21, 2018 23:16
Copy link
Contributor

@nkcsgexi nkcsgexi left a comment

Choose a reason for hiding this comment

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

No more comments from me 👍

…eation

Instead of creating syntax nodes directly, modify the parser to invoke an abstract interface 'SyntaxParseActions' while it is parsing the source code.
This decouples the act of parsing from the act of forming a syntax tree representation.
'SyntaxTreeCreator' is an implementation of SyntaxParseActions that handles the logic of creating a syntax tree.
To enforce the layering separation of parsing and syntax tree creation, a static library swiftSyntaxParse is introduced to compose the two.

This decoupling is important for introducing a syntax parser library for SwiftSyntax to directly access parsing.
…arser with ParsedSyntaxRecorder::make*

Doing a "direct ParsedSyntaxRecorder::record[some syntax]" call from the parser is not a good idea due to possibility
of being in a backtracking context when the call is made. Replace them with "ParsedSyntaxRecorder::make[some syntax]"
which will implicitly check for backtracking and create a recorded or deferred node accordingly.
* Add and improve documentation comments
* Adjust formatting
* Rename recordExactRawSyntax -> rec.recordRawSyntax
Using dumpTokenKind() function instead of getTokenText().
… null using a DataKind

Avoid implicitely assuming 'null' node if its OpaqueSyntaxNode is null, there should be no interpretation
of OpaqueSyntaxNode values, a SyntaxParseActions implementation should be able to return null pointers as OpaqueSyntaxNode.
…LLVM_COMMON_DEPENDS

Since the generated headers are in 'include/swift/Parse' they are globally accessible so make sure they are created early on.
If these get moved inside 'lib/Parse' then we can remove this dependency setting.
@akyrtzi akyrtzi force-pushed the syntax-parser-decoupling branch from 9aca893 to c929511 Compare January 8, 2019 04:28
@akyrtzi
Copy link
Contributor Author

akyrtzi commented Jan 8, 2019

@swift-ci smoke test

@akyrtzi akyrtzi merged commit b55c1e2 into swiftlang:master Jan 8, 2019
@akyrtzi akyrtzi deleted the syntax-parser-decoupling branch January 8, 2019 05:40
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.

4 participants