2019-09-26 09:18:26 +00:00
|
|
|
//! This module contains free-standing functions for creating AST fragments out
|
|
|
|
//! of smaller pieces.
|
|
|
|
use itertools::Itertools;
|
2020-03-28 10:08:19 +00:00
|
|
|
use stdx::format_to;
|
2019-09-26 09:18:26 +00:00
|
|
|
|
2020-02-04 12:22:32 +00:00
|
|
|
use crate::{ast, AstNode, SourceFile, SyntaxKind, SyntaxNode, SyntaxToken};
|
2019-09-26 09:18:26 +00:00
|
|
|
|
2019-11-13 08:40:51 +00:00
|
|
|
pub fn name(text: &str) -> ast::Name {
|
|
|
|
ast_from_text(&format!("mod {};", text))
|
|
|
|
}
|
|
|
|
|
2019-09-26 09:18:26 +00:00
|
|
|
pub fn name_ref(text: &str) -> ast::NameRef {
|
|
|
|
ast_from_text(&format!("fn f() {{ {}; }}", text))
|
|
|
|
}
|
|
|
|
|
2020-02-29 10:55:36 +00:00
|
|
|
pub fn path_segment(name_ref: ast::NameRef) -> ast::PathSegment {
|
2020-03-27 10:25:11 +00:00
|
|
|
ast_from_text(&format!("use {};", name_ref))
|
2019-09-26 09:18:26 +00:00
|
|
|
}
|
2020-02-29 12:50:47 +00:00
|
|
|
pub fn path_unqualified(segment: ast::PathSegment) -> ast::Path {
|
2020-03-27 10:25:11 +00:00
|
|
|
path_from_text(&format!("use {}", segment))
|
2020-02-29 10:55:36 +00:00
|
|
|
}
|
|
|
|
pub fn path_qualified(qual: ast::Path, segment: ast::PathSegment) -> ast::Path {
|
2020-03-27 10:25:11 +00:00
|
|
|
path_from_text(&format!("{}::{}", qual, segment))
|
2019-09-26 09:18:26 +00:00
|
|
|
}
|
|
|
|
fn path_from_text(text: &str) -> ast::Path {
|
|
|
|
ast_from_text(text)
|
|
|
|
}
|
|
|
|
|
2020-03-05 18:03:14 +00:00
|
|
|
pub fn use_tree(
|
|
|
|
path: ast::Path,
|
|
|
|
use_tree_list: Option<ast::UseTreeList>,
|
|
|
|
alias: Option<ast::Alias>,
|
2020-03-27 16:28:25 +00:00
|
|
|
add_star: bool,
|
2020-03-05 18:03:14 +00:00
|
|
|
) -> ast::UseTree {
|
|
|
|
let mut buf = "use ".to_string();
|
|
|
|
buf += &path.syntax().to_string();
|
|
|
|
if let Some(use_tree_list) = use_tree_list {
|
2020-03-28 10:08:19 +00:00
|
|
|
format_to!(buf, "::{}", use_tree_list);
|
2020-03-05 18:03:14 +00:00
|
|
|
}
|
2020-03-27 16:28:25 +00:00
|
|
|
if add_star {
|
|
|
|
buf += "::*";
|
|
|
|
}
|
|
|
|
|
2020-03-05 18:03:14 +00:00
|
|
|
if let Some(alias) = alias {
|
2020-03-28 10:08:19 +00:00
|
|
|
format_to!(buf, " {}", alias);
|
2020-03-05 18:03:14 +00:00
|
|
|
}
|
|
|
|
ast_from_text(&buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn use_tree_list(use_trees: impl IntoIterator<Item = ast::UseTree>) -> ast::UseTreeList {
|
|
|
|
let use_trees = use_trees.into_iter().map(|it| it.syntax().clone()).join(", ");
|
|
|
|
ast_from_text(&format!("use {{{}}};", use_trees))
|
|
|
|
}
|
|
|
|
|
2020-03-06 14:56:25 +00:00
|
|
|
pub fn use_item(use_tree: ast::UseTree) -> ast::UseItem {
|
2020-03-27 10:25:11 +00:00
|
|
|
ast_from_text(&format!("use {};", use_tree))
|
2020-03-06 14:56:25 +00:00
|
|
|
}
|
|
|
|
|
2019-09-26 09:18:26 +00:00
|
|
|
pub fn record_field(name: ast::NameRef, expr: Option<ast::Expr>) -> ast::RecordField {
|
|
|
|
return match expr {
|
2020-03-27 10:25:11 +00:00
|
|
|
Some(expr) => from_text(&format!("{}: {}", name, expr)),
|
|
|
|
None => from_text(&name.to_string()),
|
2019-09-26 09:18:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
fn from_text(text: &str) -> ast::RecordField {
|
|
|
|
ast_from_text(&format!("fn f() {{ S {{ {}, }} }}", text))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-07 10:51:16 +00:00
|
|
|
pub fn block_expr(
|
|
|
|
stmts: impl IntoIterator<Item = ast::Stmt>,
|
|
|
|
tail_expr: Option<ast::Expr>,
|
|
|
|
) -> ast::BlockExpr {
|
2020-03-28 10:08:19 +00:00
|
|
|
let mut buf = "{\n".to_string();
|
2020-02-07 10:51:16 +00:00
|
|
|
for stmt in stmts.into_iter() {
|
2020-03-28 10:08:19 +00:00
|
|
|
format_to!(buf, " {}\n", stmt);
|
2020-02-07 10:51:16 +00:00
|
|
|
}
|
|
|
|
if let Some(tail_expr) = tail_expr {
|
2020-03-28 10:08:19 +00:00
|
|
|
format_to!(buf, " {}\n", tail_expr)
|
2020-02-07 10:51:16 +00:00
|
|
|
}
|
2020-03-28 10:08:19 +00:00
|
|
|
buf += "}";
|
|
|
|
ast_from_text(&format!("fn f() {}", buf))
|
2020-02-07 10:51:16 +00:00
|
|
|
}
|
|
|
|
|
2019-09-26 09:18:26 +00:00
|
|
|
pub fn block_from_expr(e: ast::Expr) -> ast::Block {
|
2020-03-27 10:25:11 +00:00
|
|
|
return from_text(&format!("{{ {} }}", e));
|
2019-09-26 09:18:26 +00:00
|
|
|
|
|
|
|
fn from_text(text: &str) -> ast::Block {
|
|
|
|
ast_from_text(&format!("fn f() {}", text))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn expr_unit() -> ast::Expr {
|
|
|
|
expr_from_text("()")
|
|
|
|
}
|
2020-03-24 02:23:30 +00:00
|
|
|
pub fn expr_empty_block() -> ast::Expr {
|
|
|
|
expr_from_text("{}")
|
|
|
|
}
|
2019-09-26 09:18:26 +00:00
|
|
|
pub fn expr_unimplemented() -> ast::Expr {
|
|
|
|
expr_from_text("unimplemented!()")
|
|
|
|
}
|
2019-11-13 08:40:51 +00:00
|
|
|
pub fn expr_path(path: ast::Path) -> ast::Expr {
|
2020-03-27 10:25:11 +00:00
|
|
|
expr_from_text(&path.to_string())
|
2019-11-13 08:40:51 +00:00
|
|
|
}
|
|
|
|
pub fn expr_continue() -> ast::Expr {
|
|
|
|
expr_from_text("continue")
|
|
|
|
}
|
|
|
|
pub fn expr_break() -> ast::Expr {
|
|
|
|
expr_from_text("break")
|
|
|
|
}
|
|
|
|
pub fn expr_return() -> ast::Expr {
|
|
|
|
expr_from_text("return")
|
|
|
|
}
|
|
|
|
pub fn expr_match(expr: ast::Expr, match_arm_list: ast::MatchArmList) -> ast::Expr {
|
2020-03-27 10:25:11 +00:00
|
|
|
expr_from_text(&format!("match {} {}", expr, match_arm_list))
|
2019-11-13 08:40:51 +00:00
|
|
|
}
|
2020-03-27 10:38:00 +00:00
|
|
|
pub fn expr_if(condition: ast::Condition, then_branch: ast::BlockExpr) -> ast::Expr {
|
2020-03-27 10:25:11 +00:00
|
|
|
expr_from_text(&format!("if {} {}", condition, then_branch))
|
2020-02-07 10:51:16 +00:00
|
|
|
}
|
2020-02-07 11:07:38 +00:00
|
|
|
pub fn expr_prefix(op: SyntaxKind, expr: ast::Expr) -> ast::Expr {
|
|
|
|
let token = token(op);
|
2020-03-27 10:25:11 +00:00
|
|
|
expr_from_text(&format!("{}{}", token, expr))
|
2020-02-07 11:07:38 +00:00
|
|
|
}
|
2020-03-15 21:23:18 +00:00
|
|
|
fn expr_from_text(text: &str) -> ast::Expr {
|
2019-09-26 09:18:26 +00:00
|
|
|
ast_from_text(&format!("const C: () = {};", text))
|
|
|
|
}
|
|
|
|
|
2020-03-15 21:23:18 +00:00
|
|
|
pub fn try_expr_from_text(text: &str) -> Option<ast::Expr> {
|
|
|
|
try_ast_from_text(&format!("const C: () = {};", text))
|
|
|
|
}
|
|
|
|
|
2020-03-27 10:38:00 +00:00
|
|
|
pub fn condition(expr: ast::Expr, pattern: Option<ast::Pat>) -> ast::Condition {
|
|
|
|
match pattern {
|
|
|
|
None => ast_from_text(&format!("const _: () = while {} {{}};", expr)),
|
|
|
|
Some(pattern) => {
|
2020-03-27 11:12:17 +00:00
|
|
|
ast_from_text(&format!("const _: () = while let {} = {} {{}};", pattern, expr))
|
2020-03-27 10:38:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-26 09:18:26 +00:00
|
|
|
pub fn bind_pat(name: ast::Name) -> ast::BindPat {
|
|
|
|
return from_text(name.text());
|
|
|
|
|
|
|
|
fn from_text(text: &str) -> ast::BindPat {
|
|
|
|
ast_from_text(&format!("fn f({}: ())", text))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn placeholder_pat() -> ast::PlaceholderPat {
|
|
|
|
return from_text("_");
|
|
|
|
|
|
|
|
fn from_text(text: &str) -> ast::PlaceholderPat {
|
|
|
|
ast_from_text(&format!("fn f({}: ())", text))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-23 12:19:09 +00:00
|
|
|
/// Creates a tuple of patterns from an interator of patterns.
|
|
|
|
///
|
|
|
|
/// Invariant: `pats` must be length > 1
|
|
|
|
///
|
|
|
|
/// FIXME handle `pats` length == 1
|
2020-03-23 05:42:32 +00:00
|
|
|
pub fn tuple_pat(pats: impl IntoIterator<Item = ast::Pat>) -> ast::TuplePat {
|
2020-03-23 12:19:09 +00:00
|
|
|
let pats_str = pats.into_iter().map(|p| p.to_string()).join(", ");
|
2020-03-23 05:42:32 +00:00
|
|
|
return from_text(&format!("({})", pats_str));
|
|
|
|
|
|
|
|
fn from_text(text: &str) -> ast::TuplePat {
|
|
|
|
ast_from_text(&format!("fn f({}: ())", text))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-26 09:18:26 +00:00
|
|
|
pub fn tuple_struct_pat(
|
|
|
|
path: ast::Path,
|
2019-11-13 08:55:43 +00:00
|
|
|
pats: impl IntoIterator<Item = ast::Pat>,
|
2019-09-26 09:18:26 +00:00
|
|
|
) -> ast::TupleStructPat {
|
2020-03-27 10:25:11 +00:00
|
|
|
let pats_str = pats.into_iter().join(", ");
|
|
|
|
return from_text(&format!("{}({})", path, pats_str));
|
2019-09-26 09:18:26 +00:00
|
|
|
|
|
|
|
fn from_text(text: &str) -> ast::TupleStructPat {
|
|
|
|
ast_from_text(&format!("fn f({}: ())", text))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-13 08:55:43 +00:00
|
|
|
pub fn record_pat(path: ast::Path, pats: impl IntoIterator<Item = ast::Pat>) -> ast::RecordPat {
|
2020-03-27 10:25:11 +00:00
|
|
|
let pats_str = pats.into_iter().join(", ");
|
|
|
|
return from_text(&format!("{} {{ {} }}", path, pats_str));
|
2019-09-26 09:18:26 +00:00
|
|
|
|
|
|
|
fn from_text(text: &str) -> ast::RecordPat {
|
|
|
|
ast_from_text(&format!("fn f({}: ())", text))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-10 20:42:04 +00:00
|
|
|
/// Returns a `BindPat` if the path has just one segment, a `PathPat` otherwise.
|
|
|
|
pub fn path_pat(path: ast::Path) -> ast::Pat {
|
2020-03-27 10:25:11 +00:00
|
|
|
return from_text(&path.to_string());
|
2020-01-10 20:42:04 +00:00
|
|
|
fn from_text(text: &str) -> ast::Pat {
|
2019-09-26 09:18:26 +00:00
|
|
|
ast_from_text(&format!("fn f({}: ())", text))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-13 08:40:51 +00:00
|
|
|
pub fn match_arm(pats: impl IntoIterator<Item = ast::Pat>, expr: ast::Expr) -> ast::MatchArm {
|
2020-03-27 10:25:11 +00:00
|
|
|
let pats_str = pats.into_iter().join(" | ");
|
|
|
|
return from_text(&format!("{} => {}", pats_str, expr));
|
2019-09-26 09:18:26 +00:00
|
|
|
|
|
|
|
fn from_text(text: &str) -> ast::MatchArm {
|
|
|
|
ast_from_text(&format!("fn f() {{ match () {{{}}} }}", text))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-13 08:40:51 +00:00
|
|
|
pub fn match_arm_list(arms: impl IntoIterator<Item = ast::MatchArm>) -> ast::MatchArmList {
|
2020-02-05 09:50:07 +00:00
|
|
|
let arms_str = arms
|
|
|
|
.into_iter()
|
|
|
|
.map(|arm| {
|
|
|
|
let needs_comma = arm.expr().map_or(true, |it| !it.is_block_like());
|
|
|
|
let comma = if needs_comma { "," } else { "" };
|
|
|
|
format!(" {}{}\n", arm.syntax(), comma)
|
|
|
|
})
|
|
|
|
.collect::<String>();
|
2020-02-18 12:53:02 +00:00
|
|
|
return from_text(&arms_str);
|
2019-09-26 09:18:26 +00:00
|
|
|
|
|
|
|
fn from_text(text: &str) -> ast::MatchArmList {
|
2020-02-05 09:50:07 +00:00
|
|
|
ast_from_text(&format!("fn f() {{ match () {{\n{}}} }}", text))
|
2019-09-26 09:18:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-13 08:55:43 +00:00
|
|
|
pub fn where_pred(
|
|
|
|
path: ast::Path,
|
|
|
|
bounds: impl IntoIterator<Item = ast::TypeBound>,
|
|
|
|
) -> ast::WherePred {
|
2020-03-27 10:25:11 +00:00
|
|
|
let bounds = bounds.into_iter().join(" + ");
|
|
|
|
return from_text(&format!("{}: {}", path, bounds));
|
2019-09-26 09:18:26 +00:00
|
|
|
|
|
|
|
fn from_text(text: &str) -> ast::WherePred {
|
|
|
|
ast_from_text(&format!("fn f() where {} {{ }}", text))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-13 08:55:43 +00:00
|
|
|
pub fn where_clause(preds: impl IntoIterator<Item = ast::WherePred>) -> ast::WhereClause {
|
2020-03-27 10:25:11 +00:00
|
|
|
let preds = preds.into_iter().join(", ");
|
2019-09-26 09:18:26 +00:00
|
|
|
return from_text(preds.as_str());
|
|
|
|
|
|
|
|
fn from_text(text: &str) -> ast::WhereClause {
|
|
|
|
ast_from_text(&format!("fn f() where {} {{ }}", text))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-13 08:40:51 +00:00
|
|
|
pub fn let_stmt(pattern: ast::Pat, initializer: Option<ast::Expr>) -> ast::LetStmt {
|
|
|
|
let text = match initializer {
|
2020-03-27 10:25:11 +00:00
|
|
|
Some(it) => format!("let {} = {};", pattern, it),
|
|
|
|
None => format!("let {};", pattern),
|
2019-11-13 08:40:51 +00:00
|
|
|
};
|
|
|
|
ast_from_text(&format!("fn f() {{ {} }}", text))
|
|
|
|
}
|
2020-02-07 10:51:16 +00:00
|
|
|
pub fn expr_stmt(expr: ast::Expr) -> ast::ExprStmt {
|
2020-03-27 11:12:17 +00:00
|
|
|
let semi = if expr.is_block_like() { "" } else { ";" };
|
|
|
|
ast_from_text(&format!("fn f() {{ {}{} (); }}", expr, semi))
|
2020-02-07 10:51:16 +00:00
|
|
|
}
|
2019-11-13 08:40:51 +00:00
|
|
|
|
2020-01-15 17:14:49 +00:00
|
|
|
pub fn token(kind: SyntaxKind) -> SyntaxToken {
|
|
|
|
tokens::SOURCE_FILE
|
|
|
|
.tree()
|
|
|
|
.syntax()
|
|
|
|
.descendants_with_tokens()
|
|
|
|
.filter_map(|it| it.into_token())
|
|
|
|
.find(|it| it.kind() == kind)
|
|
|
|
.unwrap_or_else(|| panic!("unhandled token: {:?}", kind))
|
|
|
|
}
|
|
|
|
|
2020-03-26 09:16:10 +00:00
|
|
|
pub fn unreachable_macro_call() -> ast::MacroCall {
|
|
|
|
ast_from_text(&format!("unreachable!()"))
|
|
|
|
}
|
|
|
|
|
2019-09-26 09:18:26 +00:00
|
|
|
fn ast_from_text<N: AstNode>(text: &str) -> N {
|
|
|
|
let parse = SourceFile::parse(text);
|
2020-02-04 12:22:32 +00:00
|
|
|
let node = parse.tree().syntax().descendants().find_map(N::cast).unwrap();
|
|
|
|
let node = node.syntax().clone();
|
|
|
|
let node = unroot(node);
|
|
|
|
let node = N::cast(node).unwrap();
|
|
|
|
assert_eq!(node.syntax().text_range().start(), 0.into());
|
|
|
|
node
|
|
|
|
}
|
|
|
|
|
2020-03-15 21:23:18 +00:00
|
|
|
fn try_ast_from_text<N: AstNode>(text: &str) -> Option<N> {
|
|
|
|
let parse = SourceFile::parse(text);
|
|
|
|
let node = parse.tree().syntax().descendants().find_map(N::cast)?;
|
|
|
|
let node = node.syntax().clone();
|
|
|
|
let node = unroot(node);
|
|
|
|
let node = N::cast(node).unwrap();
|
|
|
|
assert_eq!(node.syntax().text_range().start(), 0.into());
|
|
|
|
Some(node)
|
|
|
|
}
|
|
|
|
|
2020-02-04 12:22:32 +00:00
|
|
|
fn unroot(n: SyntaxNode) -> SyntaxNode {
|
|
|
|
SyntaxNode::new_root(n.green().clone())
|
2019-09-26 09:18:26 +00:00
|
|
|
}
|
2019-09-26 19:08:44 +00:00
|
|
|
|
|
|
|
pub mod tokens {
|
|
|
|
use once_cell::sync::Lazy;
|
|
|
|
|
2020-03-06 14:38:48 +00:00
|
|
|
use crate::{ast, AstNode, Parse, SourceFile, SyntaxKind::*, SyntaxToken};
|
|
|
|
|
2020-01-15 17:14:49 +00:00
|
|
|
pub(super) static SOURCE_FILE: Lazy<Parse<SourceFile>> =
|
2020-02-07 11:07:38 +00:00
|
|
|
Lazy::new(|| SourceFile::parse("const C: <()>::Item = (1 != 1, 2 == 2, !true)\n;"));
|
2019-11-24 05:14:57 +00:00
|
|
|
|
2019-09-26 19:08:44 +00:00
|
|
|
pub fn single_space() -> SyntaxToken {
|
|
|
|
SOURCE_FILE
|
|
|
|
.tree()
|
|
|
|
.syntax()
|
|
|
|
.descendants_with_tokens()
|
|
|
|
.filter_map(|it| it.into_token())
|
|
|
|
.find(|it| it.kind() == WHITESPACE && it.text().as_str() == " ")
|
|
|
|
.unwrap()
|
|
|
|
}
|
|
|
|
|
2019-10-12 19:07:47 +00:00
|
|
|
pub fn whitespace(text: &str) -> SyntaxToken {
|
|
|
|
assert!(text.trim().is_empty());
|
2020-03-08 16:13:04 +00:00
|
|
|
let sf = SourceFile::parse(text).ok().unwrap();
|
|
|
|
sf.syntax().first_child_or_token().unwrap().into_token().unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn doc_comment(text: &str) -> SyntaxToken {
|
|
|
|
assert!(!text.trim().is_empty());
|
2019-10-12 19:07:47 +00:00
|
|
|
let sf = SourceFile::parse(text).ok().unwrap();
|
|
|
|
sf.syntax().first_child_or_token().unwrap().into_token().unwrap()
|
|
|
|
}
|
|
|
|
|
2020-03-02 06:05:15 +00:00
|
|
|
pub fn literal(text: &str) -> SyntaxToken {
|
|
|
|
assert_eq!(text.trim(), text);
|
|
|
|
let lit: ast::Literal = super::ast_from_text(&format!("fn f() {{ let _ = {}; }}", text));
|
|
|
|
lit.syntax().first_child_or_token().unwrap().into_token().unwrap()
|
|
|
|
}
|
|
|
|
|
2019-09-26 19:08:44 +00:00
|
|
|
pub fn single_newline() -> SyntaxToken {
|
|
|
|
SOURCE_FILE
|
|
|
|
.tree()
|
|
|
|
.syntax()
|
|
|
|
.descendants_with_tokens()
|
|
|
|
.filter_map(|it| it.into_token())
|
|
|
|
.find(|it| it.kind() == WHITESPACE && it.text().as_str() == "\n")
|
|
|
|
.unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct WsBuilder(SourceFile);
|
|
|
|
|
|
|
|
impl WsBuilder {
|
|
|
|
pub fn new(text: &str) -> WsBuilder {
|
|
|
|
WsBuilder(SourceFile::parse(text).ok().unwrap())
|
|
|
|
}
|
|
|
|
pub fn ws(&self) -> SyntaxToken {
|
|
|
|
self.0.syntax().first_child_or_token().unwrap().into_token().unwrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|