本文整理汇总了C#中SyntaxNodeOrToken.AsNode方法的典型用法代码示例。如果您正苦于以下问题:C# SyntaxNodeOrToken.AsNode方法的具体用法?C# SyntaxNodeOrToken.AsNode怎么用?C# SyntaxNodeOrToken.AsNode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SyntaxNodeOrToken
的用法示例。
在下文中一共展示了SyntaxNodeOrToken.AsNode方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Run
public override Match Run(SyntaxNodeOrToken nodeOrToken)
{
if (nodeOrToken.Kind() != SyntaxKind.Argument)
return Match.NoMatch;
var argument = (ArgumentSyntax) nodeOrToken.AsNode();
if (_variable.MinOccurrences == 1 && _variable.MaxOccurrences == 1)
return Match.Success.WithSyntaxNodeOrToken(argument);
var argumentList = argument.Parent as ArgumentListSyntax;
if (argumentList == null)
return Match.NoMatch;
var currentIndex = argumentList.Arguments.IndexOf(argument);
var availableCount = argumentList.Arguments.Count - currentIndex - _following;
if (availableCount == 0)
return Match.NoMatch;
var captureCount = _variable.MaxOccurrences == null
? availableCount
: Math.Min(availableCount, _variable.MaxOccurrences.Value);
if (captureCount < _variable.MinOccurrences)
return Match.NoMatch;
var endIndex = currentIndex + captureCount - 1;
var endArgument = argumentList.Arguments[endIndex];
return Match.Success.AddCapture(_variable, argument, endArgument);
}
示例2: ClassifyNodeOrToken
private void ClassifyNodeOrToken(SyntaxNodeOrToken nodeOrToken)
{
if (nodeOrToken.IsToken)
{
ClassifyToken(nodeOrToken.AsToken());
return;
}
ClassifyNode(nodeOrToken.AsNode());
}
示例3: Run
public override Match Run(SyntaxNodeOrToken nodeOrToken)
{
if (nodeOrToken.Kind() != _syntaxKind)
return Match.NoMatch;
var result = Match.Success;
if (nodeOrToken.IsNode)
{
var node = nodeOrToken.AsNode();
var children = node.ChildNodesAndTokens();
var matcherIndex = 0;
var matchedEnd = 0;
for (var i = 0; i < children.Count; i++)
{
if (matcherIndex >= _childMatchers.Length)
return Match.NoMatch;
while (i < children.Count && children[i].Span.Start < matchedEnd)
i++;
if (i >= children.Count)
break;
var child = children[i];
var matcher = _childMatchers[matcherIndex];
var match = matcher.Run(child);
if (!match.IsMatch)
return Match.NoMatch;
// Depending on much was captured, we need to skip some matchers.
if (match.Captures.Any())
matchedEnd = match.Captures.Max(c => c.EndNodeOrToken.Span.End);
result = result.AddCaptures(match.Captures);
matcherIndex++;
}
var allMatchersUsed = matcherIndex >= _childMatchers.Length;
if (!allMatchersUsed)
return Match.NoMatch;
}
return result;
}
示例4: CheckParents
public static void CheckParents(SyntaxNodeOrToken nodeOrToken, SyntaxTree expectedSyntaxTree)
{
Assert.Equal(expectedSyntaxTree, nodeOrToken.SyntaxTree);
var span = nodeOrToken.Span;
if (nodeOrToken.IsToken)
{
var token = nodeOrToken.AsToken();
foreach (var trivia in token.LeadingTrivia)
{
var tspan = trivia.Span;
var parentToken = trivia.Token;
Assert.Equal(parentToken, token);
if (trivia.HasStructure)
{
var parentTrivia = trivia.GetStructure().Parent;
Assert.Null(parentTrivia);
CheckParents((CSharpSyntaxNode)trivia.GetStructure(), expectedSyntaxTree);
}
}
foreach (var trivia in token.TrailingTrivia)
{
var tspan = trivia.Span;
var parentToken = trivia.Token;
Assert.Equal(parentToken, token);
if (trivia.HasStructure)
{
var parentTrivia = trivia.GetStructure().Parent;
Assert.Null(parentTrivia);
CheckParents(trivia.GetStructure(), expectedSyntaxTree);
}
}
}
else
{
var node = nodeOrToken.AsNode();
foreach (var child in node.ChildNodesAndTokens())
{
var parent = child.Parent;
Assert.Equal(node, parent);
CheckParents(child, expectedSyntaxTree);
}
}
}
示例5: GetNewSyntaxListItem
private static SyntaxNodeOrToken GetNewSyntaxListItem(SyntaxNodeOrToken item)
{
if (!item.IsNode)
{
return item;
}
var member = (AnonymousObjectMemberDeclaratorSyntax)item.AsNode();
var identifier = member.Expression as IdentifierNameSyntax;
if (identifier != null &&
identifier.Identifier.ValueText == member.NameEquals.Name.Identifier.ValueText)
{
return SyntaxFactory.AnonymousObjectMemberDeclarator(member.Expression).WithTriviaFrom(member);
}
return item;
}
开发者ID:duncanpMS,项目名称:sonarlint-vs,代码行数:17,代码来源:RedundantPropertyNamesInAnonymousClassCodeFixProvider.cs
示例6: Run
public override Match Run(SyntaxNodeOrToken nodeOrToken)
{
if (nodeOrToken.Kind() != SyntaxKind.ArgumentList)
return Match.NoMatch;
var argumentList = (ArgumentListSyntax) nodeOrToken.AsNode();
if (_variable.MinOccurrences > argumentList.Arguments.Count)
return Match.NoMatch;
if (_variable.MaxOccurrences != null && _variable.MaxOccurrences < argumentList.Arguments.Count)
return Match.NoMatch;
if (argumentList.Arguments.Count == 0)
return Match.Success;
var first = argumentList.Arguments.First();
var last = argumentList.Arguments.Last();
return Match.Success.AddCapture(_variable, first, last);
}
示例7: GetEndPosition
private static int GetEndPosition(SyntaxNodeOrToken nodeOrToken)
{
if (nodeOrToken.IsToken)
{
return nodeOrToken.Span.End;
}
else
{
return nodeOrToken.AsNode().GetLastToken().Span.End;
}
}
示例8: SourceLocationWithAssociatedNode
public SourceLocationWithAssociatedNode(SyntaxTree syntaxTree, SyntaxNodeOrToken nodeOrToken, bool associateInParent = false)
: this(syntaxTree, nodeOrToken.Span, nodeOrToken.IsNode ? nodeOrToken.AsNode() : nodeOrToken.AsToken().Parent, associateInParent)
{
}
示例9: CanReuse
private bool CanReuse(SyntaxNodeOrToken nodeOrToken)
{
// Zero width nodes and tokens always indicate that the parser had to do
// something tricky, so don't reuse them.
// NOTE: this is slightly different from IsMissing because of omitted type arguments
// and array size expressions.
if (nodeOrToken.FullWidth == 0)
{
return false;
}
// As of 2013/03/14, the compiler never attempts to incrementally parse a tree containing
// annotations. Our goal in instituting this restriction is to prevent API clients from
// taking a depedency on the survival of annotations.
if (nodeOrToken.ContainsAnnotations)
{
return false;
}
// We can't reuse a node or token if it intersects a changed text range.
if (this.IntersectsNextChange(nodeOrToken))
{
return false;
}
// don't reuse nodes or tokens with skipped text or diagnostics attached to them
if (nodeOrToken.ContainsDiagnostics ||
(nodeOrToken.IsToken && ((CSharpSyntaxNode)nodeOrToken.AsToken().Node).ContainsSkippedText && nodeOrToken.Parent.ContainsDiagnostics))
{
return false;
}
// fabricated tokens did not come from the lexer (likely from parser)
if (IsFabricatedToken(nodeOrToken.CSharpKind()))
{
return false;
}
// don't reuse nodes that are incomplete. this helps cases were an incomplete node
// completes differently after a change with far look-ahead.
//
// NOTE(cyrusn): It is very unfortunate that we even need this check given that we
// have already checked for ContainsDiagnostics above. However, there is a case where we
// can have a node with a missing token *and* there are no diagnostics.
// Specifically, this happens in the REPL when you have the last statement without a
// trailing semicolon. We treat this as an ExpressionStatement with a missing
// semicolon, but we do not report errors. It would be preferable to fix that so
// that the semicolon can be optional rather than abusing the system.
if ((nodeOrToken.IsToken && nodeOrToken.AsToken().IsMissing) ||
(nodeOrToken.IsNode && IsIncomplete((CSharp.CSharpSyntaxNode)nodeOrToken.AsNode())))
{
return false;
}
if (!nodeOrToken.ContainsDirectives)
{
return true;
}
return this.newDirectives.IncrementallyEquivalent(this.oldDirectives);
}
示例10: RewriteForStatement
//.........这里部分代码省略.........
// all the loop incrementers.
//
// We now make each one individually a sequence point:
//
// for(int i = 0, j = 0; ; i++, j++)
// ^-------^ ^---^ ^-^ ^-^
//
// If we decide that we want to preserve the native compiler stepping behavior
// then we'll need to be a bit fancy here. The initializer and increment statements
// can contain lambdas whose bodies need to have sequence points inserted, so we
// need to make sure we visit the children. But we'll also need to make sure that
// we do not generate one sequence point for each statement in the initializers
// and the incrementers.
var startLabel = new GeneratedLabelSymbol("start");
var endLabel = new GeneratedLabelSymbol("end");
// for (initializer; condition; increment)
// body;
//
// becomes the following (with
// block added for locals)
//
// {
// initializer;
// goto end;
// start:
// body;
// continue:
// increment;
// end:
// GotoIfTrue condition start;
// break:
// }
// initializer;
// goto end;
var statementBuilder = ArrayBuilder<BoundStatement>.GetInstance();
if (rewrittenInitializer != null)
{
statementBuilder.Add(rewrittenInitializer);
}
//mark the initial jump as hidden.
//We do it to tell that this is not a part of previous statement.
//This jump may be a target of another jump (for example if loops are nested) and that will make
//impression of the previous statement being re-executed
var gotoEnd = new BoundSequencePoint(null, new BoundGotoStatement(syntax, endLabel));
statementBuilder.Add(gotoEnd);
// start:
// body;
statementBuilder.Add(new BoundLabelStatement(syntax, startLabel));
Debug.Assert(rewrittenBody != null);
statementBuilder.Add(rewrittenBody);
// continue:
// increment;
statementBuilder.Add(new BoundLabelStatement(syntax, continueLabel));
if (rewrittenIncrement != null)
{
statementBuilder.Add(rewrittenIncrement);
}
// end:
// GotoIfTrue condition start;
statementBuilder.Add(new BoundLabelStatement(syntax, endLabel));
BoundStatement branchBack = null;
if (rewrittenCondition != null)
{
branchBack = new BoundConditionalGoto(rewrittenCondition.Syntax, rewrittenCondition, true, startLabel);
}
else
{
branchBack = new BoundGotoStatement(syntax, startLabel);
}
if (this.generateDebugInfo)
{
if (conditionSyntax.IsToken)
{
branchBack = new BoundSequencePointWithSpan(syntax, branchBack, conditionSyntax.Span);
}
else
{
//if there is no condition, make this a hidden point so that
//it does not count as a part of previous statement
branchBack = new BoundSequencePoint((CSharpSyntaxNode)conditionSyntax.AsNode(), branchBack);
}
}
statementBuilder.Add(branchBack);
// break:
statementBuilder.Add(new BoundLabelStatement(syntax, breakLabel));
var statements = statementBuilder.ToImmutableAndFree();
return new BoundBlock(syntax, locals, statements, hasErrors);
}
示例11: GetInnermostNamespaceScope
protected override SyntaxNode GetInnermostNamespaceScope(SyntaxNodeOrToken nodeOrToken)
{
var node = nodeOrToken.IsNode ? nodeOrToken.AsNode() : nodeOrToken.Parent;
return node.GetAncestorOrThis<NamespaceDeclarationSyntax>() ?? (SyntaxNode)node.GetAncestorOrThis<CompilationUnitSyntax>();
}
示例12: VisitNodeOrToken
private SyntaxNodeOrToken VisitNodeOrToken(SyntaxNodeOrToken nodeOrToken)
{
if (nodeOrToken.IsNode)
return Visit(nodeOrToken.AsNode());
return VisitToken(nodeOrToken.AsToken());
}
示例13: ClassifyNodeOrToken
private void ClassifyNodeOrToken(SyntaxNodeOrToken nodeOrToken)
{
var node = nodeOrToken.AsNode();
if (node != null)
{
ClassifyNode(node);
}
else
{
ClassifyToken(nodeOrToken.AsToken());
}
}
示例14: RewriteForStatement
private BoundStatement RewriteForStatement(
CSharpSyntaxNode syntax,
ImmutableArray<LocalSymbol> outerLocals,
BoundStatement rewrittenInitializer,
ImmutableArray<LocalSymbol> innerLocals,
BoundExpression rewrittenCondition,
SyntaxNodeOrToken conditionSyntax,
BoundStatement rewrittenIncrement,
BoundStatement rewrittenBody,
GeneratedLabelSymbol breakLabel,
GeneratedLabelSymbol continueLabel,
bool hasErrors)
{
Debug.Assert(rewrittenBody != null);
// The sequence point behavior exhibited here is different from that of the native compiler. In the native
// compiler, if you have something like
//
// for(int i = 0, j = 0; ; i++, j++)
// ^--------------^ ^------^
//
// then all the initializers are treated as a single sequence point, as are
// all the loop incrementers.
//
// We now make each one individually a sequence point:
//
// for(int i = 0, j = 0; ; i++, j++)
// ^-------^ ^---^ ^-^ ^-^
//
// If we decide that we want to preserve the native compiler stepping behavior
// then we'll need to be a bit fancy here. The initializer and increment statements
// can contain lambdas whose bodies need to have sequence points inserted, so we
// need to make sure we visit the children. But we'll also need to make sure that
// we do not generate one sequence point for each statement in the initializers
// and the incrementers.
var statementBuilder = ArrayBuilder<BoundStatement>.GetInstance();
if (rewrittenInitializer != null)
{
statementBuilder.Add(rewrittenInitializer);
}
var startLabel = new GeneratedLabelSymbol("start");
if (!innerLocals.IsDefaultOrEmpty)
{
var walker = new AnyLocalCapturedInALambdaWalker(innerLocals);
if (walker.Analyze(rewrittenCondition) || walker.Analyze(rewrittenIncrement) || walker.Analyze(rewrittenBody))
{
// If any inner local is captured within a lambda, we need to enter scope-block
// always from the top, that is where an instance of a display class will be created.
// The IL will be less optimal, but this shouldn't be a problem, given presence of lambdas.
// for (initializer; condition; increment)
// body;
//
// becomes the following (with
// block added for locals)
//
// {
// initializer;
// start:
// {
// GotoIfFalse condition break;
// body;
// continue:
// increment;
// goto start;
// }
// break:
// }
// start:
statementBuilder.Add(new BoundLabelStatement(syntax, startLabel));
var blockBuilder = ArrayBuilder<BoundStatement>.GetInstance();
// GotoIfFalse condition break;
if (rewrittenCondition != null)
{
BoundStatement ifNotConditionGotoBreak = new BoundConditionalGoto(rewrittenCondition.Syntax, rewrittenCondition, false, breakLabel);
if (this.generateDebugInfo)
{
if (conditionSyntax.IsToken)
{
ifNotConditionGotoBreak = new BoundSequencePointWithSpan(syntax, ifNotConditionGotoBreak, conditionSyntax.Span);
}
else
{
ifNotConditionGotoBreak = new BoundSequencePoint((CSharpSyntaxNode)conditionSyntax.AsNode(), ifNotConditionGotoBreak);
}
}
blockBuilder.Add(ifNotConditionGotoBreak);
}
// body;
blockBuilder.Add(rewrittenBody);
//.........这里部分代码省略.........
示例15: RewriteForStatement
private BoundStatement RewriteForStatement(
SyntaxNode syntax,
ReadOnlyArray<LocalSymbol> locals,
BoundStatement rewrittenInitializer,
BoundExpression rewrittenCondition,
SyntaxNodeOrToken conditionSyntax,
BoundStatement rewrittenIncrement,
BoundStatement rewrittenBody,
GeneratedLabelSymbol breakLabel,
GeneratedLabelSymbol continueLabel,
bool hasErrors)
{
var startLabel = new GeneratedLabelSymbol("start");
var endLabel = new GeneratedLabelSymbol("end");
// for (initializer; condition; increment)
// body;
//
// becomes the following (with
// block added for locals)
//
// {
// initializer;
// goto end;
// start:
// body;
// continue:
// increment;
// end:
// GotoIfTrue condition start;
// break:
// }
// initializer;
// goto end;
var statementBuilder = ArrayBuilder<BoundStatement>.GetInstance();
if (rewrittenInitializer != null)
{
statementBuilder.Add(rewrittenInitializer);
}
//mark the initial jump as hidden.
//We do it to tell that this is not a part of previous statement.
//This jump may be a target of another jump (for example if loops are nested) and that will make
//impression of the previous statement being re-executed
var gotoEnd = new BoundSequencePoint(null, new BoundGotoStatement(syntax, endLabel));
statementBuilder.Add(gotoEnd);
// start:
// body;
statementBuilder.Add(new BoundLabelStatement(syntax, startLabel));
Debug.Assert(rewrittenBody != null);
statementBuilder.Add(rewrittenBody);
// continue:
// increment;
statementBuilder.Add(new BoundLabelStatement(syntax, continueLabel));
if (rewrittenIncrement != null)
{
statementBuilder.Add(rewrittenIncrement);
}
// end:
// GotoIfTrue condition start;
statementBuilder.Add(new BoundLabelStatement(syntax, endLabel));
BoundStatement branchBack = null;
if (rewrittenCondition != null)
{
branchBack = new BoundConditionalGoto(rewrittenCondition.Syntax, rewrittenCondition, true, startLabel);
}
else
{
branchBack = new BoundGotoStatement(syntax, startLabel);
}
if (this.generateDebugInfo)
{
if (conditionSyntax.IsToken)
{
branchBack = new BoundSequencePointWithSpan(syntax, branchBack, conditionSyntax.Span);
}
else
{
//if there is no condition, make this a hidden point so that
//it does not count as a part of previous statement
branchBack = new BoundSequencePoint(conditionSyntax.AsNode(), branchBack);
}
}
statementBuilder.Add(branchBack);
// break:
statementBuilder.Add(new BoundLabelStatement(syntax, breakLabel));
var statements = statementBuilder.ToReadOnlyAndFree();
return new BoundBlock(syntax, locals, statements, hasErrors);
}