简体   繁体   English

如何将 map C# 编译器错误位置(行、列)放到 Roslyn API 生成的 SyntaxTree 上?

[英]How to map C# compiler error location (line, column) onto the SyntaxTree produced by Roslyn API?

So:所以:

  • The C# compiler outputs the (line,column) style location. C# 编译器输出(行、列)样式位置。
  • The Roslyn API expects sequential text location Roslyn API 需要顺序文本位置

How to map the former to the latter? map如何将前者转为后者?

The C# code could be UTF8 with or without the BOM or even UTF16. C# 代码可以是带或不带 BOM 的 UTF8,甚至可以是 UTF16。 It could contain all kinds of characters in the form of comments or embedded strings.它可以包含注释或嵌入字符串形式的各种字符。

Let us assume we know the encoding and have the respective Encoding object handy.让我们假设我们知道编码并且有相应的Encoding object 方便。 I can convert the file bytes to char[] .我可以将文件字节转换为char[] The problem is that some chars may contribute zero to the final sequential position.问题是某些字符可能对最终的顺序 position 贡献为零。 I know that the BOM character does.我知道 BOM 字符可以。 I have no idea if others may too.我不知道其他人是否也可以。

Now, if we know for sure that BOM is the only character that contributes 0 to the length, then I can skip it and count the characters and my question becomes trivial.现在,如果我们确定 BOM 是唯一对长度贡献为 0 的字符,那么我可以跳过它并计算字符数,我的问题就变得微不足道了。 This is what I do today - I just assume that the BOM is the only "bad" player.这就是我今天所做的——我只是假设 BOM 是唯一的“坏”玩家。

But maybe there is a better way?但也许有更好的方法? Maybe Roslyn API contains some hidden gem that knows for a change to accept (line,column) and spit the sequential position?也许 Roslyn API 包含一些隐藏的宝石,它们知道接受(行、列)的更改并吐出顺序的 position? Or maybe some of the Microsoft.Build libraries?或者也许是一些Microsoft.Build库?

EDIT 1编辑 1

As per the accepted answer the following gives the location:根据接受的答案,以下给出了位置:

var srcText = SourceText.From(File.ReadAllText(err.FilePath));
int location = srcText.Lines[err.Line - 1].Start + err.Column - 1;

You have uncovered the reason that the SourceText type exists in the roslyn apis.您已经发现了 roslyn api 中存在SourceText类型的原因。 Its entire purpose is to handle encoding of strings and preform calculations of lines, columns, and spans.它的全部目的是处理字符串的编码和行、列和跨度的预计算。

Due to the way .NET handles unicode and depending on which code pages are installed in your OS there could be cases that SourceText does not do what you need.由于 .NET 处理 unicode 的方式,并且根据您的操作系统中安装的代码页, SourceText可能无法满足您的需求。 It has generally proven "good enough" for our purposes though.不过,对于我们的目的,它通常已被证明“足够好”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM