如何忽略解析时注释掉的行?

Dar*_*don 4 rebol rebol3

我有一个PHP数组,我正在解析以获取电子邮件地址.有时我会想要评论一个条目,所以我可以使用不同的值进行测试.

这是一个数组的例子:

array('system.email' => array(
    'to' => array(
        'contactus' => 'contactus@example.com',
        'newregistration' => 'newreg@example.com', 
        'requestaccess' => 'requestaccess@example.com',
//            'workflow' => 'workflow@example.com'
        'workflow' => 'test_workflow@example.com'
    )  
));
Run Code Online (Sandbox Code Playgroud)

这是我的PARSE规则:

parse read %config.php [
    thru "'system.email'" [
        thru "'to'" [thru "'workflow'" [thru "'" copy recipient-email to "'^/"]]
    ] to end
]
Run Code Online (Sandbox Code Playgroud)

当我运行它时,值为recipient-email"workflow@example.com".如何编写我的规则,使其忽略以//?开头的行?

kea*_*ist 5

吃掉评论专线的规则看起来像这样:

spacer: charset reduce [tab cr newline #" "]
spaces: [some spacer]
any-spaces: [any spacer]

[any-spaces "//" thru newline]
Run Code Online (Sandbox Code Playgroud)

您可以根据当前规则判断您希望如何做到这一点.这是一种有点混乱的方式来处理数组中的注释.

text: {array('system.email' => array(                                                      
    'to' => array(                                                                  
        'contactus' => 'contactus@example.com',                                     
        'newregistration' => 'newreg@example.com',                                  
        'requestaccess' => 'requestaccess@example.com',                             
//            'workflow' => 'workflow@example.com'                                  
        'workflow' => 'test_workflow@example.com'                                   
    )                                                                               
));}

list: []

spacer: charset reduce [tab cr newline #" "]
any-spaces: [any spacer]

comment-rule: [any-spaces "//" thru newline]

email-rule: [
    thru "'"
    copy name to "'" skip
    thru "'" 
    copy email to "'"
    thru newline
]

system-emails: [
    thru "'system.email'" [  
        thru "to' => array(" 
        some [
            comment-rule |
            email-rule (append list reduce [name email])
        ]    
    ] to end
]

parse text system-emails
print list
Run Code Online (Sandbox Code Playgroud)

这将导致数组中的名称和电子邮件块.

也许更全面的方法可以在解析之前处理源并删除所有注释.这是我过去使用过的一个功能:

decomment: func [str [string!] /local new-str com comment-removing-rule] [

    new-str: copy ""

    com: [
        "//"
        thru newline
    ]
    comment-removing-rule: [
        some [
            com |
            copy char skip (append new-str char)
        ]
    ]

    parse str comment-removing-rule
    return new-str
]
Run Code Online (Sandbox Code Playgroud)