How do I ignore lines that are commented out when parsing?

126 Views Asked by At

I have a PHP array that I'm parsing to get email addresses. Sometimes I'll want to comment an entry so I can use a different value for testing.

Here's an example of the array:

array('system.email' => array(
    'to' => array(
        'contactus' => '[email protected]',
        'newregistration' => '[email protected]', 
        'requestaccess' => '[email protected]',
//            'workflow' => '[email protected]'
        'workflow' => '[email protected]'
    )  
));

Here's my PARSE rule:

parse read %config.php [
    thru "'system.email'" [
        thru "'to'" [thru "'workflow'" [thru "'" copy recipient-email to "'^/"]]
    ] to end
]

When I run that, the value of recipient-email is "[email protected]". How can I write my rule such that it ignores the line that begins with //?

1

There are 1 best solutions below

2
On BEST ANSWER

A rule for eating up a comment line would look something like this:

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

[any-spaces "//" thru newline]

You may judge how you want to do that with your current rule. Here's a somewhat messy way to deal with only comments in the array.

text: {array('system.email' => array(                                                      
    'to' => array(                                                                  
        'contactus' => '[email protected]',                                     
        'newregistration' => '[email protected]',                                  
        'requestaccess' => '[email protected]',                             
//            'workflow' => '[email protected]'                                  
        'workflow' => '[email protected]'                                   
    )                                                                               
));}

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

This will result in a block of the names and emails from the array.

Maybe a more holistic approach could process the source and remove all comments before parsing. This is a function I have used in the past:

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
]