1 //! This module contains free-standing functions for creating AST fragments out
3 use itertools::Itertools;
5 use crate::{ast, AstNode, SourceFile};
7 pub fn name(text: &str) -> ast::Name {
8 ast_from_text(&format!("mod {};", text))
11 pub fn name_ref(text: &str) -> ast::NameRef {
12 ast_from_text(&format!("fn f() {{ {}; }}", text))
15 pub fn path_from_name_ref(name_ref: ast::NameRef) -> ast::Path {
16 path_from_text(&name_ref.syntax().to_string())
18 pub fn path_qualified(qual: ast::Path, name_ref: ast::NameRef) -> ast::Path {
19 path_from_text(&format!("{}::{}", qual.syntax(), name_ref.syntax()))
21 fn path_from_text(text: &str) -> ast::Path {
25 pub fn record_field(name: ast::NameRef, expr: Option<ast::Expr>) -> ast::RecordField {
27 Some(expr) => from_text(&format!("{}: {}", name.syntax(), expr.syntax())),
28 None => from_text(&name.syntax().to_string()),
31 fn from_text(text: &str) -> ast::RecordField {
32 ast_from_text(&format!("fn f() {{ S {{ {}, }} }}", text))
36 pub fn block_from_expr(e: ast::Expr) -> ast::Block {
37 return from_text(&format!("{{ {} }}", e.syntax()));
39 fn from_text(text: &str) -> ast::Block {
40 ast_from_text(&format!("fn f() {}", text))
44 pub fn expr_unit() -> ast::Expr {
47 pub fn expr_unimplemented() -> ast::Expr {
48 expr_from_text("unimplemented!()")
50 pub fn expr_path(path: ast::Path) -> ast::Expr {
51 expr_from_text(&path.syntax().to_string())
53 pub fn expr_continue() -> ast::Expr {
54 expr_from_text("continue")
56 pub fn expr_break() -> ast::Expr {
57 expr_from_text("break")
59 pub fn expr_return() -> ast::Expr {
60 expr_from_text("return")
62 pub fn expr_match(expr: ast::Expr, match_arm_list: ast::MatchArmList) -> ast::Expr {
63 expr_from_text(&format!("match {} {}", expr.syntax(), match_arm_list.syntax()))
65 fn expr_from_text(text: &str) -> ast::Expr {
66 ast_from_text(&format!("const C: () = {};", text))
69 pub fn bind_pat(name: ast::Name) -> ast::BindPat {
70 return from_text(name.text());
72 fn from_text(text: &str) -> ast::BindPat {
73 ast_from_text(&format!("fn f({}: ())", text))
77 pub fn placeholder_pat() -> ast::PlaceholderPat {
78 return from_text("_");
80 fn from_text(text: &str) -> ast::PlaceholderPat {
81 ast_from_text(&format!("fn f({}: ())", text))
85 pub fn tuple_struct_pat(
87 pats: impl IntoIterator<Item = ast::Pat>,
88 ) -> ast::TupleStructPat {
89 let pats_str = pats.into_iter().map(|p| p.syntax().to_string()).join(", ");
90 return from_text(&format!("{}({})", path.syntax(), pats_str));
92 fn from_text(text: &str) -> ast::TupleStructPat {
93 ast_from_text(&format!("fn f({}: ())", text))
97 pub fn record_pat(path: ast::Path, pats: impl IntoIterator<Item = ast::Pat>) -> ast::RecordPat {
98 let pats_str = pats.into_iter().map(|p| p.syntax().to_string()).join(", ");
99 return from_text(&format!("{} {{ {} }}", path.syntax(), pats_str));
101 fn from_text(text: &str) -> ast::RecordPat {
102 ast_from_text(&format!("fn f({}: ())", text))
106 pub fn path_pat(path: ast::Path) -> ast::PathPat {
107 let path_str = path.syntax().text().to_string();
108 return from_text(path_str.as_str());
109 fn from_text(text: &str) -> ast::PathPat {
110 ast_from_text(&format!("fn f({}: ())", text))
114 pub fn match_arm(pats: impl IntoIterator<Item = ast::Pat>, expr: ast::Expr) -> ast::MatchArm {
115 let pats_str = pats.into_iter().map(|p| p.syntax().to_string()).join(" | ");
116 return from_text(&format!("{} => {}", pats_str, expr.syntax()));
118 fn from_text(text: &str) -> ast::MatchArm {
119 ast_from_text(&format!("fn f() {{ match () {{{}}} }}", text))
123 pub fn match_arm_list(arms: impl IntoIterator<Item = ast::MatchArm>) -> ast::MatchArmList {
124 let arms_str = arms.into_iter().map(|arm| format!("\n {}", arm.syntax())).join(",");
125 return from_text(&format!("{},\n", arms_str));
127 fn from_text(text: &str) -> ast::MatchArmList {
128 ast_from_text(&format!("fn f() {{ match () {{{}}} }}", text))
134 bounds: impl IntoIterator<Item = ast::TypeBound>,
135 ) -> ast::WherePred {
136 let bounds = bounds.into_iter().map(|b| b.syntax().to_string()).join(" + ");
137 return from_text(&format!("{}: {}", path.syntax(), bounds));
139 fn from_text(text: &str) -> ast::WherePred {
140 ast_from_text(&format!("fn f() where {} {{ }}", text))
144 pub fn where_clause(preds: impl IntoIterator<Item = ast::WherePred>) -> ast::WhereClause {
145 let preds = preds.into_iter().map(|p| p.syntax().to_string()).join(", ");
146 return from_text(preds.as_str());
148 fn from_text(text: &str) -> ast::WhereClause {
149 ast_from_text(&format!("fn f() where {} {{ }}", text))
153 pub fn if_expression(condition: &ast::Expr, statement: &str) -> ast::IfExpr {
154 ast_from_text(&format!(
155 "fn f() {{ if !{} {{\n {}\n}}\n}}",
156 condition.syntax().text(),
161 pub fn let_stmt(pattern: ast::Pat, initializer: Option<ast::Expr>) -> ast::LetStmt {
162 let text = match initializer {
163 Some(it) => format!("let {} = {};", pattern.syntax(), it.syntax()),
164 None => format!("let {};", pattern.syntax()),
166 ast_from_text(&format!("fn f() {{ {} }}", text))
169 fn ast_from_text<N: AstNode>(text: &str) -> N {
170 let parse = SourceFile::parse(text);
171 let res = parse.tree().syntax().descendants().find_map(N::cast).unwrap();
176 use crate::{AstNode, Parse, SourceFile, SyntaxKind, SyntaxKind::*, SyntaxToken, T};
177 use once_cell::sync::Lazy;
179 static SOURCE_FILE: Lazy<Parse<SourceFile>> =
180 Lazy::new(|| SourceFile::parse("const C: () = (1 != 1, 2 == 2)\n;"));
182 pub fn op(op: SyntaxKind) -> SyntaxToken {
186 .descendants_with_tokens()
187 .filter_map(|it| it.into_token())
188 .find(|it| it.kind() == op)
192 pub fn comma() -> SyntaxToken {
196 .descendants_with_tokens()
197 .filter_map(|it| it.into_token())
198 .find(|it| it.kind() == T![,])
202 pub fn single_space() -> SyntaxToken {
206 .descendants_with_tokens()
207 .filter_map(|it| it.into_token())
208 .find(|it| it.kind() == WHITESPACE && it.text().as_str() == " ")
212 pub fn whitespace(text: &str) -> SyntaxToken {
213 assert!(text.trim().is_empty());
214 let sf = SourceFile::parse(text).ok().unwrap();
215 sf.syntax().first_child_or_token().unwrap().into_token().unwrap()
218 pub fn single_newline() -> SyntaxToken {
222 .descendants_with_tokens()
223 .filter_map(|it| it.into_token())
224 .find(|it| it.kind() == WHITESPACE && it.text().as_str() == "\n")
228 pub struct WsBuilder(SourceFile);
231 pub fn new(text: &str) -> WsBuilder {
232 WsBuilder(SourceFile::parse(text).ok().unwrap())
234 pub fn ws(&self) -> SyntaxToken {
235 self.0.syntax().first_child_or_token().unwrap().into_token().unwrap()