scala.util.parsing.combinator

trait Parsers

[source: scala/util/parsing/combinator/Parsers.scala]

trait Parsers
extends AnyRef

Parsers is a component that provides generic parser combinators.

It requires the type of the elements these parsers should parse (each parser is polymorphic in the type of result it produces).

There are two aspects to the result of a parser: (1) success or failure, and (2) the result. A Parser[T] provides both kinds of information.

The term ``parser combinator'' refers to the fact that these parsers are constructed from primitive parsers and composition operators, such as sequencing, alternation, optionality, repetition, lifting, and so on.

A ``primitive parser'' is a parser that accepts or rejects a single piece of input, based on a certain criterion, such as whether the input...

Even more primitive parsers always produce the same result, irrespective of the input.

@requires Elem the type of elements the provided parsers consume (When consuming invidual characters, a parser is typically called a ``scanner'', which produces ``tokens'' that are consumed by what is normally called a ``parser''. Nonetheless, the same principles apply, regardless of the input type.)

@provides Input = Reader[Elem] The type of input the parsers in this component expect.

@provides Parser[+T] extends (Input => ParseResult[T]) Essentially, a `Parser[T]' is a function from `Input' to `ParseResult[T]'.

@provides ParseResult[+T] is like an `Option[T]', in the sense that it is either `Success[T]', which consists of some result (:T) (and the rest of the input) or `Failure[T]', which provides an error message (and the rest of the input).

Author
Martin Odersky, Iulian Dragos, Adriaan Moors
Direct Known Subclasses:
RegexParsers, Scanners, TokenParsers

Type Summary
abstract type Elem
the type of input elements
type Input
The parser input is an abstract reader of input elements
Value Summary
var lastNoSuccess : NoSuccess
Method Summary
def OnceParser [T](f : (Reader) => ParseResult[T]) : Parser[T] with OnceParser[T]
def Parser [T](f : (Reader) => ParseResult[T]) : Parser[T]
def accept [ES](es : ES)(implicit view$1 : (ES) => List[Elem]) : Parser[List[Elem]]
A parser that matches only the given list of element `es'
implicit def accept (e : Elem) : Parser[Elem]
A parser that matches only the given element `e'
def accept [U](expected : java.lang.String, f : PartialFunction[Elem, U]) : Parser[U]
The parser that matches an element in the domain of the partial function `f'
def acceptIf (p : (Elem) => Boolean)(err : (Elem) => java.lang.String) : Parser[Elem]
def acceptMatch [U](expected : java.lang.String, f : PartialFunction[Elem, U]) : Parser[U]
def acceptSeq [ES](es : ES)(implicit view$2 : (ES) => Iterable[Elem]) : Parser[List[Elem]]
def chainl1 [T](p : => Parser[T], q : => Parser[(T, T) => T]) : Parser[T]
A parser generator that, roughly, generalises the rep1sep generator so that `q', which parses the separator, produces a left-associative function that combines the elements it separates.
def chainl1 [T, U](first : => Parser[T], p : => Parser[U], q : => Parser[(T, U) => T]) : Parser[T]
A parser generator that, roughly, generalises the rep1sep generator so that `q', which parses the separator, produces a left-associative function that combines the elements it separates.
def chainr1 [T, U](p : => Parser[T], q : => Parser[(T, U) => U], combine : (T, U) => U, first : U) : Parser[U]
A parser generator that generalises the rep1sep generator so that `q', which parses the separator, produces a right-associative function that combines the elements it separates. Additionally, The right-most (last) element and the left-most combinating function have to be supplied. rep1sep(p: Parser[T], q) corresponds to chainr1(p, q ^^ cons, cons, Nil) (where val cons = (x: T, y: List[T]) => x :: y)
def commit [T](p : => Parser[T]) : Parser[T]
Wrap a parser so that its failures become errors (the | combinator will give up as soon as it encounters an error, on failure it simply tries the next alternative)
def elem (kind : java.lang.String, p : (Elem) => Boolean) : Parser[Elem]
A parser matching input elements that satisfy a given predicate
def elem (e : Elem) : Parser[Elem]
A parser that matches only the given element `e'
def err (msg : java.lang.String) : Parser[Nothing]
A parser that results in an error
def failure (msg : java.lang.String) : Parser[Nothing]
A parser that always fails
def log [T](p : => Parser[T])(name : java.lang.String) : Parser[T]
def mkList [T] : (~[T, List[T]]) => List[T]
def not [T](p : => Parser[T]) : Parser[Unit]
Wrap a parser so that its failures&errors become success and vice versa -- it never consumes any input
def opt [T](p : => Parser[T]) : Parser[Option[T]]
A parser generator for optional sub-phrases.
def phrase [T](p : Parser[T]) : Parser[T]
def positioned [T <: Positional](p : => Parser[T]) : Parser[T]
`positioned' decorates a parser's result with the start position of the input it consumed.
def rep [T](p : => Parser[T]) : Parser[List[T]]
A parser generator for repetitions.
def rep1 [T](p : => Parser[T]) : Parser[List[T]]
A parser generator for non-empty repetitions.
def rep1 [T](first : => Parser[T], p : => Parser[T]) : Parser[List[T]]
A parser generator for non-empty repetitions.
def rep1sep [T](p : => Parser[T], q : => Parser[Any]) : Parser[List[T]]
A parser generator for non-empty repetitions.
def repN [T](n : Int, p : => Parser[T]) : Parser[List[T]]
A parser generator for a specified number of repetitions.
def repsep [T](p : => Parser[T], q : => Parser[Any]) : Parser[List[T]]
A parser generator for interleaved repetitions.
def success [T](v : T) : Parser[T]
A parser that always succeeds
Methods inherited from AnyRef
getClass, hashCode, equals, clone, toString, notify, notifyAll, wait, wait, wait, finalize, ==, !=, eq, ne, synchronized
Methods inherited from Any
==, !=, isInstanceOf, asInstanceOf
Class Summary
case class Error (val override msg : java.lang.String, val override next : Reader) extends NoSuccess with Product
The fatal failure case of ParseResult: contains an error-message and the remaining input. No back-tracking is done when a parser returns an `Error'
case class Failure (val override msg : java.lang.String, val override next : Reader) extends NoSuccess with Product
The failure case of ParseResult: contains an error-message and the remaining input. Parsing will back-track when a failure occurs.
sealed abstract class NoSuccess (val msg : java.lang.String, val override next : Reader) extends ParseResult[Nothing]
A common super-class for unsuccessful parse results
trait OnceParser [+T] extends Parser[T]
A parser whose ~ combinator disallows back-tracking.
sealed abstract class ParseResult [+T] extends AnyRef
A base class for parser results. A result is either successful or not (failure may be fatal, i.e., an Error, or not, i.e., a Failure) On success, provides a result of type T.
abstract class Parser [+T] extends (Reader) => ParseResult[T]
The root class of parsers. Parsers are functions from the Input type to ParseResult
case class Success [+T](val result : T, val override next : Reader) extends ParseResult[T] with Product
The success case of ParseResult: contains the result and the remaining input.
case class ~ [+a, +b](val _1 : a, val _2 : b) extends Product
Type Details
abstract type Elem
the type of input elements

type Input
The parser input is an abstract reader of input elements

Value Details
var lastNoSuccess : NoSuccess

Method Details
def Parser[T](f : (Reader) => ParseResult[T]) : Parser[T]

def OnceParser[T](f : (Reader) => ParseResult[T]) : Parser[T] with OnceParser[T]

def commit[T](p : => Parser[T]) : Parser[T]
Wrap a parser so that its failures become errors (the | combinator will give up as soon as it encounters an error, on failure it simply tries the next alternative)

def elem(kind : java.lang.String, p : (Elem) => Boolean) : Parser[Elem]
A parser matching input elements that satisfy a given predicate

elem(kind, p) succeeds if the input starts with an element `e' for which p(e) is true.

Parameters
kind - The element kind, used for error messages
p - A predicate that determines which elements match.
Returns

def elem(e : Elem) : Parser[Elem]
A parser that matches only the given element `e'

elem(e) succeeds if the input starts with an element `e'

Parameters
e - the `Elem' that must be the next piece of input for the returned parser to succeed
Returns
a `Parser' that succeeds if `e' is the next available input (and returns it).

implicit def accept(e : Elem) : Parser[Elem]
A parser that matches only the given element `e'

The method is implicit so that elements can automatically be lifted to their parsers. For example, when parsing `Token's, Identifier("new") (which is a `Token') can be used directly, instead of first creating a `Parser' using accept(Identifier("new")).

Parameters
e - the `Elem' that must be the next piece of input for the returned parser to succeed
Returns
a `tParser' that succeeds if `e' is the next available input.

def accept[ES](es : ES)(implicit view$1 : (ES) => List[Elem]) : Parser[List[Elem]]
A parser that matches only the given list of element `es'

accept(es) succeeds if the input subsequently provides the elements in the list `es'.

Parameters
es - the list of expected elements
Returns
a Parser that recognizes a specified list of elements

def accept[U](expected : java.lang.String, f : PartialFunction[Elem, U]) : Parser[U]
The parser that matches an element in the domain of the partial function `f'

If `f' is defined on the first element in the input, `f' is applied to it to produce this parser's result.

Example: The parser accept("name", {case Identifier(n) => Name(n)}) accepts an Identifier(n) and returns a Name(n).

Parameters
expected - a description of the kind of element this parser expects (for error messages)
f - a partial function that determines when this parser is successful and what its output is
Returns
A parser that succeeds if `f' is applicable to the first element of the input, applying `f' to it to produce the result.

def acceptIf(p : (Elem) => Boolean)(err : (Elem) => java.lang.String) : Parser[Elem]

def acceptMatch[U](expected : java.lang.String, f : PartialFunction[Elem, U]) : Parser[U]

def acceptSeq[ES](es : ES)(implicit view$2 : (ES) => Iterable[Elem]) : Parser[List[Elem]]

def failure(msg : java.lang.String) : Parser[Nothing]
A parser that always fails
Parameters
msg - The error message describing the failure.
Returns
A parser that always fails with the specified error message.

def err(msg : java.lang.String) : Parser[Nothing]
A parser that results in an error
Parameters
msg - The error message describing the failure.
Returns
A parser that always fails with the specified error message.

def success[T](v : T) : Parser[T]
A parser that always succeeds
Parameters
v - The result for the parser
Returns
A parser that always succeeds, with the given result `v'

def log[T](p : => Parser[T])(name : java.lang.String) : Parser[T]

def rep[T](p : => Parser[T]) : Parser[List[T]]
A parser generator for repetitions.

rep(p) repeatedly uses `p' to parse the input until `p' fails (the result is a List of the consecutive results of `p')

Parameters
p - a `Parser' that is to be applied successively to the input
Returns
A parser that returns a list of results produced by repeatedly applying `p' to the input.

def repsep[T](p : => Parser[T], q : => Parser[Any]) : Parser[List[T]]
A parser generator for interleaved repetitions.

repsep(p, q) repeatedly uses `p' interleaved with `q' to parse the input, until `p' fails. (The result is a `List' of the results of `p'.)

Example: repsep(term, ",") parses a comma-separated list of term's, yielding a list of these terms

Parameters
p - a `Parser' that is to be applied successively to the input
q - a `Parser' that parses the elements that separate the elements parsed by `p'
Returns
A parser that returns a list of results produced by repeatedly applying `p' (interleaved with `q') to the input. The results of `p' are collected in a list. The results of `q' are discarded.

def rep1[T](p : => Parser[T]) : Parser[List[T]]
A parser generator for non-empty repetitions.

rep1(p) repeatedly uses `p' to parse the input until `p' fails -- `p' must succeed at least once (the result is a `List' of the consecutive results of `p')

Parameters
p - a `Parser' that is to be applied successively to the input
Returns
A parser that returns a list of results produced by repeatedly applying `p' to the input (and that only succeeds if `p' matches at least once).

def rep1[T](first : => Parser[T], p : => Parser[T]) : Parser[List[T]]
A parser generator for non-empty repetitions.

rep1(f, p) first uses `f' (which must succeed) and then repeatedly uses `p' to parse the input until `p' fails (the result is a `List' of the consecutive results of `f' and `p')

Parameters
first - a `Parser' that parses the first piece of input
p - a `Parser' that is to be applied successively to the rest of the input (if any)
Returns
A parser that returns a list of results produced by first applying `f' and then repeatedly `p' to the input (it only succeeds if `f' matches).

def repN[T](n : Int, p : => Parser[T]) : Parser[List[T]]
A parser generator for a specified number of repetitions.

repN(n, p) uses `p' exactly `n' time to parse the input (the result is a `List' of the `n' consecutive results of `p')

Parameters
p - a `Parser' that is to be applied successively to the input
n - the exact number of times `p' must succeed
Returns
A parser that returns a list of results produced by repeatedly applying `p' to the input (and that only succeeds if `p' matches exactly `n' times).

def rep1sep[T](p : => Parser[T], q : => Parser[Any]) : Parser[List[T]]
A parser generator for non-empty repetitions.

rep1sep(first, p, q) starts by using `first', followed by repeatedly uses of `p' interleaved with `q' to parse the input, until `p' fails. `first' must succeed (the result is a `List' of the consecutive results of `first' and `p')

Parameters
first - a `Parser' that is to be applied to the first element of input
p - a `Parser' that is to be applied successively to the input
q - a `Parser' that parses the elements that separate the elements parsed by `p' (interleaved with `q')
Returns
A parser that returns a list of results produced by repeatedly applying `p' to the input (and that only succeeds if `p' matches at least once). The results of `p' are collected in a list. The results of `q' are discarded.

def chainl1[T](p : => Parser[T], q : => Parser[(T, T) => T]) : Parser[T]
A parser generator that, roughly, generalises the rep1sep generator so that `q', which parses the separator, produces a left-associative function that combines the elements it separates.

From: J. Fokker. Functional parsers. In J. Jeuring and E. Meijer, editors, Advanced Functional Programming, volume 925 of Lecture Notes in Computer Science, pages 1--23. Springer, 1995.

Parameters
p - a parser that parses the elements
q - a parser that parses the token(s) separating the elements, yielding a left-associative function that combines two elements into one

def chainl1[T, U](first : => Parser[T], p : => Parser[U], q : => Parser[(T, U) => T]) : Parser[T]
A parser generator that, roughly, generalises the rep1sep generator so that `q', which parses the separator, produces a left-associative function that combines the elements it separates.
Parameters
first - a parser that parses the first element
p - a parser that parses the subsequent elements
q - a parser that parses the token(s) separating the elements, yielding a left-associative function that combines two elements into one

def chainr1[T, U](p : => Parser[T], q : => Parser[(T, U) => U], combine : (T, U) => U, first : U) : Parser[U]
A parser generator that generalises the rep1sep generator so that `q', which parses the separator, produces a right-associative function that combines the elements it separates. Additionally, The right-most (last) element and the left-most combinating function have to be supplied. rep1sep(p: Parser[T], q) corresponds to chainr1(p, q ^^ cons, cons, Nil) (where val cons = (x: T, y: List[T]) => x :: y)
Parameters
p - a parser that parses the elements
q - a parser that parses the token(s) separating the elements, yielding a right-associative function that combines two elements into one
combine - the "last" (left-most) combination function to be applied
first - the "first" (right-most) element to be combined

def opt[T](p : => Parser[T]) : Parser[Option[T]]
A parser generator for optional sub-phrases.

opt(p) is a parser that returns `Some(x)' if `p' returns `x' and `None' if `p' fails

Parameters
p - A `Parser' that is tried on the input
Returns
a `Parser' that always succeeds: either with the result provided by `p' or with the empty result

def not[T](p : => Parser[T]) : Parser[Unit]
Wrap a parser so that its failures&errors become success and vice versa -- it never consumes any input

def positioned[T <: Positional](p : => Parser[T]) : Parser[T]
`positioned' decorates a parser's result with the start position of the input it consumed.
Parameters
p - a `Parser' whose result conforms to `Positional'.
Returns
A parser that has the same behaviour as `p', but which marks its result with the start position of the input it consumed, if it didn't already have a position.

def phrase[T](p : Parser[T]) : Parser[T]

A parser generator delimiting whole phrases (i.e. programs).

phrase(p) succeeds if p succeeds and no input is left over after p.

Parameters
p - the parser that must consume all input for the resulting parser to succeed.
Returns
a parser that has the same result as `p', but that only succeeds if p consumed all the input.

def mkList[T] : (~[T, List[T]]) => List[T]