Skip to content

Handle errors found in .map #280

Open
@bwbecker

Description

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

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions