Slightly more fluent API
This commit is contained in:
parent
8296d3208d
commit
c84010e246
@ -22,7 +22,7 @@ impl ast::BinExpr {
|
|||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn replace_op(&self, op: SyntaxKind) -> Option<ast::BinExpr> {
|
pub fn replace_op(&self, op: SyntaxKind) -> Option<ast::BinExpr> {
|
||||||
let op_node: SyntaxElement = self.op_details()?.0.into();
|
let op_node: SyntaxElement = self.op_details()?.0.into();
|
||||||
let to_insert: Option<SyntaxElement> = Some(tokens::op(op).into());
|
let to_insert: Option<SyntaxElement> = Some(make::token(op).into());
|
||||||
Some(replace_children(self, single_node(op_node), to_insert.into_iter()))
|
Some(replace_children(self, single_node(op_node), to_insert.into_iter()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
//! of smaller pieces.
|
//! of smaller pieces.
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
|
|
||||||
use crate::{algo, ast, AstNode, SourceFile};
|
use crate::{algo, ast, AstNode, SourceFile, SyntaxKind, SyntaxToken};
|
||||||
|
|
||||||
pub fn name(text: &str) -> ast::Name {
|
pub fn name(text: &str) -> ast::Name {
|
||||||
ast_from_text(&format!("mod {};", text))
|
ast_from_text(&format!("mod {};", text))
|
||||||
@ -181,28 +181,28 @@ pub fn let_stmt(pattern: ast::Pat, initializer: Option<ast::Expr>) -> ast::LetSt
|
|||||||
ast_from_text(&format!("fn f() {{ {} }}", text))
|
ast_from_text(&format!("fn f() {{ {} }}", text))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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))
|
||||||
|
}
|
||||||
|
|
||||||
fn ast_from_text<N: AstNode>(text: &str) -> N {
|
fn ast_from_text<N: AstNode>(text: &str) -> N {
|
||||||
let parse = SourceFile::parse(text);
|
let parse = SourceFile::parse(text);
|
||||||
parse.tree().syntax().descendants().find_map(N::cast).unwrap()
|
parse.tree().syntax().descendants().find_map(N::cast).unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub mod tokens {
|
pub mod tokens {
|
||||||
use crate::{AstNode, Parse, SourceFile, SyntaxKind, SyntaxKind::*, SyntaxToken, T};
|
use crate::{AstNode, Parse, SourceFile, SyntaxKind::*, SyntaxToken, T};
|
||||||
use once_cell::sync::Lazy;
|
use once_cell::sync::Lazy;
|
||||||
|
|
||||||
static SOURCE_FILE: Lazy<Parse<SourceFile>> =
|
pub(super) static SOURCE_FILE: Lazy<Parse<SourceFile>> =
|
||||||
Lazy::new(|| SourceFile::parse("const C: () = (1 != 1, 2 == 2)\n;"));
|
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()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn comma() -> SyntaxToken {
|
pub fn comma() -> SyntaxToken {
|
||||||
SOURCE_FILE
|
SOURCE_FILE
|
||||||
.tree()
|
.tree()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user