// PHP语法高亮模式
CodeMirror.defineMode("php", function() {
    return {
        token: function(stream) {
            // 基础PHP语法高亮规则
            if (stream.match(/<\?php/)) return "keyword";
            if (stream.match(/<\?=/)) return "keyword";
            if (stream.match(/\?>/) && !stream.eol()) return "keyword";
            
            // 字符串
            if (stream.match(/"(?:[^"\\]|\\.)*"/)) return "string";
            if (stream.match(/'(?:[^'\\]|\\.)*'/)) return "string";
            
            // 注释
            if (stream.match(/\/\/.*/)) return "comment";
            if (stream.match(/\/\*.*?\*\//)) return "comment";
            if (stream.match(/#.*/)) return "comment";
            
            // 关键字
            if (stream.match(/\b(function|class|if|else|foreach|while|for|return)\b/)) {
                return "keyword";
            }
            
            // 变量
            if (stream.match(/\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/)) {
                return "variable";
            }
            
            // 其他
            stream.next();
            return null;
        }
    };
});

// 注册PHP混合HTML模式
CodeMirror.defineMode("application/x-httpd-php", function(config) {
    return CodeMirror.multiplexingMode(
        CodeMirror.getMode(config, "text/html"),
        {
            open: "<?", close: "?>",
            mode: CodeMirror.getMode(config, "php"),
            parseDelimiters: true
        },
        {
            open: "<?=", close: "?>",
            mode: CodeMirror.getMode(config, "php"),
            parseDelimiters: true
        }
    );
});