Enhanced C#
Language of your choice: library documentation
Nested classes | Public Types | Public fields | Public static fields | Properties | Public Member Functions | Static Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected fields | Protected static fields | List of all members
Loyc.Syntax.LNode Class Referenceabstract

All nodes in a Loyc syntax tree share this base class. More...


Source file:
Inheritance diagram for Loyc.Syntax.LNode:
Loyc.Syntax.ILNode Loyc.Syntax.ILiteralValue Loyc.ICloneable< LNode > Loyc.IHasLocation Loyc.IHasValue< object > Loyc.Collections.INegListSource< LNode > Loyc.Syntax.ILiteralValue Loyc.Collections.INegListSource< ILNode > Loyc.IHasLocation Loyc.Syntax.IUninterpretedLiteral Loyc.IHasValue< object > Loyc.Syntax.IUninterpretedLiteral Loyc.IHasValue< object > Loyc.Syntax.CallNode Loyc.Syntax.IdNode Loyc.Syntax.LiteralNode

Remarks

All nodes in a Loyc syntax tree share this base class.

Loyc defines only three types of nodes: simple symbols, literals, and calls.

See http://loyc.net/loyc-trees to learn more about the Loyc tree concept.

This class provides access to all properties of all three types of nodes, in order to make this class easier to access from plain C#, and to avoid unnecessary downcasting in some cases. In fact, you never need to use the derived classes; you can think of them simply as a way that the implementation is optimized.

Loyc nodes are immutable (except the Style property).

Important properties

The main properties of a node are

  1. Attrs: holds the attributes of the node, if any.
  2. Name: the name of an IdNode, or the name of the IdNode that is acting as the Target of a CallNode.
  3. Value: the value of a LiteralNode.
  4. Target: the target of a CallNode. It represents a method, macro, or special identifier that is being called.
  5. Args: holds the arguments to a CallNode, if any. Returns an empty list if the node does not have an argument list.
  6. Range: indicates the source file that the node came from and location in that source file.
  7. Style: an 8-bit flag value that is used as a hint to the node printer about how the node should be printed. For example, a hex literal like 0x10 has the NodeStyle.Alternate style to distinguish it from decimal literals such as 16. Custom display styles that do not fit in the Style property can be expressed with attributes.

To learn more about working with LNode, see http://loyc.net/loyc-trees/dotnet.html

Nested classes

struct  PushedPrinter
 Returned by PushPrinter(ILNodePrinter). More...
 

Public Types

enum  CompareMode { Normal = 0, Styles = 1, IgnoreTrivia = 2 }
 
enum  ReplaceOpt { ReplaceOpt.ReplaceRoot = 1, ReplaceOpt.ProcessAttrs = 2, ReplaceOpt.Default = ReplaceRoot | ProcessAttrs }
 

Public fields

virtual UString TextValue => default(UString)
 If this node IsLiteral, gets the original text of the literal, if it is available. This property is empty if the node was created programmatically from a value, or if the parser did not save the original text, or if this is not a literal. More...
 
virtual Symbol TypeMarker => null
 If this node IsLiteral, gets the type marker associated with the literal. This property is null if the node was created programmatically from a value, or if the parser did not save the type marker, or if this is not a literal. More...
 

Public static fields

static readonly EmptySourceFile SyntheticSource = new EmptySourceFile("<Synthetic Code>")
 
static readonly IdNode Missing = Id(CodeSymbols.Missing)
 The empty identifier, used to represent missing information. More...
 
static readonly LNode InParensTrivia = Id(CodeSymbols.TriviaInParens)
 Used by the quote {...} macro. More...
 

Properties

virtual SourceRange Range [get]
 Returns the location and range in source code of this node. More...
 
ISourceFile Source [get]
 Returns the source file (shortcut for Range.Source). More...
 
NodeStyle Style [get, set]
 Indicates the preferred style to use when printing the node to a text string. More...
 
NodeStyle BaseStyle [get, set]
 
virtual LNodeList Attrs [get]
 Returns the attribute list for this node. More...
 
virtual bool IsFrozen [get]
 Returns true if the node is immutable, and false if any part of it can be edited. Currently, mutable nodes are not implemented. More...
 
abstract LNodeKind Kind [get]
 Returns the LNodeKind: Symbol, Literal, or Call. More...
 
bool IsCall [get]
 
bool IsId [get]
 
bool IsLiteral [get]
 
abstract Symbol Name [get]
 Returns the Symbol if IsId. If this node is a call (IsCall) and Target.IsId is true, this property returns Target.Name. In all other cases, the name is GSymbol.Empty. Shall not return null. More...
 
bool HasSpecialName [get]
 Returns true if Name is a "special" name (i.e. starts with '#' or '\'' or '.' or any character below 48 in ASCII). More...
 
bool HasValue [get]
 
abstract object Value [get]
 Returns the value of a literal node, or NoValue.Value if this node is not a literal (IsLiteral is false). More...
 
abstract LNode Target [get]
 Returns the target of a method call, or null if IsCall is false. The target can be a symbol with no name (GSymbol.Empty) to represent a parenthesized expression, if there is one argument. More...
 
abstract LNodeList Args [get]
 Returns the argument list of this node. Always empty when IsCall==false. More...
 
static ILNodePrinter?? Printer [get, set]
 Gets or sets the default node printer on the current thread, which controls how nodes are serialized to text by default. More...
 
virtual object TriviaValue [get]
 Gets the value of Args[0].Value, if Args[0] exists; otherwise, returns NoValue.Value. More...
 
int ArgCount [get]
 
int AttrCount [get]
 
bool HasAttrs [get]
 
bool IsTrivia [get]
 
int Min [get]
 
virtual int? Max [get]
 
LNode this[int index] [get]
 
int Count [get]
 
- Properties inherited from Loyc.Syntax.ILNode
LNodeKind Kind [get]
 
Symbol Name [get]
 
ILNode Target [get]
 
ISourceRange Range [get]
 
NodeStyle Style [get, set]
 
- Properties inherited from Loyc.Syntax.IUninterpretedLiteral
UString TextValue [get]
 Represents the serialized text of the value. More...
 
Symbol TypeMarker [get]
 Represents the type of the value. More...
 
- Properties inherited from Loyc.IHasValue< object >
Value [get]
 
- Properties inherited from Loyc.Collections.INegListSource< ILNode >
int Min [get]
 Returns the minimum valid index in the collection. More...
 
int Max [get]
 Returns the maximum valid index in the collection. More...
 
- Properties inherited from Loyc.IHasLocation
object Location [get]
 
- Properties inherited from Loyc.Collections.INegListSource< LNode >
int Min [get]
 Returns the minimum valid index in the collection. More...
 
int Max [get]
 Returns the maximum valid index in the collection. More...
 

Public Member Functions

LNode SetBaseStyle (NodeStyle s)
 
LNode SetStyle (NodeStyle s)
 
virtual LNode WithName (Symbol name)
 Creates a node with a new value for Name. More...
 
abstract LiteralNode WithValue (object value)
 Creates a new literal node with a different Value than the current literal node. More...
 
virtual CallNode WithTarget (LNode target)
 
virtual CallNode WithTarget (Symbol name)
 
abstract CallNode WithArgs (LNodeList args)
 Creates a Node with a new argument list. If this node is not a call, a new node is created using this node as its target. Otherwise, the existing argument list is replaced. More...
 
virtual CallNode With (LNode target, LNodeList args)
 Creates a CallNode with the same attributes and Range, but a different target and argument list. If the current node is not a CallNode, it becomes one (the Range, Style and attributes of the current node are kept, but the Kind, Value, and Name are discarded.) More...
 
virtual CallNode With (Symbol target, LNodeList args)
 Creates a CallNode with the same attributes and Range, but a different target and argument list. If the current node is not a CallNode, it becomes one (the Range, Style and attributes of the current node are kept, but the Kind, Value, and Name are discarded.) More...
 
CallNode With (Symbol target, params LNode[] args)
 
abstract LNode Clone ()
 Creates a copy of the node. Since nodes are immutable, there is little reason for an end-user to call this, but Clone() is used internally as a helper method by the WithXyz() methods. More...
 
LNode WithRange (SourceRange range)
 
virtual LNode WithRange (int startIndex, int endIndex)
 
LNode WithStyle (NodeStyle style)
 
virtual LNode With (SourceRange range, NodeStyle style)
 
virtual LNode WithoutAttrs ()
 
abstract LNode WithAttrs (LNodeList attrs)
 
LNode WithAttrs (params LNode[] attrs)
 
CallNode WithArgs (params LNode[] args)
 
LNode PlusAttr (LNode attr)
 
LNode PlusAttrs (IEnumerable< LNode > attrs)
 
LNode PlusAttrs (params LNode[] attrs)
 
LNode PlusAttrs (LNodeList attrs)
 
LNode PlusAttrsBefore (LNodeList attrs)
 
LNode PlusAttrsBefore (params LNode[] attrs)
 
LNode PlusAttrBefore (LNode attr)
 
LNode PlusArg (LNode arg)
 
LNode PlusArgs (LNodeList args)
 
LNode PlusArgs (IEnumerable< LNode > args)
 
LNode PlusArgs (params LNode[] args)
 
LNode WithArgChanged (int index, Func< LNode, LNode > selector)
 
LNode WithArgChanged (int index, LNode newValue)
 
LNode WithAttrChanged (int index, LNode newValue)
 
LNode WithChildChanged (int index, LNode newValue)
 
abstract void Call (LNodeVisitor visitor)
 
abstract void Call (ILNodeVisitor visitor)
 
virtual string Print (ParsingMode mode=null, ILNodePrinterOptions options=null)
 
override string ToString ()
 
virtual bool Equals (ILNode other, CompareMode mode)
 Compares two nodes for structural equality. Two nodes are considered equal if they have the same kind, the same name, the same value, the same arguments, and the same attributes. More...
 
bool Equals (LNode other)
 
bool Equals (ILNode other)
 
override bool Equals (object other)
 
override int GetHashCode ()
 Gets the hash code based on the structure of the tree. More...
 
bool HasPAttrs ()
 
LNodeList PAttrs ()
 
virtual bool Calls (Symbol name, int argCount)
 Returns true if Name == name and Max + 1 == argCount (which implies Kind == LNodeKind.Call if argCount != -1). More...
 
virtual bool Calls (string name, int argCount)
 
virtual bool Calls (Symbol name)
 
virtual bool Calls (string name)
 
virtual bool CallsMin (Symbol name, int minArgCount)
 Returns true if this is a call with the specified name and the specified minimum number of arguments. More...
 
virtual bool CallsMin (string name, int minArgCount)
 Returns true if this is a call with the specified name and the specified minimum number of arguments. More...
 
virtual bool HasSimpleHead ()
 Returns true if this is not a call, or if the call's Target is an Id or a Literal. More...
 
virtual bool HasSimpleHeadWithoutPAttrs ()
 Returns true if this is not a call, or if the call's Target is an Id or a Literal, and the Target has only trivia attributes. More...
 
virtual LNode WithAttrs (Func< LNode, Maybe< LNode >> selector)
 
virtual LNode WithAttrs (Func< LNode, IReadOnlyList< LNode >> selector)
 
virtual LNode WithArgs (Func< LNode, Maybe< LNode >> selector)
 
virtual bool IsIdWithoutPAttrs ()
 
virtual bool IsIdWithoutPAttrs (Symbol name)
 
virtual bool IsIdNamed (Symbol name)
 
virtual bool IsIdNamed (string name)
 
CallNode WithSplicedArgs (int index, LNode from, Symbol listName)
 
CallNode WithSplicedArgs (LNode from, Symbol listName)
 
LNode WithSplicedAttrs (int index, LNode from, Symbol listName)
 
LNode WithSplicedAttrs (LNode from, Symbol listName)
 
NestedEnumerable< DescendantsFrame, LNodeDescendants (NodeScanMode mode=NodeScanMode.YieldAllChildren)
 
NestedEnumerable< DescendantsFrame, LNodeDescendantsAndSelf ()
 
LNode TryGet (int index, out bool fail)
 
IRange< LNodeSlice (int start, int count=int.MaxValue)
 
IEnumerator< LNodeGetEnumerator ()
 
virtual LNode Select (Func< LNode, Maybe< LNode >> selector, ReplaceOpt options=ReplaceOpt.ProcessAttrs)
 Transforms the Target, parameters, and optionally the attributes of an LNode, returning another LNode of the same Kind. If the selector makes no changes, Select() returns this. More...
 
virtual LNode SelectMany (Func< LNode, IReadOnlyList< LNode >> selector, ReplaceOpt options=ReplaceOpt.ProcessAttrs)
 
Maybe< LNodeReplaceRecursive (Func< LNode, Maybe< LNode >> matcher, ReplaceOpt options=ReplaceOpt.Default)
 Performs a recursive find-and-replace operation, by attempting to replace each child (among Attrs, Target, Args) using the specified selector. This method can also be used for simple searching, by giving a selector that always returns null. More...
 
LNode ReplaceRecursive (Func< LNode, LNode > matcher, ReplaceOpt options=ReplaceOpt.Default)
 
LNodeList RecursiveReplace (Func< LNode, LNodeList?> matcher, ReplaceOpt options=ReplaceOpt.Default)
 
- Public Member Functions inherited from Loyc.Collections.INegListSource< ILNode >
IRange< T > Slice (int start, int count=int.MaxValue)
 Returns a sub-range of this list. More...
 
- Public Member Functions inherited from Loyc.ICloneable< LNode >
Clone ()
 
- Public Member Functions inherited from Loyc.Collections.INegListSource< LNode >
IRange< T > Slice (int start, int count=int.MaxValue)
 Returns a sub-range of this list. More...
 

Static Public Member Functions

static IdNode Id (Symbol name, LNode prototype)
 
static IdNode Id (string name, LNode prototype)
 
static IdNode Id (LNodeList attrs, Symbol name, LNode prototype)
 
static IdNode Id (LNodeList attrs, string name, LNode prototype)
 
static LiteralNode Literal< V > (V value, LNode prototype)
 
static LiteralNode Literal< V > (LNodeList attrs, V value, LNode prototype)
 
static CallNode Call (Symbol name, LNode prototype)
 
static CallNode Call (LNode target, LNode prototype)
 
static CallNode Call (Symbol name, LNodeList args, LNode prototype)
 
static CallNode Call (LNode target, LNodeList args, LNode prototype)
 
static CallNode Call (LNodeList attrs, Symbol name, LNodeList args, LNode prototype)
 
static CallNode Call (LNodeList attrs, LNode target, LNodeList args, LNode prototype)
 
static CallNode Trivia (Symbol name, object value, LNode prototype)
 
static IdNode Id (Symbol name, SourceRange range, NodeStyle style=NodeStyle.Default)
 
static IdNode Id (string name, SourceRange range, NodeStyle style=NodeStyle.Default)
 
static IdNode Id (LNodeList attrs, Symbol name, SourceRange range, NodeStyle style=NodeStyle.Default)
 
static IdNode Id (LNodeList attrs, string name, SourceRange range, NodeStyle style=NodeStyle.Default)
 
static LiteralNode Literal< V > (V value, SourceRange range, NodeStyle style=NodeStyle.Default)
 
static LiteralNode Literal (SourceRange range, LiteralValue value, NodeStyle style=NodeStyle.Default)
 
static LiteralNode Literal< P > (SourceRange range, P valueProvider, NodeStyle style=NodeStyle.Default)
 
static LiteralNode Literal< V > (LNodeList attrs, V value, SourceRange range, NodeStyle style=NodeStyle.Default)
 
static LiteralNode Literal (LNodeList attrs, SourceRange range, LiteralValue value, NodeStyle style=NodeStyle.Default)
 
static LiteralNode Literal< P > (LNodeList attrs, SourceRange range, P valueProvider, NodeStyle style=NodeStyle.Default)
 
static CallNode Call (Symbol name, SourceRange range, NodeStyle style=NodeStyle.Default)
 
static CallNode Call (Symbol name, SourceRange range, int targetStart, int targetEnd, NodeStyle style=NodeStyle.Default)
 
static CallNode Call (LNode target, SourceRange range, NodeStyle style=NodeStyle.Default)
 
static CallNode Call (Symbol name, LNodeList args, SourceRange range, NodeStyle style=NodeStyle.Default)
 
static CallNode Call (Symbol name, LNodeList args, SourceRange range, int targetStart, int targetEnd, NodeStyle style=NodeStyle.Default)
 
static CallNode Call (LNode target, LNodeList args, SourceRange range, NodeStyle style=NodeStyle.Default)
 
static CallNode Call (LNodeList attrs, Symbol name, LNodeList args, SourceRange range, NodeStyle style=NodeStyle.Default)
 
static CallNode Call (LNodeList attrs, LNode target, LNodeList args, SourceRange range, NodeStyle style=NodeStyle.Default)
 
static CallNode Trivia (Symbol name, object value, SourceRange range, NodeStyle style=NodeStyle.Default)
 
static IdNode Id (Symbol name, ISourceFile file=null)
 
static IdNode Id (string name, ISourceFile file=null)
 
static IdNode Id (LNodeList attrs, Symbol name, ISourceFile file=null)
 
static IdNode Id (LNodeList attrs, string name, ISourceFile file=null)
 
static LiteralNode Literal< V > (V value, ISourceFile file=null, NodeStyle style=NodeStyle.Default)
 
static LiteralNode Literal< V > (LNodeList attrs, V value, ISourceFile file=null, NodeStyle style=NodeStyle.Default)
 
static LiteralNode Literal (object value, ISourceFile file=null, NodeStyle style=NodeStyle.Default)
 
static CallNode Call (Symbol name, ISourceFile file=null, NodeStyle style=NodeStyle.Default)
 
static CallNode Call (LNode target, ISourceFile file=null, NodeStyle style=NodeStyle.Default)
 
static CallNode Call (Symbol name, LNodeList args, ISourceFile file=null, NodeStyle style=NodeStyle.Default)
 
static CallNode Call (LNode target, LNodeList args, ISourceFile file=null, NodeStyle style=NodeStyle.Default)
 
static CallNode Call (LNodeList attrs, Symbol name, ISourceFile file=null, NodeStyle style=NodeStyle.Default)
 
static CallNode Call (LNodeList attrs, LNode target, ISourceFile file=null, NodeStyle style=NodeStyle.Default)
 
static CallNode Call (LNodeList attrs, Symbol name, LNodeList args, ISourceFile file=null, NodeStyle style=NodeStyle.Default)
 
static CallNode Call (LNodeList attrs, LNode target, LNodeList args, ISourceFile file=null, NodeStyle style=NodeStyle.Default)
 
static CallNode Trivia (Symbol name, object value, ISourceFile file=null, NodeStyle style=NodeStyle.Default)
 
static LNodeList List ()
 
static LNodeList List (LNode item_0)
 
static LNodeList List (LNode item_0, LNode item_1)
 
static LNodeList List (params LNode[] list)
 
static LNodeList List (IEnumerable< LNode > list)
 
static LNodeList List (VList< LNode > list)
 
static bool IsSpecialName (string name)
 Returns true if name is considered a "special" name that starts with any character below 48 in ASCII, such as '#', '\'', and '.'. More...
 
static bool IsSpecialName (Symbol name)
 
static SavedValue< ILNodePrinterSetPrinter (ILNodePrinter newValue)
 
static PushedPrinter PushPrinter (ILNodePrinter printer)
 Helps you change printers temporarily. Usage in C#: using (LNode.PushPrinter(myPrinter)) { ... } More...
 
static bool Equals (LNodeList a, LNodeList b, CompareMode mode=CompareMode.Normal)
 Compares two lists of nodes for structural equality. More...
 
static bool Equals (ILNode a, ILNode b, CompareMode mode=CompareMode.Normal)
 
static LNode MergeLists (LNode node1, LNode node2, Symbol listName)
 Some CallNodes are used to represent lists. This method merges two nodes, forming or appending a list (see remarks). More...
 
static LNode MergeBinary (LNode node1, LNode node2, Symbol binaryOpName)
 Combines two nodes using a binary operator or function. More...
 
static List< LNodeFlattenBinaryOpSeq (LNode expr, Symbol opName, bool? rightAssociative=null)
 Converts a sequence of the same operator (e.g. x+y+z) to a list (e.g. x, y, z). More...
 

Protected Member Functions

 LNode (LNode prototype)
 
 LNode (SourceRange range, NodeStyle style)
 

Static Protected Member Functions

static void NoNulls (LNodeList list, string propName)
 

Protected fields

RangeAndStyle RAS
 

Protected static fields

static readonly CallNode EmptySplice = Call(CodeSymbols.Splice)
 

Member Enumeration Documentation

◆ ReplaceOpt

Enumerator
ReplaceRoot 

When calling n.ReplaceRecursive, specifies that the selector should be called on n itself, not just its children.

ProcessAttrs 

When calling ReplaceRecursive(Func<LNode, Maybe<LNode>>, ReplaceOpt) or Select(Func<LNode, Maybe<LNode>>, ReplaceOpt), specifies that attributes should be processed rather than left unchanged.

Default 

ReplaceRoot and ProcessAttrs

Member Function Documentation

◆ Calls()

virtual bool Loyc.Syntax.LNode.Calls ( Symbol  name,
int  argCount 
)
inlinevirtual

Returns true if Name == name and Max + 1 == argCount (which implies Kind == LNodeKind.Call if argCount != -1).

This could have been an extension method, but when verifying that you have a certain kind of node, it's common to check both the Name and ArgCount(); checking both in one call avoids extra interface invocations.

Implements Loyc.Syntax.ILNode.

Reimplemented in Loyc.Syntax.CallNode.

Referenced by Loyc.LLParserGenerator.IntStreamCodeGenHelper.CodeToTerminalPred(), Loyc.Ecs.Parser.EcsTriviaInjector.DoneAttaching(), Loyc.Syntax.LNodeExt.GetCaptureIdentifier(), Loyc.LLParserGenerator.Rule.GetMethodSignature(), Loyc.Syntax.StandardTriviaInjector.HasImplicitLeadingNewline(), Loyc.Ecs.EcsValidators.IsVariableDeclExpr(), Loyc.Ecs.EcsValidators.KeyNameComponentOf(), Loyc.Syntax.LNode.MergeLists(), and Loyc.LLParserGenerator.CodeGenHelperBase.VisitInput().

◆ CallsMin() [1/2]

virtual bool Loyc.Syntax.LNode.CallsMin ( string  name,
int  minArgCount 
)
inlinevirtual

Returns true if this is a call with the specified name and the specified minimum number of arguments.

Reimplemented in Loyc.Syntax.CallNode.

◆ CallsMin() [2/2]

virtual bool Loyc.Syntax.LNode.CallsMin ( Symbol  name,
int  minArgCount 
)
inlinevirtual

◆ Clone()

abstract LNode Loyc.Syntax.LNode.Clone ( )
pure virtual

Creates a copy of the node. Since nodes are immutable, there is little reason for an end-user to call this, but Clone() is used internally as a helper method by the WithXyz() methods.

Implemented in Loyc.Syntax.CallNode, Loyc.Syntax.LiteralNode, and Loyc.Syntax.IdNode.

◆ Equals() [1/2]

virtual bool Loyc.Syntax.LNode.Equals ( ILNode  other,
CompareMode  mode 
)
inlinevirtual

Compares two nodes for structural equality. Two nodes are considered equal if they have the same kind, the same name, the same value, the same arguments, and the same attributes.

Parameters
modeWhether to pay attention to Style and trivia attributes

Position information (Range) is not compared.

References Loyc.Syntax.LNode.Equals().

Referenced by Loyc.Syntax.LNode.Equals().

◆ Equals() [2/2]

static bool Loyc.Syntax.LNode.Equals ( LNodeList  a,
LNodeList  b,
CompareMode  mode = CompareMode.Normal 
)
inlinestatic

Compares two lists of nodes for structural equality.

Parameters
modeWhether to pay attention to Style and trivia attributes

Position information is not compared.

Referenced by Loyc.LLParserGenerator.GeneralCodeGenHelper.CodeToTerminalPred(), Loyc.Syntax.LNode.Equals(), and Loyc.LLParserGenerator.CodeGenHelperBase.VisitInput().

◆ FlattenBinaryOpSeq()

static List<LNode> Loyc.Syntax.LNode.FlattenBinaryOpSeq ( LNode  expr,
Symbol  opName,
bool?  rightAssociative = null 
)
inlinestatic

Converts a sequence of the same operator (e.g. x+y+z) to a list (e.g. x, y, z).

Parameters
exprAn expression that may or may not call the operator.
opNameThe operator to recognize.
rightAssociativeWhether the operator is right associative. If this is null then either or both associativities will work (e.g. (a*b)*(c*d) is flattened to a,b,c,d).
Returns
A list of subexpressions with the operator removed. If expr does not call the specified binary operator, the list contains a single item which is the original expression.

This is the reverse operation of MergeBinary.

◆ GetHashCode()

override int Loyc.Syntax.LNode.GetHashCode ( )
inline

Gets the hash code based on the structure of the tree.

If the tree is large, less than the entire tree is scanned to produce the hashcode (in the absolute worst case, about 4000 nodes are examined, but usually it is less than 100).

References Loyc.Syntax.LNode.GetHashCode().

Referenced by Loyc.Syntax.LNode.GetHashCode().

◆ HasSimpleHead()

virtual bool Loyc.Syntax.LNode.HasSimpleHead ( )
inlinevirtual

Returns true if this is not a call, or if the call's Target is an Id or a Literal.

Reimplemented in Loyc.Syntax.CallNode.

◆ HasSimpleHeadWithoutPAttrs()

virtual bool Loyc.Syntax.LNode.HasSimpleHeadWithoutPAttrs ( )
inlinevirtual

Returns true if this is not a call, or if the call's Target is an Id or a Literal, and the Target has only trivia attributes.

Reimplemented in Loyc.Syntax.CallNode.

◆ IsSpecialName()

static bool Loyc.Syntax.LNode.IsSpecialName ( string  name)
inlinestatic

Returns true if name is considered a "special" name that starts with any character below 48 in ASCII, such as '#', '\'', and '.'.

This returns false for the empty string or null.

In order to keep the check trivially simple, this returns true for '$' even though it is not special in some languages (e.g. JavaScript).

Letters, underscores, digits, and some punctuation do not count as special. The full list of specials is ! " # $ % & ' ( ) * + , - . / plus the space character and the control characters.

◆ MergeBinary()

static LNode Loyc.Syntax.LNode.MergeBinary ( LNode  node1,
LNode  node2,
Symbol  binaryOpName 
)
inlinestatic

Combines two nodes using a binary operator or function.

Parameters
node1First node, list, or null.
node2Second node, list, or null.
binaryOpNameBinary operator to use when the nodes are not null.
Returns
If either node1 or node2 is null, this method returns the other node (node1 ?? node2), otherwise the nodes are joined with the specified operator.
See also
FlattenBinaryOpSeq

References Loyc.Syntax.LNode.Range, and Loyc.Syntax.SourceRange.Source.

◆ MergeLists()

static LNode Loyc.Syntax.LNode.MergeLists ( LNode  node1,
LNode  node2,
Symbol  listName 
)
inlinestatic

Some CallNodes are used to represent lists. This method merges two nodes, forming or appending a list (see remarks).

Parameters
node1First node, list, or null.
node2Second node, list, or null.
listNameThe Name used to detect whether a node is a list (typically "#splice"). Any other name is considered a normal call, not a list. If this method creates a list from two non- lists, this parameter specifies the Name that the list will have.
Returns
The merged list.

The order of the data is retained (i.e. the data in node1 is inserted before the data in node2).

  • If either node1 or node2 is null, this method returns the other (node1 ?? node2).
  • If both node1 and node2 are lists, this method merges the list into a single list by appending node2's arguments at the end of node1. The attributes of node1 are kept and those of node2 are discarded.
  • If one of the nodes is a list and the other is not, the non-list is inserted into the list's Args.
  • If neither node is a list, a list is created with both nodes as its two Args.
See also
LNodeExt.WithSpliced

References Loyc.Syntax.LNode.Calls().

◆ PushPrinter()

static PushedPrinter Loyc.Syntax.LNode.PushPrinter ( ILNodePrinter  printer)
inlinestatic

Helps you change printers temporarily. Usage in C#: using (LNode.PushPrinter(myPrinter)) { ... }

For example, to switch to the EC# printer, use using (LNode.PushPrinter(EcsNodePrinter.Printer)) { ... }. This changes the default printer. If you don't want to change the default printer, please invoke the printer directly:

var sb = new StringBuilder();
EcsNodePrinter.Printer(node, sb, MessageSink.Trace);

◆ ReplaceRecursive()

Maybe<LNode> Loyc.Syntax.LNode.ReplaceRecursive ( Func< LNode, Maybe< LNode >>  matcher,
ReplaceOpt  options = ReplaceOpt.Default 
)
inline

Performs a recursive find-and-replace operation, by attempting to replace each child (among Attrs, Target, Args) using the specified selector. This method can also be used for simple searching, by giving a selector that always returns null.

Parameters
matcherThis method is called for each descendant, and optionally the root node. If the selector returns a node, the new node replaces the node that was passed to selector and the children of the new node are ignored. If the selector returns null, children of the child are scanned recursively. If the selector returns Maybe{LNode}.NoValue then the current node is deleted if it is an argument or attribute. If the current node is a target, it cannot be deleted, so it is replaced with #splice() which, by convention, represents an empty list. If you delete the root node then this method returns Maybe{LNode}.NoValue.
optionsOptions, see ReplaceOpt.
Returns
The new node produced after all replacements have occurred.

If replaceFunc always returns null (or if replaceRoot is false and the root has no children), ReplaceRecursive returns this.

References Loyc.Syntax.LNode.Select().

◆ Select()

virtual LNode Loyc.Syntax.LNode.Select ( Func< LNode, Maybe< LNode >>  selector,
ReplaceOpt  options = ReplaceOpt.ProcessAttrs 
)
inlinevirtual

Transforms the Target, parameters, and optionally the attributes of an LNode, returning another LNode of the same Kind. If the selector makes no changes, Select() returns this.

The selector is not allowed to return null, but it can return NoValue.Value to delete a parameter or target. If the current node is a target, it cannot be deleted, so it is replaced with #splice() which, by convention, represents an empty list. If you're wondering why we don't use null for deletions, it is because the functionality of this method is actually implemented by VList<T>.WhereSelect(Func<T, Maybe<T>>); since T could be a value type, that method cannot use null as a signal to delete items from the collection.

It is not possible to delete the Target of a call, and if the selector returns NoValue.Value for the Target, the target is replaced with an empty call to #splice().

Reimplemented in Loyc.Syntax.CallNode.

Referenced by Loyc.Syntax.LNode.ReplaceRecursive().

◆ With() [1/2]

virtual CallNode Loyc.Syntax.LNode.With ( LNode  target,
LNodeList  args 
)
inlinevirtual

Creates a CallNode with the same attributes and Range, but a different target and argument list. If the current node is not a CallNode, it becomes one (the Range, Style and attributes of the current node are kept, but the Kind, Value, and Name are discarded.)

References Loyc.Syntax.LNode.Args, Loyc.Syntax.LNode.Attrs, and Loyc.Syntax.LNode.Target.

Referenced by Loyc.Syntax.CallNode.Select(), and Loyc.Syntax.CallNode.WithArgs().

◆ With() [2/2]

virtual CallNode Loyc.Syntax.LNode.With ( Symbol  target,
LNodeList  args 
)
inlinevirtual

Creates a CallNode with the same attributes and Range, but a different target and argument list. If the current node is not a CallNode, it becomes one (the Range, Style and attributes of the current node are kept, but the Kind, Value, and Name are discarded.)

References Loyc.Syntax.LNode.Attrs.

◆ WithArgs()

abstract CallNode Loyc.Syntax.LNode.WithArgs ( LNodeList  args)
pure virtual

Creates a Node with a new argument list. If this node is not a call, a new node is created using this node as its target. Otherwise, the existing argument list is replaced.

Parameters
argsNew argument list

Implemented in Loyc.Syntax.CallNode, Loyc.Syntax.LiteralNode, and Loyc.Syntax.IdNode.

Referenced by Loyc.LLParserGenerator.Rule.CreateMethod(), Loyc.LLParserGenerator.CodeGenHelperBase.CreateTryWrapperForRecognizer(), and Loyc.LLParserGenerator.Rule.GetMethodSignature().

◆ WithName()

virtual LNode Loyc.Syntax.LNode.WithName ( Symbol  name)
inlinevirtual

Creates a node with a new value for Name.

If IsId, the Name is simply changed. If IsCall, this method returns the equivalent of WithTarget(Target.WithName(name)) (which may be optimized for the particular call type). If IsLiteral, the Kind changes to LNodeKind.Id in order to set the name.

Reimplemented in Loyc.Syntax.CallNode, and Loyc.Syntax.IdNode.

References Loyc.Syntax.LNode.Attrs.

Referenced by Loyc.Syntax.CallNode.WithName().

◆ WithValue()

abstract LiteralNode Loyc.Syntax.LNode.WithValue ( object  value)
pure virtual

Creates a new literal node with a different Value than the current literal node.

Exceptions
InvalidOperationExceptionThe node was not a literal already.

Implemented in Loyc.Syntax.CallNode, Loyc.Syntax.LiteralNode, and Loyc.Syntax.IdNode.

Member Data Documentation

◆ InParensTrivia

readonly LNode Loyc.Syntax.LNode.InParensTrivia = Id(CodeSymbols.TriviaInParens)
static

Used by the quote {...} macro.

◆ Missing

readonly IdNode Loyc.Syntax.LNode.Missing = Id(CodeSymbols.Missing)
static

The empty identifier, used to represent missing information.

Referenced by Loyc.LLPG.Macros.LLLPG_lexer(), and Loyc.LLPG.Macros.LLLPG_parser().

◆ TextValue

virtual UString Loyc.Syntax.LNode.TextValue => default(UString)

If this node IsLiteral, gets the original text of the literal, if it is available. This property is empty if the node was created programmatically from a value, or if the parser did not save the original text, or if this is not a literal.

If you need a string version of the value, call ToString() on it.

◆ TypeMarker

virtual Symbol Loyc.Syntax.LNode.TypeMarker => null

If this node IsLiteral, gets the type marker associated with the literal. This property is null if the node was created programmatically from a value, or if the parser did not save the type marker, or if this is not a literal.

Property Documentation

◆ Args

abstract LNodeList Loyc.Syntax.LNode.Args
get

◆ Attrs

virtual LNodeList Loyc.Syntax.LNode.Attrs
get

◆ HasSpecialName

bool Loyc.Syntax.LNode.HasSpecialName
get

Returns true if Name is a "special" name (i.e. starts with '#' or '\'' or '.' or any character below 48 in ASCII).

Note that this property returns false for the empty identifier @``.

◆ IsFrozen

virtual bool Loyc.Syntax.LNode.IsFrozen
get

Returns true if the node is immutable, and false if any part of it can be edited. Currently, mutable nodes are not implemented.

Debugger-hidden until such time as mutable nodes actually exist.

◆ Kind

abstract LNodeKind Loyc.Syntax.LNode.Kind
get

Returns the LNodeKind: Symbol, Literal, or Call.

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

◆ Name

abstract Symbol Loyc.Syntax.LNode.Name
get

Returns the Symbol if IsId. If this node is a call (IsCall) and Target.IsId is true, this property returns Target.Name. In all other cases, the name is GSymbol.Empty. Shall not return null.

Examples (using C#/LES syntax):

Expression   Kind    Name (blank if empty)
hello        Id      hello
#if         Id      #if
Foo(x, y)    Call    Foo
x += y       Call    +=
x.Foo(y)     Call    
5.0          Literal 

Referenced by Loyc.LLParserGenerator.GeneralCodeGenHelper.CodeToTerminalPred(), Loyc.Ecs.EcsValidators.KeyNameComponentOf(), Loyc.Syntax.LNodeExt.MatchesPattern(), Loyc.Ecs.EcsValidators.MethodDefinitionKind(), and Loyc.Ecs.EcsValidators.SpaceDefinitionKind().

◆ Printer

ILNodePrinter?? Loyc.Syntax.LNode.Printer
staticgetset

Gets or sets the default node printer on the current thread, which controls how nodes are serialized to text by default.

The LES printer is the default, and will be used if you try to set this property to null.

◆ Range

virtual SourceRange Loyc.Syntax.LNode.Range
get

Returns the location and range in source code of this node.

A parser should record a sufficiently wide range for each parent node, such that all children are fully contained within the range. However, this is not an invariant; macros can splice together syntax trees from different source files or add synthetic nodes, so that the parent range does not necessarily include all child ranges. (In fact, in general it is impossible to ensure that parent ranges include child ranges because a parent can only specify a single source file, while children can come from several source files.)

Referenced by Loyc.Syntax.LNodeExt.AsLNode(), Loyc.Ecs.Parser.EcsTriviaInjector.DoneAttaching(), Loyc.Syntax.LNode.MergeBinary(), Loyc.Syntax.AbstractTriviaInjector< Token >.RunCore(), Loyc.Syntax.IdNode.WithArgs(), and Loyc.Syntax.LiteralNode.WithArgs().

◆ Source

ISourceFile Loyc.Syntax.LNode.Source
get

Returns the source file (shortcut for Range.Source).

◆ Style

NodeStyle Loyc.Syntax.LNode.Style
getset

Indicates the preferred style to use when printing the node to a text string.

The Style is an 8-bit value that acts as a hint to the node printer about how the node should be printed. Custom display styles that do not fit in the Style property can be expressed with special "trivia" attributes that have a Name starting with %. (trivia attributes, which are also used to store comments in the syntax tree, are not printed like normal attributes and are normally ignored if the node printer does not specifically recognize them.)

◆ Target

abstract LNode Loyc.Syntax.LNode.Target
get

Returns the target of a method call, or null if IsCall is false. The target can be a symbol with no name (GSymbol.Empty) to represent a parenthesized expression, if there is one argument.

Referenced by Loyc.Ecs.Parser.EcsTriviaInjector.DoneAttaching(), Loyc.Ecs.EcsValidators.KeyNameComponentOf(), Loyc.Syntax.LNodeExt.MatchesPattern(), Loyc.Syntax.CallNode.Select(), and Loyc.Syntax.LNode.With().

◆ TriviaValue

virtual object Loyc.Syntax.LNode.TriviaValue
get

Gets the value of Args[0].Value, if Args[0] exists; otherwise, returns NoValue.Value.

"Trivia nodes" are used to efficiently represent the value of trivia and non-tree Lexing.Tokens; they can be created by calling the LNode.Trivia function. Since an LNode is not allowed to have both a Name and a Value (as there is no syntax in LES or EC# for such a node), a trivia node pretends that there is an argument list with one item, and that one item is always a literal whose Value is the value stored in the trivia node. Thus, a token node is printed out as TokenType(Value) where Value is some literal.

If you suspect you're dealing with a trivia node, it is wasteful to actually call node.Args[0].Value since this causes a temporary token list to be allocated. Instead you should use this property, which returns the token value without allocating memory. Of course, if this property is called on a non-trivia node, it simply returns Args[0].Value.

◆ Value

abstract object Loyc.Syntax.LNode.Value
get

Returns the value of a literal node, or NoValue.Value if this node is not a literal (IsLiteral is false).

Referenced by Loyc.LLParserGenerator.IntStreamCodeGenHelper.CodeToTerminalPred(), Loyc.LLPG.Macros.LLLPG_lexer(), Loyc.LLPG.Macros.LLLPG_parser(), Loyc.LLPG.Macros.LllpgMacro(), and Loyc.Syntax.LNodeExt.MatchesPattern().