Gutenberg

Class Expression<T>

Represents an expression composed of operators applied to Document<T>s.

You can use Expression<T> to pretty-print code in an expression-based language with operators and precedence. Expression<T> will automatically insert parentheses into the resulting document to create an unambiguous display.

To get started, use the OperatorFactory<T> to create objects representing each of the operators in your language. Then those operators can be applied to . Document<T>s and other expressions to create complex expressions. Finally, when you PrettyPrint() the expression, it will return a Document<T> with parentheses inserted in the correct locations.

Inheritance
Implements
Declaration
public abstract class Expression<T> : Object, IPrettyPrintable<T>
Type Parameters
Name Description

T

The type of annotations in the Document<T>s.

Examples

Here is an example of a small expression language with negation, addition, and equality operators. Since ! binds tighter (has a higher precedence) than ==, the example document pretty-prints itself with parentheses around the equality expression.

var not = OperatorFactory<object>.Prefix(7, "!");
var plus = OperatorFactory<object>.InfixL(5, " + ");
var eq = OperatorFactory<object>.InfixL(3, " == ");

var doc = not.Apply(eq.Apply(plus.Apply("x", "y"), "z"));
Console.WriteLine(doc.PrettyPrint());
// Output:
// !(x + y == z)

Constructors

Expression()

Declaration
protected Expression()

Methods

FromDocument(Document<T>)

Creates a leaf-level Expression<T> from the given document.

Declaration
public static Expression<T> FromDocument(Document<T> document)
Parameters
Type Name Description

Document<T>

document

The document.

Returns
Type Description

Expression<T>

An expression representing the document.

FromString(String)

Creates a leaf-level Expression<T> from the given string.

Declaration
public static Expression<T> FromString(string text)
Parameters
Type Name Description

String

text

The document.

Returns
Type Description

Expression<T>

An expression representing the text.

Remarks

Expr.FromString(x) is equivalent to Expr.FromDocument(Doc.FromString(x)).

See Also
FromDocument(Document<T>)

PrettyPrint()

Render the current object as a Document<T>.

Declaration
public Document<T> PrettyPrint()
Returns
Type Description

Document<T>

A Document<T> containing a textual representation of the current object.

Operators

Implicit(Document<T> to Expression<T>)

Implicitly converts a Document<T> to an Expression<T>.

Declaration
public static implicit operator Expression<T>(Document<T> document)
Parameters
Type Name Description

Document<T>

document

The document.

Returns
Type Description

Expression<T>

An expression representing the document.

Remarks

This conversion is equivalent to FromDocument(Document<T>).

See Also
FromDocument(Document<T>)

Implicit(String to Expression<T>)

Implicitly converts a string to an Expression<T>.

Declaration
public static implicit operator Expression<T>(string text)
Parameters
Type Name Description

String

text

The text.

Returns
Type Description

Expression<T>

An expression representing the text.

Remarks

This conversion is equivalent to FromString(String).

See Also
FromString(String)

Implements

IPrettyPrintable<T>