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

Folders and files

NameName
Last commit message
Last commit date
Jun 29, 2020
Apr 19, 2019
Nov 4, 2020
Apr 19, 2019
Feb 16, 2021
Dec 4, 2020
Aug 16, 2013
Dec 20, 2020
Feb 28, 2021
Apr 23, 2019
Mar 29, 2019
Jan 26, 2021
Feb 17, 2021
Feb 17, 2021
Oct 29, 2020

Repository files navigation

scala-parser-combinators

Scala Standard Parser Combinator Library

This library was originally part of the Scala standard library, but is now community-maintained, under the guidance of the Scala team at Lightbend. If you are interested in helping please contact @Philippus or @SethTisue.

The latest stable release is 1.1.2.

Experimental milestone 1.2.x milestones are available that include Scala 3 support.

Documentation

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.1.2"

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

Scala.js and Scala Native

Scala-parser-combinators is also available for Scala.js and Scala Native:

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

Example

import scala.util.parsing.combinator._

case class WordFreq(word: String, count: Int) {
  override def toString = s"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(s"FAILURE: $msg")
      case Error(msg,_) => println(s"ERROR: $msg")
    }
  }
}

For a detailed unpacking of this example see Getting Started.

Contributing

Alternatives

A number of other parsing libraries for Scala are available; see https://github.com/lauris/awesome-scala#parsing

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

Languages