diff options
Diffstat (limited to 'crates/syntax')
-rw-r--r-- | crates/syntax/src/ast/make.rs | 14 |
1 files changed, 12 insertions, 2 deletions
diff --git a/crates/syntax/src/ast/make.rs b/crates/syntax/src/ast/make.rs index 4bcea28cc..f8b508a90 100644 --- a/crates/syntax/src/ast/make.rs +++ b/crates/syntax/src/ast/make.rs | |||
@@ -15,12 +15,22 @@ use stdx::format_to; | |||
15 | use crate::{ast, AstNode, SourceFile, SyntaxKind, SyntaxNode, SyntaxToken}; | 15 | use crate::{ast, AstNode, SourceFile, SyntaxKind, SyntaxNode, SyntaxToken}; |
16 | 16 | ||
17 | pub fn name(text: &str) -> ast::Name { | 17 | pub fn name(text: &str) -> ast::Name { |
18 | ast_from_text(&format!("mod {};", text)) | 18 | ast_from_text(&format!("mod {}{};", raw_ident_esc(text), text)) |
19 | } | 19 | } |
20 | 20 | ||
21 | pub fn name_ref(text: &str) -> ast::NameRef { | 21 | pub fn name_ref(text: &str) -> ast::NameRef { |
22 | ast_from_text(&format!("fn f() {{ {}; }}", text)) | 22 | ast_from_text(&format!("fn f() {{ {}{}; }}", raw_ident_esc(text), text)) |
23 | } | 23 | } |
24 | |||
25 | fn raw_ident_esc(ident: &str) -> &'static str { | ||
26 | let is_keyword = parser::SyntaxKind::from_keyword(ident).is_some(); | ||
27 | if is_keyword && !matches!(ident, "self" | "crate" | "super" | "Self") { | ||
28 | "r#" | ||
29 | } else { | ||
30 | "" | ||
31 | } | ||
32 | } | ||
33 | |||
24 | // FIXME: replace stringly-typed constructor with a family of typed ctors, a-la | 34 | // FIXME: replace stringly-typed constructor with a family of typed ctors, a-la |
25 | // `expr_xxx`. | 35 | // `expr_xxx`. |
26 | pub fn ty(text: &str) -> ast::Type { | 36 | pub fn ty(text: &str) -> ast::Type { |