<p>I'm interested in building a light-weight markup language, and sending error messages to the user, with the filename:line:column.</p>
<p>What library that provides this function, or where is the source code to take advantage of it?</p>
<hr/>**评论:**<br/><br/>TheMerovius: <pre><p>I am not quite sure, what you are asking.</p>
<p>If you want to know how to get the positional information of where a function was called to output them (like e.g. <code>(*testing.T).Logf</code> does), the answer is <a href="https://godoc.org/runtime#Caller" rel="nofollow">runtime.Caller</a>. Example: <a href="https://play.golang.org/p/ae5lIK7DguU" rel="nofollow">https://play.golang.org/p/ae5lIK7DguU</a></p>
<p>If you want to read a file and record positional informations of tokens and the like, you may be able to reuse <a href="https://godoc.org/go/token#FileSet" rel="nofollow">token.FileSet</a>, which is used by the <code>go/</code> packages. It essentially implements a mapping <code>int->(filename, line, column)</code>. Example: <a href="https://play.golang.org/p/TkVL_mt1Xz-" rel="nofollow">https://play.golang.org/p/TkVL_mt1Xz-</a></p>
<p>Note, however, that the last examples cheats, as it uses bufio.Scanner to split into words, but then <em>assumes</em> that there is a single space between each word to keep track of the position the word is at. You likely have to write your own bufio.SplitFunc to keep track of the position consistently.</p>
<p>The <a href="https://godoc.org/go/parser" rel="nofollow">go/parser</a> might also provide inspiration of how to use the <code>go/token</code> package correctly.</p>
<p>Hope this helps?</p></pre>nasciiboy: <pre><p>wonderful!</p>
<p>The former look like internal and debugging tools, but the latter are similar to what I want. although it looks like I'll have to implement a custom library</p>
<p>I've been looking around a bit and it looks like they're implementing something in the code for future versions, for example the <a href="https://godoc.org/fmt#ScanState" rel="nofollow">fmt.ScanState</a> interface that covers the previously available <a href="https://godoc.org/text/scanner" rel="nofollow">text/scanner</a>, only lacks one method to record the position (and the origin)</p>
<p>really thanks for the guide!</p></pre>nasciiboy: <pre><p>(<a href="https://play.golang.org/p/TkVL_mt1Xz-" rel="nofollow">https://play.golang.org/p/TkVL_mt1Xz-</a>) I find it a little confusing how the Scanner relates with Fileset</p>
<p>there is a dance between the text of the <code>Scanner.Scan.Text()</code>, the <code>Position</code> of token and the <code>word</code> structure</p></pre>TheMerovius: <pre><blockquote>
<p>I find it a little confusing how the Scanner relates with Fileset</p>
</blockquote>
<p>Yes. That's what I mean when I say I'm cheating here. You probably want to write your own <code>ScanFunc</code> that tokenizes as you want and keeps track of the position in the file you're in (and then adds that to the <code>(*FileSet).Base()</code> to get the final <code>token.Pos</code> of the token).</p>
<p>As we say in mathematics "that is left as an exercise for the reader" :)</p></pre>nasciiboy: <pre><p>ha, ha, ha, home work</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传