WGUNDERWOOD / tex-fmt

LaTeX formatter written in Rust
MIT License
4 stars 1 forks source link

Switch syntax parsing to rowan using texlab #2

Open michaelvanstraten opened 4 weeks ago

WGUNDERWOOD commented 4 weeks ago

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 the parser 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 a rowan syntax tree?

michaelvanstraten commented 3 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.

WGUNDERWOOD commented 3 weeks ago

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());
}