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

Handling recursive syntax using def throws StackOverflowException #116

Open
tusharmath opened this issue Nov 5, 2022 · 0 comments
Open
Labels
bug Something isn't working

Comments

@tusharmath
Copy link
Contributor

tusharmath commented Nov 5, 2022

Parsing something like this —

FOO
[FOO
[[FOO]]
[[[FOO]]]

In the above syntax, FOO can be wrapped any number of times inside a square bracket.
The parser is implemented using the following code —

val foo = Syntax.string("FOO", "foo")

def wrappedN(foo: Syntax[String, Char, Char, String]): Syntax[String, Char, Char, String] = {
  val wrapped = foo.between(Syntax.char('['), Syntax.char(']'))
  wrapped | wrappedN(wrapped)
}

val syntax = wrappedN(foo)

val input = "[[FOO]]"

println(syntax.parseString(input)) // Stack overflow exception

Failing Test: https://github.com/zio/zio-parser/pull/115/files

Update 1:
Using a lazy val instead of a def works properly.

lazy val syntax: Syntax[String, Char, Char, Unit] =
  (
    Syntax.string("FOO", {}) | syntax
  ).between(Syntax.char('['), Syntax.char(']'))
@tusharmath tusharmath changed the title Handling recursive syntax Handling recursive syntax throws StackoverflowException Nov 5, 2022
@tusharmath tusharmath changed the title Handling recursive syntax throws StackoverflowException Handling recursive syntax throws StackOverflowException Nov 5, 2022
@tusharmath tusharmath added the bug Something isn't working label Nov 5, 2022
@tusharmath tusharmath changed the title Handling recursive syntax throws StackOverflowException Handling recursive syntax using def throws StackOverflowException Nov 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant