summaryrefslogtreecommitdiffstats
path: root/paste/include/geshi/classes/php/class.geshiphpdoublestringcontext.php
blob: c7532ebdd32ba17cdc6fadb4979a6730e55db925 (plain) (blame)
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
<?php
/**
 * GeSHi - Generic Syntax Highlighter
 * 
 * For information on how to use GeSHi, please consult the documentation
 * found in the docs/ directory, or online at http://geshi.org/docs/
 * 
 *  This file is part of GeSHi.
 *
 *  GeSHi is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  GeSHi is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with GeSHi; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * You can view a copy of the GNU GPL in the COPYING file that comes
 * with GeSHi, in the docs/ directory.
 *
 * @package   lang
 * @author    Nigel McNie <nigel@geshi.org>
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL
 * @copyright (C) 2005 Nigel McNie
 * @version   1.1.0
 * 
 */

/**
 * The GeSHiPHPDoubleStringContext class represents a PHP double string
 * 
 * @package lang
 * @author  Nigel McNie <nigel@geshi.org>
 * @since   1.1.0
 * @version   1.1.0
 * @see     GeSHiStringContext, GeSHiContext
 */
class GeSHiPHPDoubleStringContext extends GeSHiStringContext
{
    /**
     * A cached copy of the parent name
     * @var string
     * @access private
     */
    var $_parentName;
    
    /**
     * The regular expressions used to match variables
     * in this context.
     * 
     * {@internal Do Not Change These! The code logic
     * depends on them, they are just assigned here so
     * that they aren't assigned every time the
     * _addParseData method is called}}
     * 
     * @var array
     * @access private
     */
    var $_regexes = array(
        'REGEX#(\{?\$\$?\{?[a-zA-Z_][a-zA-Z0-9_]*\}?)#',
        'REGEX#(\{?\$\$?\{?[a-zA-Z_][a-zA-Z0-9_]*\[[\$a-zA-Z0-9_\s\[\]\']*\]\}?)#',
        'REGEX#(\{?)(\$\$?\{?[a-zA-Z_][a-zA-Z0-9_]*)(\s*->\s*)([a-zA-Z_][a-zA-Z0-9_]*)(\}?)#'
    );
     
    /**
     * Loads data for a PHP Double String Context.
     * 
     * @var GeSHiStyler The styler to be used for this context 
     */
    function load (&$styler)
    {
        parent::load($styler);
        $this->_parentName = parent::getName();
    }
    
    /**
     * Adds code detected as being in this context to the parse data
     */    
    function _addParseData ($code, $first_char_of_next_context = '')
    {
        geshi_dbg('GeSHiPHPDoubleStringContext::_addParseData(' . substr($code, 0, 15) . '...)', GESHI_DBG_PARSE);

        while (true) {
            $earliest_data = array('pos' => false, 'len' => 0);
            foreach ($this->_regexes as $regex) {
                $data = geshi_get_position($code, $regex, 0, false, true); // request table
                if ((false != $data['pos'] && false === $earliest_data['pos']) ||
                    (false !== $data['pos']) &&
                    (($data['pos'] < $earliest_data['pos']) ||
                    ($data['pos'] == $earliest_data['pos'] && $data['len'] > $earliest_data['len']))) {
                    $earliest_data = $data;
                }
            }

            if (false === $earliest_data['pos']) {
                // No more variables in this string
                break;
            }
            
            // bugfix: because we match a var, it might have been escaped.
            // so only do to -1 so we can catch slash if it has been
            $pos = ($earliest_data['pos']) ? $earliest_data['pos'] - 1 : 0;
            $len = ($earliest_data['pos']) ? $earliest_data['len'] + 1 : $earliest_data['len'];
            parent::_addParseData(substr($code, 0, $pos));
            
            // Now the entire possible var is in:
            $possible_var = substr($code, $pos, $len);
            geshi_dbg('Found variable at position ' . $earliest_data['pos'] . '(' . $possible_var . ')', GESHI_DBG_PARSE);
            
            // Check that the dollar sign that started this variable was not escaped
            //$first_part = str_replace('\\\\', '', substr($code, 0, $pos));
            //if ('\\' == substr($first_part, -1)) {
            // If \\ before var and { is not next character after that...
            if ('\\' == substr($possible_var, 0, 1) && '{' != substr($possible_var, 1, 1)) {
                // This variable has been escaped, so add the escaped dollar sign
                // as the correct context, and the rest of the variable (recurse to catch
                // other variables inside this possible variable)
                geshi_dbg('Variable was escaped', GESHI_DBG_PARSE);
                $this->_styler->addParseData(substr($possible_var, 0, 2), $this->_parentName . '/esc');
                $this->_addParseData(substr($possible_var, 2));
            } else {
                // Add first character that might have been a \\ but in fact isn't to the parent
                // but only do it if we had to modify the position
                if ('$' != substr($possible_var, 0, 1)) {
                    parent::_addParseData(substr($possible_var, 0, 1));
                    $possible_var = substr($possible_var, 1);
                }
                
                // Many checks could go in here...
                // @todo [blocking 1.1.5] check for ${foo} variables: start { matched by end }
                // because at the moment ${foo is matched for example.
                if ('{' == substr($possible_var, 0, 1)) {
                    if ('}' == substr($possible_var, -1)) {
                        $start_brace = '{';
                    } else {
                        $start_brace = '';
                        parent::_addParseData('{');
                        // remove brace from $possible_var. This will only be used
                        // if the variable isn't an OO variable anyway...
                        $possible_var = substr($possible_var, 1);
                    }
                } else {
                    $start_brace = '';
                }

                if (isset($earliest_data['tab'][5])) {
                    // Then we matched off the third regex - the one that does objects
                    // The first { if there is one, and $this (which is in index 2
                    $this->_styler->addParseData($start_brace . $earliest_data['tab'][2], $this->_parentName . '/var');
                    // The -> with any whitespace around it
                    $this->_styler->addParseData($earliest_data['tab'][3], $this->_parentName . '/sym0');
                    // The method name
                    $this->_styler->addParseData($earliest_data['tab'][4], $this->_parentName . '/oodynamic');
                    // The closing }, if any
                    if ($earliest_data['tab'][5]) {
                        if ($start_brace) {
                            $this->_styler->addParseData($earliest_data['tab'][5], $this->_parentName . '/var');
                        } else {
                            parent::_addParseData('}');
                        }
                    } 
                } else {
                    $this->_styler->addParseData($possible_var, $this->_parentName . '/var');
                }
                
            }
            
            // Chop off what we have done
            $code = substr($code, $earliest_data['pos'] + $earliest_data['len']);
        }
        // Add the rest
        parent::_addParseData($code, $first_char_of_next_context); 
    }
}
   
?>