parser icon indicating copy to clipboard operation
parser copied to clipboard

Parser.LanguageKit.list gives stack overflow on very large lists

Open ianmackenzie opened this issue 8 years ago • 0 comments

---- elm-repl 0.18.0 -----------------------------------------------------------
 :help for help, :exit to exit, more at <https://github.com/elm-lang/elm-repl>
--------------------------------------------------------------------------------
> import Parser
> import Parser.LanguageKit
> parser = Parser.LanguageKit.list (Parser.symbol "") Parser.int
Parser <function> : Parser.Parser (List Int)
> Parser.run parser "[1,2,3,4,5]"
Ok [1,2,3,4,5] : Result.Result Parser.Error (List Int)
> longList = "[" ++ (List.range 1 5000 |> List.map toString |> String.join ",") ++ "]"
"[1,2,3,...<elided>...,4998,4999,5000]" : String
> Parser.run parser longList
RangeError: Maximum call stack size exceeded

I'm running Elm 0.18 on Windows 10 64-bit.

EDIT: My workaround for now, for anyone else encountering the same issue, is to use a custom list parsing function like this (although more complex, since this version doesn't support whitespace or empty lists):

list =
    Parser.succeed (\first rest -> first :: rest)
        |. Parser.symbol "["
        |= Parser.int
        |= (Parser.repeat Parser.zeroOrMore
                (Parser.succeed identity
                    |. Parser.symbol ","
                    |= Parser.int
                )
           )
        |. Parser.symbol "]"

I'm guessing this will have lower-quality error messages than Parser.LanguageKit.list, but in my particular case I'm parsing machine-generated text so high-quality error messages are not quite as important.

ianmackenzie avatar May 23 '17 18:05 ianmackenzie