Code Monkey home page Code Monkey logo

Comments (9)

nikomatsakis avatar nikomatsakis commented on May 23, 2024 4

Discussing with @wagenet revealed an additional feature-request for LALRPOP modules: the ability to declare generic parameters on them, so that one can have common code that is included in multiple places, but with slight tweaks.

I am thinking something like this. One can declare a partial grammar like so:

// predicate.lalrpop
#[partial] // this indicates that the grammar is not meant to be used on its own
grammar();

// in a partial grammar, you can declare empty nonterminals:
Atom;

// these can be used just like you would use a macro parameter,
// so they can appear in types and in expressions:
Predicate: Predicate<Atom> = {
    <a:Atom> => Predicate::Atom(a),
    <l:Predicate> "or" <r:Predicate> => Predicate::Or(l, r),
    <l:Predicate> "and" <r:Predicate> => Predicate::And(l, r),
};

Then one can use a #[partial] grammar like so:

use grammar "predicate.lalrpop" where {
    Atom = Identifier
};

...

Identifier: String = ...;

Foo = {
    Predicate, // this will have type Predicate<String>, since Identifier: String
};

These partial grammars would really be treated like macros. That is, we will parse partial grammars in LALRPOP but otherwise ignore them. The macro-expansion phase will replace a use grammar "xyz" by loading the file, parsing it, and expanding it inline, possibly doing various substitutions as it goes.

We could probably include the ability to make various other tweaks; for example, it might be nice if you could modify the names of the things in that grammar in some systematic way to avoid conflicts. e.g. if I have two versions of the same grammar:

// Propositional logic
use grammar "predicate.lalrpop" as P where {
    Atom = Identifier
};

// First-order logic
use grammar "predicate.lalrpop" as F where {
    Atom = FunctionCall
};

both of these would want to define Predicate. By writing as P or as F, I'd be renaming those entries to something like P::Predicate and F::Predicate. But let's leave that for future work.

from lalrpop.

nikomatsakis avatar nikomatsakis commented on May 23, 2024

Not currently possible, but I've been wondering how long until somebody asks for it. :) Seems like a good idea. I have to think about how to do that. Shouldn't be too hard to do something.

On Fri, Nov 13, 2015, at 11:34 PM, Saghm Rossi wrote:

Is there any way to import one parser file's nonterminals for use in
another? The .lalrpop file I'm writing has been getting pretty large,
so I was hoping to modularize it a bit. However, I'm not sure how to
go about doing if (or if it's possible about all).

— Reply to this email directly or view it on GitHub[1].

Links:

  1. https://github.com/nikomatsakis/lalrpop/issues/42

from lalrpop.

saghm avatar saghm commented on May 23, 2024

One idea simple I had would be to specify a vector of the names of the lalrpop files to specify the ordering rather than just having process_root, and allowing the nonterminals of a completed file be available to the later ones. I haven't dug very deeply into the code though, so I don't know how feasible this would be.

from lalrpop.

nikomatsakis avatar nikomatsakis commented on May 23, 2024

I was thinking of adding a kind of "include" directive, so that one lalrpop file can include others. (These included files would not be processed by the main driver; probably they would not have a grammar declaration at all, actually, so that we know they are not "main" files.) This would certainly be a simple stopgap measure for now. Eventually I'd like to allow a more sophisticated system for importing and evolving grammars, similar to PPG.

from lalrpop.

xasopheno avatar xasopheno commented on May 23, 2024

➕1 As my grammar is growing, it would be nice to start to split it up by concern.

from lalrpop.

QuentinJanuel avatar QuentinJanuel commented on May 23, 2024

Is this implemented yet?

from lalrpop.

Dylan-DPC-zz avatar Dylan-DPC-zz commented on May 23, 2024

@QuentinJanuel nope. But if you want to try it, feel free to give it an attempt and submit a PR :)

from lalrpop.

QuentinJanuel avatar QuentinJanuel commented on May 23, 2024

I'm not familiar enough with Rust macros yet so I don't think I am capable of solving this, but I will still give it a try I guess

from lalrpop.

 avatar commented on May 23, 2024

@QuentinJanuel any luck? I think this is a super awesome feature if it can get implemented!

from lalrpop.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.