C# Class Loyc.Syntax.Impl.PrinterState

A helper type for printer objects. Its primary purposes are to manage indentation and to "revoke" newlines; it also tracks the current line/column number.
Be careful not to duplicate this structure. When pretty-printing any language as text, it's a challenge to decide where to place newlines. You may want to break up long lines into shorter ones, as in
 if (ReallyLongIdentifier[Fully.Qualified.Name(multiple, parameters)]  > SomeConstant) { return ReallyLongIdentifier[firstThing + secondThing]  + thirdThing + fourthThing; } 
Conversely, you may want to print something on one line that you would ordinarily print on two:
 if (c) break; 
Of course, the problem is, you don't know how long the syntax tree will be in text form until after you try to print it. My first idea to solve this problem was to use a rope tree data structure - inner syntax trees would produce small strings that could be "roped" together to produce a bigger tree. But ropes tend not to use memory efficiently, and there was the challenge, which I didn't see how to solve, of how to keep the tree balanced efficiently (for this particular application perhaps a balanced tree wasn't needed, but as a perfectionist I didn't want to implement a "half-baked" data structure.) Next I thought of the solution used here, a simpler solution based on an ordinary StringBuilder. My idea was to insert newlines "pessimistically" - insert them everywhere in which they might be needed - and then selectively "revoke" them later if they turn out to be unnecessary. Only the most recently-written newline(s) can be revoked, which keeps the implementation simple and also limits the performance cost of deleting the newlines. To use, call Newline() to write a newline (with indentation). To make a decision about whether to keep or revoke the most recent newline(s), call RevokeOrCommitNewlines(cp, maxLineLength) where cp is a "checkpoint" representing some point before the first newline want to potentially revoke, and maxLineLength is the line length threshold: if the line length after combining lines, starting at the line on which the checkpoint is located, does not exceed maxLineLength, then the newlines are revoked, otherwise ALL newlines are committed (so earlier newlines can no longer be revoked.) This design allows a potentially long series of newlines to be deleted in the reverse order that they were created, but if any newline is kept then previous ones can no longer be deleted. For an example of how this is used, see the JSON printer in LLLPG samples or look at the implementation of the LESv3 printer.
Exibir arquivo Open project: qwertie/ecsharp

Public Properties

Property Type Description
IndentLevel int
IndentString string
LineNo int
NewlineString string
S StringBuilder

Public Methods

Method Description
Append ( string s ) : StringBuilder
CommitNewlines ( ) : int
Dedent ( ) : void
GetCheckpoint ( ) : Checkpoint
Indent ( ) : void
Newline ( int changeIndentLevel ) : Checkpoint

Writes a newline and the appropriate amount of indentation afterward.

Note that "revoking" a newline does NOT restore the original indent level.

PrinterState ( StringBuilder s, string indent = "\t", string newline = "\n" ) : System
RevokeNewlinesSince ( Checkpoint cp ) : int
RevokeOrCommitNewlines ( Checkpoint cp, int maxLineWidth ) : int

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.

This method does not affect the indent level.

Private Methods

Method Description
Revoke ( Revokable r ) : void

Revokes (deletes) the last newline created, and its indent.

Only the most recent newline can be revoked, and of course, it can only be revoked once. Multiple newlines can be revoked if they are revoked in the reverse order in which they were created.

RevokeNewlinesStartingAtIndex ( int i0 ) : int

Method Details

Append() public method

public Append ( string s ) : StringBuilder
s string
return StringBuilder

CommitNewlines() public method

public CommitNewlines ( ) : int
return int

Dedent() public method

public Dedent ( ) : void
return void

GetCheckpoint() public method

public GetCheckpoint ( ) : Checkpoint
return Checkpoint

Indent() public method

public Indent ( ) : void
return void

Newline() public method

Writes a newline and the appropriate amount of indentation afterward.
Note that "revoking" a newline does NOT restore the original indent level.
public Newline ( int changeIndentLevel ) : Checkpoint
changeIndentLevel int Amount by which to change before writing the newline
return Checkpoint

PrinterState() public method

public PrinterState ( StringBuilder s, string indent = "\t", string newline = "\n" ) : System
s StringBuilder
indent string
newline string
return System

RevokeNewlinesSince() public method

public RevokeNewlinesSince ( Checkpoint cp ) : int
cp Checkpoint
return int

RevokeOrCommitNewlines() public method

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.
This method does not affect the indent level.
public RevokeOrCommitNewlines ( Checkpoint cp, int maxLineWidth ) : int
cp Checkpoint
maxLineWidth int
return int

Property Details

IndentLevel public_oe property

public int IndentLevel
return int

IndentString public_oe property

public string IndentString
return string

LineNo public_oe property

public int LineNo
return int

NewlineString public_oe property

public string NewlineString
return string

S public_oe property

public StringBuilder S
return StringBuilder