如何匹配Lark中的开始和结束标记?

2024-05-12 14:32:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试为WordPress shortcodes使用Lark创建一个解析器。该语言中的自动结束标记与标准的开始标记没有区别,即使在完全有效的语法中也会造成相当多的歧义。我已经让它大部分工作,但我挣扎着匹配的开始和结束标签彼此

以下示例包含具有纯文本内容的自动关闭标记[a]和标记[b][/b]:

[a][b] content [/b]

我的语言定义的相关部分如下所示:

shortcode: shortcode_template{shortcode_name, attribute_list} | "[" shortcode_name attribute_list "]"
shortcode_template{name, attrs}: "[" name attrs "]" value "[/" name "]"
shortcode_name: /[^\[\]\<\>\&\/\s]+/

我期望为开始/结束标记变量使用模板会做一些类似于正则表达式组逻辑的事情,例如"[" (name) attrs "]" value "[/" $1 "]",但这似乎只是将te解包到"[" shortcode_name attribute_list "]" value "[/" shortcode_name "]",导致上面的文本被解析为:

value
  shortcode
    shortcode_template
      shortcode_name    a
      attribute_list
      value
        shortcode
          shortcode_name    b
          attribute_list
      shortcode_name    b

是否有选项告诉Lark开始标记中的名称值应与结束标记中的名称值相同


Tags: name标记文本名称语言解析器valuewordpress