1 module CodeRay module Scanners
8 'and', 'or', 'xor', '__FILE__', 'exception', '__LINE__', 'array', 'as', 'break', 'case',
9 'class', 'const', 'continue', 'declare', 'default',
10 'die', 'do', 'echo', 'else', 'elseif',
11 'empty', 'enddeclare', 'endfor', 'endforeach', 'endif',
12 'endswitch', 'endwhile', 'eval', 'exit', 'extends',
13 'for', 'foreach', 'function', 'global', 'if',
14 'include', 'include_once', 'isset', 'list', 'new',
15 'print', 'require', 'require_once', 'return', 'static',
16 'switch', 'unset', 'use', 'var', 'while',
17 '__FUNCTION__', '__CLASS__', '__METHOD__', 'final', 'php_user_filter',
18 'interface', 'implements', 'extends', 'public', 'private',
19 'protected', 'abstract', 'clone', 'try', 'catch',
20 'throw', 'cfunction', 'old_function'
23 PREDEFINED_CONSTANTS = [
24 'null', '$this', 'true', 'false'
27 IDENT_KIND = WordList.new(:ident).
28 add(RESERVED_WORDS, :reserved).
29 add(PREDEFINED_CONSTANTS, :pre_constant)
31 ESCAPE = / [\$\wrbfnrtv\n\\\/'"] | x[a-fA-F0-9]{1,2} | [0-7]{1,3} /x
32 UNICODE_ESCAPE = / u[a-fA-F0-9]{4} | U[a-fA-F0-9]{8} /x
34 def scan_tokens tokens, options
47 if scan(/ \s+ | \\\n /x)
54 elsif scan(%r{ (//|\#) [^\n\\]* (?: \\. [^\n\\]* )* | /\* (?: .*? \*/ | .* ) }mx)
56 regexp_allowed = false
58 elsif match = scan(/ \# \s* if \s* 0 /x)
59 match << scan_until(/ ^\# (?:elif|else|endif) .*? $ | \z /xm) unless eos?
61 regexp_allowed = false
63 elsif regexp_allowed and scan(/\//)
64 tokens << [:open, :regexp]
68 elsif scan(/ [-+*\/=<>?:;,!&^|()\[\]{}~%] | \.(?!\d) /x)
72 elsif match = scan(/ [$@A-Za-z_][A-Za-z_0-9]* /x)
73 kind = IDENT_KIND[match]
76 elsif match = scan(/["']/)
77 tokens << [:open, :string]
82 elsif scan(/0[xX][0-9A-Fa-f]+/)
86 elsif scan(/(?:0[0-7]+)(?![89.eEfF])/)
90 elsif scan(/(?:\d+)(?![.eEfF])/)
94 elsif scan(/\d[fF]?|\d*\.\d+(?:[eE][+-]?\d+)?[fF]?|\d+[eE][+-]?\d+[fF]?/)
102 elsif state == :regex
105 elsif scan(/\\\/|\\/)
108 tokens << [matched, :delimiter]
109 tokens << [:close, :regexp]
117 elsif state == :string
121 if string_type==matched
122 tokens << [matched, :delimiter]
123 tokens << [:close, :string]
130 elsif scan(/ \\ (?: \S ) /mox)
132 elsif scan(/ \\ | $ /x)
136 raise "else case \" reached; %p not handled." % peek(1), tokens
139 elsif state == :waiting_php
150 raise 'else-case reached', tokens
156 tokens << [match, kind]