parsley
package parsley
- Alphabetic
- Public
- Protected
Package Members
- package errors
This package contains various functionality relating to the generation and formatting of error messages.
This package contains various functionality relating to the generation and formatting of error messages.
In particular, it includes a collection of combinators for improving error messages within the parser, including labelling and providing additional information. It also contains combinators that can be used to valid data produced by a parser, to ensure it conforms to expected invariances, producing good quality error messages if this is not the case. Finally, this package contains ways of changing the formatting of error messages: this can either be changing how the default
String
-based errors are formatted, or by injectiing Parsley's errors into a custom error object. - package expr
This package contains various functionality relating to the parsing of expressions..
This package contains various functionality relating to the parsing of expressions..
This includes the "chain" combinators, which tackle the left-recursion problem and allow for the parsing and combining of operators with values. It also includes functionality for constructing larger precedence tables, which may even vary the type of each layer in the table, allowing for strongly-typed expression parsing.
- package implicits
This package contains various functionality that involve Scala's implicits mechanism.
This package contains various functionality that involve Scala's implicits mechanism.
This includes conversions from scala literals into parsers, as well as enabling new syntax on regular Scala values (such as Parsley's
lift
orzipped
syntax). Automatic conversion toParsley[Unit]
is also supported within this package. - package internal
- package token
This package provides a wealth of functionality for performing common lexing tasks.
This package provides a wealth of functionality for performing common lexing tasks.
It is organised as follows:
- the main parsing functionality is accessed via
Lexer
, which provides implementations for the combinators found in the sub-packages given aLexicalDesc
. - the
descriptions
sub-package is how a lexical structure can be described, providing the configuration that alters the behaviour of the parsers produced by theLexer
. - the other sub-packages contain the high-level interfaces that the
Lexer
exposes, which can be used to pass whitespace-aware and non-whitespace-aware combinators around in a uniform way. - the
predicate
module contains functionality to help define boolean predicates on characters or unicode codepoints.
- the main parsing functionality is accessed via
Type Members
- class Failure[Err] extends Result[Err, Nothing] with Product with Serializable
This class is used for a parser failure, and contains the error message.
This class is used for a parser failure, and contains the error message.
- Err
the type of the error message generated by the failing parse.
- final class Parsley[+A] extends AnyVal
This is the class that encapsulates the act of parsing and running an object of this class with
parse
will parse the string given as input toparse
.This is the class that encapsulates the act of parsing and running an object of this class with
parse
will parse the string given as input toparse
.- Version
4.0.0
- Note
In order to construct an object of this class you must use the combinators; the class itself is opaque.
- sealed abstract class Result[+Err, +A] extends AnyRef
This trait represents the result of a parser.
This trait represents the result of a parser.
Either a
Success[A]
or aFailure
.- A
the type of expected success result.
- case class Success[A] extends Result[Nothing, A] with Product with Serializable
This class is used for when a parser succeeds, and contains its result.
This class is used for when a parser succeeds, and contains its result.
- A
the type of expected success result.
Value Members
- object Failure extends Serializable
- object Parsley
This object contains the core "function-style" combinators: all parsers will likely require something from within!
This object contains the core "function-style" combinators: all parsers will likely require something from within!
In particular, it contains combinators for: controlling how input is consumed; injecting values into the parser, or failing; extracting position information from the parser; conditional execution of parsers; and more.
- object ap
This module contains
ap1
throughap22
, which allow for the application of a parser returning a function of arityN
toN
parsers.This module contains
ap1
throughap22
, which allow for the application of a parser returning a function of arityN
toN
parsers.The combinators contained in this module all sequence a number of parsers together, but are capable of combining the results generated by these parsers into a single value with a function of the correct arity produced by the first parser. This is a clean way of putting together multiple parsers and getting a meaningful result out.
scala> import parsley.character.char scala> import parsley.ap.{ap2, ap3} scala> case class Add(x: Int, y: Int) scala> val p = ap2(pure(Add), char('a') #> 4, char('b') #> 5) scala> p.parse("ab") val res0 = Success(Add(4, 5)) scala> val q = ap3(pure((x: Int, y: Int, z: Int) => x * y + z), char('a') #> 3, char('b') #> 2, char('c') #> 5) scala> q.parse("abc") val res1 = Success(11) scala> q.parse("ab") val res2 = Failure(..)
- Since
4.0.0
Example: - object character
This module contains many parsers to do with reading one or more characters.
This module contains many parsers to do with reading one or more characters. Almost every parser will need something from this module.
In particular, this module contains: combinators that can read specific characters; combinators that represent character classes and their negations; combinators for reading specific strings; as well as a selection of pre-made parsers to parse specific kinds of character, like digits and letters.
- Since
2.2.0
- object combinator
This module contains a huge number of pre-made combinators that are very useful for a variety of purposes.
This module contains a huge number of pre-made combinators that are very useful for a variety of purposes.
In particular, it contains combinators for: performing a parser iteratively, collecting all the results; querying whether or not any input is left; optionally performing parsers; parsing delimited constructions; handling multiple possible alternatives or parsers to sequence; handling more complex conditional execution; and more.
- Since
2.2.0
- object debug
This module contains the very useful debugging combinator, as well as breakpoints.
- object extension
These implicit classes can be used to extend the core combinator set of Parsley.
These implicit classes can be used to extend the core combinator set of Parsley.
This may mean that importing them enables combinators that can be used on non-
Parsley
types, or might enable some syntactic sugar that is not part of the core combinator "style". - object genericbridges
This module contains the definition of 23 basic generic parser bridge traits, which are used to implement the Parser Bridge pattern for types that do not require metadata.
This module contains the definition of 23 basic generic parser bridge traits, which are used to implement the Parser Bridge pattern for types that do not require metadata.
The traits within are designed to be extended by the companion object of some case class that is produced as the result of a parser: by using these traits, it enables a new
apply
method that makes it appear like the constructor is applied to the parsers themselves. This can be very useful for performing extra verification on the produced results, or to incorporate metadata into the result. Specifically, these traits are designed to be the bare-minimum functionaity, and do not interact with any metadata.- Since
4.0.0
- object io
This module contains utilities to have parsers interact with IO, including the very useful
parseFromFile
method (exposed byParseFromIO
)This module contains utilities to have parsers interact with IO, including the very useful
parseFromFile
method (exposed byParseFromIO
)- Since
3.0.0
- object lift
This module contains
lift1
throughlift22
, which allow for the application of a function of arityN
toN
parsers.This module contains
lift1
throughlift22
, which allow for the application of a function of arityN
toN
parsers.The combinators contained in this module all sequence a number of parsers together, but are capable of combining the results generated by these parsers into a single value with a given function of the correct arity. This is a clean way of putting together multiple parsers and getting a meaningful result out.
scala> import parsley.character.char scala> import parsley.lift.{lift2, lift3} scala> case class Add(x: Int, y: Int) scala> val p = lift2(Add, char('a') #> 4, char('b') #> 5) scala> p.parse("ab") val res0 = Success(Add(4, 5)) scala> val q = lift3((x: Int, y: Int, z: Int) => x * y + z, char('a') #> 3, char('b') #> 2, char('c') #> 5) scala> q.parse("abc") val res1 = Success(11) scala> q.parse("ab") val res2 = Failure(..) scala> val q2 = lift3[Int, Int, Int, Int](_ * _ + _, char('a') #> 3, char('b') #> 2, char('c') #> 5)
- Since
2.2.0
Example: - object position
This module contains parsers that provide a way to extract position information during a parse.
This module contains parsers that provide a way to extract position information during a parse.
Position parsers can be important for when the final result of the parser needs to encode position information for later consumption: this is particularly useful for abstract syntax trees. Offset is also exposed by this interface, which may be useful for establishing a caret size in specialised error messages.
- Since
4.2.0
- object registers
This module contains all the functionality and operations for using and manipulating registers.
This module contains all the functionality and operations for using and manipulating registers.
These often have a role in performing context-sensitive parsing tasks, where a Turing-powerful system is required. While
flatMap
is capable of such parsing, it is much less efficient than the use of registers, though slightly more flexible. In particular, thepersist
combinator enabled byRegisterMethods
can serve as a drop-in replacement forflatMap
in many scenarios.- Since
2.2.0
This is the documentation for Parsley.
Package structure
The parsley package contains the
Parsley
class, as well as theResult
,Success
, andFailure
types. In addition to these, it also contains the following packages and "modules" (a module is defined as being an object which mocks a package):parsley.Parsley
contains the bulk of the core "function-style" combinators.parsley.combinator
contains many helpful combinators that simplify some common parser patterns.parsley.character
contains the combinators needed to read characters and strings, as well as combinators to match specific sub-sets of characters.parsley.debug
contains debugging combinators, helpful for identifying faults in parsers.parsley.extension
contains syntactic sugar combinators exposed as implicit classes.parsley.io
contains extension methods to run parsers with input sourced from IO sources.parsley.expr
contains the following sub modules:parsley.expr.chain
contains combinators used in expression parsingparsley.expr.precedence
is a builder for expression parsers built on a precedence table.parsley.expr.infix
contains combinators used in expression parsing, but with more permissive types than their equivalents inchain
.parsley.expr.mixed
contains combinators that can be used for expression parsing, but where different fixities may be mixed on the same level: this is rare in practice.parsley.implicits
contains several implicits to add syntactic sugar to the combinators. These are sub-categorised into the following sub modules:parsley.implicits.character
contains implicits to allow you to use character and string literals as parsers.parsley.implicits.combinator
contains implicits related to combinators, such as the ability to make any parser into aParsley[Unit]
automatically.parsley.implicits.lift
enables postfix application of the lift combinator onto a function (or value).parsley.implicits.zipped
enables boths a reversed form of lift where the function appears on the right and is applied on a tuple (useful when type inference has failed) as well as a.zipped
method for building tuples out of several combinators.parsley.errors
contains modules to deal with error messages, their refinement and generation.parsley.errors.combinator
provides combinators that can be used to either produce more detailed errors as well as refine existing errors.parsley.errors.tokenextractors
provides mixins for common token extraction strategies during error message generation: these can be used to avoid implementingunexpectedToken
in theErrorBuilder
.parsley.lift
contains functions which lift functions that work on regular types to those which now combine the results of parsers returning those same types. these are ubiquitous.parsley.ap
contains functions which allow for the application of a parser returning a function to several parsers returning each of the argument types.parsley.registers
contains combinators that interact with the context-sensitive functionality in the form of registers.parsley.token
contains theLexer
class that provides a host of helpful lexing combinators when provided with the description of a language.parsley.position
contains parsers for extracting position information.parsley.genericbridges
contains some basic implementations of the Parser Bridge pattern (see Design Patterns for Parser Combinators in Scala, or the parsley wiki): these can be used before more specialised generic bridge traits can be constructed.