1
0
Fork 0
mirror of https://github.com/jashkenas/coffeescript.git synced 2022-11-09 12:23:24 -05:00

Added Statement to the grammar.

This commit is contained in:
Tim Jones 2010-04-06 02:26:23 +12:00
parent 19ed63129e
commit de955dacc4
3 changed files with 260 additions and 213 deletions

View file

@ -53,25 +53,33 @@
return new Expressions();
}), o("TERMINATOR", function() {
return new Expressions();
}), o("Expressions"), o("Block TERMINATOR")
}), o("Body"), o("Block TERMINATOR")
],
// Any list of expressions or method body, seperated by line breaks or
// semicolons.
Expressions: [o("Expression", function() {
// Any list of statements and expressions, seperated by line breaks or semicolons.
Body: [o("Line", function() {
return Expressions.wrap([$1]);
}), o("Expressions TERMINATOR Expression", function() {
}), o("Body TERMINATOR Line", function() {
return $1.push($3);
}), o("Expressions TERMINATOR")
}), o("Body TERMINATOR")
],
// Expressions and statements, which make up a line in a body.
Line: [o("Expression"), o("Statement")],
// Pure statements which cannot be expressions.
Statement: [o("Return"), o("Throw"), o("BREAK", function() {
return new LiteralNode(yytext);
}), o("CONTINUE", function() {
return new LiteralNode(yytext);
})
],
// All the different types of expressions in our language. The basic unit of
// CoffeeScript is the **Expression** -- you'll notice that there is no
// "statement" nonterminal. Expressions serve as the building blocks
// of many other rules, making them somewhat circular.
Expression: [o("Value"), o("Call"), o("Curry"), o("Code"), o("Operation"), o("Assign"), o("If"), o("Try"), o("Throw"), o("Return"), o("While"), o("For"), o("Switch"), o("Extends"), o("Class"), o("Splat"), o("Existence"), o("Comment"), o("Extension")],
// CoffeeScript is the **Expression** -- everything that can be an expression
// is one. Expressions serve as the building blocks of many other rules, making
// them somewhat circular.
Expression: [o("Value"), o("Call"), o("Curry"), o("Code"), o("Operation"), o("Assign"), o("If"), o("Try"), o("While"), o("For"), o("Switch"), o("Extends"), o("Class"), o("Splat"), o("Existence"), o("Comment"), o("Extension")],
// A an indented block of expressions. Note that the [Rewriter](rewriter.html)
// will convert some postfix forms into blocks for us, by adjusting the
// token stream.
Block: [o("INDENT Expressions OUTDENT", function() {
Block: [o("INDENT Body OUTDENT", function() {
return $2;
}), o("INDENT OUTDENT", function() {
return new Expressions();
@ -98,10 +106,6 @@
return new LiteralNode(yytext);
}), o("REGEX", function() {
return new LiteralNode(yytext);
}), o("BREAK", function() {
return new LiteralNode(yytext);
}), o("CONTINUE", function() {
return new LiteralNode(yytext);
}), o("TRUE", function() {
return new LiteralNode(true);
}), o("FALSE", function() {
@ -420,7 +424,7 @@
// not an **Expression**, so if you need to use an expression in a place
// where only values are accepted, wrapping it in parentheses will always do
// the trick.
Parenthetical: [o("( Expression )", function() {
Parenthetical: [o("( Line )", function() {
return new ParentheticalNode($2);
})
],
@ -443,6 +447,8 @@
// or postfix, with a single expression. There is no do..while.
While: [o("WhileSource Block", function() {
return $1.add_body($2);
}), o("Statement WhileSource", function() {
return $2.add_body(Expressions.wrap([$1]));
}), o("Expression WhileSource", function() {
return $2.add_body(Expressions.wrap([$1]));
})
@ -450,7 +456,9 @@
// Array, object, and range comprehensions, at the most generic level.
// Comprehensions can either be normal, with a block of expressions to execute,
// or postfix, with a single expression.
For: [o("Expression FOR ForVariables ForSource", function() {
For: [o("Statement FOR ForVariables ForSource", function() {
return new ForNode($1, $4, $3[0], $3[1]);
}), o("Expression FOR ForVariables ForSource", function() {
return new ForNode($1, $4, $3[0], $3[1]);
}), o("FOR ForVariables ForSource Block", function() {
return new ForNode($4, $3, $2[0], $2[1]);
@ -556,10 +564,19 @@
],
// The full complement of *if* expressions, including postfix one-liner
// *if* and *unless*.
If: [o("IfBlock"), o("Expression IF Expression", function() {
If: [o("IfBlock"), o("Statement IF Expression", function() {
return new IfNode($3, Expressions.wrap([$1]), null, {
statement: true
});
}), o("Expression IF Expression", function() {
return new IfNode($3, Expressions.wrap([$1]), null, {
statement: true
});
}), o("Statement UNLESS Expression", function() {
return new IfNode($3, Expressions.wrap([$1]), null, {
statement: true,
invert: true
});
}), o("Expression UNLESS Expression", function() {
return new IfNode($3, Expressions.wrap([$1]), null, {
statement: true,

File diff suppressed because one or more lines are too long

View file

@ -55,22 +55,35 @@ grammar: {
Root: [
o "", -> new Expressions()
o "TERMINATOR", -> new Expressions()
o "Expressions"
o "Body"
o "Block TERMINATOR"
]
# Any list of expressions or method body, seperated by line breaks or
# semicolons.
Expressions: [
o "Expression", -> Expressions.wrap [$1]
o "Expressions TERMINATOR Expression", -> $1.push $3
o "Expressions TERMINATOR"
# Any list of statements and expressions, seperated by line breaks or semicolons.
Body: [
o "Line", -> Expressions.wrap [$1]
o "Body TERMINATOR Line", -> $1.push $3
o "Body TERMINATOR"
]
# Expressions and statements, which make up a line in a body.
Line: [
o "Expression"
o "Statement"
]
# Pure statements which cannot be expressions.
Statement: [
o "Return"
o "Throw"
o "BREAK", -> new LiteralNode yytext
o "CONTINUE", -> new LiteralNode yytext
]
# All the different types of expressions in our language. The basic unit of
# CoffeeScript is the **Expression** -- you'll notice that there is no
# "statement" nonterminal. Expressions serve as the building blocks
# of many other rules, making them somewhat circular.
# CoffeeScript is the **Expression** -- everything that can be an expression
# is one. Expressions serve as the building blocks of many other rules, making
# them somewhat circular.
Expression: [
o "Value"
o "Call"
@ -80,8 +93,6 @@ grammar: {
o "Assign"
o "If"
o "Try"
o "Throw"
o "Return"
o "While"
o "For"
o "Switch"
@ -97,7 +108,7 @@ grammar: {
# will convert some postfix forms into blocks for us, by adjusting the
# token stream.
Block: [
o "INDENT Expressions OUTDENT", -> $2
o "INDENT Body OUTDENT", -> $2
o "INDENT OUTDENT", -> new Expressions()
o "TERMINATOR Comment", -> Expressions.wrap [$2]
]
@ -120,8 +131,6 @@ grammar: {
o "AlphaNumeric"
o "JS", -> new LiteralNode yytext
o "REGEX", -> new LiteralNode yytext
o "BREAK", -> new LiteralNode yytext
o "CONTINUE", -> new LiteralNode yytext
o "TRUE", -> new LiteralNode true
o "FALSE", -> new LiteralNode false
o "YES", -> new LiteralNode true
@ -396,7 +405,7 @@ grammar: {
# where only values are accepted, wrapping it in parentheses will always do
# the trick.
Parenthetical: [
o "( Expression )", -> new ParentheticalNode $2
o "( Line )", -> new ParentheticalNode $2
]
# A language extension to CoffeeScript from the outside. We simply pass
@ -415,6 +424,7 @@ grammar: {
# or postfix, with a single expression. There is no do..while.
While: [
o "WhileSource Block", -> $1.add_body $2
o "Statement WhileSource", -> $2.add_body Expressions.wrap [$1]
o "Expression WhileSource", -> $2.add_body Expressions.wrap [$1]
]
@ -422,6 +432,7 @@ grammar: {
# Comprehensions can either be normal, with a block of expressions to execute,
# or postfix, with a single expression.
For: [
o "Statement FOR ForVariables ForSource", -> new ForNode $1, $4, $3[0], $3[1]
o "Expression FOR ForVariables ForSource", -> new ForNode $1, $4, $3[0], $3[1]
o "FOR ForVariables ForSource Block", -> new ForNode $4, $3, $2[0], $2[1]
]
@ -491,7 +502,9 @@ grammar: {
# *if* and *unless*.
If: [
o "IfBlock"
o "Statement IF Expression", -> new IfNode $3, Expressions.wrap([$1]), null, {statement: true}
o "Expression IF Expression", -> new IfNode $3, Expressions.wrap([$1]), null, {statement: true}
o "Statement UNLESS Expression", -> new IfNode $3, Expressions.wrap([$1]), null, {statement: true, invert: true}
o "Expression UNLESS Expression", -> new IfNode $3, Expressions.wrap([$1]), null, {statement: true, invert: true}
]