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
- Object
- Expression<T>
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 |
FromString(String)
Creates a leaf-level Expression<T> from the given string.
Declaration
public static Expression<T> FromString(string text)
Parameters
Type | Name | Description |
---|---|---|
text |
The document |
Returns
Type | Description |
---|---|
Expression<T> |
An expression representing the |
Remarks
Expr.FromString(x)
is equivalent to
Expr.FromDocument(Doc.FromString(x))
.
See Also
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 |
Remarks
This conversion is equivalent to FromDocument(Document<T>).
See Also
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 |
---|---|---|
text |
The text |
Returns
Type | Description |
---|---|
Expression<T> |
An expression representing the |
Remarks
This conversion is equivalent to FromString(String).