source: trunk/classes/phing/filters/StripPhpComments.php @ 1

Revision 1, 7.0 KB checked in by hans, 5 years ago (diff)

Initial checkin

Line 
1<?php
2
3/*
4 *  $Id: StripPhpComments.php,v 1.6 2004/07/16 01:36:35 hlellelid Exp $
5 *
6 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
7 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
8 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
9 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
10 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
11 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
12 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
13 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
14 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
15 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
16 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
17 *
18 * This software consists of voluntary contributions made by many individuals
19 * and is licensed under the LGPL. For more information please see
20 * <http://phing.info>.
21*/
22
23include_once 'phing/filters/BaseFilterReader.php';
24include_once 'phing/filters/ChainableReader.php';
25
26/**
27 * This is a Php comment and string stripper reader that filters
28 * those lexical tokens out for purposes of simple Php parsing.
29 * (if you have more complex Php parsing needs, use a real lexer).
30 * Since this class heavily relies on the single char read function,
31 * you are reccomended to make it work on top of a buffered reader.
32 *
33 * @author    <a href="mailto:yl@seasonfive.com">Yannick Lecaillez</a>
34 * @author    hans lellelid, hans@velum.net
35 * @version   $Revision: 1.6 $ $Date: 2004/07/16 01:36:35 $
36 * @access    public
37 * @see       FilterReader
38 * @package   phing.filters
39 * @todo -c use new PHP functions to perform this instead of regex.
40 */
41class StripPhpComments extends BaseFilterReader implements ChainableReader {
42    /**
43     * The read-ahead character, used for effectively pushing a single
44     * character back. -1 indicates that no character is in the buffer.
45     */
46    private $_readAheadCh = -1;
47
48    /**
49     * Whether or not the parser is currently in the middle of a string
50     * literal.
51     * @var boolean
52     */
53    private $_inString = false;   
54
55    /**
56     * Returns the  stream without Php comments.
57     *
58     * @return the resulting stream, or -1
59     *         if the end of the resulting stream has been reached
60     *
61     * @throws IOException if the underlying stream throws an IOException
62     *                        during reading     
63     */
64    function read($len = null) {
65   
66        $buffer = $this->in->read($len);
67        if($buffer === -1) {
68            return -1;
69        }
70       
71        // This regex replace /* */ and // style comments
72        $buffer = preg_replace('/\/\*[^*]*\*+([^\/*][^*]*\*+)*\/|\/\/[^\n]*|("(\\\\.|[^"\\\\])*"|\'(\\\\.|[^\'\\\\])*\'|.[^\/"\'\\\\]*)/s', "$2", $buffer);
73               
74        // The regex above is not identical to, but is based on the expression below:
75        //
76        // created by Jeffrey Friedl
77        //   and later modified by Fred Curtis.
78        //     s{
79        //          /\*         ##  Start of /* ... */ comment
80        //          [^*]*\*+    ##  Non-* followed by 1-or-more *'s
81        //          (
82        //            [^/*][^*]*\*+
83        //          )*          ##  0-or-more things which don't start with /
84        //                      ##    but do end with '*'
85        //          /           ##  End of /* ... */ comment
86        //
87        //        |         ##     OR  various things which aren't comments:
88        //
89        //          (
90        //            "           ##  Start of " ... " string
91        //            (
92        //              \\.           ##  Escaped char
93        //            |               ##    OR
94        //              [^"\\]        ##  Non "\
95        //            )*
96        //           "           ##  End of " ... " string
97        //
98        //          |         ##     OR
99        //
100        //            '           ##  Start of ' ... ' string
101        //            (
102        //              \\.           ##  Escaped char
103        //            |               ##    OR
104        //              [^'\\]        ##  Non '\
105        //            )*
106        //            '           ##  End of ' ... ' string
107        //
108        //          |         ##     OR
109        //
110        //            .           ##  Anything other char
111        //            [^/"'\\]*   ##  Chars which doesn't start a comment, string or escape
112        //          )
113        //        }{$2}gxs;
114                               
115        return $buffer;
116    }
117       
118   
119    /*
120     * Returns the next character in the filtered stream, not including
121     * Php comments.
122     *
123     * @return the next character in the resulting stream, or -1
124     *         if the end of the resulting stream has been reached
125     *
126     * @throws IOException if the underlying stream throws an IOException
127     *                        during reading     
128     * @deprecated
129     */
130    function readChar() {
131        $ch = -1;
132
133        if ( $this->_readAheadCh !== -1 ) {
134            $ch = $this->_readAheadCh;
135            $this->_readAheadCh = -1;
136        } else {
137            $ch = $this->in->readChar();
138            if ( $ch === "\"" ) {
139                $this->_inString = !$this->_inString;
140            } else {
141                if ( !$this->_inString ) {
142                    if ( $ch === "/" ) {
143                        $ch = $this->in->readChar();
144                        if ( $ch === "/" ) {
145                            while ( $ch !== "\n" && $ch !== -1 ) {
146                                $ch = $this->in->readChar();
147                            }
148                        } else if ( $ch === "*" ) {
149                            while ( $ch !== -1 ) {
150                                $ch = $this->in->readChar();
151                                while ( $ch === "*" && $ch !== -1 ) {
152                                    $ch = $this->in->readChar();
153                                }
154
155                                if ( $ch === "/" ) {
156                                    $ch = $this->readChar();
157                                    echo "$ch\n";
158                                    break;
159                                }
160                            }
161                        } else {
162                            $this->_readAheadCh = $ch;
163                            $ch = "/";
164                        }
165                    }
166                }
167            }
168        }
169
170        return $ch;
171    }
172
173    /**
174     * Creates a new StripJavaComments using the passed in
175     * Reader for instantiation.
176     *
177     * @param reader A Reader object providing the underlying stream.
178     *               Must not be <code>null</code>.
179     *
180     * @return a new filter based on this configuration, but filtering
181     *         the specified reader
182     */
183    function chain(Reader $reader) {
184        $newFilter = new StripPhpComments($reader);
185        $newFilter->setProject($this->getProject());       
186        return $newFilter;
187    }
188}
189
190?>
Note: See TracBrowser for help on using the repository browser.