Hatena::Groupmoz-addon

Ci.nsIZIGOROu

2007-08-22

chrome.manifestのパーサーを作るぞぃ (2)

| 18:10 |  chrome.manifestのパーサーを作るぞぃ (2) - Ci.nsIZIGOROu を含むブックマーク はてなブックマーク -  chrome.manifestのパーサーを作るぞぃ (2) - Ci.nsIZIGOROu

Parse::RecDescentを使ったmanifest flagsのパースサンプル

#!/usr/bin/perl

use strict;
use warnings;

use Data::Dump qw(dump);
use Parse::RecDescent;

$::RD_AUTOACTION = q| my $prodname = shift @item; $return = { $prodname => \@item }; 1 |;

my $grammer = q{
        parse: flags(s)
        flags: application | appversion | platform | xpcnativewrappers
        application : <leftop: 'application' '=' appid>
        appid: /[a-zA-Z0-9.-_]+@[a-zA-Z0-9.\-_]+/ | /{[a-zA-Z0-9\-]+}/
        appversion: <leftop: 'appversion' operator version>
        version: /[0-9.]+/
        operator: '=' | '>=' | '<=' | '>' | '<'
        platform: 'platform'
        xpcnativewrappers: <leftop: 'xpcnativewrappers' '=' boolean>
        boolean: 'yes' | 'no'
};

my $parser = Parse::RecDescent->new($grammer);

my $text;
$text .= $_ while (<DATA>);

my $result = $parser->parse($text);

print dump($result);

__DATA__
application={ec8030f7-c20a-464f-9b0e-13a3a9e97384}
appversion=1.5.1
application=seamonkey@applications.mozilla.org
xpcnativewrappers=yes
appversion>=2.0.0
platform
xpcnativewrappers=no

うーん、結構面倒ですね。