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.
7
* See manual.html for more info about the parser interface.
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.
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
21
// type of scope ('vardef', 'stat' (statement), 'form' (special form), '[', '{', or '(')
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.
28
// Parent scope, if any.
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;
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);
49
return lexical.indented + (closing ? 0 : indentUnit);
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
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.
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.
73
// Variables which are used by the mark, cont, and pass functions
74
// below to communicate with the driver loop in the 'next'
78
// The iterator object.
79
var parser = {next: next, copy: copy};
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)
89
var token = tokens.next();
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
103
token.indentation = indentJS(lexical);
105
// No more processing for meaningless tokens.
106
if (token.type == "whitespace" || token.type == "comment")
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;
113
// Execute actions until one 'consumes' the token and we can
116
consume = marked = false;
117
// Take and execute the topmost action.
118
cc.pop()(token.type, token.content);
120
// Marked is used to change the style of the current token.
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";
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.
139
var _context = context, _lexical = lexical, _cc = cc.concat([]), _tokenState = tokens.state;
141
return function copyParser(input){
144
cc = _cc.concat([]); // copies the array
145
column = indented = 0;
146
tokens = tokenizeJavaScript(input, _tokenState);
151
// Helper function for pushing a number of actions onto the cc
152
// stack in reverse order.
154
for (var i = fs.length - 1; i >= 0; i--)
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.
168
// Used to change the style of the current token.
169
function mark(style){
173
// Push a new scope. Will automatically link the current scope.
174
function pushcontext(){
175
context = {prev: context, vars: {"this": true, "arguments": true}};
177
// Pop off the current scope.
178
function popcontext(){
179
context = context.prev;
181
// Register a variable in the current scope.
182
function register(varname){
184
mark("js-variabledef");
185
context.vars[varname] = true;
188
// Check whether a variable is defined in the current scope.
189
function inScope(varname){
190
var cursor = context;
192
if (cursor.vars[varname])
194
cursor = cursor.prev;
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)
207
// Pop off the current lexical context.
209
lexical = lexical.prev;
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
216
// Creates an action that discards tokens until it finds one of
218
function expect(wanted){
219
return function expecting(type){
220
if (type == wanted) cont();
221
else cont(arguments.callee);
225
// Looks for a statement, and then calls itself.
226
function statements(type){
227
return pass(statement, statements);
229
function singleExpr(type){
230
return pass(expression, statements);
232
// Dispatches various types of statements based on the type of the
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);
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);
258
// Called for places where operators, function calls, or
259
// subscripts are valid. Will skip on to the next action if none
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);
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);
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();}
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);
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));
291
return function commaSeparated(type) {
292
if (type == end) cont();
293
else pass(what, proceed);
296
// Look for statements until a closing brace is found.
297
function block(type){
298
if (type == "}") cont();
299
else pass(statement, block);
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
304
function vardef1(type, value){
305
if (type == "variable"){register(value); cont(vardef2);}
308
function vardef2(type, value){
309
if (value == "=") cont(expression, vardef2);
310
else if (type == ",") cont(vardef1);
313
function forspec1(type){
314
if (type == "var") cont(vardef1, forspec2);
315
else if (type == ";") pass(forspec2);
316
else if (type == "variable") cont(formaybein);
319
function formaybein(type, value){
320
if (value == "in") cont(expression);
321
else cont(maybeoperator, forspec2);
323
function forspec2(type, value){
324
if (type == ";") cont(forspec3);
325
else if (value == "in") cont(expression);
326
else cont(expression, expect(";"), forspec3);
328
function forspec3(type) {
329
if (type == ")") pass();
330
else cont(expression);
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);
338
function funarg(type, value){
339
if (type == "variable"){register(value); cont();}
347
electricChars: "{}:",
348
configure: function(obj) {
349
if (obj.json != null) json = obj.json;