Skip to content
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

lazy not working for recursive parser #11

Open
megakilo opened this issue Feb 2, 2024 · 2 comments
Open

lazy not working for recursive parser #11

megakilo opened this issue Feb 2, 2024 · 2 comments

Comments

@megakilo
Copy link

megakilo commented Feb 2, 2024

I have this mini parser for add expression. But compiler overflows due to the recursive structure. I saw lazy seems to be the solution for this but can't figure out why it doesn't work.

term : Parser Utf8 U64
term = alt digits (lazy (\_ -> expr) |> between (codeunit '(') (codeunit ')'))

expr : Parser Utf8 U64
expr = const (\x -> \y -> x + y) |> apply (lazy \_ -> term) |> skip (codeunit '+') |> apply (lazy \_ -> term)

expect parseStr expr "1+2" == Ok 3u64
expect parseStr expr "1+(1+2)" == Ok 4u64
@megakilo megakilo changed the title How to use lazy? lazy not working for recursive parser Feb 2, 2024
@megakilo
Copy link
Author

megakilo commented Feb 2, 2024

The Elm equivalent works fine

parens = Parser.succeed (\v -> v) |. Parser.symbol "(" |= expr |. Parser.symbol ")"

term = Parser.oneOf
        [ Parser.int
        , parens
        ]


expr = Parser.succeed  (\x y -> x + y) |= Parser.lazy (\_ -> term) |. Parser.symbol "+" |= Parser.lazy (\_ -> term)


main =
    "1+(2+3)"
        |> Parser.run expr
        |> Debug.toString
        |> text

@megakilo
Copy link
Author

megakilo commented Feb 2, 2024

I think it's related to the compiler bug here roc-lang/roc#4288

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant