Scala Library
|
|
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).
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'
|
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'
|
implicit def
|
accept
(e : Elem) : Parser[Elem]
A parser that matches only the given element `e'
|
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
type
Input
Value Details |
Method Details |
def
Parser[T](f : (Reader) => ParseResult[T]) : Parser[T]
def
OnceParser[T](f : (Reader) => ParseResult[T]) : Parser[T] with
OnceParser[T]
def
elem(kind : java.lang.String, p : (Elem) => Boolean) : Parser[Elem]
elem(kind, p) succeeds if the input starts with an element `e' for which p(e) is true.
kind -
The element kind, used for error messagesp -
A predicate that determines which elements match.elem(e) succeeds if the input starts with an element `e'
e -
the `Elem' that must be the next piece of input for the returned parser to succeedThe 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")).
e -
the `Elem' that must be the next piece of input for the returned parser to succeedaccept(es) succeeds if the input subsequently provides the elements in the list `es'.
es -
the list of expected elements
def
accept[U](expected : java.lang.String, f : PartialFunction[Elem, U]) : Parser[U]
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)
.
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
def
acceptMatch[U](expected : java.lang.String, f : PartialFunction[Elem, U]) : Parser[U]
def
failure(msg : java.lang.String) : Parser[Nothing]
msg -
The error message describing the failure.
def
err(msg : java.lang.String) : Parser[Nothing]
msg -
The error message describing the failure.v -
The result for the parser
def
log[T](p : => Parser[T])(name : java.lang.String) : Parser[T]
rep(p) repeatedly uses `p' to parse the input until `p' fails (the result is a List of the consecutive results of `p')
p -
a `Parser' that is to be applied successively to the inputrepsep(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
p -
a `Parser' that is to be applied successively to the inputq -
a `Parser' that parses the elements that separate the elements parsed by `p'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')
p -
a `Parser' that is to be applied successively to the inputrep1(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')
first -
a `Parser' that parses the first piece of inputp -
a `Parser' that is to be applied successively to the rest of the input (if any)repN(n, p) uses `p' exactly `n' time to parse the input (the result is a `List' of the `n' consecutive results of `p')
p -
a `Parser' that is to be applied successively to the inputn -
the exact number of times `p' must succeedrep1sep(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')
first -
a `Parser' that is to be applied to the first element of inputp -
a `Parser' that is to be applied successively to the inputq -
a `Parser' that parses the elements that separate the elements parsed by `p' (interleaved with `q')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.
p -
a parser that parses the elementsq -
a parser that parses the token(s) separating the elements, yielding a left-associative function that combines two elements into onefirst -
a parser that parses the first elementp -
a parser that parses the subsequent elementsq -
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]
p -
a parser that parses the elementsq -
a parser that parses the token(s) separating the elements, yielding a right-associative function that combines two elements into onecombine -
the "last" (left-most) combination function to be appliedfirst -
the "first" (right-most) element to be combinedopt(p) is a parser that returns `Some(x)' if `p' returns `x' and `None' if `p' fails
p -
A `Parser' that is tried on the input
def
positioned[T <: Positional](p : => Parser[T]) : Parser[T]
p -
a `Parser' whose result conforms to `Positional'.A parser generator delimiting whole phrases (i.e. programs).
phrase(p)
succeeds if p
succeeds and
no input is left over after p
.
p -
the parser that must consume all input for the resulting parser to succeed.p
consumed all the input.
Scala Library
|
|