首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用ruby的treetop peg解析debian Packages.gz

使用ruby的treetop peg解析debian Packages.gz
EN

Stack Overflow用户
提问于 2010-11-09 10:39:21
回答 1查看 370关注 0票数 1

下面是我的树顶语法:

代码语言:javascript
复制
grammar Debian
  rule collection
    entry+
  end
  rule entry
    (tag space value)
  end

  rule package_details
    tag value &[^$]
  end
  rule tag 
    [A-Za-z0-9\-]+ ":"
  end
  rule value
    (!tag value_line+ "\n")+
  end
  rule value_line
    ([A-Za-z0-9 <>@()=\.\-|/,_"':])+
  end
  rule space
    [ \t]+
  end
end

下面是我的示例输入:

代码语言:javascript
复制
Package: acct
Priority: optional
Section: admin
Installed-Size: 352
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Original-Maintainer: Mathieu Trudel <mathieu.tl@gmail.com>
Architecture: i386
Version: 6.5.4-2ubuntu1
Depends: dpkg (>= 1.15.4) | install-info, libc6 (>= 2.4)
Filename: pool/main/a/acct/acct_6.5.4-2ubuntu1_i386.deb
Size: 111226
MD5sum: 10cba1458ace8c31169c0e9e915c9a0f
SHA1: 6c2dcdc480144a9922329cd4fa22c7d1cb83fcbb
SHA256: bf8d8bb8eef3939786a1cefc39f94079f43464b71099f4a59b61b24cafdbc010
Description: The GNU Accounting utilities for process and login accounting
 GNU Accounting Utilities is a set of utilities which reports and summarizes
 data about user connect times and process execution statistics.
 .
 "Login accounting" provides summaries of system resource usage based on connect
 time, and "process accounting" provides summaries based on the commands
 executed on the system.
 .
 The 'last' command is provided by the sysvinit package and not included here.
Homepage: http://www.gnu.org/software/acct/
Bugs: https://bugs.launchpad.net/ubuntu/+filebug
Origin: Ubuntu
Supported: 18m

这几乎100%有效,但在检查url时失败。

这是我的第一个PEG语法,但我可以告诉你,我需要让它更清晰/更简洁。浏览一下高级文档,我想将标记定义为

代码语言:javascript
复制
rule tag
  !(!'\n' .) [A-Za-z0-9\-]+ ":"
end

但我并不完全理解它在做什么。其中的微妙之处我想不起来了。

切换到这种格式会对我有帮助吗?有人知道为什么不匹配吗?

EN

回答 1

Stack Overflow用户

发布于 2010-11-11 03:19:26

代码语言:javascript
复制
grammar Debian
  # The file is too big for us to emit a package_list. Look at parser.rb to see how I just split the string.
  #rule package_list
  #  (package "\n"?)+ <DebianSyntaxNode::PackageList>
  #end
  rule package
    (tag / value)+ <DebianSyntaxNode::Package>
  end

  rule tag
    tag_value tag_stop <DebianSyntaxNode::Tag>
  end
  rule tag_value
    [\w\-]+ <DebianSyntaxNode::TagValue>
  end
  rule tag_stop
    ": " <DebianSyntaxNode::TagStop>
  end

  rule value
    value_line value_stop <DebianSyntaxNode::Value>
    # value_line value_stop <DebianSyntaxNode::Value>
  end
  rule value_line
    (!"\n" .)+ <DebianSyntaxNode::ValueLine>
    # ([\w \. " , \- ' : / < > @ ( ) = | \[ \] + ; ~ í á * % `])+ <DebianSyntaxNode::ValueLine>
  end
  rule value_stop
    "\n"? <DebianSyntaxNode::ValueStop>
  end
end

问题是现在当value_line是一个多行条目时,它不包括"\n“。

如果您想了解这段代码的发展方向,请查看我启动的github小项目:https://github.com/derdewey/Debian-Packages-Parser

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4130022

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档