2
function keywords(str) {
3
var obj = {}, words = str.split(" ");
4
for (var i = 0; i < words.length; ++i) obj[words[i]] = true;
7
function heredoc(delim) {
8
return function(stream, state) {
9
if (stream.match(delim)) state.tokenize = null;
10
else stream.skipToEnd();
16
keywords: keywords("abstract and array as break case catch class clone const continue declare default " +
17
"do else elseif enddeclare endfor endforeach endif endswitch endwhile extends final " +
18
"for foreach function global goto if implements interface instanceof namespace " +
19
"new or private protected public static switch throw trait try use var while xor " +
20
"die echo empty exit eval include include_once isset list require require_once return " +
21
"print unset __halt_compiler self static parent"),
22
blockKeywords: keywords("catch do else elseif for foreach if switch try while"),
23
atoms: keywords("true false null TRUE FALSE NULL"),
24
multiLineStrings: true,
26
"$": function(stream, state) {
27
stream.eatWhile(/[\w\$_]/);
30
"<": function(stream, state) {
31
if (stream.match(/<</)) {
32
stream.eatWhile(/[\w\.]/);
33
state.tokenize = heredoc(stream.current().slice(3));
34
return state.tokenize(stream, state);
38
"#": function(stream, state) {
39
while (!stream.eol() && !stream.match("?>", false)) stream.next();
42
"/": function(stream, state) {
43
if (stream.eat("/")) {
44
while (!stream.eol() && !stream.match("?>", false)) stream.next();
52
CodeMirror.defineMode("php", function(config, parserConfig) {
53
var htmlMode = CodeMirror.getMode(config, {name: "xml", htmlMode: true});
54
var jsMode = CodeMirror.getMode(config, "javascript");
55
var cssMode = CodeMirror.getMode(config, "css");
56
var phpMode = CodeMirror.getMode(config, phpConfig);
58
function dispatch(stream, state) { // TODO open PHP inside text/css
59
var isPHP = state.mode == "php";
60
if (stream.sol() && state.pending != '"') state.pending = null;
61
if (state.curMode == htmlMode) {
62
if (stream.match(/^<\?\w*/)) {
63
state.curMode = phpMode;
64
state.curState = state.php;
65
state.curClose = "?>";
69
if (state.pending == '"') {
70
while (!stream.eol() && stream.next() != '"') {}
72
} else if (state.pending && stream.pos < state.pending.end) {
73
stream.pos = state.pending.end;
74
var style = state.pending.style;
76
var style = htmlMode.token(stream, state.curState);
79
var cur = stream.current(), openPHP = cur.search(/<\?/);
81
if (style == "string" && /\"$/.test(cur) && !/\?>/.test(cur)) state.pending = '"';
82
else state.pending = {end: stream.pos, style: style};
83
stream.backUp(cur.length - openPHP);
84
} else if (style == "tag" && stream.current() == ">" && state.curState.context) {
85
if (/^script$/i.test(state.curState.context.tagName)) {
86
state.curMode = jsMode;
87
state.curState = jsMode.startState(htmlMode.indent(state.curState, ""));
88
state.curClose = /^<\/\s*script\s*>/i;
89
state.mode = "javascript";
91
else if (/^style$/i.test(state.curState.context.tagName)) {
92
state.curMode = cssMode;
93
state.curState = cssMode.startState(htmlMode.indent(state.curState, ""));
94
state.curClose = /^<\/\s*style\s*>/i;
99
} else if ((!isPHP || state.php.tokenize == null) &&
100
stream.match(state.curClose, isPHP)) {
101
state.curMode = htmlMode;
102
state.curState = state.html;
103
state.curClose = null;
105
if (isPHP) return "meta";
106
else return dispatch(stream, state);
108
return state.curMode.token(stream, state.curState);
113
startState: function() {
114
var html = htmlMode.startState();
116
php: phpMode.startState(),
117
curMode: parserConfig.startOpen ? phpMode : htmlMode,
118
curState: parserConfig.startOpen ? phpMode.startState() : html,
119
curClose: parserConfig.startOpen ? /^\?>/ : null,
120
mode: parserConfig.startOpen ? "php" : "html",
124
copyState: function(state) {
125
var html = state.html, htmlNew = CodeMirror.copyState(htmlMode, html),
126
php = state.php, phpNew = CodeMirror.copyState(phpMode, php), cur;
127
if (state.curState == html) cur = htmlNew;
128
else if (state.curState == php) cur = phpNew;
129
else cur = CodeMirror.copyState(state.curMode, state.curState);
130
return {html: htmlNew, php: phpNew, curMode: state.curMode, curState: cur,
131
curClose: state.curClose, mode: state.mode,
132
pending: state.pending};
137
indent: function(state, textAfter) {
138
if ((state.curMode != phpMode && /^\s*<\//.test(textAfter)) ||
139
(state.curMode == phpMode && /^\?>/.test(textAfter)))
140
return htmlMode.indent(state.html, textAfter);
141
return state.curMode.indent(state.curState, textAfter);
144
electricChars: "/{}:"
147
CodeMirror.defineMIME("application/x-httpd-php", "php");
148
CodeMirror.defineMIME("application/x-httpd-php-open", {name: "php", startOpen: true});
149
CodeMirror.defineMIME("text/x-php", phpConfig);