aboutsummaryrefslogtreecommitdiff
path: root/js/codemirror/mode/php/php.js
diff options
context:
space:
mode:
Diffstat (limited to 'js/codemirror/mode/php/php.js')
-rw-r--r--js/codemirror/mode/php/php.js110
1 files changed, 110 insertions, 0 deletions
diff --git a/js/codemirror/mode/php/php.js b/js/codemirror/mode/php/php.js
new file mode 100644
index 00000000..495e4415
--- /dev/null
+++ b/js/codemirror/mode/php/php.js
@@ -0,0 +1,110 @@
1(function() {
2 function keywords(str) {
3 var obj = {}, words = str.split(" ");
4 for (var i = 0; i < words.length; ++i) obj[words[i]] = true;
5 return obj;
6 }
7 function heredoc(delim) {
8 return function(stream, state) {
9 if (stream.match(delim)) state.tokenize = null;
10 else stream.skipToEnd();
11 return "string";
12 }
13 }
14 var phpConfig = {
15 name: "clike",
16 keywords: keywords("abstract and array as break case catch cfunction class clone const continue declare " +
17 "default do else elseif enddeclare endfor endforeach endif endswitch endwhile extends " +
18 "final for foreach function global goto if implements interface instanceof namespace " +
19 "new or private protected public static switch throw try use var while xor return"),
20 blockKeywords: keywords("catch do else elseif for foreach if switch try while"),
21 atoms: keywords("true false null"),
22 multiLineStrings: true,
23 hooks: {
24 "$": function(stream, state) {
25 stream.eatWhile(/[\w\$_]/);
26 return "variable-2";
27 },
28 "<": function(stream, state) {
29 if (stream.match(/<</)) {
30 stream.eatWhile(/[\w\.]/);
31 state.tokenize = heredoc(stream.current().slice(3));
32 return state.tokenize(stream, state);
33 }
34 return false;
35 }
36 }
37 };
38
39 CodeMirror.defineMode("php", function(config, parserConfig) {
40 var htmlMode = CodeMirror.getMode(config, "text/html");
41 var jsMode = CodeMirror.getMode(config, "text/javascript");
42 var cssMode = CodeMirror.getMode(config, "text/css");
43 var phpMode = CodeMirror.getMode(config, phpConfig);
44
45 function dispatch(stream, state) { // TODO open PHP inside text/css
46 if (state.curMode == htmlMode) {
47 var style = htmlMode.token(stream, state.curState);
48 if (style == "meta" && /^<\?/.test(stream.current())) {
49 state.curMode = phpMode;
50 state.curState = state.php;
51 state.curClose = /^\?>/;
52 }
53 else if (style == "tag" && stream.current() == ">" && state.curState.context) {
54 if (/^script$/i.test(state.curState.context.tagName)) {
55 state.curMode = jsMode;
56 state.curState = jsMode.startState(htmlMode.indent(state.curState, ""));
57 state.curClose = /^<\/\s*script\s*>/i;
58 }
59 else if (/^style$/i.test(state.curState.context.tagName)) {
60 state.curMode = cssMode;
61 state.curState = cssMode.startState(htmlMode.indent(state.curState, ""));
62 state.curClose = /^<\/\s*style\s*>/i;
63 }
64 }
65 return style;
66 }
67 else if (stream.match(state.curClose, false)) {
68 state.curMode = htmlMode;
69 state.curState = state.html;
70 state.curClose = null;
71 return dispatch(stream, state);
72 }
73 else return state.curMode.token(stream, state.curState);
74 }
75
76 return {
77 startState: function() {
78 var html = htmlMode.startState();
79 return {html: html,
80 php: phpMode.startState(),
81 curMode: parserConfig.startOpen ? phpMode : htmlMode,
82 curState: parserConfig.startOpen ? phpMode.startState() : html,
83 curClose: parserConfig.startOpen ? /^\?>/ : null}
84 },
85
86 copyState: function(state) {
87 var html = state.html, htmlNew = CodeMirror.copyState(htmlMode, html),
88 php = state.php, phpNew = CodeMirror.copyState(phpMode, php), cur;
89 if (state.curState == html) cur = htmlNew;
90 else if (state.curState == php) cur = phpNew;
91 else cur = CodeMirror.copyState(state.curMode, state.curState);
92 return {html: htmlNew, php: phpNew, curMode: state.curMode, curState: cur, curClose: state.curClose};
93 },
94
95 token: dispatch,
96
97 indent: function(state, textAfter) {
98 if ((state.curMode != phpMode && /^\s*<\//.test(textAfter)) ||
99 (state.curMode == phpMode && /^\?>/.test(textAfter)))
100 return htmlMode.indent(state.html, textAfter);
101 return state.curMode.indent(state.curState, textAfter);
102 },
103
104 electricChars: "/{}:"
105 }
106 });
107 CodeMirror.defineMIME("application/x-httpd-php", "php");
108 CodeMirror.defineMIME("application/x-httpd-php-open", {name: "php", startOpen: true});
109 CodeMirror.defineMIME("text/x-php", phpConfig);
110})();