~ubuntu-branches/ubuntu/utopic/codemirror-js/utopic

« back to all changes in this revision

Viewing changes to mode/php/php.js

  • Committer: Package Import Robot
  • Author(s): David Paleino
  • Date: 2012-04-12 12:25:28 UTC
  • Revision ID: package-import@ubuntu.com-20120412122528-8xp5a8frj4h1d3ee
Tags: upstream-2.23
ImportĀ upstreamĀ versionĀ 2.23

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 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,
 
25
    hooks: {
 
26
      "$": function(stream, state) {
 
27
        stream.eatWhile(/[\w\$_]/);
 
28
        return "variable-2";
 
29
      },
 
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);
 
35
        }
 
36
        return false;
 
37
      },
 
38
      "#": function(stream, state) {
 
39
        while (!stream.eol() && !stream.match("?>", false)) stream.next();
 
40
        return "comment";
 
41
      },
 
42
      "/": function(stream, state) {
 
43
        if (stream.eat("/")) {
 
44
          while (!stream.eol() && !stream.match("?>", false)) stream.next();
 
45
          return "comment";
 
46
        }
 
47
        return false;
 
48
      }
 
49
    }
 
50
  };
 
51
 
 
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);
 
57
 
 
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 = "?>";
 
66
          state.mode = "php";
 
67
          return "meta";
 
68
        }
 
69
        if (state.pending == '"') {
 
70
          while (!stream.eol() && stream.next() != '"') {}
 
71
          var style = "string";
 
72
        } else if (state.pending && stream.pos < state.pending.end) {
 
73
          stream.pos = state.pending.end;
 
74
          var style = state.pending.style;
 
75
        } else {
 
76
          var style = htmlMode.token(stream, state.curState);
 
77
        }
 
78
        state.pending = null;
 
79
        var cur = stream.current(), openPHP = cur.search(/<\?/);
 
80
        if (openPHP != -1) {
 
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";
 
90
          }
 
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;
 
95
            state.mode = "css";
 
96
          }
 
97
        }
 
98
        return style;
 
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;
 
104
        state.mode = "html";
 
105
        if (isPHP) return "meta";
 
106
        else return dispatch(stream, state);
 
107
      } else {
 
108
        return state.curMode.token(stream, state.curState);
 
109
      }
 
110
    }
 
111
 
 
112
    return {
 
113
      startState: function() {
 
114
        var html = htmlMode.startState();
 
115
        return {html: html,
 
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",
 
121
                pending: null}
 
122
      },
 
123
 
 
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};
 
133
      },
 
134
 
 
135
      token: dispatch,
 
136
 
 
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);
 
142
      },
 
143
 
 
144
      electricChars: "/{}:"
 
145
    }
 
146
  });
 
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);
 
150
})();