Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

Unable to start a line with an Int parser #309

Closed
sbeitzel opened this issue Aug 8, 2023 · 1 comment
Closed

Unable to start a line with an Int parser #309

sbeitzel opened this issue Aug 8, 2023 · 1 comment

Comments

@sbeitzel
Copy link

sbeitzel commented Aug 8, 2023

Given that I want to parse an input line that looks like this: (AoC 2022, number 4)
2-4,6-8

I reckon that what I want to wind up with is four integers: the starting and ending points for Elf A and Elf B.

Let's define ElfPair as:

    struct ElfPair {
        let firstA: Int
        let firstB: Int
        let secondA: Int
        let secondB: Int
  }

Now, let's try to parse it:

        let pairParser = Parse(ElfPair.init) {
            Int.parser()
            "-"
            Int.parser()
            ","
            Int.parser()
            "-"
            Int.parser()
        }

The compiler complains on the first Int.parser(), saying, "Ambiguous use of buildExpression"

I can make the compiler shut up by adding an initial literal parser for the empty string, but this seems ... icky:

        let pairParser = Parse(ElfPair.init) {
            ""
            Int.parser()
            "-"
            Int.parser()
            ","
            Int.parser()
            "-"
            Int.parser()
        }
@mbrandonw
Copy link
Member

You need to supply more type information due to certain limitations of result builders in Swift. This was detailed in this discussion.

To fix you can either do this:

let pairParser = Parse.init(input: Substring.self, ElfPair.init) {
  Int.parser()
  "-"
  Int.parser()
  ","
  Int.parser()
  "-"
  Int.parser()
}

Or you can define a whole new parser type using the body-style of parsing, which can be easier for the compiler to process for large, complex parsers:

struct ElfPairParser: Parser {
  var body: some Parser<Substring, ElfPair> {
    Parse(ElfPair.init) {
      Int.parser()
      "-"
      Int.parser()
      ","
      Int.parser()
      "-"
      Int.parser()
    }
  }
}

Also this is not an issue with the library so I am going to convert this to a discussion.

@pointfreeco pointfreeco locked and limited conversation to collaborators Aug 8, 2023
@mbrandonw mbrandonw converted this issue into discussion #310 Aug 8, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants