8000 GitHub - scala/scala-parser-combinators at 31895c52f85a91b1a1403d11833f303136113598
[go: up one dir, main page]

Skip to content

simple combinator-based parsing for Scala. formerly part of the Scala standard library, now a separate community-maintained module

License

Notifications You must be signed in to change notification settings

scala/scala-parser-combinators

Repository files navigation

scala-parser-combinators Gitter

Scala Standard Parser Combinator Library

This library is now community-maintained. If you are interested in helping please contact @gourlaysama or mention it on Gitter.

As of Scala 2.11, this library is a separate jar that can be omitted from Scala projects that do not use Parser Combinators.

Documentation

< 9E49 ul dir="auto">
  • Current API
  • The Getting Started guide
  • A more complicated example, Building a lexer and parser with Scala's Parser Combinators
  • "Combinator Parsing", chapter 33 of Programming in Scala, Third Edition, shows how to use this library to parse arithmetic expressions and JSON. The second half of the chapter examines how the library is implemented.
  • Adding an SBT dependency

    To depend on scala-parser-combinators in SBT, add something like this to your build.sbt:

    libraryDependencies += "org.scala-lang.modules" %% "scala-parser-combinators" % "1.0.6"
    

    (Assuming you're using a scalaVersion for which a scala-parser-combinators is published. The first 2.11 milestone for which this is true is 2.11.0-M4.)

    To support multiple Scala versions, see the example in https://github.com/scala/scala-module-dependency-sample.

    Example

    import scala.util.parsing.combinator._
    
    case class WordFreq(word: String, count: Int) {
        override def toString = "Word <" + word + "> " +
                                "occurs with frequency " + count
    }
    
    class SimpleParser extends RegexParsers {
        def word: Parser[String]   = """[a-z]+""".r       ^^ { _.toString }
        def number: Parser[Int]    = """(0|[1-9]\d*)""".r ^^ { _.toInt }
        def freq: Parser[WordFreq] = word ~ number        ^^ { case wd ~ fr => WordFreq(wd,fr) }
    }
    
    object TestSimpleParser extends SimpleParser {
        def main(args: Array[String]) = {
            parse(freq, "johnny 121") match {
                case Success(matched,_) => println(matched)
                case Failure(msg,_) => println("FAILURE: " + msg)
                case Error(msg,_) => println("ERROR: " + msg)
            }
        }
    }

    For a detailed unpacking of this example see Getting Started.

    ScalaJS support

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

    libraryDependencies += "org.scala-lang.modules" %%% "scala-parser-combinators" % "1.0.6"
    

    Contributing

    About

    simple combinator-based parsing for Scala. formerly part of the Scala standard library, now a separate community-maintained module

    Topics

    Resources

    License

    Code of conduct

    Stars

    Watchers

    Forks

    Packages

    No packages published

    Contributors 52

    Languages

    0