Skip to content

Proposal: Support grammar aliases. #22

Open

Description

@alecthomas

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`
}

Activity

  1. shahms commented on Feb 12, 2019

    @shahms

    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)
    }
    
  2. alecthomas commented on Feb 14, 2019

    @alecthomas
    OwnerAuthor

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

  3. alecthomas commented on Feb 14, 2019

    @alecthomas
    OwnerAuthor

    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.

  4. alecthomas commented on Jan 22, 2022

    @alecthomas
    OwnerAuthor

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

  5. alecthomas commented on Jan 22, 2022

    @alecthomas
    OwnerAuthor

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

  6. jmalloc commented on Jan 24, 2024

    @jmalloc

    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.

  7. Pastalikek65 commented on Aug 9, 2026

    @Pastalikek65
    Contributor

    Implemented: PR #474 — participle.Alias(name, grammar) + <name> tag syntax for non-capturing reusable fragments. Captures remain on struct fields; aliases are pattern fragments. Any desire for capturing aliases can be a follow-up (noted in the PR).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions