parser icon indicating copy to clipboard operation
parser copied to clipboard

Maximum call stack size exceeded using andThen

Open pablohirafuji opened this issue 8 years ago • 1 comments

Minimal example

module Main exposing (..)

import Html exposing (Html, text)
import Parser exposing (andThen, keyword, oneOf, run)


main =
    view (createBigFile 10000)


createBigFile n =
    String.repeat n "1"


crashWithLotsOfText =
    oneOf
        [ keyword "1"
            |> andThen (\_ -> crashWithLotsOfText)
        , Parser.end
        ]


view txt =
    run crashWithLotsOfText txt
        |> Result.mapError (always "")
        |> Result.map (always "")
        |> (toString >> text)

Changing 10000 to 1000 compiles fine.

Error message

Uncaught RangeError: Maximum call stack size exceeded

Ellie reproduction

https://ellie-app.com/42MsxddqX9ya1/0

pablohirafuji avatar Aug 14 '17 22:08 pablohirafuji

A few notes:

  • I can reproduce the problem, but I had to add another zero to the large number.
  • The analogous code for elm-community/parser-combinators crashes similarly.
  • You can parse the same language just fine using repeat zeroOrMore (keyword "1"). I would suggest that very long documents would generally have a repetitive structure, so might allow parsing using repeat.

I'm a bit curious to what extent an Elm parser combinator library with andThen necessarily exhibits this problem. It feels that due to the general type ((a -> Parser b) -> Parser a -> Parser b as opposed to e.g. (a -> Parser (List a)) -> Parser a -> Parser (List a), some form of recursion might be implied?

robx avatar Jan 22 '18 21:01 robx