summaryrefslogtreecommitdiff
path: root/buildscripts/phing/classes/phing/filters/StripPhpComments.php
blob: 0abb8a6751c5eb5cfebbf18d70b4f7b64aff4cf5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php

/*
 *  $Id: 6c68ae0ad1aa7f2f7825087c1c54233bd2462124 $
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the LGPL. For more information please see
 * <http://phing.info>.
*/

include_once 'phing/filters/BaseFilterReader.php';
include_once 'phing/filters/ChainableReader.php';

/**
 * This is a Php comment and string stripper reader that filters
 * those lexical tokens out for purposes of simple Php parsing.
 * (if you have more complex Php parsing needs, use a real lexer).
 * Since this class heavily relies on the single char read function,
 * you are reccomended to make it work on top of a buffered reader.
 *
 * @author    <a href="mailto:yl@seasonfive.com">Yannick Lecaillez</a>
 * @author    hans lellelid, hans@velum.net
 * @version   $Id$
 * @access    public
 * @see       FilterReader
 * @package   phing.filters
 */
class StripPhpComments extends BaseFilterReader implements ChainableReader {
    /**
     * The read-ahead character, used for effectively pushing a single
     * character back. -1 indicates that no character is in the buffer.
     */
    private $_readAheadCh = -1;

    /**
     * Whether or not the parser is currently in the middle of a string
     * literal.
     * @var boolean
     */
    private $_inString = false;    

    /**
     * Returns the  stream without Php comments.
     * 
     * @return the resulting stream, or -1
     *         if the end of the resulting stream has been reached
     * 
     * @throws IOException if the underlying stream throws an IOException
     *                        during reading     
     */
    function read($len = null) {
    
        $buffer = $this->in->read($len);
        if($buffer === -1) {
            return -1;
        }
        
        // This regex replace /* */ and // style comments
        $buffer = preg_replace('/\/\*[^*]*\*+([^\/*][^*]*\*+)*\/|\/\/[^\n]*|("(\\\\.|[^"\\\\])*"|\'(\\\\.|[^\'\\\\])*\'|.[^\/"\'\\\\]*)/s', "$2", $buffer);
                
        // The regex above is not identical to, but is based on the expression below:
        //
        // created by Jeffrey Friedl
        //   and later modified by Fred Curtis.
        //     s{
        //          /\*         ##  Start of /* ... */ comment
        //          [^*]*\*+    ##  Non-* followed by 1-or-more *'s
        //          (
        //            [^/*][^*]*\*+
        //          )*          ##  0-or-more things which don't start with /
        //                      ##    but do end with '*'
        //          /           ##  End of /* ... */ comment
        //
        //        |         ##     OR  various things which aren't comments:
        //
        //          (
        //            "           ##  Start of " ... " string
        //            (
        //              \\.           ##  Escaped char
        //            |               ##    OR
        //              [^"\\]        ##  Non "\
        //            )*
        //           "           ##  End of " ... " string
        //
        //          |         ##     OR
        //
        //            '           ##  Start of ' ... ' string
        //            (
        //              \\.           ##  Escaped char
        //            |               ##    OR
        //              [^'\\]        ##  Non '\
        //            )*
        //            '           ##  End of ' ... ' string
        //
        //          |         ##     OR
        //
        //            .           ##  Anything other char
        //            [^/"'\\]*   ##  Chars which doesn't start a comment, string or escape
        //          )
        //        }{$2}gxs;
                                
        return $buffer;
    }
        
    
    /*
     * Returns the next character in the filtered stream, not including
     * Php comments.
     * 
     * @return the next character in the resulting stream, or -1
     *         if the end of the resulting stream has been reached
     * 
     * @throws IOException if the underlying stream throws an IOException
     *                        during reading     
     * @deprecated
     */
    function readChar() {
        $ch = -1;

        if ( $this->_readAheadCh !== -1 ) {
            $ch = $this->_readAheadCh;
            $this->_readAheadCh = -1;
        } else {
            $ch = $this->in->readChar();
            if ( $ch === "\"" ) {
                $this->_inString = !$this->_inString;
            } else {
                if ( !$this->_inString ) {
                    if ( $ch === "/" ) {
                        $ch = $this->in->readChar();
                        if ( $ch === "/" ) {
                            while ( $ch !== "\n" && $ch !== -1 ) {
                                $ch = $this->in->readChar();
                            }
                        } else if ( $ch === "*" ) {
                            while ( $ch !== -1 ) {
                                $ch = $this->in->readChar();
                                while ( $ch === "*" && $ch !== -1 ) {
                                    $ch = $this->in->readChar();
                                }

                                if ( $ch === "/" ) {
                                    $ch = $this->readChar();
                                    echo "$ch\n";
                                    break;
                                }
                            }
                        } else {
                            $this->_readAheadCh = $ch;
                            $ch = "/";
                        }
                    }
                }
            }
        }

        return $ch;
    }

    /**
     * Creates a new StripPhpComments using the passed in
     * Reader for instantiation.
     * 
     * @param reader A Reader object providing the underlying stream.
     *               Must not be <code>null</code>.
     * 
     * @return a new filter based on this configuration, but filtering
     *         the specified reader
     */
    function chain(Reader $reader) {
        $newFilter = new StripPhpComments($reader);
        $newFilter->setProject($this->getProject());        
        return $newFilter;
    }
}