本文整理汇总了C#中SyntaxToken.WithDiagnostic方法的典型用法代码示例。如果您正苦于以下问题:C# SyntaxToken.WithDiagnostic方法的具体用法?C# SyntaxToken.WithDiagnostic怎么用?C# SyntaxToken.WithDiagnostic使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SyntaxToken
的用法示例。
在下文中一共展示了SyntaxToken.WithDiagnostic方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TryExpandMacro
private bool TryExpandMacro(SyntaxToken token, IMacroExpansionLexer lexer, out List<SyntaxToken> expandedTokens)
{
expandedTokens = null;
// First, check if this token might be a macro.
DefineDirectiveTriviaSyntax directive;
if (_directives.IsDefined(token.Text, out directive) != DefineState.Defined)
return false;
// Check that this macro is not disabled.
if (_currentlyExpandingMacros.Contains(directive))
return false;
MacroReference macroReference;
List<SyntaxToken> macroBody;
SyntaxToken lastToken;
switch (directive.Kind)
{
case SyntaxKind.FunctionLikeDefineDirectiveTrivia:
// For function-like macros, check for, and expand, macro arguments.
var functionLikeDefine = (FunctionLikeDefineDirectiveTriviaSyntax) directive;
// ... check to see if the next token is an open paren.
if (lexer.Peek(_mode).Kind != SyntaxKind.OpenParenToken)
return false;
// If it is, then parse the macro arguments, and
// check that we have the correct number of arguments.
var macroArguments = new MacroArgumentsParser(lexer).ParseArgumentList();
if (macroArguments.Arguments.Count != functionLikeDefine.Parameters.Parameters.Count)
{
expandedTokens = new List<SyntaxToken>
{
token
.WithDiagnostic(Diagnostic.Format(token.Span, DiagnosticId.NotEnoughMacroParameters, token.Text))
.WithTrailingTrivia(new[] { macroArguments })
};
return true;
}
var functionLikeDefineDirective = (FunctionLikeDefineDirectiveTriviaSyntax) directive;
macroReference = new FunctionLikeMacroReference(token, macroArguments, functionLikeDefineDirective);
// Expand arguments.
var expandedArguments = macroArguments.Arguments
.Select(x => ExpandNestedMacro(new NestedMacroExpansionLexer(x.Tokens)).ToList())
.ToList();
// Replace parameters with possibly-expanded arguments.
macroBody = ReplaceParameters(expandedArguments, functionLikeDefineDirective);
lastToken = macroArguments.DescendantTokens().LastOrDefault(x => !x.IsMissing) ?? token;
break;
case SyntaxKind.ObjectLikeDefineDirectiveTrivia:
macroReference = new ObjectLikeMacroReference(token, (ObjectLikeDefineDirectiveTriviaSyntax) directive);
macroBody = directive.MacroBody;
lastToken = token;
break;
default:
throw new ArgumentOutOfRangeException();
}
// Push the current macro onto the stack - this prevents recursive expansion.
_currentlyExpandingMacros.Push(directive);
// Scan macro body for nested macros.
expandedTokens = ExpandNestedMacro(new NestedMacroExpansionLexer(macroBody));
var localExpandedTokens = expandedTokens;
expandedTokens = expandedTokens
.Select((x, i) =>
{
var result = x
.WithOriginalMacroReference(macroReference, i == 0)
.WithSpan(macroReference.SourceRange, macroReference.Span);
if (i == 0)
result = result.WithLeadingTrivia(token.LeadingTrivia);
if (i == localExpandedTokens.Count - 1)
result = result.WithTrailingTrivia(lastToken.TrailingTrivia);
return result;
})
.ToList();
_currentlyExpandingMacros.Pop();
return true;
}