precedence
This object is used to construct precedence parsers from either a Prec
or many Ops[A, A]
.
Contained within this object are three different shapes of apply
functions: they allows for
the construction of a precedence parser from either: a collection of levels of operators and atoms,
in either weak-to-strong or strong-to-weak orderings; or a heterogeneous precedence table Prec
.
Attributes
- Since:
2.2.0
- Source:
- precedence.scala
- Graph
- Supertypes
- Self type
- precedence.type
Members list
Value members
Concrete methods
This combinator builds an expression parser given a collection of homogeneous atoms and operators.
This combinator builds an expression parser given a collection of homogeneous atoms and operators.
An expression parser will be formed by parsing atom0
and the remaining atoms
at
the base of the table. Then lvlTightest
will be the level containing the tightest
operators. The remaining lvls
will be from tightest-to-weakest binding. All levels
must consume and produce the same type.
Attributes
- A
the type of the expression parsers generated by this combinator.
- atom0
the first atom at the base of the table.
- atoms
the remaining atoms at the base of the table.
- lvlTightest
the tightest binding operators.
- lvls
the remaining levels of operators, ordered tightest-to-weakest.
- Returns:
an expression parser for the described precedence table.
- Since:
3.0.0
- Example:
scala> import parsley.Parsley, parsley.character.{char, digit} scala> import parsley.expr.{Ops, InfixL, precedence} scala> val expr = precedence(digit.map(_.asDigit)) (Ops(InfixL)(char('*') #> (_ * _)), Ops(InfixL)(char('+') #> (_ + _), char('-') #> (_ - _))) scala> expr.parse("1+8*7+4") val res0 = Success(61)
- Source:
- precedence.scala
This combinator builds an expression parser given a collection of homogeneous atoms and operators.
This combinator builds an expression parser given a collection of homogeneous atoms and operators.
An expression parser will be formed by parsing atom0
and the remaining atoms
at
the base of the table. Then lvlWeakest
will be the level containing the weakest
operators at the outermost layer of the table. The remaining lvls
will be from
tightest-to-weakest binding. All levels must consume and produce the same type.
Attributes
- A
the type of the expression parsers generated by this combinator.
- atom0
the first atom at the base of the table.
- atoms
the remaining atoms at the base of the table.
- lvlWeakest
the weakest binding operators.
- lvls
the remaining levels of operators, ordered weakest-to-tightest.
- Returns:
an expression parser for the described precedence table.
- Since:
4.0.0
- Example:
scala> import parsley.Parsley, parsley.character.{char, digit} scala> import parsley.expr.{Ops, InfixL, precedence} scala> val expr = precedence[Int](Ops(InfixL)(char('+') #> (_ + _), char('-') #> (_ - _))), Ops(InfixL)(char('*') #> (_ * _)) (digit.map(_.asDigit))) scala> expr.parse("1+8*7+4") val res0 = Success(61)
Note that the type ascription on
precedence
is needed for this example, to avoid specifying the argument types of the operators in the table: this wouldn't be required with the tightest-to-weakest variant, as the inference is better on the atoms.- Source:
- precedence.scala
This combinator builds an expression parser given a heterogeneous precedence table.
This combinator builds an expression parser given a heterogeneous precedence table.
An expression parser will be formed by collapsing the given precedence table layer-by-layer. Since this table is heterogeneous, each level of the table produces a difference type, which is then consumed by the next level above.
Attributes
- A
the type of the expression parsers generated by this combinator.
- table
the description of the heterogeneous table, where each level can vary in output and input types.
- Returns:
an expression parser for the described precedence table.
- See also:
Prec
and its subtypes for a description of how the types work.- Since:
4.0.0
- Example:
This is overkill for this particular example, as each layer has the same type: it would be better to use one of the other forms of
precedence
for simplicity. This is best used in conjunction withSOps
orGOps
; or a mix ofSOps
,GOps
, andOps
.scala> import parsley.Parsley, parsley.character.{char, digit} scala> import parsley.expr.{Atoms, Ops, InfixL, precedence} scala> val expr = precedence(Atoms(digit.map(_.asDigit)) :+ Ops[Int](InfixL)(char('*') #> (_ * _)) :+ Ops[Int](InfixL)(char('+') #> (_ + _), char('-') #> (_ - _))) scala> expr.parse("1+8*7+4") val res0 = Success(61)
- Source:
- precedence.scala