Enhanced C#
Loyc library documentation
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Events Pages
Nested classes | Public fields | Properties | Public Member Functions | Protected Member Functions | Protected fields | List of all members
Loyc.Syntax.Impl.LNodePrinterHelper Class Reference

A helper type for printer objects; for details, please see ILNodePrinterHelper{S}, its derived interface ILNodePrinterHelperWithRevokableNewlines{S,C}, and the documentation of the constructor LNodePrinterHelper(StringBuilder, string, string, bool, string, string). More...


Source file:
Inheritance diagram for Loyc.Syntax.Impl.LNodePrinterHelper:
Loyc.Syntax.Impl.ILNodePrinterHelperWithRevokableNewlines< Checkpoint, out Self > Loyc.Syntax.Impl.ILNodePrinterHelper Loyc.Syntax.Impl.ILNodePrinterHelper< out Self > Loyc.Syntax.Impl.IPrinterHelper< out Self > Loyc.Syntax.Impl.PrinterState

Remarks

A helper type for printer objects; for details, please see ILNodePrinterHelper{S}, its derived interface ILNodePrinterHelperWithRevokableNewlines{S,C}, and the documentation of the constructor LNodePrinterHelper(StringBuilder, string, string, bool, string, string).

Public fields

int LineStartIndex => _lineStartIndex
 
int IndexInCurrentLine => _s.Length - _lineStartIndex
 
int IndexInCurrentLineAfterIndent => _s.Length - _lineStartAfterIndent
 
bool AtStartOfLine => IsAtStartOfLine
 
bool IsAtStartOfLine => _s.Length <= _lineStartAfterIndent | (_pendingAction & PendingAction.Newline) != 0
 
char LastCharWritten => _s.TryGet(_s.Length - 1, '\uFFFF')
 
int LineWidth => _s.Length - _lineStartIndex
 
int LineNumber => _lineNo
 
int Length => StringBuilder.Length
 Current length of the output string. This length can decrease if newlines are revoked. More...
 

Properties

string IndentString [get, set]
 
string NewlineString [get, set]
 
string LabelIndentString [get, set]
 
string BracketIndentString [get, set]
 
int MaxBracketIndents [get, set]
 
StringBuilder StringBuilder [get, set]
 
Action< ILNode, IndexRange, int > SaveRange [get]
 Gets or sets a method that is called when the node's range in the output is locked in (after you call EndNode()). More...
 
- Properties inherited from Loyc.Syntax.Impl.ILNodePrinterHelperWithRevokableNewlines< Checkpoint, out Self >
int LineWidth [get]
 Gets the current width of the current line (typically measured in characters). More...
 
- Properties inherited from Loyc.Syntax.Impl.IPrinterHelper< out Self >
bool IsAtStartOfLine [get]
 Returns true iff nothing has been written since the last call to Newline or NewlineIsRequiredHere. More...
 
char LastCharWritten [get]
 Gets the character most recently written to the stream, or '' if no characters have been written. More...
 

Public Member Functions

 LNodePrinterHelper (StringBuilder s, Action< ILNode, IndexRange, int >?saveRange=null, bool allowNewlineRevocation=true, string indent="\t", string newline="\n", string labelIndent=" ", string bracketIndent=" ", int maxBracketIndents=4)
 
virtual void Reset ()
 Calls Dispose() and resets the state of this object, like calling the constructor. Does not clear the StringBuilder. More...
 
virtual LNodePrinterHelper Write (char c)
 Appends a character to the output stream or StringBuilder.Do not call Write('
')
; call Newline() instead.
 
virtual LNodePrinterHelper Write (string s)
 Appends a string to the output stream or StringBuilder.The string should not include newlines (if it might, call LNodePrinterHelperExt.WriteSmartly instead).
 
virtual LNodePrinterHelper Write (UString s)
 Appends a string to the output stream or StringBuilder.The string should not include newlines (if it might, call LNodePrinterHelperExt.WriteSmartly instead).
 
LNodePrinterHelper Space ()
 Writes a space character unless the last character written was a space.
 
StringBuilder Append (string s)
 
StringBuilder Append (UString s)
 
LNodePrinterHelperLocation Newline (int changeIndentLevel)
 Older version of Newline method, which returns a checkpoint instead of this. More...
 
virtual LNodePrinterHelper Newline (bool deferIndent=false)
 Appends a newline, with indentation afterward according to the current indentation level.
Parameters
deferIndentRequests that the indentation after the newline not be printed until Write or FlushIndent is called. This option is provided because you may know that you want to print a newline before deciding what indent is needed.

 
LNodePrinterHelper FlushIndent ()
 Writes the pending indent, if applicable. This allows you to call Indent or Dedent afterward while making sure the indentation on the current line is unaffected. More...
 
virtual LNodePrinterHelper NewlineIsRequiredHere ()
 Requests that a newline be written at this location. If this method is called multiple times at the same location, or if Newline(int) is called immediately afterward, only a single newline is written.This method does not officially write a newline; if you call this method followed by Dedent, for example, the newline that is eventually written will be followed by a lower amount of indentation.
 
virtual LNodePrinterHelper Indent (PrinterIndentHint?modeHint=null)
 Informs the helper that the printer is starting to write the specified node. The printer must call EndNode when it is done. This information may be used to record range information (to understand why this is useful, please read about LNodeRangeMapper)
Parameters
nodeThe node that begins here.
Increases the current indent level.
 
virtual LNodePrinterHelper Dedent (PrinterIndentHint?modeHint=null)
 Decreases the current indent level.
Parameters
modeIf the hint is not null, the writer may be able to check that it was the same hint that was passed to Indent and throw an exception if not.
Exceptions
ArgumentExceptionHint provided doesn't match Indent hint

 
virtual LNodePrinterHelper BeginNode (ILNode node)
 
virtual LNodePrinterHelper EndNode ()
 Informs the helper that the printer is done writing the most recently started node. The helper may save the range of the node, e.g. by calling ILNodePrinterOptions.SaveRange.
Parameters
abortIndicates that the range of this node should not be saved.

 
LNodePrinterHelper BeginNode (ILNode node, PrinterIndentHint indentHint)
 Combines the BeginNode and Indent operations.
Parameters
indentHintTypically a member of PrinterIndentHint that influences how indentation is performed

 
LNodePrinterHelper EndNode (PrinterIndentHint indentHint)
 Combines the EndNode and Dedent operations.
 
virtual LNodePrinterHelperLocation GetCheckpoint ()
 Gets a value that can be passed later to RevokeNewlinesSince(Checkpoint).
 
virtual LNodePrinterHelperLocation NewlineAfterCheckpoint ()
 Appends a newline, returning a checkpoint from with indentation afterward according to the current indentation level.
 
int RevokeOrCommitNewlines (LNodePrinterHelperLocation cp, int maxLineWidth)
 
LNodePrinterHelper CommitNewlines ()
 Commits all uncommitted newlines permanently.Also causes ranges after the uncommitted newlines to be saved.
 
int RevokeNewlinesSince (LNodePrinterHelperLocation cp)
 
void Dispose ()
 Commits uncommitted newlines and node ranges. Throws if a SaveRange method was provided but EndNode has not been called the correct number of times. More...
 
- Public Member Functions inherited from Loyc.Syntax.Impl.ILNodePrinterHelperWithRevokableNewlines< Checkpoint, out Self >
int RevokeNewlinesSince (Checkpoint cp)
 Deletes uncommitted newlines that were written after the specified checkpoint. More...
 
int RevokeOrCommitNewlines (Checkpoint cp, int maxLineWidth)
 Revokes or commits newlines added since the specified checkpoint. Recent newlines are revoked if the combined line length after revokation does not exceed maxLineWidth, otherwise ALL newlines are committed permanently. More...
 

Protected Member Functions

virtual void OnNodeChanged (char firstChar)
 This method, which does nothing by default, is called by all three versions of Write() when the first character is written after a call to BeginNode(ILNode) or EndNode(). You can override this method can detect conflicts between this character and the previous characters in the stream (LastCharWritten). More...
 
virtual void AppendIndentAfterNewline (int indentStackSize)
 Called just after a newline is emitted to append indentation. More...
 

Protected fields

int _indentLevel
 
int _lineNo
 
StringBuilder _s
 
InternalList< Pair< ILNode, int > > _nodeStack
 
InternalList< object > _indentStack
 

Member Function Documentation

virtual void Loyc.Syntax.Impl.LNodePrinterHelper.AppendIndentAfterNewline ( int  indentStackSize)
inlineprotectedvirtual

Called just after a newline is emitted to append indentation.

Parameters
hintThe hint that was passed to Newline

References Loyc.Syntax.Impl.PrinterIndentHint.Brackets, Loyc.Syntax.Impl.PrinterIndentHint.Label, Loyc.Syntax.Impl.PrinterIndentHint.NoIndent, and Loyc.Syntax.Impl.PrinterIndentHint.Subexpression.

void Loyc.Syntax.Impl.LNodePrinterHelper.Dispose ( )
inline

Commits uncommitted newlines and node ranges. Throws if a SaveRange method was provided but EndNode has not been called the correct number of times.

LNodePrinterHelper Loyc.Syntax.Impl.LNodePrinterHelper.FlushIndent ( )
inline

Writes the pending indent, if applicable. This allows you to call Indent or Dedent afterward while making sure the indentation on the current line is unaffected.

This method has no effect if indentation has already been written, e.g. right after calling Newline(false) or Write(char). If a newline is pending as a result of a call to NewlineIsRequiredHere, it is implementation- defined whether that additional newline (and its indent) is written. LNodePrinterHelper write a newline when this method is called, but it sets a special internal flag so that a call sequence such as

NewlineIsRequiredHere().FlushIndent().NewlineIsRequiredHere().Newline()

writes only a single newline.

Implements Loyc.Syntax.Impl.IPrinterHelper< out Self >.

References Loyc.Newline.

LNodePrinterHelperLocation Loyc.Syntax.Impl.LNodePrinterHelper.Newline ( int  changeIndentLevel)
inline

Older version of Newline method, which returns a checkpoint instead of this.

Parameters
changeIndentLevelAmount by which to change the indent level (positive, negative or zero).
virtual void Loyc.Syntax.Impl.LNodePrinterHelper.OnNodeChanged ( char  firstChar)
inlineprotectedvirtual

This method, which does nothing by default, is called by all three versions of Write() when the first character is written after a call to BeginNode(ILNode) or EndNode(). You can override this method can detect conflicts between this character and the previous characters in the stream (LastCharWritten).

Parameters
firstCharFirst character in the new node, which has not been written yet

For example, suppose a language has a prefix operator . and two binary operators called ?. and ?. If the last character printed was ? then it should not be followed by . if the . is part of a different node, since the parser would treat ?. as a single token rather than the intended two tokens. A derived class can use this method to detect the conflict and prevent it by adding a space to StringBuilder. (If you call Write(char) to do this, it will cause this method to be called again.)

virtual void Loyc.Syntax.Impl.LNodePrinterHelper.Reset ( )
inlinevirtual

Calls Dispose() and resets the state of this object, like calling the constructor. Does not clear the StringBuilder.

Member Data Documentation

int Loyc.Syntax.Impl.LNodePrinterHelper.Length => StringBuilder.Length

Current length of the output string. This length can decrease if newlines are revoked.

Property Documentation

Action<ILNode, IndexRange, int> Loyc.Syntax.Impl.LNodePrinterHelper.SaveRange
get

Gets or sets a method that is called when the node's range in the output is locked in (after you call EndNode()).

The third parameter of the method is the node's depth in the syntax tree (e.g. 3 means that the node has three known parents).