Enhanced C#
Language of your choice: library documentation
Properties | Public Member Functions | Protected Member Functions | List of all members
Loyc.Syntax.StandardTriviaInjector Class Reference

Encapsulates an algorithm that consumes trivia (comments and newlines) from a list and adds it as trivia attributes into LNodes. More...


Source file:
Inheritance diagram for Loyc.Syntax.StandardTriviaInjector:
Loyc.Syntax.AbstractTriviaInjector< Token > Loyc.Ecs.Parser.EcsTriviaInjector

Remarks

Encapsulates an algorithm that consumes trivia (comments and newlines) from a list and adds it as trivia attributes into LNodes.

Usage: Call the constructor, then call AbstractTriviaInjector<T>.Run. See AbstractTriviaInjector<T> for more information.

In brief, given input code with C-style comments like

{
    // Leading Comment 1
    /* Leading Comment 2 * /
    /* Leading Comment 3 * / x = y; // Trailing Comment 1
    /* Trailing Comment 2 * /
    y = z; TheEnd();
}

[NOTE: the space in "* /" is a workaround for a serious bug in Doxygen, the html doc generator]

The output, expressed in LESv2, is

{
    @[#trivia_SLComment(" Leading Comment 1"),
      #trivia_MLComment(" Leading Comment 2 "),
      #trivia_newline,
      #trivia_MLComment(" Leading Comment 3 "),
      #trivia_beginTrailingTrivia,
      #trivia_SLComment(" Trailing Comment 1"),
      #trivia_MLComment(" Trailing Comment 2 "),
      #trivia_newline]
    x = y;
    y = z;
    @[#trivia_appendStatement] TheEnd();
}

By default, printers add newlines between statements within a braced block. Therefore, this class does not add trivia to mark a single newline between statements; instead, it adds a #trivia_appendStatement attribute when the expected newline prior to a statement in a braced block was NOT present. Also, a newline is expected after a single-line comment and no #trivia_newline is created for the expected newline.

Finally, since printers typically add a newline before the closing brace by default, this class avoids adding an attribute for that newline, if present.

Properties

ISourceFile SourceFile [get, set]
 
int NewlineTypeInt [get, set]
 
string SLCommentPrefix [get, set]
 
string SLCommentSuffix [get, set]
 
string MLCommentPrefix [get, set]
 
string MLCommentSuffix [get, set]
 
- Properties inherited from Loyc.Syntax.AbstractTriviaInjector< Token >
IListSource< Trivia > SortedTrivia [get, set]
 List of trivia to be injected by Run. Must be sorted. More...
 
int NextIndex [get, set]
 Index of next trivia to be injected. More...
 

Public Member Functions

 StandardTriviaInjector (IListSource< Token > sortedTrivia, ISourceFile sourceFile, int newlineTypeInt, string mlCommentPrefix, string mlCommentSuffix, string slCommentPrefix)
 Initializes StandardTriviaInjector. More...
 
- Public Member Functions inherited from Loyc.Syntax.AbstractTriviaInjector< Token >
 AbstractTriviaInjector (IListSource< Trivia > sortedTrivia)
 Initializes the SortedTrivia property. More...
 
IEnumerator< LNodeRun (IEnumerator< LNode > nodes)
 Attaches trivia to the input nodes provided. More...
 

Protected Member Functions

override VList< LNodeAttachTriviaTo (ref LNode node, IListSource< Token > trivia, TriviaLocation loc, LNode parent, int indexInParent)
 
virtual bool HasImplicitLeadingNewline (LNode child, LNode parent, int childIndex)
 Called to find out if a newline is to be added implicitly before the current child of the specified node. More...
 
virtual LNode MakeTriviaAttribute (Token t)
 Called to transform a trivia token into a trivia attribute. More...
 
override bool IsNewline (Token trivia)
 
override SourceRange GetRange (Token trivia)
 
- Protected Member Functions inherited from Loyc.Syntax.AbstractTriviaInjector< Token >
abstract VList< LNodeAttachTriviaTo (ref LNode node, IListSource< Trivia > trivia, TriviaLocation loc, LNode parent, int indexInParent)
 Derived class should associate the given list of trivia with the specified node. Leading trivia will be attached to a given node before trailing trivia. More...
 
virtual void MarkOneLiner (ref LNode node)
 This method is called when a node has no newlines or comments within it (although the node may still have a leading or trailing comment). It informs the derived class that AbstractTriviaInjector<Trivia> will not traverse into the node. More...
 
abstract SourceRange GetRange (Trivia trivia)
 Gets the SourceRange for an element of trivia. More...
 
abstract bool IsNewline (Trivia trivia)
 Returns true if the trivia represents a newline, false otherwise. More...
 
virtual LNode GetEmptyResultSet ()
 A method called to create a virtual node to apply trivia to an empty source file. More...
 
virtual LNode DoneAttaching (LNode node, LNode parent, int indexInParent)
 This method is called after a node has been processed and any applicable trivia was attached. More...
 
IEnumerator< Pair< LNode, int > > RunCore (IEnumerator< Pair< LNode, int >> nodes, LNode parent)
 Core trivia associaton algorithm. More...
 

Additional Inherited Members

- Static Protected Member Functions inherited from Loyc.Syntax.AbstractTriviaInjector< Token >
static IEnumerator< Pair< T, int > > WithIndexes< T > (IEnumerator< T > e)
 

Constructor & Destructor Documentation

Loyc.Syntax.StandardTriviaInjector.StandardTriviaInjector ( IListSource< Token sortedTrivia,
ISourceFile  sourceFile,
int  newlineTypeInt,
string  mlCommentPrefix,
string  mlCommentSuffix,
string  slCommentPrefix 
)
inline

Initializes StandardTriviaInjector.

Parameters
sortedTriviaA list of trivia that will be injected into the nodes passed to AbstractTriviaInjector<T>.Run. Normally, text of comments is extracted from the provided ISourceFile, but comment tokens could instead store their text as a string in their Token.Value.
sourceFileThis is used as the source file of the LNode.Range of all trivia attributes that the algorithm injects.
newlineTypeIntA token is interpreted as a newline when this value equals Token.TypeInt.
mlCommentPrefixIf a token's text begins with this prefix it is assumed to be a multi-line comment and the prefix is removed.
mlCommentSuffixIf a multi-line comment's text ends with this suffix, the suffix is removed.
slCommentPrefixIf a token's text begins with this prefix it is assumed to be a single-line comment and the prefix is removed.

References Loyc.Collections.VList< T >.Add(), Loyc.Syntax.LNode.Calls(), Loyc.Collections.VList< T >.Last, and Loyc.Collections.VList< T >.Pop().

Member Function Documentation

virtual bool Loyc.Syntax.StandardTriviaInjector.HasImplicitLeadingNewline ( LNode  child,
LNode  parent,
int  childIndex 
)
inlineprotectedvirtual

Called to find out if a newline is to be added implicitly before the current child of the specified node.

Returns
By default, returns true if the node is a braced block.

References Loyc.Syntax.LNode.Calls().

virtual LNode Loyc.Syntax.StandardTriviaInjector.MakeTriviaAttribute ( Token  t)
inlineprotectedvirtual

Called to transform a trivia token into a trivia attribute.

If a trivia token is not recognized, null is returned to ignore the trivia.

References Loyc.UString.Left(), Loyc.Syntax.Lexing.Token.Length, Loyc.Syntax.Lexing.Token.StartIndex, Loyc.UString.StartsWith(), Loyc.UString.Substring(), Loyc.Syntax.Lexing.Token.TypeInt, and Loyc.Syntax.Lexing.Token.Value.