如何使用 QoreProgramLocation class 来识别 Qore 源代码中的特定位置

How to use QoreProgramLocation class to identify particular location in Qore source code

我需要获取与文件中特定位置相关的语句(i.e.instance of AbstractStatement class)(例如 foo.q:150 ) 所以我以某种方式处理 parent class (struct) QoreProgramLocation 它定义了两个成员 filesource (当后者通常为 null 时)。目前尚不清楚有什么区别。我怀疑当包含文件时可能会使用它来指向 include 中的原始文件和实际位置。

还有offset个成员。它是否适用于 Qore 嵌入另一个程序时使用的部分(例如 Qorus)?

struct QoreProgramLocation : public QoreProgramLineLocation {
public:
   const char* file;
   const char* source;
   int offset;
...
}

这是一个很好的问题。

QoreProgramLocation中成员如下:

  • file:指解析代码时给出的标签
  • source:指源代码的实际文件名或路径(如果标签没有提供此信息)
  • offset:代码在source
  • 中的偏移量

所以sourceoffset只有在同一个文件中定义了多个代码对象时才给出。

如果一个文件一个代码对象,则file为集合,sourcenullptroffset为0,行号为直接取自 start_lineend_line.

如果一个文件中有多个代码对象,则所有成员都被赋值,这种情况下start_lineend_line指的是代码对象中的行号,行号在文件中是通过添加 offset 来计算的,给出 source.

内的行号偏移量

例如,在以下 Qore 方法的文档中:

描述了这个案例;请注意 label 将在 QoreProgramLocation.

中设置为 file

因为 offset 在没有 source 时始终为 0,您始终可以通过使用:start_line + offsetend_line + offset.[=35 得出实际行号=]

我希望这是清楚的!