Class ExpressionPrinter
- Namespace
- Invex.StructuredText.Expressions
- Assembly
- Invex.StructuredText.dll
public abstract class ExpressionPrinter
- Inheritance
-
objectExpressionPrinter
- Derived
Remarks
The base class handles operator precedence (adding parentheses only where needed), literal quoting and escaping, casts, raw text and context paths. Platforms override the members that differ: how operators and functions are spelled, how ReferenceExpr nodes and ResultLiterals are written, and how an embedded expression is delimited inside a Template.
Nodes the printer doesn't recognise cause a System.NotSupportedException.
Constructors
ExpressionPrinter()
protected ExpressionPrinter()
Fields
AndPrecedence
Precedence of AND.
protected const int AndPrecedence = 2
Field Value
- int
AtomPrecedence
Precedence of atoms: literals, paths, calls and access.
protected const int AtomPrecedence = 5
Field Value
- int
ComparisonPrecedence
Precedence of comparisons.
protected const int ComparisonPrecedence = 3
Field Value
- int
NotPrecedence
Precedence of unary NOT.
protected const int NotPrecedence = 4
Field Value
- int
OrPrecedence
Precedence of OR.
protected const int OrPrecedence = 1
Field Value
- int
RawPrecedence
Precedence given to raw text that isn't a plain dotted path. It is the lowest, so raw text is parenthesized whenever it is nested inside an operator, and written verbatim when it stands alone.
protected const int RawPrecedence = 0
Field Value
- int
Methods
FunctionName(string)
Maps a canonical function name (see FunctionNames) to the platform's name. Returns the name unchanged by default.
protected virtual string FunctionName(string name)
Parameters
namestringThe canonical name.
Returns
- string
The platform's name.
IsDotAccessible(string)
true when name can be written with property dereference
(.name): letters, digits, _ and -, not starting with a digit or -.
public static bool IsDotAccessible(string name)
Parameters
namestringThe property name.
Returns
- bool
Whether dot syntax is valid for the name.
Print(Expr)
Renders expression as an expression (without template delimiters).
public string Print(Expr expression)
Parameters
expressionExprThe expression.
Returns
- string
The rendered text.
Print(Expr, int)
Renders expression, adding parentheses when its precedence is below
minimumPrecedence.
protected string Print(Expr expression, int minimumPrecedence)
Parameters
expressionExprThe expression.
minimumPrecedenceintThe lowest precedence that may appear without parentheses.
Returns
- string
The rendered text.
PrintCall(string, IReadOnlyList<Expr>)
Renders a function call. Writes FunctionName(string) followed by the arguments in order by default; platforms whose functions take arguments in a different order override this.
protected virtual string PrintCall(string name, IReadOnlyList<Expr> arguments)
Parameters
namestringThe canonical function name (see FunctionNames).
argumentsIReadOnlyList<Expr>The arguments, in canonical order.
Returns
- string
The rendered call.
PrintComparison(ComparisonExpr)
Renders a comparison. Uses infix operators (==, !=, <, …) by default.
protected virtual (string Text, int Precedence) PrintComparison(ComparisonExpr comparison)
Parameters
comparisonComparisonExprThe comparison.
Returns
- (string Text, int Precedence)
The rendered text and its precedence.
PrintLogical(LogicalExpr)
Renders an AND or OR. Uses infix && and || by default.
protected virtual (string Text, int Precedence) PrintLogical(LogicalExpr logical)
Parameters
logicalLogicalExprThe logical expression.
Returns
- (string Text, int Precedence)
The rendered text and its precedence.
PrintMember(string)
Renders a property access suffix: .name where valid, otherwise ['name'] with quotes escaped.
protected virtual string PrintMember(string name)
Parameters
namestringThe property name.
Returns
- string
The suffix to append to the target.
PrintNot(NotExpr)
Renders a negation. Uses prefix ! by default.
protected virtual (string Text, int Precedence) PrintNot(NotExpr not)
Parameters
notNotExprThe negation.
Returns
- (string Text, int Precedence)
The rendered text and its precedence.
PrintOrNull(Expr?)
public string? PrintOrNull(Expr? expression)
Parameters
expressionExprThe expression.
Returns
- string
The rendered text, or null.
PrintOther(Expr)
Renders a node the base class doesn't know, such as a platform-specific node. Throws System.NotSupportedException by default.
protected virtual (string Text, int Precedence) PrintOther(Expr expression)
Parameters
expressionExprThe node.
Returns
- (string Text, int Precedence)
The rendered text and its precedence.
PrintReference(ReferenceExpr)
Renders a portable reference such as a secret or a job output.
protected abstract string PrintReference(ReferenceExpr reference)
Parameters
referenceReferenceExprThe reference.
Returns
- string
The rendered text.
PrintResult(RunResult)
Renders a run result literal.
protected abstract string PrintResult(RunResult result)
Parameters
resultRunResultThe result.
Returns
- string
The rendered text.
QuoteString(string)
Quotes and escapes a string literal. Uses single quotes, doubling embedded single quotes, by default.
protected virtual string QuoteString(string value)
Parameters
valuestringThe unquoted value.
Returns
- string
The literal.
Render(Template)
Renders template: literal text as-is, embedded expressions wrapped with
WrapEmbedded(string).
public virtual string Render(Template template)
Parameters
templateTemplateThe template.
Returns
- string
The rendered text.
Exceptions
- NotSupportedException
Thrown when the template contains an expression, or a combination of expressions and text, the platform can't express.
RenderEmbedded(Expr)
Renders an expression embedded in a Template. By default it is printed and wrapped with WrapEmbedded(string); platforms with several embedding syntaxes override this.
protected virtual string RenderEmbedded(Expr expression)
Parameters
expressionExprThe embedded expression.
Returns
- string
The rendered text.
RenderOrNull(Template?)
public string? RenderOrNull(Template? template)
Parameters
templateTemplateThe template.
Returns
- string
The rendered text, or null.
WrapEmbedded(string)
Wraps a rendered expression that is embedded in literal text, e.g. in expression delimiters.
protected abstract string WrapEmbedded(string printedExpression)
Parameters
printedExpressionstringThe rendered expression.
Returns
- string
The wrapped text.