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

Foreign rule invocation (reference namespaced grammars without extending them) #191

Open
nickclaw opened this issue Feb 25, 2017 · 3 comments

Comments

@nickclaw
Copy link

For example, something like:

import ohm from 'ohm';
import { grammar as json } from 'ohm-grammar-json';

const namespace = ohm.namespace({ JSON: json });
const definition = ```
Example {
  Start
    = "foo" JSON.Value
}
```;

export default ns.grammar(definition, namespace);

where JSON.Value represents the Value syntactic rule of the JSON namespace.

@alexwarth
Copy link
Contributor

alexwarth commented Feb 25, 2017 via email

@nickclaw
Copy link
Author

Thanks for responding to this so quickly, I'm glad you're interested in something like this.

What about a format like this for actions and attributes?

{
   Start(...args) { /* code */ },
    
   // this way you could potentially get the logic here from the module
   // that implements the grammar. The `<Symbol>` (tbd) would be
   // there to distinguish the namespace from rules of the same name
   <Symbol>CustomNamespaceNameForJson: {
      Start(...args) {  /* code */ },
   },
}

As for your other points I think:

  1. I personally prefer always having fully qualified names, I think it's important for "self documenting" so other people reading the grammar on have no question about ownership of a rule.

  2. No strong opinion about the syntax. Another form could be Rule@Grammar.

@philippotto
Copy link

philippotto commented Jun 21, 2017

Hello Alex,

What do you people think? Does this seem reasonable? Can you think of a
better alternative to what I've proposed?

I think your suggestion sounds very reasonable. Regarding the fully qualified names, I would make them mandatory unless the rule belongs to the "own" grammar. As to the syntax, I would slightly prefer Rule$Grammar over "Rule.Grammar", because having a valid JS identifier can be quite useful.

Since I could really use foreign rule invocation for my master's thesis, I would be willing to invest a few days (if this is realistic) to implement it for Ohm. Could you give me some pointers on how to approach this, @alexwarth? This would be super helpful, since I do not really have a good understanding of the inner parts of Ohm.

My first idea would be to change pexprs.Apply.prototype.reallyEval so that it detects foreign rule invocations and then swaps the current grammar in the MatchState with the foreign one. After the grammar was swapped, it could do the usual rule lookup and then swap the grammar back. However, I'm not sure if this is the best way to approach this, is it?

@pdubroy pdubroy changed the title Is it possible to reference namespaced grammars without extending them? Foreign rule invocation (reference namespaced grammars without extending them) Aug 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants