7.3 Location Tracking 位置跟踪

https://lalrpop.github.io/lalrpop/location_tracking.html

Sometimes it may be helpful to know where in the input stream a particular token was encountered. For example, this can be useful when generating user-visible messages that reference specific points in the input.

MST -- 有时,了解在 input 流中遇到特定标记的位置可能会有所帮助。例如,在生成引用输入中特定点的用户可见消息时,这可能很有用。

GPT -- “有时,了解在输入流中遇到特定令牌的位置可能会很有帮助。例如,这在生成引用输入中特定位置的用户可见消息时非常有用。”

This is achieved via the location tracking macros, @L and @R. @L binds the start position - in bytes - of the token immediately to its right in the input stream. Similarly, @R binds the index of the last byte plus one of the token immediately to its left. Both symbols bind as type usize

MST -- 这是通过位置跟踪宏 @L 和 @R 实现的。@L 将令牌的起始位置(以字节为单位)绑定到输入流中的右侧。同样,@R 将最后一个字节的索引加上紧邻其左侧的标记之一绑定。两个符号都绑定为 usize 类型

GPT -- “这是通过位置跟踪宏 @L@R 实现的。@L 绑定输入流中紧随其后的令牌的起始位置(以字节为单位)。类似地,@R 绑定输入流中紧随其前的令牌的最后一个字节索引加一的位置。这两个符号的类型都是 usize。”

Here's an example rule using location tracking macros:

Symbol = {
    <start: @L> <s: r"a-z"> <end: @R> => {
        // `start` is the byte location of the start of our string
        // `s` is the string itself
        // `end` is the byte location of the end
    }
}

You can also see another example in this test, where location tracking is wrapped in a macro.

MST -- 您还可以在此测试中看到另一个示例,其中位置跟踪包装在宏中。

GPT -- “你还可以在这个测试中看到另一个示例,其中位置跟踪被包装在一个宏中。”

Since @R2 binds to the index of the byte immediately to the right of the token, this makes the bound values suitable to immediately construct a Range which is bounded inclusively above and exclusively below:

MST -- 由于 @R2 绑定到紧邻令牌右侧的字节索引,这使得绑定值适合立即构造一个 Range,该范围包括上方和下方:

GPT -- “由于 @R2 绑定的是令牌右侧紧邻的字节的索引,这使得绑定的值适合直接构造一个范围,该范围在上界是包含的,在下界是排除的。”

Symbol: Range<usize> = {
    <start: @L> <s: r"a-z"> <end: @R> => {
        start..end
    }
}

posted on 2025-01-05 19:55  及途又八  阅读(41)  评论(0)    收藏  举报

导航