-
Notifications
You must be signed in to change notification settings - Fork 4
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
feat: named capture groups and reference (alternative approach) #78
Conversation
wip chore: more tests feat: improved refs refactor: merge name and ref refactor: self code review refactor: tweaks refactor: rename reference to ref feat: example with html tags chore: self code review
One advantage I see with the ts-regex-builder approach is that it is can be external DSL, separate from JS/TS. An external DSL can facilitate:
Storing explicit refs in vars breaks this paradigm (in the Swift approach too). Is it possible to get the benefits of the explicit refs by using implicit refs? E.g: notes: In Typescript:
const tagMatcher = buildRegExp(['<', tagName, '>', tagContent, '</', useGroup("htmlTag"), '>'], { Or, in my imaginary DSL: As a first step, these group names could be scoped globally, meaning each name |
@PaulJPhilp Hmmm, I'm not sure I get your point. Could you explain what would be your goal(s) in terms of API design, and how the above options realize/don't realize it? For brief summary here's the recap of the two approaches:
So the following examples actually worked exactly the same from technical point of view:
Would no longer be possible. |
Maciej:
This is a different topic. As I mentioned, I am writing an article about
the ts-regex-builder library. Here is a link to the
first draft. I'd appreciate any feedback you can provide.
***@***.***_93037/from-ancient-hieroglyphics-to-modern-software-8bbc6012560f
Thank you,
Paul
…On Mon, Apr 1, 2024 at 5:50 PM Maciej Jastrzebski ***@***.***> wrote:
@PaulJPhilp <https://github.com/PaulJPhilp> Hmmm, I'm not sure I get your
point. Could you explain what would be your goal(s) in terms of API design,
and how the above options realize/don't realize it?
For brief summary here's the recap of the two approaches:
1. In the original PR #66
<#66> standalone
ref("abc") was just a holder for name ({ type: "reference", name:
"abc" }). When assigning it to a capture group: capture([...], { name:
ref }) the capture group did not actually "hold" the ref in any
meaningful way, it just used its name when encoding the capture regex.
The goal here is to not require user to type the name twice, once in the
capture group and second in the backreference.
So the following examples actually worked exactly the same from technical
point of view:
// Suggested syntax
const myRef = ref('my'):
const myRegex = buildRegExp([catpure([...], { name: myRef }, myRef);
// Other option that actually worked
const myRegex = buildRegExp([catpure([...], { name: "my" }, ref("my"));
2. In case of this PR, this is somewhat similar, ref() is not a real
reference, just ensures that the capture/backref names are the same.
However, this PR removes ref("my") standalone construct so
const myRegex = buildRegExp([catpure([...], { name: "my" }, ref("my"));
Would no longer be possible.
—
Reply to this email directly, view it on GitHub
<#78 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/BA6V6ZXTFTSUONBIIMQM52DY3HJEDAVCNFSM6AAAAABFMZZKZWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDAMZQGYZDKNZYGM>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
@PaulJPhilp The URL you have posted seems to be obfuscated. Perhaps this is somesecurity feature from GitHub. |
That's weird.
Can you follow me on twitter so I can send it to you? Or, is there a
better way?
Paul
…On Tue, Apr 2, 2024 at 5:57 PM Maciej Jastrzebski ***@***.***> wrote:
@PaulJPhilp <https://github.com/PaulJPhilp> The URL you have posted seems
to be obfuscated. Perhaps this is somesecurity feature from GitHub.
—
Reply to this email directly, view it on GitHub
<#78 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/BA6V6ZRAODNPYTHV4XYEDKLY3MSUHAVCNFSM6AAAAABFMZZKZWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDAMZTGE3TGMJUGM>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
Closing in favor of more restrictive syntax in #66. |
@PaulJPhilp I've followed you on Twitter, you can send the draft by DMs. |
Great, thanks. I'm just finishing a 2nd draft. I'll send that in the next
day or two.
Paul
p.s. I'm going to update my PR (URL pattern) now that named capture is
available. Might as well hold off on reviewing it now.
…On Thu, Apr 4, 2024 at 11:08 PM Maciej Jastrzebski ***@***.***> wrote:
@PaulJPhilp <https://github.com/PaulJPhilp> I've followed you on Twitter,
you can send the draft by DMs.
—
Reply to this email directly, view it on GitHub
<#78 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/BA6V6ZW5N2Z6TMCIIB67ONLY3YITTAVCNFSM6AAAAABFMZZKZWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDAMZYG42TINRXGE>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
Summary
This is an alternative syntax for backreferences proposed by @Killavus.
It requires assigning a capture group to a JS variable which then can generate a backreference by calling
.ref()
method on it.Comparison to #66
Pros:
capture
needs to be assigned to a separate variableCons:
capture
in the main regex expressionname
for capture, as we want to avoid creating named capturing group with automatically generated name if not needed.Real life example (HTML open-closing tags matching):
@PaulJPhilp pls take a look at this approach vs the one in #66 .
Test plan
Added automated tests & real-life example.