Open michaelvanstraten opened 4 weeks ago
There is the parse_latex
function, which basically does what we need.
Since rowan
is a lossless parser, we can then take the generated tree and, directly, transform it back into the same input. Formatting is then just the act of mutating the tree until the desired result is achieved. I've thought about different abstraction levels yesterday, but I haven't come to a satisfactory solution yet.
I've messaged you on Keybase.
Here's an explicit example of how this might work.
use texlab::parser::parse_latex;
use texlab::syntax::latex::SyntaxNode;
fn main() {
// sample text
let text = "\
\\documentclass{article}\n\
\\begin{document}\n\
\\begin{equation}\n \
E = m c^2\n\
\\end{equation}\n\
\\end{document}";
// convert the text to a syntax tree
let tree = SyntaxNode::new_root(parse_latex(&text));
// display the syntax tree
dbg!(&tree);
// convert the syntax tree back to text
println!("{}", &tree.to_string());
}
Thanks for this suggestion; I would definitely be interested in moving the syntax parsing to a system based on
texlab
. From a brief look around, it seems that I might need to use theparser
crate at https://github.com/latex-lsp/texlab/tree/master/crates/parser/src. Let me know if this sounds correct -- maybe there is a high-level function we can use to parse the document initially into arowan
syntax tree?