C# Класс 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.
Показать файл Открыть проект

Открытые свойства

Свойство Тип Описание
IndentLevel int
IndentString string
LineNo int
NewlineString string
S StringBuilder

Открытые методы

Метод Описание
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.

Приватные методы

Метод Описание
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

Описание методов

Append() публичный Метод

public Append ( string s ) : StringBuilder
s string
Результат StringBuilder

CommitNewlines() публичный Метод

public CommitNewlines ( ) : int
Результат int

Dedent() публичный Метод

public Dedent ( ) : void
Результат void

GetCheckpoint() публичный Метод

public GetCheckpoint ( ) : Checkpoint
Результат Checkpoint

Indent() публичный Метод

public Indent ( ) : void
Результат void

Newline() публичный Метод

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
Результат Checkpoint

PrinterState() публичный Метод

public PrinterState ( StringBuilder s, string indent = "\t", string newline = "\n" ) : System
s StringBuilder
indent string
newline string
Результат System

RevokeNewlinesSince() публичный Метод

public RevokeNewlinesSince ( Checkpoint cp ) : int
cp Checkpoint
Результат int

RevokeOrCommitNewlines() публичный Метод

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
Результат int

Описание свойств

IndentLevel публичное свойство

public int IndentLevel
Результат int

IndentString публичное свойство

public string IndentString
Результат string

LineNo публичное свойство

public int LineNo
Результат int

NewlineString публичное свойство

public string NewlineString
Результат string

S публичное свойство

public StringBuilder S
Результат StringBuilder