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);
});
})();

View File

@ -117,7 +117,7 @@
this.tag(1, 'PROPERTY_ACCESS');
}
}
this.token(tag, id);
tag === 'ELSE' && this.tag() === 'IF' ? this.tag(1, 'ELSIF') : this.token(tag, id);
this.i += id.length;
return true;
};

File diff suppressed because one or more lines are too long

View File

@ -34,7 +34,7 @@ operators: [
["right", 'THROW', 'FOR', 'NEW', 'SUPER']
["left", 'EXTENDS']
["right", 'ASSIGN', 'RETURN']
["right", '->', '=>', 'UNLESS', 'IF', 'ELSE', 'WHILE']
["right", '->', '=>', 'UNLESS', 'IF', 'ELSE', 'ELSIF', 'WHILE']
]
# Grammar ==============================================================
@ -412,34 +412,23 @@ grammar: {
# The most basic form of "if".
IfBlock: [
o "IF Expression Block", -> new IfNode($2, $3)
o "IF Expression Block ElsIfs", -> (new IfNode($2, $3)).add_else($4)
]
# An elsif portion of an if-else block.
ElsIf: [
o "ELSE IfBlock", -> $2.force_statement()
o "ELSEIF Expression Block", -> new IfNode($2, $3)
]
# Multiple elsifs can be chained together.
ElsIfs: [
o "ElsIf", -> $1
o "ElsIf", -> $1.force_statement()
o "ElsIfs ElsIf", -> $1.add_else($2)
]
# Terminating else bodies are strictly optional.
ElseBody: [
o "", -> null
o "ELSE Block", -> $2
]
# All the alternatives for ending an if-else block.
IfEnd: [
o "ElseBody", -> $1
o "ElsIfs ElseBody", -> $1.add_else($2)
]
# The full complement of if blocks, including postfix one-liner ifs and unlesses.
If: [
o "IfBlock IfEnd", -> $1.add_else($2)
o "IfBlock", -> $1
o "IfBlock ELSE Block", -> $1.add_else($3)
o "Expression IF Expression", -> new IfNode($3, Expressions.wrap([$1]), null, {statement: true})
o "Expression UNLESS Expression", -> new IfNode($3, Expressions.wrap([$1]), null, {statement: true, invert: true})
]
@ -466,5 +455,7 @@ js: parser.generate()
# 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 (fd) ->
parser_path: 'lib/coffee_script/parser.js'
posix.unlink parser_path
posix.open(parser_path, process.O_CREAT | process.O_WRONLY, 0755).addCallback (fd) ->
posix.write(fd, js)

View File

@ -105,7 +105,10 @@ lex::identifier_token: ->
@tokens.splice(-2, 1)
else
@tag(1, 'PROPERTY_ACCESS')
@token(tag, id)
if tag is 'ELSE' and @tag() is 'IF'
@tag(1, 'ELSIF')
else
@token(tag, id)
@i += id.length
true