Skip to content

Commit f536e54

Browse files
committed
Add Getting Started example to the README
1 parent 48dc032 commit f536e54

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,36 @@ libraryDependencies += "org.scala-lang.modules" %% "scala-parser-combinators" %
2525

2626
To 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

3060
Scala-parser-combinators directly supports scala-js 0.6+, starting with v1.0.5:

0 commit comments

Comments
 (0)