PrattParser

open class PrattParser<S : Scanner<*>, C, R>(val parsePrimary: (S, C) -> R, val buildUnary: (S, C, String, R) -> R, val buildBinary: (S, C, String, R, R) -> R, vararg operators: Operator)

A simple configurable expression parser.

  • S is the scanner type

  • C is the type of the parsing context, typically holding variable definitions etc.

  • R is the type of the parsed expression.

The parser is primarily configured by providing buildUnary and buildBinary callbacks along with Operator definitions (Operator.Prefix, Operator.Infix, Operator.InfixRtl, Operator.Suffix) specifying operator names and precedences.

Constructors

Link copied to clipboard
constructor(parsePrimary: (S, C) -> R, buildUnary: (S, C, String, R) -> R, buildBinary: (S, C, String, R, R) -> R, vararg operators: Operator)

Properties

Link copied to clipboard
val buildBinary: (S, C, String, R, R) -> R
Link copied to clipboard
val buildUnary: (S, C, String, R) -> R
Link copied to clipboard
val parsePrimary: (S, C) -> R

Functions

Link copied to clipboard
fun parseExpression(scanner: S, context: C, precedence: Int = -1): R

Parses an expression from the given scanner. Leftover tokens will be ignored and may be handled by the caller.

Link copied to clipboard
fun <S : Scanner<*>, R> PrattParser<S, Unit, R>.parseExpression(scanner: S): R

Convenience overload for PrattParser instances with Unit context.