-
Notifications
You must be signed in to change notification settings - Fork 1.8k
internal: tighten up parser API #11134
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
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
23ce31e
simplify
matklad d3ba55b
cleanup imports
matklad abc658a
internal: add prefix entry points
matklad 350d5dc
internal: move visibility to a prefix entry point
matklad 519ee21
internal: move block to prefix entry point
matklad f10f518
move stmt to entry points
matklad 5636bef
move pat to prefix entry points
matklad 04ae18d
move ty
matklad c5d8a9b
move expr
matklad 3690016
move path
matklad 8e7fc7b
simplify
matklad afffa09
add TopEntryPoint
matklad 8794892
dead code
matklad 634c768
add missing test
matklad b468bd6
internal: start isolating ssr-related parsing APIs to SSR
matklad f0fefde
remove Item::parse
matklad b8b9655
add test
matklad 2d373dc
verify during parse
matklad df2a996
add ssr fragment for expressions
matklad 2cbfcf4
add ssr fragment for statements
matklad 7e9c74d
drop dead code
matklad dacbc6a
move the rest of ssr parsing to fragments
matklad 55f1564
remove fragments from syntax
matklad 45bba40
dead code
matklad f9e06e6
last use of parse api in ssr
matklad aa1788d
clarify semantics of doc links
matklad bfc263f
introduce hacks module
matklad 660cf34
dead code
matklad ea96c37
compress
matklad File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
//! When specifying SSR rule, you generally want to map one *kind* of thing to | ||
//! the same kind of thing: path to path, expression to expression, type to | ||
//! type. | ||
//! | ||
//! The problem is, while this *kind* is generally obvious to the human, the ide | ||
//! needs to determine it somehow. We do this in a stupid way -- by pasting SSR | ||
//! rule into different contexts and checking what works. | ||
|
||
use syntax::{ast, AstNode, SyntaxNode}; | ||
|
||
pub(crate) fn ty(s: &str) -> Result<SyntaxNode, ()> { | ||
fragment::<ast::Type>("type T = {};", s) | ||
} | ||
|
||
pub(crate) fn item(s: &str) -> Result<SyntaxNode, ()> { | ||
fragment::<ast::Item>("{}", s) | ||
} | ||
|
||
pub(crate) fn pat(s: &str) -> Result<SyntaxNode, ()> { | ||
fragment::<ast::Pat>("const _: () = {let {} = ();};", s) | ||
} | ||
|
||
pub(crate) fn expr(s: &str) -> Result<SyntaxNode, ()> { | ||
fragment::<ast::Expr>("const _: () = {};", s) | ||
} | ||
|
||
pub(crate) fn stmt(s: &str) -> Result<SyntaxNode, ()> { | ||
let template = "const _: () = { {}; };"; | ||
let input = template.replace("{}", s); | ||
let parse = syntax::SourceFile::parse(&input); | ||
if !parse.errors().is_empty() { | ||
return Err(()); | ||
} | ||
let mut node = | ||
parse.tree().syntax().descendants().skip(2).find_map(ast::Stmt::cast).ok_or(())?; | ||
if !s.ends_with(';') && node.to_string().ends_with(';') { | ||
node = node.clone_for_update(); | ||
node.syntax().last_token().map(|it| it.detach()); | ||
} | ||
if node.to_string() != s { | ||
return Err(()); | ||
} | ||
Ok(node.syntax().clone_subtree()) | ||
} | ||
|
||
fn fragment<T: AstNode>(template: &str, s: &str) -> Result<SyntaxNode, ()> { | ||
let s = s.trim(); | ||
let input = template.replace("{}", s); | ||
let parse = syntax::SourceFile::parse(&input); | ||
if !parse.errors().is_empty() { | ||
return Err(()); | ||
} | ||
let node = parse.tree().syntax().descendants().find_map(T::cast).ok_or(())?; | ||
if node.syntax().text() != s { | ||
return Err(()); | ||
} | ||
Ok(node.syntax().clone_subtree()) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
cc @Veykril
This looks iffy at first, but I think that's actually the right way to check things like "is this a valid import path?" -- just construct a file with import and check this directly!
Also, looking at this, what
||
intended to be&&
?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.
Ye that was supposed to be
&&
.And I agree, this check is certainly nicer, it makes it more clear as well what is expected to be valid here.