1

I'm trying to parse an indented language using FastParse and I'm struggling finding any resources or information on it.
There is only one example I can find here that shows how to parse and calculate the sum of the integers in the tree structure. I've tried copying this code but I get the same error where it fails parsing \n.

I would like to parse this.

example
  1
  2
  3

Code

import fastparse.all._

class ExampleParser(indent: Int) {

  val word: P[String] = P("example".!)

  val number: P[String] = P( CharIn('0'to'9').rep(1).! )

  val blockBody: P[Seq[String]] = "\n" ~ deeper.flatMap(i => new ExampleParser(indent = i).number.rep(1, sep = ("\n" + " " * i).~/))

  val deeper: P[Int] = P(" ".rep(indent + 1).!.map(_.length))

  val section: P[(String, Seq[String])] = P(word ~ blockBody)

  val expr: P[(String, Seq[String])] = P(section)

}

object Main
{
  def main(args: Array[String]) =
  {
    check(
      """example
        |  1
        |  2
        |  3
      """.stripMargin.trim

    )
    println()
  }

  def check(str: String) = {
    new ExampleParser(0).expr.parse(str) match {
      case Parsed.Success(value, _) => println(value)
      case Parsed.Failure(a, b, c) => println("Failure:" + a + ":" + b + ":" + c)
    }
  }
}

Output

Failure:"\n":7:Extra(...ample
1
2
3, [traced - not evaluated])

How can I parse this correctly?

Michael
  • 3,411
  • 4
  • 25
  • 56

1 Answers1

0

If I change "expression" to "example":

check (
  """example
    |  1
    |  2
    |  3
  """.stripMargin.trim
)

I get the result:

[info] Running Main 
(example,ArrayBuffer(1, 2, 3))

Of course you might like to change it the other way round.

user unknown
  • 35,537
  • 11
  • 75
  • 121
  • I believe I mistyped when adding the question. I originally was testing it with the parser expecting `expression` but changed it to `example` for this. I'll be able to test this soon thank you. – Michael Jun 12 '18 at 10:09
  • I've changed this and it produces the same error. I've copied the code I have in the example into a new class called `ExampleParser`. I've updated `expression` to be `example` too. When running this I get `Failure:"\n":7:Extra(...ample 1 2 3, [traced - not evaluated])` This is with `fastparse_2.12`. – Michael Jun 12 '18 at 13:35
  • I have a feeling this might be dependant on the operating system used as creating the string could be spaced with `\r\n` on Windows. When running with `example\n 1\n 2` I get a null pointer exception. – Michael Jun 12 '18 at 14:48
  • I used Linux, scala-2.12 and sbt. – user unknown Jun 13 '18 at 02:32