Synthesizing Parse Values From Thin Air: JUST

I've defined JUST to synthesize the value after it:

>> uparse "a" [collect [keep just keep, keep <any>]]
== [keep "a"]

So it's acting like this:

>> uparse "a" [collect [keep ('keep), keep <any>]]
== [keep "a"]

This can be helpful when you're trying to build a rule with COMPOSE and the contention over GROUP! and trying to nest inside of it becomes a bummer.

As an example, let's say you're trying to build a KEEP parse rule inside a parse rule (which I am actually at this moment trying to do in the whitespace interpreter project):

name: "Binky"
uparse ... [... collect [
     keep (compose [keep (to word! name)])
] ...]

Okay so that gives you [keep Binky]. Not what you wanted since it will give an error that Binky is not defined. You can quote it...

keep (compose [keep '(to word! name)])

Now you've got [keep 'Binky]. But you're not trying to match Binky in the input, you're trying to synthesize it out of thin air. Imagine we need it in a GROUP!... let's just go ahead and use the wacky engroup operator to do that:

keep (compose [keep (engroup quote to word! name)])

So we got our [keep ('Binky)]... but that was annoying. :cloud_with_rain:

Since JUST can literally synthesize a value without matching in parse, we can make something less head-scratchy:

keep (compose [keep just (to word! name)])

And that gives us [keep just Binky] which is much nicer. The key to seeing why this breaks us out of the problem is that it lets us get at literal values without a GROUP!, which means we aren't trying to COMPOSE inside of COMPOSE groups.

1 Like

Changes have made it so that @FOO is now a PINNED!, and not a THE-WORD!.

This frees up the word THE to have less baggage, and be used in these circumstances.

One idea on the difference in meaning between THE and JUST might be whether or not a binding is considered, e.g. THE will bind, but JUST gives you "just" the value with no binding.

On balance, I think probably the LIT/LITERAL/LITERALLY is likely still the best choice for "match this thing literally", because it implies a quote is involved (e.g. 'foo is still called a "lit-word") and if a quoted item occurs it's expected to match in the input.

1 Like