| 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 ? expressions : 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 |
if (lexical.type == ")") |
| 210 |
indented = lexical.indented; |
| 211 |
lexical = lexical.prev; |
| 212 |
} |
| 213 |
poplex.lex = true; |
| 214 |
// The 'lex' flag on these actions is used by the 'next' function |
| 215 |
// to know they can (and have to) be ran before moving on to the |
| 216 |
// next token. |
| 217 |
|
| 218 |
// Creates an action that discards tokens until it finds one of |
| 219 |
// the given type. |
| 220 |
function expect(wanted){ |
| 221 |
return function expecting(type){ |
| 222 |
if (type == wanted) cont(); |
| 223 |
else if (wanted == ";") pass(); |
| 224 |
else cont(arguments.callee); |
| 225 |
}; |
| 226 |
} |
| 227 |
|
| 228 |
// Looks for a statement, and then calls itself. |
| 229 |
function statements(type){ |
| 230 |
return pass(statement, statements); |
| 231 |
} |
| 232 |
function expressions(type){ |
| 233 |
return pass(expression, expressions); |
| 234 |
} |
| 235 |
// Dispatches various types of statements based on the type of the |
| 236 |
// current token. |
| 237 |
function statement(type){ |
| 238 |
if (type == "var") cont(pushlex("vardef"), vardef1, expect(";"), poplex); |
| 239 |
else if (type == "keyword a") cont(pushlex("form"), expression, statement, poplex); |
| 240 |
else if (type == "keyword b") cont(pushlex("form"), statement, poplex); |
| 241 |
else if (type == "{") cont(pushlex("}"), block, poplex); |
| 242 |
else if (type == ";") cont(); |
| 243 |
else if (type == "function") cont(functiondef); |
| 244 |
else if (type == "for") cont(pushlex("form"), expect("("), pushlex(")"), forspec1, expect(")"), poplex, statement, poplex); |
| 245 |
else if (type == "variable") cont(pushlex("stat"), maybelabel); |
| 246 |
else if (type == "switch") cont(pushlex("form"), expression, pushlex("}", "switch"), expect("{"), block, poplex, poplex); |
| 247 |
else if (type == "case") cont(expression, expect(":")); |
| 248 |
else if (type == "default") cont(expect(":")); |
| 249 |
else if (type == "catch") cont(pushlex("form"), pushcontext, expect("("), funarg, expect(")"), statement, poplex, popcontext); |
| 250 |
else pass(pushlex("stat"), expression, expect(";"), poplex); |
| 251 |
} |
| 252 |
// Dispatch expression types. |
| 253 |
function expression(type){ |
| 254 |
if (atomicTypes.hasOwnProperty(type)) cont(maybeoperator); |
| 255 |
else if (type == "function") cont(functiondef); |
| 256 |
else if (type == "keyword c") cont(expression); |
| 257 |
else if (type == "(") cont(pushlex(")"), expression, expect(")"), poplex, maybeoperator); |
| 258 |
else if (type == "operator") cont(expression); |
| 259 |
else if (type == "[") cont(pushlex("]"), commasep(expression, "]"), poplex, maybeoperator); |
| 260 |
else if (type == "{") cont(pushlex("}"), commasep(objprop, "}"), poplex, maybeoperator); |
| 261 |
else cont(); |
| 262 |
} |
| 263 |
// Called for places where operators, function calls, or |
| 264 |
// subscripts are valid. Will skip on to the next action if none |
| 265 |
// is found. |
| 266 |
function maybeoperator(type, value){ |
| 267 |
if (type == "operator" && /\+\+|--/.test(value)) cont(maybeoperator); |
| 268 |
else if (type == "operator") cont(expression); |
| 269 |
else if (type == ";") pass(); |
| 270 |
else if (type == "(") cont(pushlex(")"), commasep(expression, ")"), poplex, maybeoperator); |
| 271 |
else if (type == ".") cont(property, maybeoperator); |
| 272 |
else if (type == "[") cont(pushlex("]"), expression, expect("]"), poplex, maybeoperator); |
| 273 |
} |
| 274 |
// When a statement starts with a variable name, it might be a |
| 275 |
// label. If no colon follows, it's a regular statement. |
| 276 |
function maybelabel(type){ |
| 277 |
if (type == ":") cont(poplex, statement); |
| 278 |
else pass(maybeoperator, expect(";"), poplex); |
| 279 |
} |
| 280 |
// Property names need to have their style adjusted -- the |
| 281 |
// tokenizer thinks they are variables. |
| 282 |
function property(type){ |
| 283 |
if (type == "variable") {mark("js-property"); cont();} |
| 284 |
} |
| 285 |
// This parses a property and its value in an object literal. |
| 286 |
function objprop(type){ |
| 287 |
if (type == "variable") mark("js-property"); |
| 288 |
if (atomicTypes.hasOwnProperty(type)) cont(expect(":"), expression); |
| 289 |
} |
| 290 |
// Parses a comma-separated list of the things that are recognized |
| 291 |
// by the 'what' argument. |
| 292 |
function commasep(what, end){ |
| 293 |
function proceed(type) { |
| 294 |
if (type == ",") cont(what, proceed); |
| 295 |
else if (type == end) cont(); |
| 296 |
else cont(expect(end)); |
| 297 |
} |
| 298 |
return function commaSeparated(type) { |
| 299 |
if (type == end) cont(); |
| 300 |
else pass(what, proceed); |
| 301 |
}; |
| 302 |
} |
| 303 |
// Look for statements until a closing brace is found. |
| 304 |
function block(type){ |
| 305 |
if (type == "}") cont(); |
| 306 |
else pass(statement, block); |
| 307 |
} |
| 308 |
// Variable definitions are split into two actions -- 1 looks for |
| 309 |
// a name or the end of the definition, 2 looks for an '=' sign or |
| 310 |
// a comma. |
| 311 |
function vardef1(type, value){ |
| 312 |
if (type == "variable"){register(value); cont(vardef2);} |
| 313 |
else cont(); |
| 314 |
} |
| 315 |
function vardef2(type, value){ |
| 316 |
if (value == "=") cont(expression, vardef2); |
| 317 |
else if (type == ",") cont(vardef1); |
| 318 |
} |
| 319 |
// For loops. |
| 320 |
function forspec1(type){ |
| 321 |
if (type == "var") cont(vardef1, forspec2); |
| 322 |
else if (type == ";") pass(forspec2); |
| 323 |
else if (type == "variable") cont(formaybein); |
| 324 |
else pass(forspec2); |
| 325 |
} |
| 326 |
function formaybein(type, value){ |
| 327 |
if (value == "in") cont(expression); |
| 328 |
else cont(maybeoperator, forspec2); |
| 329 |
} |
| 330 |
function forspec2(type, value){ |
| 331 |
if (type == ";") cont(forspec3); |
| 332 |
else if (value == "in") cont(expression); |
| 333 |
else cont(expression, expect(";"), forspec3); |
| 334 |
} |
| 335 |
function forspec3(type) { |
| 336 |
if (type == ")") pass(); |
| 337 |
else cont(expression); |
| 338 |
} |
| 339 |
// A function definition creates a new context, and the variables |
| 340 |
// in its argument list have to be added to this context. |
| 341 |
function functiondef(type, value){ |
| 342 |
if (type == "variable"){register(value); cont(functiondef);} |
| 343 |
else if (type == "(") cont(pushcontext, commasep(funarg, ")"), statement, popcontext); |
| 344 |
} |
| 345 |
function funarg(type, value){ |
| 346 |
if (type == "variable"){register(value); cont();} |
| 347 |
} |
| 348 |
|
| 349 |
return parser; |
| 350 |
} |
| 351 |
|
| 352 |
return { |
| 353 |
make: parseJS, |
| 354 |
electricChars: "{}:", |
| 355 |
configure: function(obj) { |
| 356 |
if (obj.json != null) json = obj.json; |
| 357 |
} |
| 358 |
}; |
| 359 |
})(); |