Chris@0
|
1 module CodeRay
|
Chris@0
|
2 module Scanners
|
Chris@0
|
3
|
Chris@0
|
4 load :html
|
Chris@0
|
5
|
Chris@0
|
6 # Original by Stefan Walk.
|
Chris@0
|
7 class PHP < Scanner
|
Chris@0
|
8
|
Chris@0
|
9 register_for :php
|
Chris@0
|
10 file_extension 'php'
|
Chris@0
|
11
|
Chris@0
|
12 KINDS_NOT_LOC = HTML::KINDS_NOT_LOC
|
Chris@0
|
13
|
Chris@0
|
14 def setup
|
Chris@0
|
15 @html_scanner = CodeRay.scanner :html, :tokens => @tokens, :keep_tokens => true, :keep_state => true
|
Chris@0
|
16 end
|
Chris@0
|
17
|
Chris@0
|
18 def reset_instance
|
Chris@0
|
19 super
|
Chris@0
|
20 @html_scanner.reset
|
Chris@0
|
21 end
|
Chris@0
|
22
|
Chris@0
|
23 module Words
|
Chris@0
|
24
|
Chris@0
|
25 # according to http://www.php.net/manual/en/reserved.keywords.php
|
Chris@0
|
26 KEYWORDS = %w[
|
Chris@0
|
27 abstract and array as break case catch class clone const continue declare default do else elseif
|
Chris@0
|
28 enddeclare endfor endforeach endif endswitch endwhile extends final for foreach function global
|
Chris@0
|
29 goto if implements interface instanceof namespace new or private protected public static switch
|
Chris@0
|
30 throw try use var while xor
|
Chris@0
|
31 cfunction old_function
|
Chris@0
|
32 ]
|
Chris@0
|
33
|
Chris@0
|
34 TYPES = %w[ int integer float double bool boolean string array object resource ]
|
Chris@0
|
35
|
Chris@0
|
36 LANGUAGE_CONSTRUCTS = %w[
|
Chris@0
|
37 die echo empty exit eval include include_once isset list
|
Chris@0
|
38 require require_once return print unset
|
Chris@0
|
39 ]
|
Chris@0
|
40
|
Chris@0
|
41 CLASSES = %w[ Directory stdClass __PHP_Incomplete_Class exception php_user_filter Closure ]
|
Chris@0
|
42
|
Chris@0
|
43 # according to http://php.net/quickref.php on 2009-04-21;
|
Chris@0
|
44 # all functions with _ excluded (module functions) and selected additional functions
|
Chris@0
|
45 BUILTIN_FUNCTIONS = %w[
|
Chris@0
|
46 abs acos acosh addcslashes addslashes aggregate array arsort ascii2ebcdic asin asinh asort assert atan atan2
|
Chris@0
|
47 atanh basename bcadd bccomp bcdiv bcmod bcmul bcpow bcpowmod bcscale bcsqrt bcsub bin2hex bindec
|
Chris@0
|
48 bindtextdomain bzclose bzcompress bzdecompress bzerrno bzerror bzerrstr bzflush bzopen bzread bzwrite
|
Chris@0
|
49 calculhmac ceil chdir checkdate checkdnsrr chgrp chmod chop chown chr chroot clearstatcache closedir closelog
|
Chris@0
|
50 compact constant copy cos cosh count crc32 crypt current date dcgettext dcngettext deaggregate decbin dechex
|
Chris@0
|
51 decoct define defined deg2rad delete dgettext die dirname diskfreespace dl dngettext doubleval each
|
Chris@0
|
52 ebcdic2ascii echo empty end ereg eregi escapeshellarg escapeshellcmd eval exec exit exp explode expm1 extract
|
Chris@0
|
53 fclose feof fflush fgetc fgetcsv fgets fgetss file fileatime filectime filegroup fileinode filemtime fileowner
|
Chris@0
|
54 fileperms filepro filesize filetype floatval flock floor flush fmod fnmatch fopen fpassthru fprintf fputcsv
|
Chris@0
|
55 fputs fread frenchtojd fscanf fseek fsockopen fstat ftell ftok ftruncate fwrite getallheaders getcwd getdate
|
Chris@0
|
56 getenv gethostbyaddr gethostbyname gethostbynamel getimagesize getlastmod getmxrr getmygid getmyinode getmypid
|
Chris@0
|
57 getmyuid getopt getprotobyname getprotobynumber getrandmax getrusage getservbyname getservbyport gettext
|
Chris@0
|
58 gettimeofday gettype glob gmdate gmmktime gmstrftime gregoriantojd gzclose gzcompress gzdecode gzdeflate
|
Chris@0
|
59 gzencode gzeof gzfile gzgetc gzgets gzgetss gzinflate gzopen gzpassthru gzputs gzread gzrewind gzseek gztell
|
Chris@0
|
60 gzuncompress gzwrite hash header hebrev hebrevc hexdec htmlentities htmlspecialchars hypot iconv idate
|
Chris@0
|
61 implode include intval ip2long iptcembed iptcparse isset
|
Chris@0
|
62 jddayofweek jdmonthname jdtofrench jdtogregorian jdtojewish jdtojulian jdtounix jewishtojd join jpeg2wbmp
|
Chris@0
|
63 juliantojd key krsort ksort lcfirst lchgrp lchown levenshtein link linkinfo list localeconv localtime log
|
Chris@0
|
64 log10 log1p long2ip lstat ltrim mail main max md5 metaphone mhash microtime min mkdir mktime msql natcasesort
|
Chris@0
|
65 natsort next ngettext nl2br nthmac octdec opendir openlog
|
Chris@0
|
66 ord overload pack passthru pathinfo pclose pfsockopen phpcredits phpinfo phpversion pi png2wbmp popen pos pow
|
Chris@0
|
67 prev print printf putenv quotemeta rad2deg rand range rawurldecode rawurlencode readdir readfile readgzfile
|
Chris@0
|
68 readline readlink realpath recode rename require reset rewind rewinddir rmdir round rsort rtrim scandir
|
Chris@0
|
69 serialize setcookie setlocale setrawcookie settype sha1 shuffle signeurlpaiement sin sinh sizeof sleep snmpget
|
Chris@0
|
70 snmpgetnext snmprealwalk snmpset snmpwalk snmpwalkoid sort soundex split spliti sprintf sqrt srand sscanf stat
|
Chris@0
|
71 strcasecmp strchr strcmp strcoll strcspn strftime stripcslashes stripos stripslashes stristr strlen
|
Chris@0
|
72 strnatcasecmp strnatcmp strncasecmp strncmp strpbrk strpos strptime strrchr strrev strripos strrpos strspn
|
Chris@0
|
73 strstr strtok strtolower strtotime strtoupper strtr strval substr symlink syslog system tan tanh tempnam
|
Chris@0
|
74 textdomain time tmpfile touch trim uasort ucfirst ucwords uksort umask uniqid unixtojd unlink unpack
|
Chris@0
|
75 unserialize unset urldecode urlencode usleep usort vfprintf virtual vprintf vsprintf wordwrap
|
Chris@0
|
76 array_change_key_case array_chunk array_combine array_count_values array_diff array_diff_assoc
|
Chris@0
|
77 array_diff_key array_diff_uassoc array_diff_ukey array_fill array_fill_keys array_filter array_flip
|
Chris@0
|
78 array_intersect array_intersect_assoc array_intersect_key array_intersect_uassoc array_intersect_ukey
|
Chris@0
|
79 array_key_exists array_keys array_map array_merge array_merge_recursive array_multisort array_pad
|
Chris@0
|
80 array_pop array_product array_push array_rand array_reduce array_reverse array_search array_shift
|
Chris@0
|
81 array_slice array_splice array_sum array_udiff array_udiff_assoc array_udiff_uassoc array_uintersect
|
Chris@0
|
82 array_uintersect_assoc array_uintersect_uassoc array_unique array_unshift array_values array_walk
|
Chris@0
|
83 array_walk_recursive
|
Chris@0
|
84 assert_options base_convert base64_decode base64_encode
|
Chris@0
|
85 chunk_split class_exists class_implements class_parents
|
Chris@0
|
86 count_chars debug_backtrace debug_print_backtrace debug_zval_dump
|
Chris@0
|
87 error_get_last error_log error_reporting extension_loaded
|
Chris@0
|
88 file_exists file_get_contents file_put_contents load_file
|
Chris@0
|
89 func_get_arg func_get_args func_num_args function_exists
|
Chris@0
|
90 get_browser get_called_class get_cfg_var get_class get_class_methods get_class_vars
|
Chris@0
|
91 get_current_user get_declared_classes get_declared_interfaces get_defined_constants
|
Chris@0
|
92 get_defined_functions get_defined_vars get_extension_funcs get_headers get_html_translation_table
|
Chris@0
|
93 get_include_path get_included_files get_loaded_extensions get_magic_quotes_gpc get_magic_quotes_runtime
|
Chris@0
|
94 get_meta_tags get_object_vars get_parent_class get_required_filesget_resource_type
|
Chris@0
|
95 gc_collect_cycles gc_disable gc_enable gc_enabled
|
Chris@0
|
96 halt_compiler headers_list headers_sent highlight_file highlight_string
|
Chris@0
|
97 html_entity_decode htmlspecialchars_decode
|
Chris@0
|
98 in_array include_once inclued_get_data
|
Chris@0
|
99 is_a is_array is_binary is_bool is_buffer is_callable is_dir is_double is_executable is_file is_finite
|
Chris@0
|
100 is_float is_infinite is_int is_integer is_link is_long is_nan is_null is_numeric is_object is_readable
|
Chris@0
|
101 is_real is_resource is_scalar is_soap_fault is_string is_subclass_of is_unicode is_uploaded_file
|
Chris@0
|
102 is_writable is_writeable
|
Chris@0
|
103 locale_get_default locale_set_default
|
Chris@0
|
104 number_format override_function parse_str parse_url
|
Chris@0
|
105 php_check_syntax php_ini_loaded_file php_ini_scanned_files php_logo_guid php_sapi_name
|
Chris@0
|
106 php_strip_whitespace php_uname
|
Chris@0
|
107 preg_filter preg_grep preg_last_error preg_match preg_match_all preg_quote preg_replace
|
Chris@0
|
108 preg_replace_callback preg_split print_r
|
Chris@0
|
109 require_once register_shutdown_function register_tick_function
|
Chris@0
|
110 set_error_handler set_exception_handler set_file_buffer set_include_path
|
Chris@0
|
111 set_magic_quotes_runtime set_time_limit shell_exec
|
Chris@0
|
112 str_getcsv str_ireplace str_pad str_repeat str_replace str_rot13 str_shuffle str_split str_word_count
|
Chris@0
|
113 strip_tags substr_compare substr_count substr_replace
|
Chris@0
|
114 time_nanosleep time_sleep_until
|
Chris@0
|
115 token_get_all token_name trigger_error
|
Chris@0
|
116 unregister_tick_function use_soap_error_handler user_error
|
Chris@0
|
117 utf8_decode utf8_encode var_dump var_export
|
Chris@0
|
118 version_compare
|
Chris@0
|
119 zend_logo_guid zend_thread_id zend_version
|
Chris@0
|
120 create_function call_user_func_array
|
Chris@0
|
121 posix_access posix_ctermid posix_get_last_error posix_getcwd posix_getegid
|
Chris@0
|
122 posix_geteuid posix_getgid posix_getgrgid posix_getgrnam posix_getgroups
|
Chris@0
|
123 posix_getlogin posix_getpgid posix_getpgrp posix_getpid posix_getppid
|
Chris@0
|
124 posix_getpwnam posix_getpwuid posix_getrlimit posix_getsid posix_getuid
|
Chris@0
|
125 posix_initgroups posix_isatty posix_kill posix_mkfifo posix_mknod
|
Chris@0
|
126 posix_setegid posix_seteuid posix_setgid posix_setpgid posix_setsid
|
Chris@0
|
127 posix_setuid posix_strerror posix_times posix_ttyname posix_uname
|
Chris@0
|
128 pcntl_alarm pcntl_exec pcntl_fork pcntl_getpriority pcntl_setpriority
|
Chris@0
|
129 pcntl_signal pcntl_signal_dispatch pcntl_sigprocmask pcntl_sigtimedwait
|
Chris@0
|
130 pcntl_sigwaitinfo pcntl_wait pcntl_waitpid pcntl_wexitstatus pcntl_wifexited
|
Chris@0
|
131 pcntl_wifsignaled pcntl_wifstopped pcntl_wstopsig pcntl_wtermsig
|
Chris@0
|
132 ]
|
Chris@0
|
133 # TODO: more built-in PHP functions?
|
Chris@0
|
134
|
Chris@0
|
135 EXCEPTIONS = %w[
|
Chris@0
|
136 E_ERROR E_WARNING E_PARSE E_NOTICE E_CORE_ERROR E_CORE_WARNING E_COMPILE_ERROR E_COMPILE_WARNING
|
Chris@0
|
137 E_USER_ERROR E_USER_WARNING E_USER_NOTICE E_DEPRECATED E_USER_DEPRECATED E_ALL E_STRICT
|
Chris@0
|
138 ]
|
Chris@0
|
139
|
Chris@0
|
140 CONSTANTS = %w[
|
Chris@0
|
141 null true false self parent
|
Chris@0
|
142 __LINE__ __DIR__ __FILE__ __LINE__
|
Chris@0
|
143 __CLASS__ __NAMESPACE__ __METHOD__ __FUNCTION__
|
Chris@0
|
144 PHP_VERSION PHP_MAJOR_VERSION PHP_MINOR_VERSION PHP_RELEASE_VERSION PHP_VERSION_ID PHP_EXTRA_VERSION PHP_ZTS
|
Chris@0
|
145 PHP_DEBUG PHP_MAXPATHLEN PHP_OS PHP_SAPI PHP_EOL PHP_INT_MAX PHP_INT_SIZE DEFAULT_INCLUDE_PATH
|
Chris@0
|
146 PEAR_INSTALL_DIR PEAR_EXTENSION_DIR PHP_EXTENSION_DIR PHP_PREFIX PHP_BINDIR PHP_LIBDIR PHP_DATADIR
|
Chris@0
|
147 PHP_SYSCONFDIR PHP_LOCALSTATEDIR PHP_CONFIG_FILE_PATH PHP_CONFIG_FILE_SCAN_DIR PHP_SHLIB_SUFFIX
|
Chris@0
|
148 PHP_OUTPUT_HANDLER_START PHP_OUTPUT_HANDLER_CONT PHP_OUTPUT_HANDLER_END
|
Chris@0
|
149 __COMPILER_HALT_OFFSET__
|
Chris@0
|
150 EXTR_OVERWRITE EXTR_SKIP EXTR_PREFIX_SAME EXTR_PREFIX_ALL EXTR_PREFIX_INVALID EXTR_PREFIX_IF_EXISTS
|
Chris@0
|
151 EXTR_IF_EXISTS SORT_ASC SORT_DESC SORT_REGULAR SORT_NUMERIC SORT_STRING CASE_LOWER CASE_UPPER COUNT_NORMAL
|
Chris@0
|
152 COUNT_RECURSIVE ASSERT_ACTIVE ASSERT_CALLBACK ASSERT_BAIL ASSERT_WARNING ASSERT_QUIET_EVAL CONNECTION_ABORTED
|
Chris@0
|
153 CONNECTION_NORMAL CONNECTION_TIMEOUT INI_USER INI_PERDIR INI_SYSTEM INI_ALL M_E M_LOG2E M_LOG10E M_LN2 M_LN10
|
Chris@0
|
154 M_PI M_PI_2 M_PI_4 M_1_PI M_2_PI M_2_SQRTPI M_SQRT2 M_SQRT1_2 CRYPT_SALT_LENGTH CRYPT_STD_DES CRYPT_EXT_DES
|
Chris@0
|
155 CRYPT_MD5 CRYPT_BLOWFISH DIRECTORY_SEPARATOR SEEK_SET SEEK_CUR SEEK_END LOCK_SH LOCK_EX LOCK_UN LOCK_NB
|
Chris@0
|
156 HTML_SPECIALCHARS HTML_ENTITIES ENT_COMPAT ENT_QUOTES ENT_NOQUOTES INFO_GENERAL INFO_CREDITS
|
Chris@0
|
157 INFO_CONFIGURATION INFO_MODULES INFO_ENVIRONMENT INFO_VARIABLES INFO_LICENSE INFO_ALL CREDITS_GROUP
|
Chris@0
|
158 CREDITS_GENERAL CREDITS_SAPI CREDITS_MODULES CREDITS_DOCS CREDITS_FULLPAGE CREDITS_QA CREDITS_ALL STR_PAD_LEFT
|
Chris@0
|
159 STR_PAD_RIGHT STR_PAD_BOTH PATHINFO_DIRNAME PATHINFO_BASENAME PATHINFO_EXTENSION PATH_SEPARATOR CHAR_MAX
|
Chris@0
|
160 LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_ALL LC_MESSAGES ABDAY_1 ABDAY_2 ABDAY_3 ABDAY_4 ABDAY_5
|
Chris@0
|
161 ABDAY_6 ABDAY_7 DAY_1 DAY_2 DAY_3 DAY_4 DAY_5 DAY_6 DAY_7 ABMON_1 ABMON_2 ABMON_3 ABMON_4 ABMON_5 ABMON_6
|
Chris@0
|
162 ABMON_7 ABMON_8 ABMON_9 ABMON_10 ABMON_11 ABMON_12 MON_1 MON_2 MON_3 MON_4 MON_5 MON_6 MON_7 MON_8 MON_9
|
Chris@0
|
163 MON_10 MON_11 MON_12 AM_STR PM_STR D_T_FMT D_FMT T_FMT T_FMT_AMPM ERA ERA_YEAR ERA_D_T_FMT ERA_D_FMT ERA_T_FMT
|
Chris@0
|
164 ALT_DIGITS INT_CURR_SYMBOL CURRENCY_SYMBOL CRNCYSTR MON_DECIMAL_POINT MON_THOUSANDS_SEP MON_GROUPING
|
Chris@0
|
165 POSITIVE_SIGN NEGATIVE_SIGN INT_FRAC_DIGITS FRAC_DIGITS P_CS_PRECEDES P_SEP_BY_SPACE N_CS_PRECEDES
|
Chris@0
|
166 N_SEP_BY_SPACE P_SIGN_POSN N_SIGN_POSN DECIMAL_POINT RADIXCHAR THOUSANDS_SEP THOUSEP GROUPING YESEXPR NOEXPR
|
Chris@0
|
167 YESSTR NOSTR CODESET LOG_EMERG LOG_ALERT LOG_CRIT LOG_ERR LOG_WARNING LOG_NOTICE LOG_INFO LOG_DEBUG LOG_KERN
|
Chris@0
|
168 LOG_USER LOG_MAIL LOG_DAEMON LOG_AUTH LOG_SYSLOG LOG_LPR LOG_NEWS LOG_UUCP LOG_CRON LOG_AUTHPRIV LOG_LOCAL0
|
Chris@0
|
169 LOG_LOCAL1 LOG_LOCAL2 LOG_LOCAL3 LOG_LOCAL4 LOG_LOCAL5 LOG_LOCAL6 LOG_LOCAL7 LOG_PID LOG_CONS LOG_ODELAY
|
Chris@0
|
170 LOG_NDELAY LOG_NOWAIT LOG_PERROR
|
Chris@0
|
171 ]
|
Chris@0
|
172
|
Chris@0
|
173 PREDEFINED = %w[
|
Chris@0
|
174 $GLOBALS $_SERVER $_GET $_POST $_FILES $_REQUEST $_SESSION $_ENV
|
Chris@0
|
175 $_COOKIE $php_errormsg $HTTP_RAW_POST_DATA $http_response_header
|
Chris@0
|
176 $argc $argv
|
Chris@0
|
177 ]
|
Chris@0
|
178
|
Chris@0
|
179 IDENT_KIND = CaseIgnoringWordList.new(:ident).
|
Chris@0
|
180 add(KEYWORDS, :reserved).
|
Chris@0
|
181 add(TYPES, :pre_type).
|
Chris@0
|
182 add(LANGUAGE_CONSTRUCTS, :reserved).
|
Chris@0
|
183 add(BUILTIN_FUNCTIONS, :predefined).
|
Chris@0
|
184 add(CLASSES, :pre_constant).
|
Chris@0
|
185 add(EXCEPTIONS, :exception).
|
Chris@0
|
186 add(CONSTANTS, :pre_constant)
|
Chris@0
|
187
|
Chris@0
|
188 VARIABLE_KIND = WordList.new(:local_variable).
|
Chris@0
|
189 add(PREDEFINED, :predefined)
|
Chris@0
|
190 end
|
Chris@0
|
191
|
Chris@0
|
192 module RE
|
Chris@0
|
193
|
Chris@0
|
194 PHP_START = /
|
Chris@0
|
195 <script\s+[^>]*?language\s*=\s*"php"[^>]*?> |
|
Chris@0
|
196 <script\s+[^>]*?language\s*=\s*'php'[^>]*?> |
|
Chris@0
|
197 <\?php\d? |
|
Chris@0
|
198 <\?(?!xml)
|
Chris@0
|
199 /xi
|
Chris@0
|
200
|
Chris@0
|
201 PHP_END = %r!
|
Chris@0
|
202 </script> |
|
Chris@0
|
203 \?>
|
Chris@0
|
204 !xi
|
Chris@0
|
205
|
Chris@0
|
206 HTML_INDICATOR = /<!DOCTYPE html|<(?:html|body|div|p)[> ]/i
|
Chris@0
|
207
|
Chris@0
|
208 IDENTIFIER = /[a-z_\x7f-\xFF][a-z0-9_\x7f-\xFF]*/i
|
Chris@0
|
209 VARIABLE = /\$#{IDENTIFIER}/
|
Chris@0
|
210
|
Chris@0
|
211 OPERATOR = /
|
Chris@0
|
212 \.(?!\d)=? | # dot that is not decimal point, string concatenation
|
Chris@0
|
213 && | \|\| | # logic
|
Chris@0
|
214 :: | -> | => | # scope, member, dictionary
|
Chris@0
|
215 \\(?!\n) | # namespace
|
Chris@0
|
216 \+\+ | -- | # increment, decrement
|
Chris@0
|
217 [,;?:()\[\]{}] | # simple delimiters
|
Chris@0
|
218 [-+*\/%&|^]=? | # ordinary math, binary logic, assignment shortcuts
|
Chris@0
|
219 [~$] | # whatever
|
Chris@0
|
220 =& | # reference assignment
|
Chris@0
|
221 [=!]=?=? | <> | # comparison and assignment
|
Chris@0
|
222 <<=? | >>=? | [<>]=? # comparison and shift
|
Chris@0
|
223 /x
|
Chris@0
|
224
|
Chris@0
|
225 end
|
Chris@0
|
226
|
Chris@0
|
227 def scan_tokens tokens, options
|
Chris@0
|
228
|
Chris@0
|
229 if check(RE::PHP_START) || # starts with <?
|
Chris@0
|
230 (match?(/\s*<\S/) && exist?(RE::PHP_START)) || # starts with tag and contains <?
|
Chris@0
|
231 exist?(RE::HTML_INDICATOR) ||
|
Chris@0
|
232 check(/.{1,100}#{RE::PHP_START}/om) # PHP start after max 100 chars
|
Chris@0
|
233 # is HTML with embedded PHP, so start with HTML
|
Chris@0
|
234 states = [:initial]
|
Chris@0
|
235 else
|
Chris@0
|
236 # is just PHP, so start with PHP surrounded by HTML
|
Chris@0
|
237 states = [:initial, :php]
|
Chris@0
|
238 end
|
Chris@0
|
239
|
Chris@0
|
240 label_expected = true
|
Chris@0
|
241 case_expected = false
|
Chris@0
|
242
|
Chris@0
|
243 heredoc_delimiter = nil
|
Chris@0
|
244 delimiter = nil
|
Chris@0
|
245 modifier = nil
|
Chris@0
|
246
|
Chris@0
|
247 until eos?
|
Chris@0
|
248
|
Chris@0
|
249 match = nil
|
Chris@0
|
250 kind = nil
|
Chris@0
|
251
|
Chris@0
|
252 case states.last
|
Chris@0
|
253
|
Chris@0
|
254 when :initial # HTML
|
Chris@0
|
255 if scan RE::PHP_START
|
Chris@0
|
256 kind = :inline_delimiter
|
Chris@0
|
257 label_expected = true
|
Chris@0
|
258 states << :php
|
Chris@0
|
259 else
|
Chris@0
|
260 match = scan_until(/(?=#{RE::PHP_START})/o) || scan_until(/\z/)
|
Chris@0
|
261 @html_scanner.tokenize match unless match.empty?
|
Chris@0
|
262 next
|
Chris@0
|
263 end
|
Chris@0
|
264
|
Chris@0
|
265 when :php
|
Chris@0
|
266 if match = scan(/\s+/)
|
Chris@0
|
267 tokens << [match, :space]
|
Chris@0
|
268 next
|
Chris@0
|
269
|
Chris@0
|
270 elsif scan(%r! (?m: \/\* (?: .*? \*\/ | .* ) ) | (?://|\#) .*? (?=#{RE::PHP_END}|$) !xo)
|
Chris@0
|
271 kind = :comment
|
Chris@0
|
272
|
Chris@0
|
273 elsif match = scan(RE::IDENTIFIER)
|
Chris@0
|
274 kind = Words::IDENT_KIND[match]
|
Chris@0
|
275 if kind == :ident && label_expected && check(/:(?!:)/)
|
Chris@0
|
276 kind = :label
|
Chris@0
|
277 label_expected = true
|
Chris@0
|
278 else
|
Chris@0
|
279 label_expected = false
|
Chris@0
|
280 if kind == :ident && match =~ /^[A-Z]/
|
Chris@0
|
281 kind = :constant
|
Chris@0
|
282 elsif kind == :reserved
|
Chris@0
|
283 case match
|
Chris@0
|
284 when 'class'
|
Chris@0
|
285 states << :class_expected
|
Chris@0
|
286 when 'function'
|
Chris@0
|
287 states << :function_expected
|
Chris@0
|
288 when 'case', 'default'
|
Chris@0
|
289 case_expected = true
|
Chris@0
|
290 end
|
Chris@0
|
291 elsif match == 'b' && check(/['"]/) # binary string literal
|
Chris@0
|
292 modifier = match
|
Chris@0
|
293 next
|
Chris@0
|
294 end
|
Chris@0
|
295 end
|
Chris@0
|
296
|
Chris@0
|
297 elsif scan(/(?:\d+\.\d*|\d*\.\d+)(?:e[-+]?\d+)?|\d+e[-+]?\d+/i)
|
Chris@0
|
298 label_expected = false
|
Chris@0
|
299 kind = :float
|
Chris@0
|
300
|
Chris@0
|
301 elsif scan(/0x[0-9a-fA-F]+/)
|
Chris@0
|
302 label_expected = false
|
Chris@0
|
303 kind = :hex
|
Chris@0
|
304
|
Chris@0
|
305 elsif scan(/\d+/)
|
Chris@0
|
306 label_expected = false
|
Chris@0
|
307 kind = :integer
|
Chris@0
|
308
|
Chris@0
|
309 elsif scan(/'/)
|
Chris@0
|
310 tokens << [:open, :string]
|
Chris@0
|
311 if modifier
|
Chris@0
|
312 tokens << [modifier, :modifier]
|
Chris@0
|
313 modifier = nil
|
Chris@0
|
314 end
|
Chris@0
|
315 kind = :delimiter
|
Chris@0
|
316 states.push :sqstring
|
Chris@0
|
317
|
Chris@0
|
318 elsif match = scan(/["`]/)
|
Chris@0
|
319 tokens << [:open, :string]
|
Chris@0
|
320 if modifier
|
Chris@0
|
321 tokens << [modifier, :modifier]
|
Chris@0
|
322 modifier = nil
|
Chris@0
|
323 end
|
Chris@0
|
324 delimiter = match
|
Chris@0
|
325 kind = :delimiter
|
Chris@0
|
326 states.push :dqstring
|
Chris@0
|
327
|
Chris@0
|
328 elsif match = scan(RE::VARIABLE)
|
Chris@0
|
329 label_expected = false
|
Chris@0
|
330 kind = Words::VARIABLE_KIND[match]
|
Chris@0
|
331
|
Chris@0
|
332 elsif scan(/\{/)
|
Chris@0
|
333 kind = :operator
|
Chris@0
|
334 label_expected = true
|
Chris@0
|
335 states.push :php
|
Chris@0
|
336
|
Chris@0
|
337 elsif scan(/\}/)
|
Chris@0
|
338 if states.size == 1
|
Chris@0
|
339 kind = :error
|
Chris@0
|
340 else
|
Chris@0
|
341 states.pop
|
Chris@0
|
342 if states.last.is_a?(::Array)
|
Chris@0
|
343 delimiter = states.last[1]
|
Chris@0
|
344 states[-1] = states.last[0]
|
Chris@0
|
345 tokens << [matched, :delimiter]
|
Chris@0
|
346 tokens << [:close, :inline]
|
Chris@0
|
347 next
|
Chris@0
|
348 else
|
Chris@0
|
349 kind = :operator
|
Chris@0
|
350 label_expected = true
|
Chris@0
|
351 end
|
Chris@0
|
352 end
|
Chris@0
|
353
|
Chris@0
|
354 elsif scan(/@/)
|
Chris@0
|
355 label_expected = false
|
Chris@0
|
356 kind = :exception
|
Chris@0
|
357
|
Chris@0
|
358 elsif scan RE::PHP_END
|
Chris@0
|
359 kind = :inline_delimiter
|
Chris@0
|
360 states = [:initial]
|
Chris@0
|
361
|
Chris@0
|
362 elsif match = scan(/<<<(?:(#{RE::IDENTIFIER})|"(#{RE::IDENTIFIER})"|'(#{RE::IDENTIFIER})')/o)
|
Chris@0
|
363 tokens << [:open, :string]
|
Chris@0
|
364 warn 'heredoc in heredoc?' if heredoc_delimiter
|
Chris@0
|
365 heredoc_delimiter = Regexp.escape(self[1] || self[2] || self[3])
|
Chris@0
|
366 kind = :delimiter
|
Chris@0
|
367 states.push self[3] ? :sqstring : :dqstring
|
Chris@0
|
368 heredoc_delimiter = /#{heredoc_delimiter}(?=;?$)/
|
Chris@0
|
369
|
Chris@0
|
370 elsif match = scan(/#{RE::OPERATOR}/o)
|
Chris@0
|
371 label_expected = match == ';'
|
Chris@0
|
372 if case_expected
|
Chris@0
|
373 label_expected = true if match == ':'
|
Chris@0
|
374 case_expected = false
|
Chris@0
|
375 end
|
Chris@0
|
376 kind = :operator
|
Chris@0
|
377
|
Chris@0
|
378 else
|
Chris@0
|
379 getch
|
Chris@0
|
380 kind = :error
|
Chris@0
|
381
|
Chris@0
|
382 end
|
Chris@0
|
383
|
Chris@0
|
384 when :sqstring
|
Chris@0
|
385 if scan(heredoc_delimiter ? /[^\\\n]+/ : /[^'\\]+/)
|
Chris@0
|
386 kind = :content
|
Chris@0
|
387 elsif !heredoc_delimiter && scan(/'/)
|
Chris@0
|
388 tokens << [matched, :delimiter]
|
Chris@0
|
389 tokens << [:close, :string]
|
Chris@0
|
390 delimiter = nil
|
Chris@0
|
391 label_expected = false
|
Chris@0
|
392 states.pop
|
Chris@0
|
393 next
|
Chris@0
|
394 elsif heredoc_delimiter && match = scan(/\n/)
|
Chris@0
|
395 kind = :content
|
Chris@0
|
396 if scan heredoc_delimiter
|
Chris@0
|
397 tokens << ["\n", :content]
|
Chris@0
|
398 tokens << [matched, :delimiter]
|
Chris@0
|
399 tokens << [:close, :string]
|
Chris@0
|
400 heredoc_delimiter = nil
|
Chris@0
|
401 label_expected = false
|
Chris@0
|
402 states.pop
|
Chris@0
|
403 next
|
Chris@0
|
404 end
|
Chris@0
|
405 elsif scan(heredoc_delimiter ? /\\\\/ : /\\[\\'\n]/)
|
Chris@0
|
406 kind = :char
|
Chris@0
|
407 elsif scan(/\\./m)
|
Chris@0
|
408 kind = :content
|
Chris@0
|
409 elsif scan(/\\/)
|
Chris@0
|
410 kind = :error
|
Chris@0
|
411 end
|
Chris@0
|
412
|
Chris@0
|
413 when :dqstring
|
Chris@0
|
414 if scan(heredoc_delimiter ? /[^${\\\n]+/ : (delimiter == '"' ? /[^"${\\]+/ : /[^`${\\]+/))
|
Chris@0
|
415 kind = :content
|
Chris@0
|
416 elsif !heredoc_delimiter && scan(delimiter == '"' ? /"/ : /`/)
|
Chris@0
|
417 tokens << [matched, :delimiter]
|
Chris@0
|
418 tokens << [:close, :string]
|
Chris@0
|
419 delimiter = nil
|
Chris@0
|
420 label_expected = false
|
Chris@0
|
421 states.pop
|
Chris@0
|
422 next
|
Chris@0
|
423 elsif heredoc_delimiter && match = scan(/\n/)
|
Chris@0
|
424 kind = :content
|
Chris@0
|
425 if scan heredoc_delimiter
|
Chris@0
|
426 tokens << ["\n", :content]
|
Chris@0
|
427 tokens << [matched, :delimiter]
|
Chris@0
|
428 tokens << [:close, :string]
|
Chris@0
|
429 heredoc_delimiter = nil
|
Chris@0
|
430 label_expected = false
|
Chris@0
|
431 states.pop
|
Chris@0
|
432 next
|
Chris@0
|
433 end
|
Chris@0
|
434 elsif scan(/\\(?:x[0-9A-Fa-f]{1,2}|[0-7]{1,3})/)
|
Chris@0
|
435 kind = :char
|
Chris@0
|
436 elsif scan(heredoc_delimiter ? /\\[nrtvf\\$]/ : (delimiter == '"' ? /\\[nrtvf\\$"]/ : /\\[nrtvf\\$`]/))
|
Chris@0
|
437 kind = :char
|
Chris@0
|
438 elsif scan(/\\./m)
|
Chris@0
|
439 kind = :content
|
Chris@0
|
440 elsif scan(/\\/)
|
Chris@0
|
441 kind = :error
|
Chris@0
|
442 elsif match = scan(/#{RE::VARIABLE}/o)
|
Chris@0
|
443 kind = :local_variable
|
Chris@0
|
444 if check(/\[#{RE::IDENTIFIER}\]/o)
|
Chris@0
|
445 tokens << [:open, :inline]
|
Chris@0
|
446 tokens << [match, :local_variable]
|
Chris@0
|
447 tokens << [scan(/\[/), :operator]
|
Chris@0
|
448 tokens << [scan(/#{RE::IDENTIFIER}/o), :ident]
|
Chris@0
|
449 tokens << [scan(/\]/), :operator]
|
Chris@0
|
450 tokens << [:close, :inline]
|
Chris@0
|
451 next
|
Chris@0
|
452 elsif check(/\[/)
|
Chris@0
|
453 match << scan(/\[['"]?#{RE::IDENTIFIER}?['"]?\]?/o)
|
Chris@0
|
454 kind = :error
|
Chris@0
|
455 elsif check(/->#{RE::IDENTIFIER}/o)
|
Chris@0
|
456 tokens << [:open, :inline]
|
Chris@0
|
457 tokens << [match, :local_variable]
|
Chris@0
|
458 tokens << [scan(/->/), :operator]
|
Chris@0
|
459 tokens << [scan(/#{RE::IDENTIFIER}/o), :ident]
|
Chris@0
|
460 tokens << [:close, :inline]
|
Chris@0
|
461 next
|
Chris@0
|
462 elsif check(/->/)
|
Chris@0
|
463 match << scan(/->/)
|
Chris@0
|
464 kind = :error
|
Chris@0
|
465 end
|
Chris@0
|
466 elsif match = scan(/\{/)
|
Chris@0
|
467 if check(/\$/)
|
Chris@0
|
468 kind = :delimiter
|
Chris@0
|
469 states[-1] = [states.last, delimiter]
|
Chris@0
|
470 delimiter = nil
|
Chris@0
|
471 states.push :php
|
Chris@0
|
472 tokens << [:open, :inline]
|
Chris@0
|
473 else
|
Chris@0
|
474 kind = :string
|
Chris@0
|
475 end
|
Chris@0
|
476 elsif scan(/\$\{#{RE::IDENTIFIER}\}/o)
|
Chris@0
|
477 kind = :local_variable
|
Chris@0
|
478 elsif scan(/\$/)
|
Chris@0
|
479 kind = :content
|
Chris@0
|
480 end
|
Chris@0
|
481
|
Chris@0
|
482 when :class_expected
|
Chris@0
|
483 if scan(/\s+/)
|
Chris@0
|
484 kind = :space
|
Chris@0
|
485 elsif match = scan(/#{RE::IDENTIFIER}/o)
|
Chris@0
|
486 kind = :class
|
Chris@0
|
487 states.pop
|
Chris@0
|
488 else
|
Chris@0
|
489 states.pop
|
Chris@0
|
490 next
|
Chris@0
|
491 end
|
Chris@0
|
492
|
Chris@0
|
493 when :function_expected
|
Chris@0
|
494 if scan(/\s+/)
|
Chris@0
|
495 kind = :space
|
Chris@0
|
496 elsif scan(/&/)
|
Chris@0
|
497 kind = :operator
|
Chris@0
|
498 elsif match = scan(/#{RE::IDENTIFIER}/o)
|
Chris@0
|
499 kind = :function
|
Chris@0
|
500 states.pop
|
Chris@0
|
501 else
|
Chris@0
|
502 states.pop
|
Chris@0
|
503 next
|
Chris@0
|
504 end
|
Chris@0
|
505
|
Chris@0
|
506 else
|
Chris@0
|
507 raise_inspect 'Unknown state!', tokens, states
|
Chris@0
|
508 end
|
Chris@0
|
509
|
Chris@0
|
510 match ||= matched
|
Chris@0
|
511 if $CODERAY_DEBUG and not kind
|
Chris@0
|
512 raise_inspect 'Error token %p in line %d' %
|
Chris@0
|
513 [[match, kind], line], tokens, states
|
Chris@0
|
514 end
|
Chris@0
|
515 raise_inspect 'Empty token', tokens, states unless match
|
Chris@0
|
516
|
Chris@0
|
517 tokens << [match, kind]
|
Chris@0
|
518
|
Chris@0
|
519 end
|
Chris@0
|
520
|
Chris@0
|
521 tokens
|
Chris@0
|
522 end
|
Chris@0
|
523
|
Chris@0
|
524 end
|
Chris@0
|
525
|
Chris@0
|
526 end
|
Chris@0
|
527 end
|