|
| 1 | +package scala.util.parsing.combinator |
| 2 | + |
| 3 | +import scala.util.parsing.input._ |
| 4 | +import scala.collection.immutable.PagedSeq |
| 5 | + |
| 6 | +import org.junit.Test |
| 7 | +import org.junit.Assert.assertTrue |
| 8 | + |
| 9 | +import scala.util.parsing.combinator.syntactical.StandardTokenParsers |
| 10 | + |
| 11 | +class gh45 { |
| 12 | + |
| 13 | + @Test |
| 14 | + def test4: Unit = { |
| 15 | + def check(rd: Reader[Char]): Unit = { |
| 16 | + val g = new grammar |
| 17 | + val p = g.phrase(g.script) |
| 18 | + val parseResult = p(new g.lexical.Scanner(rd)) |
| 19 | + assertTrue(parseResult.isInstanceOf[g.Success[_]]) |
| 20 | + } |
| 21 | + |
| 22 | + val str = "x once y" |
| 23 | + check(new CharSequenceReader(str)) |
| 24 | + /* Note that this only tests PagedSeq.rest since neither |
| 25 | + * PackratReader nor lexical.Scanner override/use the drop method. |
| 26 | + */ |
| 27 | + check(new PagedSeqReader(PagedSeq.fromStrings(List(str)))) |
| 28 | + } |
| 29 | + |
| 30 | +} |
| 31 | + |
| 32 | +private final class grammar extends StandardTokenParsers with PackratParsers { |
| 33 | + lexical.reserved ++= List("x", "y", "z", "once") |
| 34 | + |
| 35 | + var onceCnt: Int = 0 |
| 36 | + lazy val once: PackratParser[String] = memo("once") ^? { |
| 37 | + case s if onceCnt == 0 => |
| 38 | + onceCnt += 1 |
| 39 | + s |
| 40 | + } |
| 41 | + |
| 42 | + lazy val script: PackratParser[Any] = |
| 43 | + ( "x" ~ once ~ "z" |
| 44 | + | "x" ~ once ~ "y" |
| 45 | + ) |
| 46 | +} |
0 commit comments