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

Handle errors found in .map #280

Open
bwbecker opened this issue Mar 22, 2023 · 0 comments
Open

Handle errors found in .map #280

bwbecker opened this issue Mar 22, 2023 · 0 comments

Comments

@bwbecker
Copy link

I think fastparse would benefit by explicitly handling errors found in .map.

For an example, refer to the Math example parser in the fastparse documentation. In the interactive example at the end, try dividing by 0 instead of by 3. The error message says it was expecting a number. But I gave it a number! It's just a number that caused a problem.

In my own case, I'm looking up a value in a symbol table. If it's not there, I'd like to present a more sensible error message.

I've experimented with Fail(...). My conclusion is that it's useful for when the input does not match the grammar. I haven't found a way to use it to report a failure in one of the actions.

I can approximate what I want with .opaque, but information I'd like to include in the message (name) is out of scope.

  def courseSpecRef[p: P]: P[CourseSpec] = P(courseSpecName.flatMap { name =>
    this.symTable.lookup(name) match {
      case Some(cs) => Pass(CourseSpecRef(name, cs))
      case None     => Fail("courseSpecRef fail")        // this message does not appear in the output
    }
  }).opaque(s"A name from ${symTable.toString}")

An error message from the above is expected: (<Course range eg CS340-398> | courseSpecRE | "(" | A name from usableCourseAttempts; unusableCourseAttempts; nonMathCourses; mathCourses)

What I'd like to see is something similar to Fail but where the message forms the entirety of the "expected" message. For example, I replace Fail in my example with Fail2("Found '${name}' but expected one of ${symTable}") and my error message would be expected: Found 'mathCourse' but expected one of usableCourseAttempts; unusableCourseAttempts; nonMathCourses; mathCourses

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