mirror of
https://github.com/jashkenas/coffeescript.git
synced 2022-11-09 12:23:24 -05:00
renamed Nodes to Expressions
This commit is contained in:
parent
d124f7fc0d
commit
83944950ac
2 changed files with 21 additions and 20 deletions
|
@ -40,14 +40,14 @@ rule
|
|||
|
||||
# All parsing will end in this rule, being the trunk of the AST.
|
||||
Root:
|
||||
/* nothing */ { result = Nodes.new([]) }
|
||||
| Terminator { result = Nodes.new([]) }
|
||||
/* nothing */ { result = Expressions.new([]) }
|
||||
| Terminator { result = Expressions.new([]) }
|
||||
| Expressions { result = val[0] }
|
||||
;
|
||||
|
||||
# Any list of expressions or method body, seperated by line breaks or semis.
|
||||
Expressions:
|
||||
Expression { result = Nodes.new(val) }
|
||||
Expression { result = Expressions.new(val) }
|
||||
| Expressions Terminator Expression { result = val[0] << val[2] }
|
||||
| Expressions Terminator { result = val[0] }
|
||||
| Terminator Expressions { result = val[1] }
|
||||
|
@ -158,7 +158,7 @@ rule
|
|||
|
||||
# The body of a function.
|
||||
CodeBody:
|
||||
/* nothing */ { result = Nodes.new([]) }
|
||||
/* nothing */ { result = Expressions.new([]) }
|
||||
| Expressions { result = val[0] }
|
||||
;
|
||||
|
||||
|
@ -245,8 +245,8 @@ rule
|
|||
| IF Expression
|
||||
Then Expressions
|
||||
ELSE Expressions "." { result = IfNode.new(val[1], val[3], val[5]) }
|
||||
| Expression IF Expression { result = IfNode.new(val[2], Nodes.new([val[0]])) }
|
||||
| Expression UNLESS Expression { result = IfNode.new(val[2], Nodes.new([val[0]]), nil, :invert) }
|
||||
| Expression IF Expression { result = IfNode.new(val[2], Expressions.new([val[0]])) }
|
||||
| Expression UNLESS Expression { result = IfNode.new(val[2], Expressions.new([val[0]]), nil, :invert) }
|
||||
;
|
||||
|
||||
# Try/catch/finally exception handling blocks.
|
||||
|
@ -285,11 +285,11 @@ rule
|
|||
IN Expression "." { result = ForNode.new(val[0], val[6], val[2], val[4]) }
|
||||
| Expression FOR IDENTIFIER
|
||||
IN Expression
|
||||
IF Expression "." { result = ForNode.new(IfNode.new(val[6], Nodes.new([val[0]])), val[4], val[2]) }
|
||||
IF Expression "." { result = ForNode.new(IfNode.new(val[6], Expressions.new([val[0]])), val[4], val[2]) }
|
||||
| Expression FOR
|
||||
IDENTIFIER "," IDENTIFIER
|
||||
IN Expression
|
||||
IF Expression "." { result = ForNode.new(IfNode.new(val[8], Nodes.new([val[0]])), val[6], val[2], val[4]) }
|
||||
IF Expression "." { result = ForNode.new(IfNode.new(val[8], Expressions.new([val[0]])), val[6], val[2], val[4]) }
|
||||
;
|
||||
|
||||
# Switch/Case blocks.
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
module CoffeeScript
|
||||
|
||||
# The abstract base class for all CoffeeScript nodes.
|
||||
class Node
|
||||
# Tabs are two spaces for pretty-printing.
|
||||
TAB = ' '
|
||||
|
@ -13,25 +14,25 @@ module CoffeeScript
|
|||
def compile(indent='', scope=nil, opts={}); end
|
||||
end
|
||||
|
||||
# Collection of nodes each one representing an expression.
|
||||
class Nodes < Node
|
||||
attr_reader :nodes
|
||||
# A collection of nodes, each one representing an expression.
|
||||
class Expressions < Node
|
||||
attr_reader :expressions
|
||||
|
||||
def self.wrap(node)
|
||||
node.is_a?(Nodes) ? node : Nodes.new([node])
|
||||
node.is_a?(Expressions) ? node : Expressions.new([node])
|
||||
end
|
||||
|
||||
def initialize(nodes)
|
||||
@nodes = nodes
|
||||
@expressions = nodes
|
||||
end
|
||||
|
||||
def <<(node)
|
||||
@nodes << node
|
||||
@expressions << node
|
||||
self
|
||||
end
|
||||
|
||||
def flatten
|
||||
@nodes.length == 1 ? @nodes.first : self
|
||||
@expressions.length == 1 ? @expressions.first : self
|
||||
end
|
||||
|
||||
def begin_compile
|
||||
|
@ -46,8 +47,8 @@ module CoffeeScript
|
|||
# inner statements (to make expressions out of them).
|
||||
def compile(indent='', scope=nil, opts={})
|
||||
return begin_compile unless scope
|
||||
@nodes.map { |n|
|
||||
if opts[:return] && n == @nodes.last
|
||||
@expressions.map { |n|
|
||||
if opts[:return] && n == @expressions.last
|
||||
if n.statement? || n.custom_return?
|
||||
"#{indent}#{n.compile(indent, scope, opts)}#{n.line_ending}"
|
||||
else
|
||||
|
@ -458,8 +459,8 @@ module CoffeeScript
|
|||
end
|
||||
|
||||
def compile_statement(indent, scope, opts)
|
||||
if_part = "if (#{@condition.compile(indent, scope, :no_paren => true)}) {\n#{Nodes.wrap(@body).compile(indent + TAB, scope, opts)}\n#{indent}}"
|
||||
else_part = @else_body ? " else {\n#{Nodes.wrap(@else_body).compile(indent + TAB, scope, opts)}\n#{indent}}" : ''
|
||||
if_part = "if (#{@condition.compile(indent, scope, :no_paren => true)}) {\n#{Expressions.wrap(@body).compile(indent + TAB, scope, opts)}\n#{indent}}"
|
||||
else_part = @else_body ? " else {\n#{Expressions.wrap(@else_body).compile(indent + TAB, scope, opts)}\n#{indent}}" : ''
|
||||
if_part + else_part
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in a new issue