Keep track of locations in the AST#21
Conversation
|
Thanks for reminding me about #18. I've merged it so you can now rebase. At first glance, your patch looks alright but the way you've added locs looks a bit clumsy to me. Why not have a type like @Drup you're a loc expert, could you please comment about the above? |
|
Rebased. About locations, I'm not sure to understand what you mean. Do you want to have locations separate from the mustache AST? I'm not sure how to do that, given each node of the AST is supposed to be annotated with a location. |
|
Ah yes, you're right. Shouldn't have commented in haste. It's a been annoying to have to impose locations on people who won't necessarily Will have a closer look at this in the next couple of days. In-Reply-To: rgrinberg/ocaml-mustache/pull/21/c240966472@github.com On 08/19, Armael wrote:
|
|
I would have done something like: It simplifies things a bit, and avoid repetitions. |
|
Also, it may be a good idea to have two datatypes, one AST with loc (and that could be much more precise), and one that is just the (simplified) datatype, appropriate for manipulation in OCaml. |
|
Would that mean duplicating the parser? An other option is to have a function that takes an annotated AST and produces a simplified AST, by removing locations (and the parser would produce annotated ASTs). |
|
The second solution, of course. |
|
I'm sorry it took me so long to get back to this. Existing tests pass; I did not add tests checking locations at the moment. |
|
I added a test doing a roundtrip add_dummy_locs -> erase_locs. |
|
I thought a tuple for the loc would be more natural. But it doesn't matter so much. @Drup Would you mind taking a look? It looks good to me but it would be good to have your opinion here. |
|
I adapted my mustache-ppx to the new module One nice (future ?) addition would to raise/return a better error when parsing fails, including the location of the failure. |
Probably future addition If I was implementing it. I have no idea how to accomplish this without rewriting the parser by hand. |
|
Thanks @Armael |
This is WIP for keeping track of locations in the AST.
The motivation for this is ocsigen/tyxml#128 .
The core feature itself is implemented, namely producing an AST whose nodes are annotated by locations. What remains is polishing the API & tooling, and for that I thought I'd ask before implementing something potentially unsatisfactory.
Remarks/questions:
Mustache.ttype; I guess make Mustache.t non abstract #18 should be merged first, then this PR rebased on top of it;Mustachemodule ignore locations, and put a dummy value when one is needed. I'm not sure what would be a nicer API for these (which would probably be incompatible with the current one);Mustacheinsert dummy locations, which do not match the actual locations provided by the parser. One solution I see would be to directly hand-write the AST (tedious), or improve these helper functions (equally or less tedious depending on how they are improved, I guess).