protected TNode AddError <TNode>(TNode node, LeeSyntaxNode location, ErrorCode code, params object[] args) where TNode : LeeSyntaxNode { // assumes non-terminals will at most appear once in sub-tree int offset; FindOffset(node, location, out offset); return(WithAdditionalDiagnostics(node, MakeError(offset, location.Width, code, args))); }
public virtual TResult Visit(LeeSyntaxNode node) { if (node == null) { return(default(TResult)); } return(node.Accept(this)); }
public ParsedSyntaxTree(SourceText textOpt, string path, ParseOptions options, LeeSyntaxNode root, DirectiveStack directives) { Debug.Assert(root != null); Debug.Assert(options != null); Debug.Assert(textOpt != null); _lazyText = textOpt; _options = options; _path = path ?? string.Empty; _root = root; _hasCompilationUnitRoot = root.Kind == SyntaxKind.CompilationUnit; this.SetDirectiveStack(directives); }
/// <summary> /// This function searches for the given location node within the subtree rooted at root node. /// If it finds it, the function computes the offset span of that child node within the root and returns true, /// otherwise it returns false. /// </summary> /// <param name="root">Root node</param> /// <param name="location">Node to search in the subtree rooted at root node</param> /// <param name="offset">Offset of the location node within the subtree rooted at child</param> /// <returns></returns> private bool FindOffset(GreenNode root, LeeSyntaxNode location, out int offset) { int currentOffset = 0; offset = 0; if (root != null) { for (int i = 0, n = root.SlotCount; i < n; i++) { var child = root.GetSlot(i); if (child == null) { // ignore null slots continue; } // check if the child node is the location node if (child == location) { // Found the location node in the subtree // Initialize offset with the offset of the location node within its parent // and walk up the stack of recursive calls adding the offset of each node // within its parent offset = currentOffset; return(true); } // search for the location node in the subtree rooted at child node if (this.FindOffset(child, location, out offset)) { // Found the location node in child's subtree // Add the offset of child node within its parent to offset // and continue walking up the stack offset += child.GetLeadingTriviaWidth() + currentOffset; return(true); } // We didn't find the location node in the subtree rooted at child // Move on to the next child currentOffset += child.FullWidth; } } // We didn't find the location node within the subtree rooted at root node return(false); }
/// <summary> /// Gets the root node of the syntax tree if it is already available. /// </summary> public abstract bool TryGetRoot(out LeeSyntaxNode root);
protected virtual TResult DefaultVisit(LeeSyntaxNode node) { return(default(TResult)); }
public static LeeSyntaxNode GetStandaloneNode(LeeSyntaxNode node) { if (node == null || !(node is ExpressionSyntax || node is CrefSyntax)) { return(node); } switch (node.Kind) { case SyntaxKind.IdentifierName: case SyntaxKind.GenericName: case SyntaxKind.NameMemberCref: case SyntaxKind.IndexerMemberCref: case SyntaxKind.OperatorMemberCref: case SyntaxKind.ConversionOperatorMemberCref: case SyntaxKind.ArrayType: case SyntaxKind.NullableType: // Adjustment may be required. break; default: return(node); } var parent = (LeeSyntaxNode)node.Parent; if (parent == null) { return(node); } switch (parent.Kind) { case SyntaxKind.QualifiedName: if (((QualifiedNameSyntax)parent).Right == node) { return(parent); } break; case SyntaxKind.AliasQualifiedName: if (((AliasQualifiedNameSyntax)parent).Name == node) { return(parent); } break; case SyntaxKind.SimpleMemberAccessExpression: case SyntaxKind.PointerMemberAccessExpression: if (((MemberAccessExpressionSyntax)parent).Name == node) { return(parent); } break; case SyntaxKind.MemberBindingExpression: { if (((MemberBindingExpressionSyntax)parent).Name == node) { return(parent); } break; } // Only care about name member crefs because the other cref members // are identifier by keywords, not syntax nodes. case SyntaxKind.NameMemberCref: if (((NameMemberCrefSyntax)parent).Name == node) { LeeSyntaxNode grandparent = (LeeSyntaxNode)parent.Parent; return(grandparent != null && grandparent.Kind == SyntaxKind.QualifiedCref ? grandparent : parent); } break; case SyntaxKind.QualifiedCref: if (((QualifiedCrefSyntax)parent).Member == node) { return(parent); } break; case SyntaxKind.ArrayCreationExpression: if (((ArrayCreationExpressionSyntax)parent).Type == node) { return(parent); } break; case SyntaxKind.ObjectCreationExpression: if (node.Kind == SyntaxKind.NullableType && ((ObjectCreationExpressionSyntax)parent).Type == node) { return(parent); } break; case SyntaxKind.StackAllocArrayCreationExpression: if (((StackAllocArrayCreationExpressionSyntax)parent).Type == node) { return(parent); } break; case SyntaxKind.NameColon: if (parent.Parent.IsKind(SyntaxKind.Subpattern)) { return((LeeSyntaxNode)parent.Parent); } break; } return(node); }
public static GreenNode ListNode(LeeSyntaxNode node0, LeeSyntaxNode node1, LeeSyntaxNode node2) { return(SyntaxList.List(node0, node1, node2)); }
public SourceLocation(LeeSyntaxNode node) : this(node.SyntaxTree, node.Span) { }
public static SyntaxToken StringLiteral(LeeSyntaxNode leading, string text, LeeSyntaxNode trailing) { return(new SyntaxTokenWithValueAndTrivia <string>(SyntaxKind.StringLiteralToken, text, text, leading, trailing)); }
public override bool TryGetRoot(out LeeSyntaxNode root) { root = _root; return(true); }