Scala Library
|
|
trait
PackratParsers
extends
Parsers
PackratParsers
is a component that extends the parser combinators
provided by Parsers
with a memoization facility
(``Packrat Parsing'').
Packrat Parsing is a technique for implementing backtracking, recursive-descent parsers, with the advantage that it guarantees unlimited lookahead and a linear parse time. Using this technique, left recursive grammars can also be accepted.
Using PackratParsers
is very similar to using Parsers
:
Parsers
(directly or through a subclass) can
mix in PackratParsers
. Example:
object MyGrammar extends StandardTokenParsers with PackratParsers
def
without formal parameters
becomes a lazy val
, and its type is changed from Parser[Elem]
to PackratParser[Elem]
. So, for example, def production: Parser[Int] = {...}
becomes lazy val production: PackratParser[Int] = {...}
PackratParser
s is not an ``all or nothing'' decision. They
can be free mixed with regular Parser
s in a single grammar.
Cached parse results are attached to the input, not the grammar.
Therefore, PackratsParser
s require a PackratReader
as input, which
adds memoization to an underlying Reader
. Programmers can create PackratReader
objects either manually, as in production(new PackratReader(new lexical.Scanner("input")))
,
but the common way should be to rely on the combinator phrase
to wrap a given
input with a PackratReader
if the input is not one itself.
Values and Variables inherited from Parsers | |
lastNoSuccess |
Method Summary | |
def
|
memo
[T](p : Parser[T]) : PackratParser[T]
Explicitly convert a given parser to a memoizing packrat parser.
In most cases, client code should avoid calling
memo directly
and rely on implicit conversion instead. |
implicit def
|
parser2packrat
[T](p : => Parser[T]) : PackratParser[T]
Implicitly convert a parser to a packrat parser.
The conversion is triggered by giving the appropriate target type:
val myParser: PackratParser[MyResult] = aParser
|
override def
|
phrase [T](p : Parser[T]) : PackratParser[T] |
Methods inherited from Parsers | |
Parser, OnceParser, commit, elem, elem, accept, accept, accept, acceptIf, acceptMatch, acceptSeq, failure, err, success, log, rep, repsep, rep1, rep1, repN, rep1sep, chainl1, chainl1, chainr1, opt, not, guard, positioned, mkList |
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 | |
abstract class
|
PackratParser
[+T] extends Parser[T]
The root class of packrat parsers.
|
class
|
PackratReader
[+T](underlying : Reader[T]) extends Reader[T]
A specialized
Reader class that wraps an underlying Reader
and provides memoization of parse results. |
Method Details |
override
def
phrase[T](p : Parser[T]) : PackratParser[T]
A parser generator delimiting whole phrases (i.e. programs).
Overridden to make sure any input passed to the argument parser
is wrapped in a PackratReader
.
implicit
def
parser2packrat[T](p : => Parser[T]) : PackratParser[T]
def
memo[T](p : Parser[T]) : PackratParser[T]
memo
directly
and rely on implicit conversion instead.
Scala Library
|
|