com-lihaoyi / fastparse

Writing Fast Parsers Fast in Scala
https://com-lihaoyi.github.io/fastparse
MIT License
1.09k stars 164 forks source link

Easily produced stacktrace #292

Closed tballard closed 9 months ago

tballard commented 9 months ago

I just started playing with fastparse and quickly produced a stacktrace. Tut, tut.

import $ivy.`com.lihaoyi::fastparse:3.0.2`

import fastparse._, NoWhitespace._

def pct[$: P] = P("%")
def nonPct[$: P] = P( CharPred('%' != _).rep.!)
def number[$: P] = P( CharIn("0-9").rep.!)
def variable[$: P] = P( (pct ~ number).rep(1).!)
def pcrAtEnd[$: P] = P( pct ~ End )
def notNumber[$: P] = P( CharPred(c => c < '0' || c > '9').rep.! )
def nonVarPct[$: P] = P( (pct ~ &(notNumber)).rep(1).! )
def justText[$: P] = P( (nonPct | pcrAtEnd | nonVarPct).rep.! )
val Parsed.Success(v4, _) = parse("%ety%gh67sd%", justText(_) )

produces:

java.lang.OutOfMemoryError: Java heap space
  scala.collection.mutable.ArrayBuffer$.scala$collection$mutable$ArrayBuffer$$ensureSize(ArrayBuffer.scala:300)
  scala.collection.mutable.ArrayBuffer.ensureSize(ArrayBuffer.scala:69)
  scala.collection.mutable.ArrayBuffer.addOne(ArrayBuffer.scala:139)
  scala.collection.mutable.ArrayBuffer.addOne(ArrayBuffer.scala:43)
  scala.collection.mutable.Growable.$plus$eq(Growable.scala:36)
  scala.collection.mutable.Growable.$plus$eq$(Growable.scala:36)
  scala.collection.mutable.AbstractBuffer.$plus$eq(Buffer.scala:232)
  fastparse.Implicits$LowPriRepeater$GenericRepeatedImplicit0$.accumulate(Implicits.scala:53)
  fastparse.Implicits$LowPriRepeater$GenericRepeatedImplicit0$.accumulate(Implicits.scala:50)
  ammonite.$file.order$.rec$macro$7$4(order.sc:23)
  ammonite.$file.order$.justText(order.sc:23)
  ammonite.$file.order$.$anonfun$v4$2(order.sc:24)
  ammonite.$file.order$$$Lambda$1510/0x00000008011e43c8.apply(Unknown Source)
  fastparse.package$.parseInputRaw(package.scala:69)
  fastparse.package$.$anonfun$parse$1(package.scala:35)
  fastparse.package$$$Lambda$176/0x0000000800dc2a28.apply(Unknown Source)
  fastparse.ParserInputSource$fromParserInput.parseThrough(ParserInput.scala:25)
  fastparse.package$.parse(package.scala:35)
  ammonite.$file.order$.<clinit>(order.sc:24)
lihaoyi commented 9 months ago

Please take a look at the docs for debugging your parser https://com-lihaoyi.github.io/fastparse/#DebuggingParsers. This seems likely to be a problem in your parser and not the library. But if your debugging narrows down the problem to Fastparse, we can reopen this ticket