fixing if/else rules

This commit is contained in:
Jeremy Ashkenas
2010-02-12 22:16:26 -05:00
parent b36196286a
commit 249bd99656
5 changed files with 41 additions and 60 deletions

View File

@@ -1,5 +1,5 @@
(function(){
var Parser, __a, __b, __c, __d, __e, __f, bnf, grammar, js, name, non_terminal, o, operators, option, parser, part, posix, tokens, unwrap;
var Parser, __a, __b, __c, __d, __e, __f, bnf, grammar, js, name, non_terminal, o, operators, option, parser, parser_path, part, posix, tokens, unwrap;
var __hasProp = Object.prototype.hasOwnProperty;
Parser = require('jison').Parser;
// DSL ===================================================================
@@ -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', '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', 'ELSIF', 'WHILE']];
// Grammar ==============================================================
grammar = {
// All parsing will end in this rule, being the trunk of the AST.
@@ -485,37 +485,26 @@
// The most basic form of "if".
IfBlock: [o("IF Expression Block", function() {
return new IfNode($2, $3);
}), o("IF Expression Block ElsIfs", function() {
return (new IfNode($2, $3)).add_else($4);
})
],
// An elsif portion of an if-else block.
ElsIf: [o("ELSE IfBlock", function() {
return $2.force_statement();
ElsIf: [o("ELSEIF Expression Block", function() {
return new IfNode($2, $3);
})
],
// Multiple elsifs can be chained together.
ElsIfs: [o("ElsIf", function() {
return $1;
return $1.force_statement();
}), o("ElsIfs ElsIf", function() {
return $1.add_else($2);
})
],
// Terminating else bodies are strictly optional.
ElseBody: [o("", function() {
return null;
}), o("ELSE Block", function() {
return $2;
})
],
// All the alternatives for ending an if-else block.
IfEnd: [o("ElseBody", function() {
return $1;
}), o("ElsIfs ElseBody", function() {
return $1.add_else($2);
})
],
// The full complement of if blocks, including postfix one-liner ifs and unlesses.
If: [o("IfBlock IfEnd", function() {
return $1.add_else($2);
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
@@ -568,7 +557,9 @@
// Save the parser to a file.
// puts parser.generate()
posix = require('posix');
posix.open('lib/coffee_script/parser.js', process.O_CREAT | process.O_WRONLY, 0755).addCallback(function(fd) {
parser_path = 'lib/coffee_script/parser.js';
posix.unlink(parser_path);
posix.open(parser_path, process.O_CREAT | process.O_WRONLY, 0755).addCallback(function(fd) {
return posix.write(fd, js);
});
})();