mirror of
https://github.com/fish-shell/fish-shell.git
synced 2024-11-29 05:03:46 +08:00
971d257e67
The translation is fairly direct though it adds some duplication, for example there are multiple "match" statements that mimic function overloading. Rust has no overloading, and we cannot have generic methods in the Node trait (due to a Rust limitation, the error is like "cannot be made into an object") so we include the type name in method names. Give clients like "indent_visitor_t" a Rust companion ("IndentVisitor") that takes care of the AST traversal while the AST consumption remains in C++ for now. In future, "IndentVisitor" should absorb the entirety of "indent_visitor_t". This pattern requires that "fish_indent" be exposed includable header to the CXX bridge. Alternatively, we could define FFI wrappers for recursive AST traversal. Rust requires we separate the AST visitors for "mut" and "const" scenarios. Take this opportunity to concretize both visitors: The only client that requires mutable access is the populator. To match the structure of the C++ populator which makes heavy use of function overloading, we need to add a bunch of functions to the trait. Since there is no other mutable visit, this seems acceptable. The "const" visitors never use "will_visit_fields_of()" or "did_visit_fields_of()", so remove them (though this is debatable). Like in the C++ implementation, the AST nodes themselves are largely defined via macros. Union fields like "Statement" and "ArgumentOrRedirection" do currently not use macros but may in future. This commit also introduces a precedent for a type that is defined in one CXX bridge and used in another one - "ParseErrorList". To make this work we need to manually define "ExternType". There is one annoyance with CXX: functions that take explicit lifetime parameters require to be marked as unsafe. This makes little sense because functions that return `&Foo` with implicit lifetime can be misused the same way on the C++ side. One notable change is that we cannot directly port "find_block_open_keyword()" (which is used to compute an error) because it relies on the stack of visited nodes. We cannot modify a stack of node references while we do the "mut" walk. Happily, an idiomatic solution is easy: we can tell the AST visitor to backtrack to the parent node and create the error there. Since "node_t::accept_base" is no longer a template we don't need the "node_visitation_t" trampoline anymore. The added copying at the FFI boundary makes things slower (memcpy dominates the profile) but it's not unusable, which is good news: $ hyperfine ./fish.{old,new}" -c 'source ../share/completions/git.fish'" Benchmark 1: ./fish.old -c 'source ../share/completions/git.fish' Time (mean ± σ): 195.5 ms ± 2.9 ms [User: 190.1 ms, System: 4.4 ms] Range (min … max): 193.2 ms … 205.1 ms 15 runs Benchmark 2: ./fish.new -c 'source ../share/completions/git.fish' Time (mean ± σ): 677.5 ms ± 62.0 ms [User: 665.4 ms, System: 10.0 ms] Range (min … max): 611.7 ms … 805.5 ms 10 runs Summary './fish.old -c 'source ../share/completions/git.fish'' ran 3.47 ± 0.32 times faster than './fish.new -c 'source ../share/completions/git.fish'' Leftovers: - Enum variants are still snakecase; I didn't get around to changing this yet. - "ast_type_to_string()" still returns a snakecase name. This could be changed since it's not user visible.
93 lines
3.2 KiB
Rust
93 lines
3.2 KiB
Rust
use crate::ast::{self, Category, Node, NodeFfi, NodeVisitor, Type};
|
|
use crate::ffi::pretty_printer_t;
|
|
use crate::parse_constants::ParseTokenType;
|
|
use std::pin::Pin;
|
|
|
|
struct PrettyPrinter<'a> {
|
|
companion: Pin<&'a mut pretty_printer_t>,
|
|
}
|
|
impl<'a> NodeVisitor<'a> for &mut PrettyPrinter<'a> {
|
|
// Default implementation is to just visit children.
|
|
fn visit(&mut self, node: &'a dyn Node) {
|
|
let ffi_node = NodeFfi::new(node);
|
|
// Leaf nodes we just visit their text.
|
|
if node.as_keyword().is_some() {
|
|
self.companion
|
|
.as_mut()
|
|
.emit_node_text((&ffi_node as *const NodeFfi<'_>).cast());
|
|
return;
|
|
}
|
|
if let Some(token) = node.as_token() {
|
|
if token.token_type() == ParseTokenType::end {
|
|
self.companion
|
|
.as_mut()
|
|
.visit_semi_nl((&ffi_node as *const NodeFfi<'_>).cast());
|
|
return;
|
|
}
|
|
self.companion
|
|
.as_mut()
|
|
.emit_node_text((&ffi_node as *const NodeFfi<'_>).cast());
|
|
return;
|
|
}
|
|
match node.typ() {
|
|
Type::argument | Type::variable_assignment => {
|
|
self.companion
|
|
.as_mut()
|
|
.emit_node_text((&ffi_node as *const NodeFfi<'_>).cast());
|
|
}
|
|
Type::redirection => {
|
|
self.companion.as_mut().visit_redirection(
|
|
(node.as_redirection().unwrap() as *const ast::Redirection).cast(),
|
|
);
|
|
}
|
|
Type::maybe_newlines => {
|
|
self.companion.as_mut().visit_maybe_newlines(
|
|
(node.as_maybe_newlines().unwrap() as *const ast::MaybeNewlines).cast(),
|
|
);
|
|
}
|
|
Type::begin_header => {
|
|
// 'begin' does not require a newline after it, but we insert one.
|
|
node.accept(self, false);
|
|
self.companion.as_mut().visit_begin_header();
|
|
}
|
|
_ => {
|
|
// For branch and list nodes, default is to visit their children.
|
|
if [Category::branch, Category::list].contains(&node.category()) {
|
|
node.accept(self, false);
|
|
return;
|
|
}
|
|
panic!("unexpected node type");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
#[cxx::bridge]
|
|
#[allow(clippy::needless_lifetimes)] // false positive
|
|
mod fish_indent_ffi {
|
|
extern "C++" {
|
|
include!("ast.h");
|
|
include!("fish_indent_common.h");
|
|
type pretty_printer_t = crate::ffi::pretty_printer_t;
|
|
type Ast = crate::ast::Ast;
|
|
type NodeFfi<'a> = crate::ast::NodeFfi<'a>;
|
|
}
|
|
extern "Rust" {
|
|
type PrettyPrinter<'a>;
|
|
unsafe fn new_pretty_printer(
|
|
companion: Pin<&mut pretty_printer_t>,
|
|
) -> Box<PrettyPrinter<'_>>;
|
|
#[cxx_name = "visit"]
|
|
unsafe fn visit_ffi<'a>(self: &mut PrettyPrinter<'a>, node: &'a NodeFfi<'a>);
|
|
}
|
|
}
|
|
|
|
fn new_pretty_printer(companion: Pin<&mut pretty_printer_t>) -> Box<PrettyPrinter<'_>> {
|
|
Box::new(PrettyPrinter { companion })
|
|
}
|
|
impl<'a> PrettyPrinter<'a> {
|
|
fn visit_ffi(mut self: &mut PrettyPrinter<'a>, node: &'a NodeFfi<'a>) {
|
|
self.visit(node.as_node());
|
|
}
|
|
}
|