File tree Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Original file line number Diff line number Diff line change @@ -25,6 +25,36 @@ libraryDependencies += "org.scala-lang.modules" %% "scala-parser-combinators" %
2525
2626To support multiple Scala versions, see the example in https://github.com/scala/scala-module-dependency-sample .
2727
28+ ## Example
29+
30+ ```
31+ import scala.util.parsing.combinator._
32+
33+ case class WordFreq(word: String, count: Int) {
34+ override def toString = "Word <" + word + "> " +
35+ "occurs with frequency " + count
36+ }
37+
38+ class SimpleParser extends RegexParsers {
39+ def word: Parser[String] = """[a-z]+""".r ^^ { _.toString }
40+ def number: Parser[Int] = """(0|[1-9]\d*)""".r ^^ { _.toInt }
41+ def freq: Parser[WordFreq] = word ~ number ^^ { case wd ~ fr => WordFreq(wd,fr) }
42+ }
43+
44+ object TestSimpleParser extends SimpleParser {
45+ def main(args: Array[String]) = {
46+ parse(freq, "johnny 121") match {
47+ case Success(matched,_) => println(matched)
48+ case Failure(msg,_) => println("FAILURE: " + msg)
49+ case Error(msg,_) => println("ERROR: " + msg)
50+ }
51+ }
52+ }
53+ ```
54+
55+ For a detailed unpacking of this example see
56+ [ Getting Started] ( docs/Getting_Started.md ) .
57+
2858## ScalaJS support
2959
3060Scala-parser-combinators directly supports scala-js 0.6+, starting with v1.0.5:
You can’t perform that action at this time.
0 commit comments