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

Generic grammar traits #350

Closed
nblei opened this issue Jun 9, 2024 · 8 comments
Closed

Generic grammar traits #350

nblei opened this issue Jun 9, 2024 · 8 comments
Labels
wontfix This will not be worked on

Comments

@nblei
Copy link

nblei commented Jun 9, 2024

Jörg,

It would be useful to have the following:

pub trait <name>GrammarTrait {
    type RESULT=();
    type ERROR=();

    fn <non-terminal>(&mut self, _arg: &<non-terminal>) -> Result<Result<Self::RESULT, Self::ERROR>> {
        Ok((()))
    }
...
@jsinger67
Copy link
Owner

I see.
The problem is that the parser calls the semantic actions and needs to handle the errors right.
I'll check if there's a viable solution for it 😊

@nblei
Copy link
Author

nblei commented Jun 9, 2024

Thanks, Jörg.

This would be very nice because currently, to pass information through the AST requires creating some other data structure to store the information, which can be quite ackward!

@nblei
Copy link
Author

nblei commented Jun 9, 2024

Probably has to be type RESULT;, return value of Result<Option<Self::RESULT>> and default implementation of Ok(None)

@jsinger67
Copy link
Owner

Sounds like a plan 👍

@jsinger67
Copy link
Owner

Hi @nblei,
To fully grasp your intent, could you give me a small example of the type of problem you're currently struggling with?
Maybe it can be solved with the possibilities we already have. That's at least my hope 😊.

@nblei
Copy link
Author

nblei commented Jun 10, 2024

Jörg,

I'm working on type checking for Veryl. So I need to transform a large number of terminals and non-terminals into their type. This can likely be done by implementing TryFrom trait for them, but that's a ton of additional boilerplate which I would prefer to avoid.

@jsinger67
Copy link
Owner

The problem with your proposal is that the parser calls the semantic actions and simply doesn't know how to handle return values.
The intended way is to only return errors from semantic actions when it is ok to exit the parsing process.
So the ok type of result is always unit. It can't be used by the parser.
The error type already can be tweaked by using ParolError::UserError that can hold an anyhow::Error.
But as I said this can be used only as a value at exit.
But there is a mutable reference to the item that implements the grammar trait at each semantic action. Here you can perform any processing by manipulating the internal data of this item, i.e. you can update a symbol table.
Also you can use a stack that you can manipulate utilizing the fact that semantic actions are always called in depth first post order traversal. This means that all actions that belong to the sub tree of the AST node that is visited had been called before and their results are on the stack in the right order. Parol itself does this when filling the data structures of the AST from mere tokens.
Actually you have all freedom of choice.

@jsinger67
Copy link
Owner

jsinger67 commented Jun 10, 2024

I recomment you to implement only a view sematic actions where symbols play a role and collect the information for later evaluation.
Than do the actual evaluation in the semantic action of the start symbol.
This action is called at the end of the parsing process.
Or, instead of using a semantic action for the final evaluation you also can provide an extra function that can be called after successfull parse.
This depends on the structure of your application and on your preferences.

@jsinger67 jsinger67 added the wontfix This will not be worked on label Jun 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

2 participants