finally, think we have this if/else/if/else/else thing licke

This commit is contained in:
Jeremy Ashkenas
2010-02-13 01:02:10 -05:00
parent 0f26072ad0
commit 3dc456572b
7 changed files with 36 additions and 38 deletions

View File

@@ -16,7 +16,7 @@
}
};
// Precedence ===========================================================
operators = [["left", '?'], ["nonassoc", 'UMINUS', 'UPLUS', 'NOT', '!', '!!', '~', '++', '--'], ["left", '*', '/', '%'], ["left", '+', '-'], ["left", '<<', '>>', '>>>'], ["left", '&', '|', '^'], ["left", '<=', '<', '>', '>='], ["right", 'DELETE', 'INSTANCEOF', 'TYPEOF'], ["right", '==', '!=', 'IS', 'ISNT'], ["left", '&&', '||', 'AND', 'OR'], ["right", '-=', '+=', '/=', '*=', '%=', '||=', '&&=', '?='], ["left", '.'], ["right", 'INDENT'], ["left", 'OUTDENT'], ["right", 'WHEN', 'LEADING_WHEN', 'IN', 'OF', 'BY'], ["right", 'THROW', 'FOR', 'NEW', 'SUPER'], ["left", 'EXTENDS'], ["right", 'ASSIGN', 'RETURN'], ["right", '->', '=>', 'UNLESS', 'IF', 'ELSE', 'ELSIF', 'WHILE']];
operators = [["left", '?'], ["nonassoc", 'UMINUS', 'UPLUS', 'NOT', '!', '!!', '~', '++', '--'], ["left", '*', '/', '%'], ["left", '+', '-'], ["left", '<<', '>>', '>>>'], ["left", '&', '|', '^'], ["left", '<=', '<', '>', '>='], ["right", 'DELETE', 'INSTANCEOF', 'TYPEOF'], ["right", '==', '!=', 'IS', 'ISNT'], ["left", '&&', '||', 'AND', 'OR'], ["right", '-=', '+=', '/=', '*=', '%=', '||=', '&&=', '?='], ["left", '.'], ["right", 'INDENT'], ["left", 'OUTDENT'], ["right", 'WHEN', 'LEADING_WHEN', 'IN', 'OF', 'BY'], ["right", 'THROW', 'FOR', 'NEW', 'SUPER'], ["left", 'EXTENDS'], ["right", 'ASSIGN', 'RETURN'], ["right", '->', '=>', 'UNLESS', 'IF', 'ELSE', 'WHILE']];
// Grammar ==============================================================
grammar = {
// All parsing will end in this rule, being the trunk of the AST.
@@ -483,19 +483,21 @@
})
],
// The most basic form of "if".
IfBlock: [o("IF Expression Block", function() {
IfStart: [o("IF Expression Block", function() {
return new IfNode($2, $3);
}), o("IF Expression Block ElsIfs", function() {
return (new IfNode($2, $3)).add_else($4);
}), o("IfStart ElsIfs", function() {
return $1.add_else($2);
})
],
ElsIf: [o("ELSEIF Expression Block", function() {
return new IfNode($2, $3);
IfBlock: [o("IfStart", function() {
return $1;
}), o("IfStart ELSE Block", function() {
return $1.add_else($3);
})
],
// Multiple elsifs can be chained together.
ElsIfs: [o("ElsIf", function() {
return $1.force_statement();
ElsIfs: [o("ELSE IF Expression Block", function() {
return (new IfNode($3, $4)).force_statement();
}), o("ElsIfs ElsIf", function() {
return $1.add_else($2);
})
@@ -503,8 +505,6 @@
// The full complement of if blocks, including postfix one-liner ifs and unlesses.
If: [o("IfBlock", function() {
return $1;
}), o("IfBlock ELSE Block", function() {
return $1.add_else($3);
}), o("Expression IF Expression", function() {
return new IfNode($3, Expressions.wrap([$1]), null, {
statement: true