lambdaisland / lambdaisland/regal

Support backreferences

Open
#43 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement help wanted
Dominant language
Clojure
Stars
345
Forks
14
PR merge metrics
No merged PRs in 30d

Description

As discussed on the lambdaisland channel of the clojurians slack (https://clojurians.slack.com/archives/C1DV21X9P/p1643329544995089)

I'm trying to replicate this in regal using a backrefernce to match any 3 or more of the same consecutive character:

;;=> ["1111" "1"]```

But can't seem to get it:
```(regal/regex
  [:cat [:capture  :any] [:repeat ::_ 3 nil]]
  {:resolver (fn [x] "\\1")})

;;=>  #"(.)(?:\\1){3,}"

(regal/regex
  [:capture [:capture  :any] [:repeat ::_ 3 nil]]
  {:resolver (fn [x] "\1")})

;;=> #"((.){3,})"


(regal/regex
  [:capture [:capture  :any] [:repeat ::_ 3 nil]]
  {:resolver (fn [x] "\\\\1")})

;;=> #"((.)(?:\\\\1){3,})"```

Any tips?

@plexus Responded:

(ns repl-sessions.poke
  (:require [lambdaisland.regal :as regal]
            [lambdaisland.regal.parse :as regal-parse]))

(regal-parse/parse #"(.)\1{3,}")
;; => [:cat
;;     [:capture :any]
;;     [:repeat [:lambdaisland.regal.parse/not-implemented [:BackReference "1"]] 3]]

backreferences aren't implemented, but seems like a common enough feature that they should be. Would you mind creating a ticket?

here's a workaround you can do yourself:

(defmethod regal/-regal->ir [:ref :common] [[op idx] opts]
  `^::regal/grouped ("\\" ~(str idx)))

(regal/regex
 [:cat
  [:capture  :any]
  [:repeat [:ref 1] 3 nil]])
;; => #"(.)\1{3,}"

It was also questioned if all engines, Java, ECMA, Re2 support this. It looks the like the first two do but Re2 doesn't
https://github.com/google/re2/issues/101

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with regal-parse/parse and the parsed :BackReference example, then inspect how regal emits regexes through regal/-regal->ir. Check Java, ECMA, and Re2 behavior described in the issue; done means backreferences are represented and emitted correctly where supported, with unsupported engines handled explicitly.

Written by the indexing model from the issue text.

Assessment

Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
34/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.