softdevteam / grmtools

Rust grammar tool libraries and binaries
Other
507 stars 31 forks source link

How to use the code generated by grmtools as a lib for others to use #455

Closed liluyue closed 4 months ago

liluyue commented 4 months ago

I want to use the generated parser as a library. Is there a command that can directly put all the code to the target location

ltratt commented 4 months ago

You can make a crate that's a library that is "just" the result of a grmtools parser if you want. As such, there's nothing special that grmtools needs to do.

ratmice commented 4 months ago

Is there a command that can directly put all the code to the target location

Just to elaborate, I believe what you are asking for here is provided by the CTParserBuilder::output_path function. But the normal routine of using build.rs should also work for library targets.

liluyue commented 4 months ago

CTParserBuilder::output_path It's what I need 。 But

But the normal routine of using build.rs should also work for library targets.

I don't understand. If I directly call and parse in the lib.rs of the project, an error will occur, so my understanding is that it is necessary to copy the generated code in order to create a lib

error[E0412]: cannot find type `TextList` in this scope
   --> /Users/liluyue/rust_workspace/markdown_parser/target/debug/build/markdown_parser-88f87a6945fb5904/out/markdown.y.rs:252:28
    |
252 |                  -> Result<TextList<'input>,()> {
    |                            ^^^^^^^^ not found in this scope
    |
ltratt commented 4 months ago

You probably need to use ...::TextList in the bottom part of your grammar (after a %%).

liluyue commented 4 months ago
But the normal routine of using build.rs should also work for library targets.

This should be feasible when the code is all placed in a. y file. For the sake of simplicity, I placed most of the code in lib.rs, . y and lib became interdependent, resulting in the occurrence of not found in this scope

ltratt commented 4 months ago

Without the actual grammar file, there is no way we can more specific. But my guess remains that you haven't imported TextList into your .y file.