scallion
This package is used to describe syntax for LL(1) languages.
To use the package, mix-in the scallion.Parsers trait.
object MyParser extends Parsers {
// Type of tokens.
type Token = MyToken
// Type of token kinds.
type Kind = MyKind
// Define the token kind of tokens.
override def getKind(token: Token): Kind = ...
// Then define your syntax using combinators.
lazy val mySyntax = ...
}
Attributes
Members list
Grouped members
Parsing
Main trait for defining parsers.
Main trait for defining parsers.
Attributes
- Supertypes
-
trait Debugtrait Operatorstrait PrettyPrintingtrait Enumerationtrait Parsingtrait Syntaxesclass Objecttrait Matchableclass AnyShow all
Syntax
Contains utilities to write syntaxes with infix, prefix and postfix operators. Expected to be mixed-in to Syntaxes
.
Contains utilities to write syntaxes with infix, prefix and postfix operators. Expected to be mixed-in to Syntaxes
.
==Example==
The following example shows how to build a syntax for a language with unary operators and infix operators of different priority levels.
// Syntax for basic expressions, which are our operands.
lazy val basic: Syntax[Expr] = number | open.skip ~ value ~ close.skip
// Syntax for postfix application of an operator.
lazy val postfixed: Syntax[Expr] = postfixes(basic, fac)({
// Indicates how to apply the operator.
case (e, op) => UnaryExpr(op, e)
}, {
// Optionally, indicates how to unapply an operator.
// This is only used for pretty printing.
case UnaryExpr(op, e) => (e, op)
})
// Syntax for infix application of various operators.
lazy val value: Syntax[Expr] = recursive {
// In this case, our operands are postfixed basic expressions.
operators(postfixed)(
// Indicates the various operators and their associativity.
// First level in multiplication and division, which are
// left associative and bind the strongest.
times | div is LeftAssociative,
// On the next priority level are addition and subtraction,
// which are also left associative.
plus | minus is LeftAssociative
)({
// Indicates how to apply a binary operator.
case (l, op, r) => BinaryExpr(op, l, r)
}, {
// Optionally, indicates how to unapply a binary operator.
// This is only used for pretty printing.
case BinaryExpr(op, l, r) => (l, op, r)
})
}
Attributes
- Supertypes
-
class Objecttrait Matchableclass Any
- Known subtypes
-
trait Parsers
- Self type
Debugging
Pairs
Adds an ~
methods to build pairs.
Adds an ~
methods to build pairs.
Attributes
- Supertypes
-
class Objecttrait Matchableclass Any
Adds an ~
methods to build pairs.
Adds an ~
methods to build pairs.
Attributes
Simply a pair.
Simply a pair.
Can be used in infix position in pattern matching.
Value parameters
- _1
-
First element.
- _2
-
Second element.
Attributes
- Supertypes
-
trait Serializabletrait Producttrait Equalsclass Objecttrait Matchableclass AnyShow all