Skip to content

Commit

Permalink
Fixed mapping of horizontal split view
Browse files Browse the repository at this point in the history
  • Loading branch information
Rob Linton committed Nov 2, 2010
1 parent 09dfa43 commit d7dd7e1
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 106 deletions.
108 changes: 66 additions & 42 deletions lib/parse-js._js
Expand Up @@ -50,8 +50,6 @@
***********************************************************************/

var jsp = {};

/* -----[ Tokenizer (constants) ]----- */

var KEYWORDS = array_to_hash([
Expand Down Expand Up @@ -79,8 +77,7 @@ var KEYWORDS = array_to_hash([
"var",
"void",
"while",
"with",
"NaN"
"with"
]);

var RESERVED_WORDS = array_to_hash([
Expand Down Expand Up @@ -120,15 +117,15 @@ var KEYWORDS_BEFORE_EXPRESSION = array_to_hash([
"return",
"new",
"delete",
"throw"
"throw",
"else"
]);

var KEYWORDS_ATOM = array_to_hash([
"false",
"null",
"true",
"undefined",
"NaN"
"undefined"
]);

var OPERATOR_CHARS = array_to_hash(characters("+-*&%=<>!?|~^"));
Expand Down Expand Up @@ -181,6 +178,7 @@ var OPERATORS = array_to_hash([
"%=",
"|=",
"^=",
"&=",
"&&",
"||"
]);
Expand Down Expand Up @@ -250,7 +248,7 @@ var EX_EOF = {};
function tokenizer($TEXT, skip_comments) {

var S = {
text : $TEXT.replace(/\r\n?|[\n\u2028\u2029]/g, "\n"),
text : $TEXT.replace(/\r\n?|[\n\u2028\u2029]/g, "\n").replace(/^\uFEFF/, ''),
pos : 0,
tokpos : 0,
line : 0,
Expand Down Expand Up @@ -502,7 +500,9 @@ function tokenizer($TEXT, skip_comments) {
}
};

function next_token() {
function next_token(force_regexp) {
if (force_regexp)
return read_regexp();
skip_whitespace();
start_token();
var ch = peek();
Expand Down Expand Up @@ -549,7 +549,7 @@ var ASSIGNMENT = (function(a, ret, i){
}
return ret;
})(
["+=", "-=", "/=", "*=", "%=", ">>=", "<<=", ">>>=", "~=", "%=", "|=", "^="],
["+=", "-=", "/=", "*=", "%=", ">>=", "<<=", ">>>=", "~=", "%=", "|=", "^=", "&="],
{ "=": true },
0
);
Expand Down Expand Up @@ -680,14 +680,18 @@ function parse($TEXT, strict_mode, embed_tokens) {
return new NodeWithToken(str, start, end);
};

var statement = embed_tokens ? function(allow_case) {
var statement = embed_tokens ? function() {
var start = S.token;
var stmt = $statement(allow_case);
var stmt = $statement();
stmt[0] = add_tokens(stmt[0], start, prev());
return stmt;
} : $statement;

function $statement(allow_case) {
function $statement() {
if (is("operator", "/")) {
S.peeked = null;
S.token = S.input(true); // force regexp
}
switch (S.token.type) {
case "num":
case "string":
Expand Down Expand Up @@ -723,21 +727,10 @@ function parse($TEXT, strict_mode, embed_tokens) {
case "continue":
return break_cont("continue");

case "case":
if (!allow_case)
unexpected();
return as("case", prog1(expression, curry(expect, ":")));

case "debugger":
semicolon();
return as("debugger");

case "default":
if (!allow_case)
unexpected();
expect(":");
return as("default");

case "do":
return (function(body){
expect_token("keyword", "while");
Expand All @@ -764,7 +757,7 @@ function parse($TEXT, strict_mode, embed_tokens) {
: prog1(expression, semicolon));

case "switch":
return as("switch", parenthesised(), in_loop(curry(block_, true)));
return as("switch", parenthesised(), switch_block_());

case "throw":
return as("throw", prog1(expression, semicolon));
Expand Down Expand Up @@ -804,14 +797,14 @@ function parse($TEXT, strict_mode, embed_tokens) {
};

function break_cont(type) {
if (S.in_loop == 0)
croak(type + " not inside a loop or switch");
var name = is("name") ? S.token.value : null;
if (name != null) {
next();
if (!member(name, S.labels))
croak("Label " + name + " without matching loop or statement");
}
else if (S.in_loop == 0)
croak(type + " not inside a loop or switch");
semicolon();
return as(type, name);
};
Expand Down Expand Up @@ -861,8 +854,11 @@ function parse($TEXT, strict_mode, embed_tokens) {
// body
(function(){
++S.in_function;
var loop = S.in_loop;
S.in_loop = 0;
var a = block_();
--S.in_function;
S.in_loop = loop;
return a;
})());
};
Expand All @@ -876,17 +872,43 @@ function parse($TEXT, strict_mode, embed_tokens) {
return as("if", cond, body, belse);
};

function block_(allow_case) {
function block_() {
expect("{");
var a = [];
while (!is("punc", "}")) {
if (is("eof")) unexpected();
a.push(statement(allow_case));
a.push(statement());
}
next();
return a;
};

var switch_block_ = curry(in_loop, function(){
expect("{");
var a = [], cur = null;
while (!is("punc", "}")) {
if (is("eof")) unexpected();
if (is("keyword", "case")) {
next();
cur = [];
a.push([ expression(), cur ]);
expect(":");
}
else if (is("keyword", "default")) {
next();
expect(":");
cur = [];
a.push([ null, cur ]);
}
else {
if (!cur) unexpected();
cur.push(statement());
}
}
next();
return a;
});

function try_() {
var body = block_(), bcatch, bfinally;
if (is("keyword", "catch")) {
Expand Down Expand Up @@ -1078,32 +1100,33 @@ function parse($TEXT, strict_mode, embed_tokens) {
return expr_op(expr_atom(true), 0);
};

function maybe_conditional(commas) {
if (arguments.length == 0)
commas = true;
function maybe_conditional() {
var expr = expr_ops();
if (is("operator", "?")) {
next();
var yes = expression();
var yes = expression(false);
expect(":");
return as("conditional", expr, yes, expression(commas));
return as("conditional", expr, yes, expression(false));
}
return expr;
};

function is_assignable(expr) {
expr = expr[0];
return expr == "name" || expr == "dot" || expr == "sub";
switch (expr[0]) {
case "dot":
case "sub":
return true;
case "name":
return expr[1] != "this";
}
};

function maybe_assign(commas) {
if (arguments.length == 0)
commas = true;
var left = maybe_conditional(commas), val = S.token.value;
function maybe_assign() {
var left = maybe_conditional(), val = S.token.value;
if (is("operator") && HOP(ASSIGNMENT, val)) {
if (is_assignable(left)) {
next();
return as("assign", ASSIGNMENT[val], left, maybe_assign(commas));
return as("assign", ASSIGNMENT[val], left, maybe_assign());
}
croak("Invalid assignment");
}
Expand All @@ -1113,7 +1136,7 @@ function parse($TEXT, strict_mode, embed_tokens) {
function expression(commas) {
if (arguments.length == 0)
commas = true;
var expr = maybe_assign(commas);
var expr = maybe_assign();
if (commas && is("punc", ",")) {
next();
return as("seq", expr, expression());
Expand Down Expand Up @@ -1181,6 +1204,7 @@ function HOP(obj, prop) {

/* -----[ Exports ]----- */

var jsp = {};
jsp.tokenizer = tokenizer;
jsp.parse = parse;
jsp.slice = slice;
Expand Down

0 comments on commit d7dd7e1

Please sign in to comment.