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

Proposal: Support grammar aliases. #22

Open
alecthomas opened this issue May 10, 2018 · 7 comments
Open

Proposal: Support grammar aliases. #22

alecthomas opened this issue May 10, 2018 · 7 comments
Labels

Comments

@alecthomas
Copy link
Owner

In moderately complex grammars it's fairly common to see duplicate patterns emerge. For example, when matching a dot-separated identifier (eg. foo.bar) the pattern (Ident { "." Ident }) is used repeatedly. This can be handled by a Go type alias implementing the Parseable interface, but that is quite onerous.

I propose adding support for grammar aliases. Here's an example creating and using an Identifier alias:

type Assignment struct {
  Name string `@Identifier=(Ident { "." Ident })`
  Variable string `"=" @Identifier`
}
@shahms
Copy link

shahms commented Feb 12, 2019

Alternatively (or in addition to) just making it easier to go from a grammar string to something which implements Parseable would be nice. One way of factoring out the common patterns is to put them in a separate struct and using @@, but that introduces an otherwise unnecessary layer of indirection.

Having a facility parallel to Build which took a "root" instance and grammar string and returned a semi-parser would go a long way, e.g.

var identParser = participle.MustParseable(&Identifier{}, `@(Ident { "." Ident })`)

type Identifer string
type Assignment struct {
  Name Identifier `@@`
  Variable Identifier `"=" @@`
}

func (i *Identifer) Parse(l lexer.PeekingLexer) error {
  return identParser.ParseInto(i, l)
}

@alecthomas
Copy link
Owner Author

That's not a bad idea at all. All it would take would be to add an extra method to the Parser.

@alecthomas
Copy link
Owner Author

Though it's a bit inconvenient in that, as you say, you end up with a separate struct where you might only need a string or int or whatever.

@testitm
Copy link

testitm commented Jan 22, 2022

any updates?

@alecthomas
Copy link
Owner Author

I haven't thought of a good API for this yet unfortunately.

@alecthomas
Copy link
Owner Author

My usual pattern is to pull the field out into a struct. It's a bit uglier for the AST though.

@jmalloc
Copy link

jmalloc commented Jan 24, 2024

Is there any accepted approach that I could use to do what @shahms suggested in this comment?

I'm trying to understand the most appropriate way to "jump back" into the normal grammar from within an implementation of Parseable that implements precedence climbing as per the example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants