- Roslyn Cookbook
- Manish Vasani
- 189字
- 2021-07-15 17:07:33
How it works...
Syntax tree analyzers register callbacks to analyze syntax of all source files in the compilation. Our analysis works by getting the roots of the syntax tree and then operating on all the descendant syntax nodes of the roots which are of type StatementSyntax. First, we note that a block statement is itself an aggregate statement, and by definition has curly braces, so we skip past these.
// Skip analyzing block statements.
if (statement is BlockSyntax)
{
continue;
}
We then perform syntactic checks for the parent of statement syntax. If the parent of the statement is also a statement, but not a block with curly braces, then we report a diagnostic on the first syntax token of the statement recommending usage of curly braces.
// Report issue for all statements that are nested within a statement,
// but not a block statement.
if (statement.Parent is StatementSyntax && !(statement.Parent is BlockSyntax))
{
var diagnostic = Diagnostic.Create(Rule, statement.GetFirstToken().GetLocation());
syntaxTreeContext.ReportDiagnostic(diagnostic);
}
SyntaxTreeAnalysisContext provided to syntax tree actions does not expose the semantic model for the source file, hence no semantic analysis can be performed within a syntax tree action.
推薦閱讀
- ClickHouse性能之巔:從架構設計解讀性能之謎
- 自己動手實現Lua:虛擬機、編譯器和標準庫
- Vue.js 2 and Bootstrap 4 Web Development
- Java FX應用開發教程
- 人臉識別原理及算法:動態人臉識別系統研究
- Mastering Google App Engine
- Unity 5 for Android Essentials
- Oracle 18c 必須掌握的新特性:管理與實戰
- Angular開發入門與實戰
- Learning jQuery(Fourth Edition)
- “笨辦法”學C語言
- 時空數據建模及其應用
- Docker on Windows
- 秒懂算法:用常識解讀數據結構與算法
- Java EE框架開發技術與案例教程