代码之家  ›  专栏  ›  技术社区  ›  FriendFX

正确使用SkipTo

  •  1
  • FriendFX  · 技术社区  · 7 年前

    我以前使用过pyparsing,但只用于小任务,这次我尝试将其用于更复杂的任务。

    我想跳过一个VHDL架构块,它看起来像这样

    architecture Behav of Counter is
      ...many statements I'm not interested in at this point...
    end architecture;
    

    以下是我尝试的:

    import pyparsing as pp
    pp_identifier = pp.Regex(r'([a-zA-Z_][\w]*)')('identifier')
    def Keyword(matchString):
        'VHDL keywords are caseless and consist only of alphas'
        return pp.Keyword(matchString, identChars=pp.alphas, caseless=True)
    
    pp_architecture = (
        Keyword('architecture')
        + pp_identifier
        + Keyword('of').suppress()
        + pp_identifier
        + Keyword('is').suppress()
        + Keyword('end')
        + Keyword('architecture')
        )
    
    print(pp_architecture.parseString('''
    architecture beh of sram is end architecture
    ''', parseAll=True))
    
    # this works as I expected, it prints
    # ['architecture', 'beh', 'sram', 'end', 'architecture']
    

    pp_architecture 使用 SkipTo 它失败了:

    pp_architecture = (
        Keyword('architecture')
        + pp_identifier
        + Keyword('of').suppress()
        + pp_identifier
        + Keyword('is').suppress()
        + pp.SkipTo(Keyword('end') + Keyword('architecture'))
        )
    
    print(pp_architecture.parseString('''
    architecture beh of sram is end architecture
    ''', parseAll=True))
    Traceback (most recent call last):
      File "<stdin>", line 3, in <module>
      File "C:\Python27\lib\site-packages\pyparsing.py", line 1125, in parseString
        raise exc
    pyparsing.ParseException: Expected end of text (at char 29), (line:2, col:29)
    

    我还尝试在两个文本之间添加其他文本 is end (我希望被跳过)确保没有“空跳过”的问题,但这也没有帮助。

    1 回复  |  直到 7 年前
        1
  •  1
  •   PaulMcG    7 年前

    SkipTo 跳转到匹配的文本,但默认情况下会跳转到匹配的文本

    您可以:

    1. 添加 Keyword('end') + Keyword('architecture') 斯基普托
    2. 通过 include=True 在你的 表情,让它跳到