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.
파일 보기 프로젝트 열기: qwertie/ecsharp

공개 프로퍼티들

프로퍼티 타입 설명
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