com-lihaoyi / fastparse

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

CharsWhile with rep can produce infinity testing. #313

Closed He-Pin closed 2 weeks ago

He-Pin commented 1 month ago
function-name       = function-name-first *function-name-char
function-name-first = LCALPHA
function-name-char  = function-name-first / "_" / DIGIT
LCALPHA             = %x61-7A  ; "a".."z"

function-expr       = function-name "(" S [function-argument
                         *(S "," S function-argument)] S ")"

https://www.rfc-editor.org/rfc/rfc9535#name-collected-abnf-grammars

  private def isFunctionNameAcceptable(c: Char): Boolean = {
    println("testing: " + c)
    'a' <= c && c <= 'z'
  }
  private def `function-name-first`[_: P]: P[Unit] = P(CharsWhile(isFunctionNameAcceptable, 1))
  private def isFunctionNameCharAcceptable(c: Char): Boolean =
    isFunctionNameAcceptable(c) || c == '_' || c.isDigit

  private def `function-name`[_: P]: P[String] = P(`function-name-first` ~~ `function-name-char`.rep).!

Which will print:

testing: (
testing: (
testing: (
testing: (
testing: (
testing: (
...

I will change to use CharIn and CharsWhileIn

lihaoyi commented 2 weeks ago

Can you minimize the issue and open a discussion? I both don't have enough and have too much code here to see what's wrong, and it's very likely user error.