FENCE! In PARSE: What Should It Do?

FENCE! is coming soon. How soon? ...soon.

When it gets here I'm going to want to kick the tires a bit with purposes in dialects. And so of course a big question is: What will FENCE! do in PARSE? (By default, I mean...you can override it with whatever FENCE! combinator you want.)

It might seem an obvious use for it would be to make objects, e.g. act as GATHER:

So instead of:

>> parse "aaabbb" [
      gather [
          emit x: collect some ["a", keep (<a>)]
          emit y: collect some ["b", keep (<b>)]
      ]
  ]
== make object! [
    x: [<a> <a> <a>]
    y: [<b> <b> <b>]
]

You might imagine writing:

>> parse "aaabbb" [
      {
          x: collect some ["a", keep (<a>)]
          y: collect some ["b", keep (<b>)]
      }
  ]
== make object! [
    x: [<a> <a> <a>]
    y: [<b> <b> <b>]
]

But I don't know if that's obviously a great application.

We shouldn't be thinking inside-the-box that just because FENCE! is used for making objects sometimes, that's the most pressing need for them in dialects. I actually think GATHER does a good enough job at what it does...and I think the above example reads more clearly with it.

But I've pointed to other rather weak areas, like how we don't have implicit capture.

parse isodate [
    year: between <here> "-"
    month: between <here> "-"
    day: between <here> "T"
    ...
]

I'm not saying this is what we should use fences for, but just to kick-off that "outside-the-box" brainstorm:

parse isodate [{year} "-" {month} "-" {day} "T" ...]

If my only choices were between that and an alternate for GATHER+EMIT, I'd pick that, because I think we'd get a lot more mileage out of it.

Remember also that there are lots of variations, like ${...} and :{...} and whatever combinations in paths in chains and tuples you can think of. Ideally how it's used makes some sense across these variants. But we should remember that dialecting is about serving the domain, not making sacrifices to some god-of-consistency.

Anyway...it's going to take time to build up a sense of what being "fency" means.

But if any pet applications start coming to mind, pitch them.

I've suggested elsewhere that might be done with :var:

parse isodate [:year: "-" :month: "-" :day: "T" ...]

One thing I've noticed is that ACROSS comes up a lot, and is kind of wordy:

parse3 ldate [
    year: across repeat 4 digit
    month: across repeat 2 digit
    day: across repeat 2 digit
]

I've suggested QUASI-SPLICE! for this...

parse3 ldate [
    year: ~(repeat 4 digit)~
    month: ~(repeat 2 digit)~
    day: ~(repeat 2 digit)~
]

Though that's pretty weird. It does come up often, so... maybe FENCE! for "synthesize the result of the span of the rule"?

parse3 ldate [
    year: {repeat 4 digit}
    month: {repeat 2 digit}
    day: {repeat 2 digit}
]

Just a thought, I do feel like some shorthand for ACROSS that also makes apparent the boundaries of the span is probably a smart thing.

1 Like