• Stars
    star
    631
  • Rank 65,165 (Top 2 %)
  • Language
    Scala
  • License
    Apache License 2.0
  • Created over 10 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

scala-parser-combinators

This 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 joining the maintainers team, please contact @Philippus or @SethTisue.

Choosing a parsing library

This library's main strengths are:

  • Stability. It's been around and in wide use for more than a decade.
  • The codebase is modest in size and its internals are fairly simple.
  • It's plain vanilla Scala. No macros, code generation, or other magic is involved.
  • Multiple versions of Scala (2.12, 2.13, 3) are supported on all back ends (JVM, JS, Native).

Its main weaknesses are:

  • Performance. If you are ingesting large amounts of data, you may want something faster.
  • Minimal feature set.
  • Inflexible, unstructured error reporting.

A number of other parsing libraries for Scala are available -- see list on Scaladex.

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" % <version>

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" % <version>

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

More Repositories

1

scala

Scala 2 compiler and standard library. Bugs at https://github.com/scala/bug; Scala 3 at https://github.com/lampepfl/dotty
Scala
14,149
star
2

scala-async

An asynchronous programming facility for Scala
Scala
1,136
star
3

pickling

Fast, customizable, boilerplate-free pickling support for Scala
Scala
833
star
4

docs.scala-lang

The Scala Documentation website
HTML
541
star
5

scala-java8-compat

A Java 8 compatibility kit for Scala.
Scala
437
star
6

legacy-svn-scala

OBSOLETE, we're over there:
Scala
369
star
7

scala3-example-project

An example sbt project that compiles using Dotty
Scala
326
star
8

scala-xml

The standard Scala XML library
Scala
285
star
9

scala-dist

sbt project that packages the Scala distribution
Scala
278
star
10

scala-lang

sources for the Scala language website
SCSS
250
star
11

scala-abide

obsolete; visit https://github.com/scalacenter/scalafix instead
Scala
234
star
12

bug

Scala 2 bug reports only. Please, no questions โ€” proper bug reports only.
229
star
13

collection-strawman

Implementation of the new Scala 2.13 Collections
Scala
202
star
14

scala-collection-compat

makes some Scala 2.13 APIs (primarily collections, also some others) available on 2.11 and 2.12, to aid cross-building
Scala
195
star
15

scala-parallel-collections

Parallel collections standard library module for Scala 2.13+
Scala
183
star
16

scala-seed.g8

Giter8 template for a simple hello world app in Scala.
Scala
144
star
17

scala-dev

Scala 2 team issues. Not for user-facing bugs or directly actionable user-facing improvements. For build/test/infra and for longer-term planning and idea tracking. Our bug tracker is at https://github.com/scala/bug/issues
127
star
18

community-build

Scala 2 community build โ€”ย a corpus of open-source repos built against Scala nightlies
Shell
125
star
19

scala-swing

Scala wrappers for Java's Swing API for desktop GUIs
Scala
123
star
20

scala3.g8

Scala
115
star
21

scala-collection-contrib

community-contributed additions to the Scala 2.13 collections
Scala
104
star
22

scala-module-dependency-sample

Depend on Scala modules like a pro
Scala
96
star
23

scala-continuations

the Scala delimited continuations plugin and library
Scala
89
star
24

make-release-notes

The project that generates Scala release notes.
HTML
84
star
25

toolkit

The batteries-included Scala
Scala
71
star
26

slip

obsolete โ€”ย archival use only
69
star
27

vscode-scala-syntax

Visual Studio Code extension for syntax highlighting Scala sources
Scala
69
star
28

compiler-benchmark

Benchmarks for scalac
Scala
68
star
29

scala-library-next

backwards-binary-compatible Scala standard library additions
Scala
66
star
30

improvement-proposals

Scala Improvement Proposals
38
star
31

scala.epfl.ch

web site for the Scala Center @ EPFL
SCSS
37
star
32

scala-tool-support

XML
32
star
33

hello-world.g8

Scala
27
star
34

scala-collection-laws

partially-automatic generation of tests for the entire collections library
Scala
21
star
35

scala3-cross.g8

Scala
16
star
36

scabot

Scala's PR&CI automation bot
Scala
14
star
37

scala-jenkins-infra

A Chef cookbook that manages Scala's CI infrastructure.
Shell
14
star
38

sbt-scala-module

sbt plugin for scala modules.
Scala
12
star
39

scala-asm

A fork of https://gitlab.ow2.org/asm/asm for the Scala compiler
11
star
40

compiler-interface

a binary contract between Zinc and Scala Compilers
Scala
10
star
41

scala3-mill-example-project

Shell
10
star
42

scala-partest

Legacy repo for testing framework for Scala versions <= 2.12
Scala
9
star
43

scala-asm-legacy

A fork of asm.ow2.org for the Scala compiler
Java
8
star
44

scala3-staging.g8

Scala
6
star
45

scala-modules-build

Build support for the various Scala Modules
Shell
1
star
46

actors-migration

Scala
1
star
47

scala-partest-interface

SBT interface to partest
1
star
48

scala-library-all

Conglomerate pom file to pull in components of Scala standard library easily.
Scala
1
star
49

scala-dist-smoketest

Smoke Test for newly created Scala distributions
Shell
1
star