scala / scala/scala-parser-combinators

Incorrect mismatch location reported by RegexParsers

Open
#573 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Scala
Stars
679
Forks
131
Avg merge
8h 25m
Merged PRs (30d)
3

Description

implicit def literal(s: String): Parser[String] = new Parser[String] {
  def apply(in: Input) = {
    val source = in.source
    val offset = in.offset
    val start = handleWhiteSpace(source, offset)
    var i = 0
    var j = start
    while (i < s.length && j < source.length && s.charAt(i) == source.charAt(j)) {
      i += 1
      j += 1
    }
    if (i == s.length)
      Success(source.subSequence(start, j).toString, in.drop(j - offset), None)
    else  {
      val found = if (start == source.length()) "end of source" else "'"+source.charAt(start)+"'"
      Failure("'"+s+"' expected but "+found+" found", in.drop(start - offset))
    }
  }
}

Note the error reporting at the end where the index of the mismatch is reported as start. If the string is "apple", and the input "...apply...", the mismatch is at the character 'y', which is pointed to by the variable j, not at the character 'a', which is pointed to by start.

println(parse("apple", "apply"))

Failure('apple' expected but 'a' found,CharSequenceReader('a', ...))

A potential fix may be as follows:

val rest = in.drop(start - offset)
if (start == source.length()) Failure(s"'$s' expected but end of source found", rest)
else Failure(s"'$s' expected but '${source.charAt(j)}' found at index $j", rest)

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the implicit literal parser implementation shown in the issue and reproduce parse("apple", "apply"). Trace the mismatch through start and j, then verify that the reported failure identifies the mismatching character and location rather than the initial offset.

Written by the indexing model from the issue text.

Assessment

Tech stack
scala
Domain
compilers
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.