2010-11-09 36 views
2

我试图打破一个Packages.gz使用Ruby的树梢,我有困难使关键字和值明确。这里是我的树梢语法:使用红宝石的树梢钉解析debian Packages.gz

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 

这是我的样品输入:

Package: acct 
Priority: optional 
Section: admin 
Installed-Size: 352 
Maintainer: Ubuntu Developers <[email protected]> 
Original-Maintainer: Mathieu Trudel <[email protected]> 
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语法,但我可以告诉我需要使它不那么含糊/更简洁。看看先进的文档,我想定义一个标签为

rule tag 
    !(!'\n' .) [A-Za-z0-9\-]+ ":" 
end 

但我不完全理解它在做什么。这个标签一定不能(没有新的一行或任何东西),我认为(无论是换行还是没有)。细微之处让我想起...

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

回答

1

我似乎已经得到了一个工作的语法在这一点上:

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