Skip to content

Pattern matcher groupCount throws #1727

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

Closed
japgolly opened this issue Jun 16, 2015 · 8 comments
Closed

Pattern matcher groupCount throws #1727

japgolly opened this issue Jun 16, 2015 · 8 comments
Labels
bug Confirmed bug. Needs to be fixed. duplicate Duplicate of another issue. Closed.
Milestone

Comments

@japgolly
Copy link
Contributor

The following snippet behaves very differently in Scala.JS vs JVM.

"(a)(b)cd".r.pattern.matcher("").groupCount

In JVM, it prints 2. In JS it throws java.lang.IllegalStateException: No match available.

@sjrd
Copy link
Member

sjrd commented Jun 16, 2015

Unfortunately, this is virtually not fixable. With a JS regexp, there is no way to get the group count without having a successful match. So either we parse the regexp on an ad hoc basis to get that count (hard), or we derive automatically a matching string from the regexp to match against it (which requires to parse the regex in the first place).

Unless someone else has an idea...

@sjrd sjrd added the bug Confirmed bug. Needs to be fixed. label Jun 16, 2015
@gzm0
Copy link
Contributor

gzm0 commented Jun 16, 2015

Yet another issue that would be solved by our own regex parser as proposed in #1201

@japgolly
Copy link
Contributor Author

I played around with JS RegExps and read some ref doc - definitely doesn't seem possible.

Regex continues to be the largest source of incompatible Scala/Scala.JS behaviour. It's a significant problem because you only hit the errors at runtime - compilation & linking passes silently. The only way to catch incompatibilities like this one, unsupported regex like (?<!x), etc is probably going to along the lines of a custom regex lib like @gzm0 suggested. I don't know what else can be done. It might not be pleasant but it is better than a partially-discovered mountain of runtime-only errors, which is what we have today.

@sjrd
Copy link
Member

sjrd commented Jun 19, 2015

I'll close this as Won't Fix. If it must be fixed, it will go into the larger scheme of things of fixing #1201 anyway.

@sjrd sjrd closed this as completed Jun 19, 2015
@sjrd sjrd added the wontfix We decided not to fix this issue/not implement that feature request. label Jun 19, 2015
@MikaelMayer
Copy link
Contributor

For people having a problem with groupCount, my fix is the following:

def countGroups(m: Regex.Match): Int = {
   try {m.groupCount} catch { case e: Throwable =>
     Stream.from(0).takeWhile(i => try { val x = m.group(i); x != null } catch { case e: Throwable => false} ).length - 1
   }
 }

If the call to groupCount fails, it tries to enumerate the groups and return the inferred count. It saved my tool (scalajs 0.6.19)

@easel
Copy link

easel commented Mar 12, 2018

Thanks @MikaelMayer for the fix, it works for me too.

@sjrd is this something we could fold into the stldlib .groupCount?

@sjrd
Copy link
Member

sjrd commented Mar 12, 2018

I don't think so, because the workaround builds on top of Scala's Regex.Match, not on top of Java's regex.Matcher.

@sjrd sjrd linked a pull request Mar 28, 2021 that will close this issue
4 tasks
@sjrd
Copy link
Member

sjrd commented Mar 29, 2021

This was in fact fixed in e5d3d6b as part of fixing #3280, which should have been marked as a duplicate of this one, but we did not notice.

@sjrd sjrd added duplicate Duplicate of another issue. Closed. and removed wontfix We decided not to fix this issue/not implement that feature request. labels Mar 29, 2021
@sjrd sjrd added this to the v0.6.26 milestone Mar 29, 2021
@sjrd sjrd removed a link to a pull request Mar 29, 2021
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Confirmed bug. Needs to be fixed. duplicate Duplicate of another issue. Closed.
Projects
None yet
Development

No branches or pull requests

5 participants