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

Standard extension methods for LNode. More...


Source file:

Remarks

Standard extension methods for LNode.

Static Public Member Functions

static LNodeList GetTrivia (this LNode node)
 
static LNodeList GetTrivia (this LNodeList attrs)
 
static LNodeList GetTrailingTrivia (this LNode node)
 Gets all trailing trivia attached to the specified node. More...
 
static LNodeList GetTrailingTrivia (this LNodeList attrs)
 Gets all trailing trivia attached to the specified node. More...
 
static LNode WithTrailingTrivia (this LNode node, LNodeList trivia)
 Removes a node's trailing trivia and adds a new list of trailing trivia. More...
 
static LNodeList WithTrailingTrivia (this LNodeList attrs, LNodeList trivia)
 Removes all existing trailing trivia from an attribute list and adds a new list of trailing trivia. More...
 
static LNodeList WithoutTrailingTrivia (this LNodeList attrs)
 Gets a new list with any trailing attributes removed. More...
 
static LNodeList WithoutTrailingTrivia (this LNodeList attrs, out LNodeList trailingTrivia)
 Gets a new list with any trailing attributes removed. Trailing trivia inside those attributes are returned in an out parameter. More...
 
static LNode PlusTrailingTrivia (this LNode node, LNodeList trivia)
 Adds additional trailing trivia to a node. More...
 
static LNode PlusTrailingTrivia (this LNode node, LNode trivia)
 Adds additional trailing trivia to a node. More...
 
static LNodeList PlusTrailingTrivia (this LNodeList attrs, LNodeList trivia)
 Adds additional trailing trivia to an attribute list. Has no effect if trivia is empty. More...
 
static LNodeList PlusTrailingTrivia (this LNodeList attrs, LNode trivia)
 Adds additional trailing trivia to an attribute list. More...
 
static LNodeList AsList (this LNode block, Symbol listIdentifier)
 Interprets a node as a list by returning block.Args if block.Calls(listIdentifier), otherwise returning a one-item list of nodes with block as the only item. More...
 
static LNodeList Unsplice (this LNode node)
 Converts an expression to a list. Similar to calling AsList(block, CodeSymbols.Splice), if the expression calls #splice then the arguments of the splice are returned, and if not then the argument is converted to a list with one item. However, if the call to #splice has attached trivia/attributes, those attributes are attached to the output list using PlusAttributes(LNodeList, LNodeList). More...
 
static LNode IncludingTriviaFrom (this LNode node, LNode otherNode)
 Finds trivia attributes attached directly to otherNode, and returns a new version of node with these attributes added. More...
 
static LNodeList IncludingAttributes (this LNodeList list, LNodeList attributes)
 Prepends attributes to the first item in a list, except for trailing trivia (trailing(...)), which is appended to the last item in the list. If the list is empty, the attributes are ignored. More...
 
static LNode AsLNode (this LNodeList list, Symbol listIdentifier)
 Converts a list of LNodes to a single LNode by using the list as the argument list in a call to the specified identifier, or, if the list contains a single item, by returning that single item. More...
 
static LNodeList WithSpliced (this LNodeList list, int index, LNode node, Symbol listName=null)
 
static LNodeList WithSpliced (this LNodeList list, LNode node, Symbol listName=null)
 
static void SpliceInsert (this WList< LNode > list, int index, LNode node, Symbol listName=null)
 
static void SpliceAdd (this WList< LNode > list, LNode node, Symbol listName=null)
 
static LNode AttrNamed (this LNode self, Symbol name)
 
static LNode WithoutAttrNamed (this LNode self, Symbol name)
 
static LNodeList Without (this LNodeList list, LNode node)
 
static LNode WithoutAttr (this LNode self, LNode node)
 
static LNode WithoutAttrNamed (this LNode self, Symbol name, out LNode removedAttr)
 
static LNodeList WithoutNodeNamed (this LNodeList a, Symbol name)
 
static LNodeList WithoutNodeNamed (this LNodeList list, Symbol name, out LNode removedNode)
 
static LNode ArgNamed (this LNode self, Symbol name)
 
static int IndexWithName (this LNodeList self, Symbol name, int resultIfNotFound=-1)
 
static LNode NodeNamed (this LNodeList self, Symbol name)
 
static LNodeList RecursiveReplace (this LNodeList self, Func< LNode, LNodeList?> matcher, LNode.ReplaceOpt options=LNode.ReplaceOpt.Default)
 
static bool IsParenthesizedExpr (this LNode node)
 
static LNode InParens (this LNode node)
 Returns the same node with a parentheses attribute added. More...
 
static LNode InParens (this LNode node, SourceRange range)
 Returns the same node with a parentheses attribute added. More...
 
static LNode InParens (this LNode node, ISourceFile file, int startIndex, int endIndex)
 Returns the same node with a parentheses attribute added. More...
 
static LNode WithoutOuterParens (this LNode self)
 Removes a single pair of parentheses, if the node has a inParens attribute. Returns the same node when no parens are present. More...
 
static bool MatchesPattern (this LNode candidate, LNode pattern, ref MMap< Symbol, LNode > captures, out LNodeList unmatchedAttrs)
 Determines whether one Loyc tree "matches" another. This is different from a simple equality test in that (1) trivia atributes do not have to match, and (2) the pattern can contain placeholders represented by calls to $ (the substitution operator) with an identifier as a parameter. Placeholders match any subtree, and are saved to the captures map. More...
 
static bool MatchesPattern (this LNode candidate, LNode pattern, out IDictionary< Symbol, LNode > captures, out LNodeList unmatchedAttrs)
 
static bool MatchesPattern (this LNode candidate, LNode pattern, out IDictionary< Symbol, LNode > captures)
 
static LNode GetCaptureIdentifier (LNode pattern, bool identifierRequired=true)
 Checks if pattern matches one of the syntax trees $x or $(..x) or $(...x) for some identifier x. These are conventionally used to represent partial syntax trees. More...
 
static bool IsCall (this ILNode node)
 
static bool IsId (this ILNode node)
 
static bool IsLiteral (this ILNode node)
 
static int ArgCount (this ILNode node)
 
static int AttrCount (this ILNode node)
 
static NegListSlice< ILNodeAttrs (this ILNode node)
 
static NegListSlice< ILNodeArgs (this ILNode node)
 
static bool IsTrivia (this ILNode node)
 
static NodeStyle BaseStyle (this ILNode node)
 
static bool IsIdNamed (this ILNode node, Symbol name)
 
static bool IsIdNamed (this ILNode node, string name)
 
static bool Calls (this ILNode node, Symbol name)
 
static bool IsParenthesizedExpr (this ILNode node)
 
static bool HasSpecialName (this ILNode node)
 
static bool HasAttrs (this ILNode node)
 
static bool HasPAttrs (this ILNode node)
 
static ILNode AttrNamed (this ILNode node, Symbol name)
 
static ILNode NodeNamed (this NegListSlice< ILNode > self, Symbol name)
 
static IListSource< ILNodeGetTrailingTrivia (this ILNode node)
 
static LNode ToLNode (this ILNode node)
 Converts ILNode to LNode recursively. More...
 
static VList< LNodeToVList (this LNodeList list)
 
static FVList< LNodeToFVList (this LNodeList list)
 
static WList< LNodeToWList (this LNodeList list)
 
static LNodeList ToLNodeList (this WList< LNode > list)
 
static LNodeList ToLNodeList (this VList< LNode > list)
 

Member Function Documentation

◆ AsList()

static LNodeList Loyc.Syntax.LNodeExt.AsList ( this LNode  block,
Symbol  listIdentifier 
)
static

Interprets a node as a list by returning block.Args if block.Calls(listIdentifier), otherwise returning a one-item list of nodes with block as the only item.

◆ AsLNode()

static LNode Loyc.Syntax.LNodeExt.AsLNode ( this LNodeList  list,
Symbol  listIdentifier 
)
inlinestatic

Converts a list of LNodes to a single LNode by using the list as the argument list in a call to the specified identifier, or, if the list contains a single item, by returning that single item.

Parameters
listIdentifierTarget of the node that is created if list does not contain exactly one item. Typical values include "'{}" and "#splice".

This is the reverse of the operation performed by AsList(LNode,Symbol).

References Loyc.Syntax.LNode.Range.

◆ GetCaptureIdentifier()

static LNode Loyc.Syntax.LNodeExt.GetCaptureIdentifier ( LNode  pattern,
bool  identifierRequired = true 
)
inlinestatic

Checks if pattern matches one of the syntax trees $x or $(..x) or $(...x) for some identifier x. These are conventionally used to represent partial syntax trees.

Returns
The matched identifier (x in the examples above), or null if pattern was not a match.

References Loyc.Syntax.LNode.Args, and Loyc.Syntax.LNode.Calls().

Referenced by Loyc.Syntax.LNodeExt.MatchesPattern().

◆ GetTrailingTrivia() [1/2]

static LNodeList Loyc.Syntax.LNodeExt.GetTrailingTrivia ( this LNode  node)
inlinestatic

Gets all trailing trivia attached to the specified node.

References Loyc.Syntax.LNode.Attrs, and Loyc.Syntax.LNodeExt.GetTrailingTrivia().

Referenced by Loyc.Syntax.LNodeExt.GetTrailingTrivia().

◆ GetTrailingTrivia() [2/2]

static LNodeList Loyc.Syntax.LNodeExt.GetTrailingTrivia ( this LNodeList  attrs)
inlinestatic

Gets all trailing trivia attached to the specified node.

Trailing trivia is represented by a call to trailing in a node's attribute list; each argument to trailing represents one piece of trivia. If the attribute list has multiple calls to trailing, this method combines those lists into a single list.

◆ IncludingAttributes()

static LNodeList Loyc.Syntax.LNodeExt.IncludingAttributes ( this LNodeList  list,
LNodeList  attributes 
)
inlinestatic

Prepends attributes to the first item in a list, except for trailing trivia (trailing(...)), which is appended to the last item in the list. If the list is empty, the attributes are ignored.

Returns
A modified version of the list with attributes added. If the attribute list is empty, the empty list is returned unchanged.

◆ IncludingTriviaFrom()

static LNode Loyc.Syntax.LNodeExt.IncludingTriviaFrom ( this LNode  node,
LNode  otherNode 
)
inlinestatic

Finds trivia attributes attached directly to otherNode, and returns a new version of node with these attributes added.

◆ InParens() [1/3]

static LNode Loyc.Syntax.LNodeExt.InParens ( this LNode  node)
inlinestatic

Returns the same node with a parentheses attribute added.

Referenced by Loyc.Syntax.LNodeExt.InParens().

◆ InParens() [2/3]

static LNode Loyc.Syntax.LNodeExt.InParens ( this LNode  node,
ISourceFile  file,
int  startIndex,
int  endIndex 
)
inlinestatic

Returns the same node with a parentheses attribute added.

References Loyc.Syntax.LNodeExt.InParens().

◆ InParens() [3/3]

static LNode Loyc.Syntax.LNodeExt.InParens ( this LNode  node,
SourceRange  range 
)
inlinestatic

Returns the same node with a parentheses attribute added.

The node's range is changed to the provided SourceRange.

◆ MatchesPattern()

static bool Loyc.Syntax.LNodeExt.MatchesPattern ( this LNode  candidate,
LNode  pattern,
ref MMap< Symbol, LNode captures,
out LNodeList  unmatchedAttrs 
)
inlinestatic

Determines whether one Loyc tree "matches" another. This is different from a simple equality test in that (1) trivia atributes do not have to match, and (2) the pattern can contain placeholders represented by calls to $ (the substitution operator) with an identifier as a parameter. Placeholders match any subtree, and are saved to the captures map.

Parameters
candidateA node that you want to compare with a 'pattern'.
patternA syntax tree that may contain placeholders. A placeholder is a call to the $ operator with one parameter, which must be either (A) a simple identifier, or (B) the ".." operator with a simple identifier as its single parameter. Otherwise, the $ operator is treated literally as something that must exist in candidate). The subtree in candidate corresponding to the placeholder is saved in captures.
capturesA table that maps placeholder names from pattern to subtrees in candidate. You can set your map to null and a map will be created for you if necessary. If you already have a map, you should clear it before calling this method.
unmatchedAttrsOn return, a list of trivia attributes in candidate that were not present in pattern.
Returns
true if pattern matches candidate, false otherwise.

Attributes in patterns are not yet supported.

This method supports multi-part captures, which are matched to placeholders whose identifier either (A) has a #params attribute or (B) has the unary ".." operator applied to it (for example, if the placeholder is called p, this is written as $(params p) in EC#.) A placeholder that looks like this can match multiple arguments or multiple statements in the candidate (or no arguments, or no statements), and will become a #splice(...) node in captures if it matches multiple items. Multi-part captures are often useful for getting lists of statements before and after some required element, e.g. { $(params before); MatchThis($something); $(params after); }

If the same placeholder appears twice then the two matching items are combined into a single output node (calling #splice).

If matching is unsuccessful, captures and unmatchedAttrs may contain irrelevant information gathered during the attempt to match.

In EC#, the quote(...) macro can be used to create the LNode object for a pattern.

References Loyc.Syntax.LNode.Args, Loyc.Syntax.LNodeExt.GetCaptureIdentifier(), Loyc.Syntax.LNode.Kind, Loyc.Syntax.LNode.Name, Loyc.Syntax.LNode.Target, and Loyc.Syntax.LNode.Value.

◆ PlusTrailingTrivia() [1/4]

static LNode Loyc.Syntax.LNodeExt.PlusTrailingTrivia ( this LNode  node,
LNode  trivia 
)
inlinestatic

Adds additional trailing trivia to a node.

References Loyc.Syntax.LNode.Attrs, and Loyc.Syntax.LNodeExt.PlusTrailingTrivia().

◆ PlusTrailingTrivia() [2/4]

static LNode Loyc.Syntax.LNodeExt.PlusTrailingTrivia ( this LNode  node,
LNodeList  trivia 
)
inlinestatic

Adds additional trailing trivia to a node.

References Loyc.Syntax.LNode.Attrs.

Referenced by Loyc.Syntax.LNodeExt.PlusTrailingTrivia().

◆ PlusTrailingTrivia() [3/4]

static LNodeList Loyc.Syntax.LNodeExt.PlusTrailingTrivia ( this LNodeList  attrs,
LNode  trivia 
)
inlinestatic

Adds additional trailing trivia to an attribute list.

References Loyc.Syntax.CodeSymbols.Add, and Loyc.Syntax.LNodeExt.WithoutTrailingTrivia().

◆ PlusTrailingTrivia() [4/4]

static LNodeList Loyc.Syntax.LNodeExt.PlusTrailingTrivia ( this LNodeList  attrs,
LNodeList  trivia 
)
inlinestatic

Adds additional trailing trivia to an attribute list. Has no effect if trivia is empty.

Trailing trivia is represented by a call to trailing in a node's attribute list; each argument to trailing represents one piece of trivia.

In the current design, this method has a side-effect of recreating the trailing node at the end of the attribute list, and if there are multiple trailing lists, consolidating them into a single list, but only if the specified trivia list is not empty.

References Loyc.Syntax.LNodeExt.WithoutTrailingTrivia().

◆ ToLNode()

static LNode Loyc.Syntax.LNodeExt.ToLNode ( this ILNode  node)
inlinestatic

◆ Unsplice()

static LNodeList Loyc.Syntax.LNodeExt.Unsplice ( this LNode  node)
inlinestatic

Converts an expression to a list. Similar to calling AsList(block, CodeSymbols.Splice), if the expression calls #splice then the arguments of the splice are returned, and if not then the argument is converted to a list with one item. However, if the call to #splice has attached trivia/attributes, those attributes are attached to the output list using PlusAttributes(LNodeList, LNodeList).

Parameters
blockA node that may or may not be a call to #splice
Returns
A list of nodes that block is equivalent to.

Attributes attached to #splice are ordinarily attached to the first item in the output list, but any trailing attribute is attached to the last item instead. If the #splice() call has no arguments, then (i) if it has no trivia attributes, an empty list is returned, but (ii) if it has trivia attributes, the attributes themselves are returned as the content of the list. This assumes that printers can print

References Loyc.Syntax.LNode.Attrs, and Loyc.Syntax.CodeSymbols.Splice.

◆ WithoutOuterParens()

static LNode Loyc.Syntax.LNodeExt.WithoutOuterParens ( this LNode  self)
inlinestatic

Removes a single pair of parentheses, if the node has a inParens attribute. Returns the same node when no parens are present.

◆ WithoutTrailingTrivia() [1/2]

static LNodeList Loyc.Syntax.LNodeExt.WithoutTrailingTrivia ( this LNodeList  attrs)
inlinestatic

Gets a new list with any trailing attributes removed.

References Loyc.Syntax.LNodeList.SmartWhere().

Referenced by Loyc.Syntax.LNodeExt.PlusTrailingTrivia(), and Loyc.Syntax.LNodeExt.WithTrailingTrivia().

◆ WithoutTrailingTrivia() [2/2]

static LNodeList Loyc.Syntax.LNodeExt.WithoutTrailingTrivia ( this LNodeList  attrs,
out LNodeList  trailingTrivia 
)
inlinestatic

Gets a new list with any trailing attributes removed. Trailing trivia inside those attributes are returned in an out parameter.

References Loyc.Syntax.LNodeList.SmartWhere().

◆ WithTrailingTrivia() [1/2]

static LNode Loyc.Syntax.LNodeExt.WithTrailingTrivia ( this LNode  node,
LNodeList  trivia 
)
inlinestatic

Removes a node's trailing trivia and adds a new list of trailing trivia.

References Loyc.Syntax.LNode.Attrs.

◆ WithTrailingTrivia() [2/2]

static LNodeList Loyc.Syntax.LNodeExt.WithTrailingTrivia ( this LNodeList  attrs,
LNodeList  trivia 
)
inlinestatic

Removes all existing trailing trivia from an attribute list and adds a new list of trailing trivia.

This method has a side-effect of recreating the trailing node, if there is one, at the end of the attribute list. If trivia is empty then all calls to trailing are removed.

References Loyc.Syntax.LNodeExt.WithoutTrailingTrivia().