cenotelie / hime

Apache License 2.0
27 stars 4 forks source link

How to make language specific AST? #99

Open stevefan1999-personal opened 11 months ago

stevefan1999-personal commented 11 months ago

I want to produce my own AST like this:

pub struct Expr {
    Atom(Identifier),
    Constant(Constant),
    Unary(Operator, Box<Expr>),
    Binary(Box<Expr>, Operator, Box<Expr>),
    Declare(Type, Identifier, Option<Box<Expr>>),
}

What is the best way to let us handle the AST forming part?

woutersl commented 11 months ago

At this time, there is no specific support for this in Hime. You can manually walk the AST/SPPF to reconstruct domain objects. That is what is done in the Hime SDK to load the grammar AST into domain objects for variables, rules, etc. I have other private examples like this.

What could be done for the Rust-specific runtime, is to expose a bunch of traits to mark structures that could be reconstructed from AST/SPPF and use that to produce the domain objects from the AST. Maybe we could hijack serde for this.

The ultimate solution would be to not produce the AST/SPPF at all and use user-configurable logic to build the domain objects, but that is way more complicated.

Ultimately, there is a difference in language theory between the concrete syntax tree, what we call AST/SPPF in Hime, i.e. the tree of tokens in the input ; and the real abstract syntax tree, which is what you mentions, domain objects. Those are separate things and the term AST in a bit of a misnomer in the parser generator ecosystem.