~ivle-dev/ivle/codemirror

« back to all changes in this revision

Viewing changes to ivle/webapp/filesystem/browser/media/codemirror/js/parsejavascript.js

  • Committer: David Coles
  • Date: 2010-05-31 10:38:53 UTC
  • Revision ID: coles.david@gmail.com-20100531103853-8xypjpracvwy0qt4
Editor: Added CodeMirror-0.67 Javascript code editor source from 
http://marijn.haverbeke.nl/codemirror/ (zlib-style licence)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Parse function for JavaScript. Makes use of the tokenizer from
 
2
 * tokenizejavascript.js. Note that your parsers do not have to be
 
3
 * this complicated -- if you don't want to recognize local variables,
 
4
 * in many languages it is enough to just look for braces, semicolons,
 
5
 * parentheses, etc, and know when you are inside a string or comment.
 
6
 *
 
7
 * See manual.html for more info about the parser interface.
 
8
 */
 
9
 
 
10
var JSParser = Editor.Parser = (function() {
 
11
  // Token types that can be considered to be atoms.
 
12
  var atomicTypes = {"atom": true, "number": true, "variable": true, "string": true, "regexp": true};
 
13
  // Setting that can be used to have JSON data indent properly.
 
14
  var json = false;
 
15
  // Constructor for the lexical context objects.
 
16
  function JSLexical(indented, column, type, align, prev, info) {
 
17
    // indentation at start of this line
 
18
    this.indented = indented;
 
19
    // column at which this scope was opened
 
20
    this.column = column;
 
21
    // type of scope ('vardef', 'stat' (statement), 'form' (special form), '[', '{', or '(')
 
22
    this.type = type;
 
23
    // '[', '{', or '(' blocks that have any text after their opening
 
24
    // character are said to be 'aligned' -- any lines below are
 
25
    // indented all the way to the opening character.
 
26
    if (align != null)
 
27
      this.align = align;
 
28
    // Parent scope, if any.
 
29
    this.prev = prev;
 
30
    this.info = info;
 
31
  }
 
32
 
 
33
  // My favourite JavaScript indentation rules.
 
34
  function indentJS(lexical) {
 
35
    return function(firstChars) {
 
36
      var firstChar = firstChars && firstChars.charAt(0), type = lexical.type;
 
37
      var closing = firstChar == type;
 
38
      if (type == "vardef")
 
39
        return lexical.indented + 4;
 
40
      else if (type == "form" && firstChar == "{")
 
41
        return lexical.indented;
 
42
      else if (type == "stat" || type == "form")
 
43
        return lexical.indented + indentUnit;
 
44
      else if (lexical.info == "switch" && !closing)
 
45
        return lexical.indented + (/^(?:case|default)\b/.test(firstChars) ? indentUnit : 2 * indentUnit);
 
46
      else if (lexical.align)
 
47
        return lexical.column - (closing ? 1 : 0);
 
48
      else
 
49
        return lexical.indented + (closing ? 0 : indentUnit);
 
50
    };
 
51
  }
 
52
 
 
53
  // The parser-iterator-producing function itself.
 
54
  function parseJS(input, basecolumn) {
 
55
    // Wrap the input in a token stream
 
56
    var tokens = tokenizeJavaScript(input);
 
57
    // The parser state. cc is a stack of actions that have to be
 
58
    // performed to finish the current statement. For example we might
 
59
    // know that we still need to find a closing parenthesis and a
 
60
    // semicolon. Actions at the end of the stack go first. It is
 
61
    // initialized with an infinitely looping action that consumes
 
62
    // whole statements.
 
63
    var cc = [json ? singleExpr : statements];
 
64
    // Context contains information about the current local scope, the
 
65
    // variables defined in that, and the scopes above it.
 
66
    var context = null;
 
67
    // The lexical scope, used mostly for indentation.
 
68
    var lexical = new JSLexical((basecolumn || 0) - indentUnit, 0, "block", false);
 
69
    // Current column, and the indentation at the start of the current
 
70
    // line. Used to create lexical scope objects.
 
71
    var column = 0;
 
72
    var indented = 0;
 
73
    // Variables which are used by the mark, cont, and pass functions
 
74
    // below to communicate with the driver loop in the 'next'
 
75
    // function.
 
76
    var consume, marked;
 
77
  
 
78
    // The iterator object.
 
79
    var parser = {next: next, copy: copy};
 
80
 
 
81
    function next(){
 
82
      // Start by performing any 'lexical' actions (adjusting the
 
83
      // lexical variable), or the operations below will be working
 
84
      // with the wrong lexical state.
 
85
      while(cc[cc.length - 1].lex)
 
86
        cc.pop()();
 
87
 
 
88
      // Fetch a token.
 
89
      var token = tokens.next();
 
90
 
 
91
      // Adjust column and indented.
 
92
      if (token.type == "whitespace" && column == 0)
 
93
        indented = token.value.length;
 
94
      column += token.value.length;
 
95
      if (token.content == "\n"){
 
96
        indented = column = 0;
 
97
        // If the lexical scope's align property is still undefined at
 
98
        // the end of the line, it is an un-aligned scope.
 
99
        if (!("align" in lexical))
 
100
          lexical.align = false;
 
101
        // Newline tokens get an indentation function associated with
 
102
        // them.
 
103
        token.indentation = indentJS(lexical);
 
104
      }
 
105
      // No more processing for meaningless tokens.
 
106
      if (token.type == "whitespace" || token.type == "comment")
 
107
        return token;
 
108
      // When a meaningful token is found and the lexical scope's
 
109
      // align is undefined, it is an aligned scope.
 
110
      if (!("align" in lexical))
 
111
        lexical.align = true;
 
112
 
 
113
      // Execute actions until one 'consumes' the token and we can
 
114
      // return it.
 
115
      while(true) {
 
116
        consume = marked = false;
 
117
        // Take and execute the topmost action.
 
118
        cc.pop()(token.type, token.content);
 
119
        if (consume){
 
120
          // Marked is used to change the style of the current token.
 
121
          if (marked)
 
122
            token.style = marked;
 
123
          // Here we differentiate between local and global variables.
 
124
          else if (token.type == "variable" && inScope(token.content))
 
125
            token.style = "js-localvariable";
 
126
          return token;
 
127
        }
 
128
      }
 
129
    }
 
130
 
 
131
    // This makes a copy of the parser state. It stores all the
 
132
    // stateful variables in a closure, and returns a function that
 
133
    // will restore them when called with a new input stream. Note
 
134
    // that the cc array has to be copied, because it is contantly
 
135
    // being modified. Lexical objects are not mutated, and context
 
136
    // objects are not mutated in a harmful way, so they can be shared
 
137
    // between runs of the parser.
 
138
    function copy(){
 
139
      var _context = context, _lexical = lexical, _cc = cc.concat([]), _tokenState = tokens.state;
 
140
  
 
141
      return function copyParser(input){
 
142
        context = _context;
 
143
        lexical = _lexical;
 
144
        cc = _cc.concat([]); // copies the array
 
145
        column = indented = 0;
 
146
        tokens = tokenizeJavaScript(input, _tokenState);
 
147
        return parser;
 
148
      };
 
149
    }
 
150
 
 
151
    // Helper function for pushing a number of actions onto the cc
 
152
    // stack in reverse order.
 
153
    function push(fs){
 
154
      for (var i = fs.length - 1; i >= 0; i--)
 
155
        cc.push(fs[i]);
 
156
    }
 
157
    // cont and pass are used by the action functions to add other
 
158
    // actions to the stack. cont will cause the current token to be
 
159
    // consumed, pass will leave it for the next action.
 
160
    function cont(){
 
161
      push(arguments);
 
162
      consume = true;
 
163
    }
 
164
    function pass(){
 
165
      push(arguments);
 
166
      consume = false;
 
167
    }
 
168
    // Used to change the style of the current token.
 
169
    function mark(style){
 
170
      marked = style;
 
171
    }
 
172
 
 
173
    // Push a new scope. Will automatically link the current scope.
 
174
    function pushcontext(){
 
175
      context = {prev: context, vars: {"this": true, "arguments": true}};
 
176
    }
 
177
    // Pop off the current scope.
 
178
    function popcontext(){
 
179
      context = context.prev;
 
180
    }
 
181
    // Register a variable in the current scope.
 
182
    function register(varname){
 
183
      if (context){
 
184
        mark("js-variabledef");
 
185
        context.vars[varname] = true;
 
186
      }
 
187
    }
 
188
    // Check whether a variable is defined in the current scope.
 
189
    function inScope(varname){
 
190
      var cursor = context;
 
191
      while (cursor) {
 
192
        if (cursor.vars[varname])
 
193
          return true;
 
194
        cursor = cursor.prev;
 
195
      }
 
196
      return false;
 
197
    }
 
198
  
 
199
    // Push a new lexical context of the given type.
 
200
    function pushlex(type, info) {
 
201
      var result = function(){
 
202
        lexical = new JSLexical(indented, column, type, null, lexical, info)
 
203
      };
 
204
      result.lex = true;
 
205
      return result;
 
206
    }
 
207
    // Pop off the current lexical context.
 
208
    function poplex(){
 
209
      lexical = lexical.prev;
 
210
    }
 
211
    poplex.lex = true;
 
212
    // The 'lex' flag on these actions is used by the 'next' function
 
213
    // to know they can (and have to) be ran before moving on to the
 
214
    // next token.
 
215
  
 
216
    // Creates an action that discards tokens until it finds one of
 
217
    // the given type.
 
218
    function expect(wanted){
 
219
      return function expecting(type){
 
220
        if (type == wanted) cont();
 
221
        else cont(arguments.callee);
 
222
      };
 
223
    }
 
224
 
 
225
    // Looks for a statement, and then calls itself.
 
226
    function statements(type){
 
227
      return pass(statement, statements);
 
228
    }
 
229
    function singleExpr(type){
 
230
      return pass(expression, statements);
 
231
    }
 
232
    // Dispatches various types of statements based on the type of the
 
233
    // current token.
 
234
    function statement(type){
 
235
      if (type == "var") cont(pushlex("vardef"), vardef1, expect(";"), poplex);
 
236
      else if (type == "keyword a") cont(pushlex("form"), expression, statement, poplex);
 
237
      else if (type == "keyword b") cont(pushlex("form"), statement, poplex);
 
238
      else if (type == "{") cont(pushlex("}"), block, poplex);
 
239
      else if (type == "function") cont(functiondef);
 
240
      else if (type == "for") cont(pushlex("form"), expect("("), pushlex(")"), forspec1, expect(")"), poplex, statement, poplex);
 
241
      else if (type == "variable") cont(pushlex("stat"), maybelabel);
 
242
      else if (type == "switch") cont(pushlex("form"), expression, pushlex("}", "switch"), expect("{"), block, poplex, poplex);
 
243
      else if (type == "case") cont(expression, expect(":"));
 
244
      else if (type == "default") cont(expect(":"));
 
245
      else if (type == "catch") cont(pushlex("form"), pushcontext, expect("("), funarg, expect(")"), statement, poplex, popcontext);
 
246
      else pass(pushlex("stat"), expression, expect(";"), poplex);
 
247
    }
 
248
    // Dispatch expression types.
 
249
    function expression(type){
 
250
      if (atomicTypes.hasOwnProperty(type)) cont(maybeoperator);
 
251
      else if (type == "function") cont(functiondef);
 
252
      else if (type == "keyword c") cont(expression);
 
253
      else if (type == "(") cont(pushlex(")"), expression, expect(")"), poplex, maybeoperator);
 
254
      else if (type == "operator") cont(expression);
 
255
      else if (type == "[") cont(pushlex("]"), commasep(expression, "]"), poplex, maybeoperator);
 
256
      else if (type == "{") cont(pushlex("}"), commasep(objprop, "}"), poplex, maybeoperator);
 
257
    }
 
258
    // Called for places where operators, function calls, or
 
259
    // subscripts are valid. Will skip on to the next action if none
 
260
    // is found.
 
261
    function maybeoperator(type){
 
262
      if (type == "operator") cont(expression);
 
263
      else if (type == "(") cont(pushlex(")"), expression, commasep(expression, ")"), poplex, maybeoperator);
 
264
      else if (type == ".") cont(property, maybeoperator);
 
265
      else if (type == "[") cont(pushlex("]"), expression, expect("]"), poplex, maybeoperator);
 
266
    }
 
267
    // When a statement starts with a variable name, it might be a
 
268
    // label. If no colon follows, it's a regular statement.
 
269
    function maybelabel(type){
 
270
      if (type == ":") cont(poplex, statement);
 
271
      else pass(maybeoperator, expect(";"), poplex);
 
272
    }
 
273
    // Property names need to have their style adjusted -- the
 
274
    // tokenizer thinks they are variables.
 
275
    function property(type){
 
276
      if (type == "variable") {mark("js-property"); cont();}
 
277
    }
 
278
    // This parses a property and its value in an object literal.
 
279
    function objprop(type){
 
280
      if (type == "variable") mark("js-property");
 
281
      if (atomicTypes.hasOwnProperty(type)) cont(expect(":"), expression);
 
282
    }
 
283
    // Parses a comma-separated list of the things that are recognized
 
284
    // by the 'what' argument.
 
285
    function commasep(what, end){
 
286
      function proceed(type) {
 
287
        if (type == ",") cont(what, proceed);
 
288
        else if (type == end) cont();
 
289
        else cont(expect(end));
 
290
      }
 
291
      return function commaSeparated(type) {
 
292
        if (type == end) cont();
 
293
        else pass(what, proceed);
 
294
      };
 
295
    }
 
296
    // Look for statements until a closing brace is found.
 
297
    function block(type){
 
298
      if (type == "}") cont();
 
299
      else pass(statement, block);
 
300
    }
 
301
    // Variable definitions are split into two actions -- 1 looks for
 
302
    // a name or the end of the definition, 2 looks for an '=' sign or
 
303
    // a comma.
 
304
    function vardef1(type, value){
 
305
      if (type == "variable"){register(value); cont(vardef2);}
 
306
      else cont();
 
307
    }
 
308
    function vardef2(type, value){
 
309
      if (value == "=") cont(expression, vardef2);
 
310
      else if (type == ",") cont(vardef1);
 
311
    }
 
312
    // For loops.
 
313
    function forspec1(type){
 
314
      if (type == "var") cont(vardef1, forspec2);
 
315
      else if (type == ";") pass(forspec2);
 
316
      else if (type == "variable") cont(formaybein);
 
317
      else pass(forspec2);
 
318
    }
 
319
    function formaybein(type, value){
 
320
      if (value == "in") cont(expression);
 
321
      else cont(maybeoperator, forspec2);
 
322
    }
 
323
    function forspec2(type, value){
 
324
      if (type == ";") cont(forspec3);
 
325
      else if (value == "in") cont(expression);
 
326
      else cont(expression, expect(";"), forspec3);
 
327
    }
 
328
    function forspec3(type) {
 
329
      if (type == ")") pass();
 
330
      else cont(expression);
 
331
    }
 
332
    // A function definition creates a new context, and the variables
 
333
    // in its argument list have to be added to this context.
 
334
    function functiondef(type, value){
 
335
      if (type == "variable"){register(value); cont(functiondef);}
 
336
      else if (type == "(") cont(pushcontext, commasep(funarg, ")"), statement, popcontext);
 
337
    }
 
338
    function funarg(type, value){
 
339
      if (type == "variable"){register(value); cont();}
 
340
    }
 
341
  
 
342
    return parser;
 
343
  }
 
344
 
 
345
  return {
 
346
    make: parseJS,
 
347
    electricChars: "{}:",
 
348
    configure: function(obj) {
 
349
      if (obj.json != null) json = obj.json;
 
350
    }
 
351
  };
 
352
})();