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-01-10 17:26:18 +00:00
|
|
|
use crate::{algo, ast, AstNode, SourceFile};
|
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))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn path_from_name_ref(name_ref: ast::NameRef) -> ast::Path {
|
|
|
|
path_from_text(&name_ref.syntax().to_string())
|
|
|
|
}
|
|
|
|
pub fn path_qualified(qual: ast::Path, name_ref: ast::NameRef) -> ast::Path {
|
|
|
|
path_from_text(&format!("{}::{}", qual.syntax(), name_ref.syntax()))
|
|
|
|
}
|
|
|
|
fn path_from_text(text: &str) -> ast::Path {
|
|
|
|
ast_from_text(text)
|
|
|
|
}
|
2020-01-03 18:58:56 +00:00
|
|
|
pub fn path_with_type_arg_list(path: ast::Path, args: Option<ast::TypeArgList>) -> ast::Path {
|
|
|
|
if let Some(args) = args {
|
2020-01-10 17:26:18 +00:00
|
|
|
let syntax = path.syntax();
|
|
|
|
// FIXME: remove existing type args
|
|
|
|
let new_syntax = algo::insert_children(
|
|
|
|
syntax,
|
|
|
|
crate::algo::InsertPosition::Last,
|
|
|
|
&mut Some(args).into_iter().map(|n| n.syntax().clone().into()),
|
|
|
|
);
|
|
|
|
ast::Path::cast(new_syntax).unwrap()
|
2020-01-03 18:58:56 +00:00
|
|
|
} else {
|
|
|
|
path
|
|
|
|
}
|
|
|
|
}
|
2019-09-26 09:18:26 +00:00
|
|
|
|
|
|
|
pub fn record_field(name: ast::NameRef, expr: Option<ast::Expr>) -> ast::RecordField {
|
|
|
|
return match expr {
|
|
|
|
Some(expr) => from_text(&format!("{}: {}", name.syntax(), expr.syntax())),
|
|
|
|
None => from_text(&name.syntax().to_string()),
|
|
|
|
};
|
|
|
|
|
|
|
|
fn from_text(text: &str) -> ast::RecordField {
|
|
|
|
ast_from_text(&format!("fn f() {{ S {{ {}, }} }}", text))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn block_from_expr(e: ast::Expr) -> ast::Block {
|
|
|
|
return from_text(&format!("{{ {} }}", e.syntax()));
|
|
|
|
|
|
|
|
fn from_text(text: &str) -> ast::Block {
|
|
|
|
ast_from_text(&format!("fn f() {}", text))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn expr_unit() -> ast::Expr {
|
|
|
|
expr_from_text("()")
|
|
|
|
}
|
|
|
|
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 {
|
|
|
|
expr_from_text(&path.syntax().to_string())
|
|
|
|
}
|
|
|
|
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 {
|
|
|
|
expr_from_text(&format!("match {} {}", expr.syntax(), match_arm_list.syntax()))
|
|
|
|
}
|
2019-09-26 09:18:26 +00:00
|
|
|
fn expr_from_text(text: &str) -> ast::Expr {
|
|
|
|
ast_from_text(&format!("const C: () = {};", text))
|
|
|
|
}
|
|
|
|
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2019-11-13 08:55:43 +00:00
|
|
|
let pats_str = pats.into_iter().map(|p| p.syntax().to_string()).join(", ");
|
2019-09-26 09:18:26 +00:00
|
|
|
return from_text(&format!("{}({})", path.syntax(), pats_str));
|
|
|
|
|
|
|
|
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 {
|
|
|
|
let pats_str = pats.into_iter().map(|p| p.syntax().to_string()).join(", ");
|
2019-10-26 15:03:55 +00:00
|
|
|
return from_text(&format!("{} {{ {} }}", path.syntax(), pats_str));
|
2019-09-26 09:18:26 +00:00
|
|
|
|
|
|
|
fn from_text(text: &str) -> ast::RecordPat {
|
|
|
|
ast_from_text(&format!("fn f({}: ())", text))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn path_pat(path: ast::Path) -> ast::PathPat {
|
|
|
|
let path_str = path.syntax().text().to_string();
|
|
|
|
return from_text(path_str.as_str());
|
|
|
|
fn from_text(text: &str) -> ast::PathPat {
|
|
|
|
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 {
|
|
|
|
let pats_str = pats.into_iter().map(|p| p.syntax().to_string()).join(" | ");
|
2019-09-26 09:18:26 +00:00
|
|
|
return from_text(&format!("{} => {}", pats_str, expr.syntax()));
|
|
|
|
|
|
|
|
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 {
|
|
|
|
let arms_str = arms.into_iter().map(|arm| format!("\n {}", arm.syntax())).join(",");
|
2019-09-26 09:18:26 +00:00
|
|
|
return from_text(&format!("{},\n", arms_str));
|
|
|
|
|
|
|
|
fn from_text(text: &str) -> ast::MatchArmList {
|
|
|
|
ast_from_text(&format!("fn f() {{ match () {{{}}} }}", text))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-13 08:55:43 +00:00
|
|
|
pub fn where_pred(
|
|
|
|
path: ast::Path,
|
|
|
|
bounds: impl IntoIterator<Item = ast::TypeBound>,
|
|
|
|
) -> ast::WherePred {
|
|
|
|
let bounds = bounds.into_iter().map(|b| b.syntax().to_string()).join(" + ");
|
2019-09-26 09:18:26 +00:00
|
|
|
return from_text(&format!("{}: {}", path.syntax(), bounds));
|
|
|
|
|
|
|
|
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 {
|
|
|
|
let preds = preds.into_iter().map(|p| p.syntax().to_string()).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-10-20 18:00:09 +00:00
|
|
|
pub fn if_expression(condition: &ast::Expr, statement: &str) -> ast::IfExpr {
|
2019-10-30 17:36:37 +00:00
|
|
|
ast_from_text(&format!(
|
2019-10-20 18:00:09 +00:00
|
|
|
"fn f() {{ if !{} {{\n {}\n}}\n}}",
|
|
|
|
condition.syntax().text(),
|
|
|
|
statement
|
2019-10-30 17:36:37 +00:00
|
|
|
))
|
2019-10-20 18:00:09 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
Some(it) => format!("let {} = {};", pattern.syntax(), it.syntax()),
|
|
|
|
None => format!("let {};", pattern.syntax()),
|
|
|
|
};
|
|
|
|
ast_from_text(&format!("fn f() {{ {} }}", text))
|
|
|
|
}
|
|
|
|
|
2019-09-26 09:18:26 +00:00
|
|
|
fn ast_from_text<N: AstNode>(text: &str) -> N {
|
|
|
|
let parse = SourceFile::parse(text);
|
2019-12-20 20:14:30 +00:00
|
|
|
parse.tree().syntax().descendants().find_map(N::cast).unwrap()
|
2019-09-26 09:18:26 +00:00
|
|
|
}
|
2019-09-26 19:08:44 +00:00
|
|
|
|
|
|
|
pub mod tokens {
|
2019-11-24 05:14:57 +00:00
|
|
|
use crate::{AstNode, Parse, SourceFile, SyntaxKind, SyntaxKind::*, SyntaxToken, T};
|
2019-09-26 19:08:44 +00:00
|
|
|
use once_cell::sync::Lazy;
|
|
|
|
|
2019-11-24 05:14:57 +00:00
|
|
|
static SOURCE_FILE: Lazy<Parse<SourceFile>> =
|
|
|
|
Lazy::new(|| SourceFile::parse("const C: () = (1 != 1, 2 == 2)\n;"));
|
|
|
|
|
|
|
|
pub fn op(op: SyntaxKind) -> SyntaxToken {
|
|
|
|
SOURCE_FILE
|
|
|
|
.tree()
|
|
|
|
.syntax()
|
|
|
|
.descendants_with_tokens()
|
|
|
|
.filter_map(|it| it.into_token())
|
|
|
|
.find(|it| it.kind() == op)
|
|
|
|
.unwrap()
|
|
|
|
}
|
2019-09-26 19:08:44 +00:00
|
|
|
|
|
|
|
pub fn comma() -> SyntaxToken {
|
|
|
|
SOURCE_FILE
|
|
|
|
.tree()
|
|
|
|
.syntax()
|
|
|
|
.descendants_with_tokens()
|
|
|
|
.filter_map(|it| it.into_token())
|
|
|
|
.find(|it| it.kind() == T![,])
|
|
|
|
.unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
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());
|
|
|
|
let sf = SourceFile::parse(text).ok().unwrap();
|
|
|
|
sf.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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|