udoprog / genco

A whitespace-aware quasiquoter for beautiful code generation.
Apache License 2.0
181 stars 11 forks source link

make api like `quote`. #19

Closed dvc94ch closed 2 years ago

dvc94ch commented 2 years ago

So I've been using your crate and quite like it. Once I get some time I'll have to read the code to understand how it works. For 1.0 it might make sense to make the macro work like the quote crate, unless there's a good reason not to. The areas where it differs would be:

udoprog commented 2 years ago

Thanks for the suggestion and sorry for late reply! I'll be going through some thoughts of mine below.

A handful of things speak against adopting quote's loop syntax (which was used initially). genco targets any language, including whitespace sensitive ones like python. So being parsimonious with "reserved" syntax is useful. We currently only reserve the use of #<ident>, #(...), and #_(...) IIRC. We avoid having to figure out how to solve ambiguous expansions like #(...)* where we actually want it to expand to the inner statement directly followed by a *. This is a non-issue with Rust's token streams (which quote targets) because adding a space there makes no difference in how it's parsed in Rust. Finally genco supports more control flows (match and if), so staying consistent across control flow structures is more important to me than doing the same as quote.

For string formatting. This syntax was chosen because building stringy things by providing the quote function with a pre-computed string can be quite intrusive (i.e. the string has to be computed outside of the quote). Formatting a string outside also necessarily requires extra allocations. You can see an example of what computed string are expanded to here.

Identifiers in genco do not exist in the sense as they have to be Ident tokens like they do in quote. Everything is literal values as strings (in one form or another), quoted strings, or various forms of whitespace. So we don't need to make formatting "identifiers" harder when we don't have the same requirements.

dvc94ch commented 2 years ago

makes sense, thanks for elaborating. one more issue is it working on stable. could parsing the span Debug implementation work for now?

udoprog commented 2 years ago

It might, all though Debug impls are intentionally not part of stable public API so it could break in the future if it's even possible right now. It might not the best foundation to stabilize on.

dvc94ch commented 2 years ago

well, it working on stable would be great, as now we need to use nightly everywhere due to using genco in https://github.com/cloudpeers/ffi-gen. currently care less about breakage as ffi-gen is used internally.

udoprog commented 2 years ago

Yeah, I know. I wouldn't recommend using genco anywhere except in end applications that are OK with building on nightly or internal tooling.

See https://github.com/rust-lang/rust/issues/54725