Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

appropriate to build an ast using nom? #255

Closed
mspiegel opened this issue Apr 30, 2016 · 6 comments
Closed

appropriate to build an ast using nom? #255

mspiegel opened this issue Apr 30, 2016 · 6 comments

Comments

@mspiegel
Copy link

I was trying to adapt the arithmetic.rs example and have the parser generate an AST rather than compute the result. I am very new to rust and was getting tripped up. When I am creating new values of the AST enum I think I needed to use the try! macro but I wasn't sure how to combine that with the nom macros. Is nom a good use case for generating the ASTs?

@mspiegel
Copy link
Author

I'm going to make another attempt to write this in the next few days. I think I know what mistakes I made in the first attempt.

@mspiegel mspiegel closed this as completed May 1, 2016
@Geal
Copy link
Collaborator

Geal commented May 2, 2016

Hi! Any reason you closed this issue? Did you find a way to generate an AST? I'm sorry I can't answer fast, I'm currently traveling.

@mspiegel
Copy link
Author

mspiegel commented May 2, 2016

Thanks! I closed the ticket because I wasn't sure if reporting my question as an issue was the appropriate forum. In any event I've made some progress in a branch: https://github.com/mspiegel/nom/blob/arithmetic_ast/tests/arithmetic_ast.rs. The downside is that in a few instances I have to clone the entire AST which I suspect is not the right way to do it. See for example, https://github.com/mspiegel/nom/blob/arithmetic_ast/tests/arithmetic_ast.rs#L75. Whenever you have the chance any help would be 1000% appreciated.

Once I get this AST calculator working I was planning on applying the same strategy to submit a pull request for ehuss/license-exprs#3

@mspiegel
Copy link
Author

mspiegel commented May 5, 2016

Yup I'm very stuck about how to rewrite my initial version without relying on clone. I think there are two problems but my knowledge of rust is very poor so there may be N problems for any value of N. One problem is that I can't assign "expr" to a new Expr value that is constructed out of the old "expr" reference. The other problem is that the Expr gets moved into the closure because it's not copyable so then I can't return "acc" on line https://github.com/mspiegel/nom/blob/arithmetic_ast/tests/arithmetic_ast.rs#L79. I've tried playing around with Expr, Box, RefCell, unsafe, I couldn't get a working solution.

@mspiegel mspiegel reopened this May 5, 2016
@mspiegel
Copy link
Author

mspiegel commented May 5, 2016

Aha wait I had an insight. Instead of trying to modifying the Expr while parsing, I should be appending some things onto a vector while consuming the input and then in the return closure I can build the final Expr using the vector. I will try that tomorrow morning. Sorry for the comment spam.

@mspiegel
Copy link
Author

mspiegel commented May 5, 2016

Success!!!!!!! I'll submit a pull request.

@mspiegel mspiegel closed this as completed May 5, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants