Skip to content

[Feature (self-)request] Rule Templates #634

@Fordi

Description

@Fordi

Writing the same rule with little variations has gotten annoying. I want templates. By Templates, I mean in the C semantics sense - which means I'm asking for something grammatically like TS generics, but are actually used at build time, and have imperative value instead of just validating the build.

e.g.,

TABLE = CSV / TSV

LIST_OF<T, D, _ = " "*> 
  = head:T 
    tail:(_ D _ item:T { return item; })
    { return [head, ...tail]; }

CSV = LIST_OF<ROW<",">, "\r"? "\n">
TSV = LIST_OF<ROW<"\t">, "\r"? "\n">

ROW<D> = LIST_OF<CELL<D>, D>
CELL<D> = QUOTED / $((!(D / [\r\n"]) .)*)
QUOTED = "\"" v:$("\"\"" / [^"])* "\"" {
  return v;
}

I could have used function syntax for that, but I felt that would be more confusing in complicated grammars, which would be counterproductive. e.g.,

ROW(D) = LIST_OF(CELL(D), D)

Please, someone either convince me of one of:

  • that this is a stupid, pointless idea that will get zero community buy-in.
  • that it's a bad/confusing idea for an already beleaguered language.
  • that it'd be infeasible given the current implementation.
  • that parser theory says this will be much, much harder than I think, which is already pretty hard.
  • that it's something that's already available via [extant feature]; didn't I RTFM?
  • that this is a brilliant idea and that I should absolutely sink the requisite time into it, which I recognize is non-trivial.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions