mirror of
https://github.com/jashkenas/coffeescript.git
synced 2026-02-19 03:44:23 -05:00
adding comprehensive attr_readers to the AST for testing
This commit is contained in:
@@ -86,6 +86,8 @@ module CoffeeScript
|
|||||||
class LiteralNode < Node
|
class LiteralNode < Node
|
||||||
STATEMENTS = ['break', 'continue']
|
STATEMENTS = ['break', 'continue']
|
||||||
|
|
||||||
|
attr_reader :value
|
||||||
|
|
||||||
def initialize(value)
|
def initialize(value)
|
||||||
@value = value
|
@value = value
|
||||||
end
|
end
|
||||||
@@ -104,6 +106,8 @@ module CoffeeScript
|
|||||||
statement
|
statement
|
||||||
custom_return
|
custom_return
|
||||||
|
|
||||||
|
attr_reader :expression
|
||||||
|
|
||||||
def initialize(expression)
|
def initialize(expression)
|
||||||
@expression = expression
|
@expression = expression
|
||||||
end
|
end
|
||||||
@@ -120,6 +124,8 @@ module CoffeeScript
|
|||||||
class CallNode < Node
|
class CallNode < Node
|
||||||
LEADING_DOT = /\A\./
|
LEADING_DOT = /\A\./
|
||||||
|
|
||||||
|
attr_reader :variable, :arguments
|
||||||
|
|
||||||
def initialize(variable, arguments=[])
|
def initialize(variable, arguments=[])
|
||||||
@variable, @arguments = variable, arguments
|
@variable, @arguments = variable, arguments
|
||||||
end
|
end
|
||||||
@@ -148,10 +154,10 @@ module CoffeeScript
|
|||||||
|
|
||||||
# A value, indexed or dotted into or vanilla.
|
# A value, indexed or dotted into or vanilla.
|
||||||
class ValueNode < Node
|
class ValueNode < Node
|
||||||
attr_reader :name, :properties, :last
|
attr_reader :literal, :properties, :last
|
||||||
|
|
||||||
def initialize(name, properties=[])
|
def initialize(literal, properties=[])
|
||||||
@name, @properties = name, properties
|
@literal, @properties = literal, properties
|
||||||
end
|
end
|
||||||
|
|
||||||
def <<(other)
|
def <<(other)
|
||||||
@@ -164,7 +170,7 @@ module CoffeeScript
|
|||||||
end
|
end
|
||||||
|
|
||||||
def compile(indent, scope, opts={})
|
def compile(indent, scope, opts={})
|
||||||
parts = [@name, @properties].flatten.map do |v|
|
parts = [@literal, @properties].flatten.map do |v|
|
||||||
v.respond_to?(:compile) ? v.compile(indent, scope) : v.to_s
|
v.respond_to?(:compile) ? v.compile(indent, scope) : v.to_s
|
||||||
end
|
end
|
||||||
@last = parts.last
|
@last = parts.last
|
||||||
@@ -174,6 +180,8 @@ module CoffeeScript
|
|||||||
|
|
||||||
# A dotted accessor into a part of a value.
|
# A dotted accessor into a part of a value.
|
||||||
class AccessorNode
|
class AccessorNode
|
||||||
|
attr_reader :name
|
||||||
|
|
||||||
def initialize(name)
|
def initialize(name)
|
||||||
@name = name
|
@name = name
|
||||||
end
|
end
|
||||||
@@ -185,6 +193,8 @@ module CoffeeScript
|
|||||||
|
|
||||||
# An indexed accessor into a part of an array or object.
|
# An indexed accessor into a part of an array or object.
|
||||||
class IndexNode
|
class IndexNode
|
||||||
|
attr_reader :index
|
||||||
|
|
||||||
def initialize(index)
|
def initialize(index)
|
||||||
@index = index
|
@index = index
|
||||||
end
|
end
|
||||||
@@ -198,6 +208,8 @@ module CoffeeScript
|
|||||||
# specifies the index of the end of the slice (just like the first parameter)
|
# specifies the index of the end of the slice (just like the first parameter)
|
||||||
# is the index of the beginning.
|
# is the index of the beginning.
|
||||||
class SliceNode
|
class SliceNode
|
||||||
|
attr_reader :from, :to
|
||||||
|
|
||||||
def initialize(from, to)
|
def initialize(from, to)
|
||||||
@from, @to = from, to
|
@from, @to = from, to
|
||||||
end
|
end
|
||||||
@@ -248,6 +260,8 @@ module CoffeeScript
|
|||||||
}
|
}
|
||||||
CONDITIONALS = ['||=', '&&=']
|
CONDITIONALS = ['||=', '&&=']
|
||||||
|
|
||||||
|
attr_reader :operator, :first, :second
|
||||||
|
|
||||||
def initialize(operator, first, second=nil)
|
def initialize(operator, first, second=nil)
|
||||||
@first, @second = first, second
|
@first, @second = first, second
|
||||||
@operator = CONVERSIONS[operator] || operator
|
@operator = CONVERSIONS[operator] || operator
|
||||||
@@ -277,6 +291,8 @@ module CoffeeScript
|
|||||||
|
|
||||||
# A function definition. The only node that creates a new Scope.
|
# A function definition. The only node that creates a new Scope.
|
||||||
class CodeNode < Node
|
class CodeNode < Node
|
||||||
|
attr_reader :params, :body
|
||||||
|
|
||||||
def initialize(params, body)
|
def initialize(params, body)
|
||||||
@params = params
|
@params = params
|
||||||
@body = body
|
@body = body
|
||||||
@@ -293,6 +309,8 @@ module CoffeeScript
|
|||||||
|
|
||||||
# An object literal.
|
# An object literal.
|
||||||
class ObjectNode < Node
|
class ObjectNode < Node
|
||||||
|
attr_reader :properties
|
||||||
|
|
||||||
def initialize(properties = [])
|
def initialize(properties = [])
|
||||||
@properties = properties
|
@properties = properties
|
||||||
end
|
end
|
||||||
@@ -305,6 +323,8 @@ module CoffeeScript
|
|||||||
|
|
||||||
# An array literal.
|
# An array literal.
|
||||||
class ArrayNode < Node
|
class ArrayNode < Node
|
||||||
|
attr_reader :objects
|
||||||
|
|
||||||
def initialize(objects=[])
|
def initialize(objects=[])
|
||||||
@objects = objects
|
@objects = objects
|
||||||
end
|
end
|
||||||
@@ -320,6 +340,8 @@ module CoffeeScript
|
|||||||
class WhileNode < Node
|
class WhileNode < Node
|
||||||
statement
|
statement
|
||||||
|
|
||||||
|
attr_reader :condition, :body
|
||||||
|
|
||||||
def initialize(condition, body)
|
def initialize(condition, body)
|
||||||
@condition, @body = condition, body
|
@condition, @body = condition, body
|
||||||
end
|
end
|
||||||
@@ -342,6 +364,8 @@ module CoffeeScript
|
|||||||
custom_return
|
custom_return
|
||||||
custom_assign
|
custom_assign
|
||||||
|
|
||||||
|
attr_reader :body, :source, :name, :index
|
||||||
|
|
||||||
def initialize(body, source, name, index=nil)
|
def initialize(body, source, name, index=nil)
|
||||||
@body, @source, @name, @index = body, source, name, index
|
@body, @source, @name, @index = body, source, name, index
|
||||||
end
|
end
|
||||||
@@ -383,6 +407,8 @@ module CoffeeScript
|
|||||||
class TryNode < Node
|
class TryNode < Node
|
||||||
statement
|
statement
|
||||||
|
|
||||||
|
attr_reader :try, :error, :recovery, :finally
|
||||||
|
|
||||||
def initialize(try, error, recovery, finally=nil)
|
def initialize(try, error, recovery, finally=nil)
|
||||||
@try, @error, @recovery, @finally = try, error, recovery, finally
|
@try, @error, @recovery, @finally = try, error, recovery, finally
|
||||||
end
|
end
|
||||||
@@ -402,6 +428,8 @@ module CoffeeScript
|
|||||||
class ThrowNode < Node
|
class ThrowNode < Node
|
||||||
statement
|
statement
|
||||||
|
|
||||||
|
attr_reader :expression
|
||||||
|
|
||||||
def initialize(expression)
|
def initialize(expression)
|
||||||
@expression = expression
|
@expression = expression
|
||||||
end
|
end
|
||||||
@@ -413,6 +441,8 @@ module CoffeeScript
|
|||||||
|
|
||||||
# An extra set of parenthesis, supplied by the script source.
|
# An extra set of parenthesis, supplied by the script source.
|
||||||
class ParentheticalNode < Node
|
class ParentheticalNode < Node
|
||||||
|
attr_reader :expressions
|
||||||
|
|
||||||
def initialize(expressions)
|
def initialize(expressions)
|
||||||
@expressions = expressions
|
@expressions = expressions
|
||||||
end
|
end
|
||||||
@@ -429,6 +459,8 @@ module CoffeeScript
|
|||||||
# Single-expression IfNodes are compiled into ternary operators if possible,
|
# Single-expression IfNodes are compiled into ternary operators if possible,
|
||||||
# because ternaries are first-class returnable assignable expressions.
|
# because ternaries are first-class returnable assignable expressions.
|
||||||
class IfNode < Node
|
class IfNode < Node
|
||||||
|
attr_reader :condition, :body, :else_body
|
||||||
|
|
||||||
def initialize(condition, body, else_body=nil, tag=nil)
|
def initialize(condition, body, else_body=nil, tag=nil)
|
||||||
@condition = condition
|
@condition = condition
|
||||||
@body = body && body.unwrap
|
@body = body && body.unwrap
|
||||||
|
|||||||
@@ -13,25 +13,29 @@ class ParserTest < Test::Unit::TestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_parsing_a_basic_assignment
|
def test_parsing_a_basic_assignment
|
||||||
nodes = @par.parse("a: 'one'")
|
nodes = @par.parse("a: 'one'").expressions
|
||||||
assert nodes.expressions.length == 1
|
assert nodes.length == 1
|
||||||
assign = nodes.expressions.first
|
assign = nodes.first
|
||||||
assert assign.is_a? AssignNode
|
assert assign.is_a? AssignNode
|
||||||
assert assign.variable.name == 'a'
|
assert assign.variable.literal == 'a'
|
||||||
end
|
end
|
||||||
#
|
|
||||||
# def test_lexing_object_literal
|
def test_parsing_an_object_literal
|
||||||
# code = "{one : 1}"
|
nodes = @par.parse("{one : 1 \n two : 2}").expressions
|
||||||
# assert @lex.tokenize(code) == [["{", "{"], [:IDENTIFIER, "one"], [":", ":"],
|
obj = nodes.first.literal
|
||||||
# [:NUMBER, "1"], ["}", "}"]]
|
assert obj.is_a? ObjectNode
|
||||||
# end
|
assert obj.properties.first.variable == "one"
|
||||||
#
|
assert obj.properties.last.variable == "two"
|
||||||
# def test_lexing_function_definition
|
end
|
||||||
# code = "x => x * x."
|
|
||||||
# assert @lex.tokenize(code) == [[:PARAM, "x"], ["=>", "=>"],
|
def test_parsing_an_function_definition
|
||||||
# [:IDENTIFIER, "x"], ["*", "*"], [:IDENTIFIER, "x"], [".", "."]]
|
code = @par.parse("x, y => x * y.").expressions.first
|
||||||
# end
|
assert code.params == ['x', 'y']
|
||||||
#
|
body = code.body.expressions.first
|
||||||
|
assert body.is_a? OpNode
|
||||||
|
assert body.operator == '*'
|
||||||
|
end
|
||||||
|
|
||||||
# def test_lexing_if_statement
|
# def test_lexing_if_statement
|
||||||
# code = "clap_your_hands() if happy"
|
# code = "clap_your_hands() if happy"
|
||||||
# assert @lex.tokenize(code) == [[:IDENTIFIER, "clap_your_hands"], ["(", "("],
|
# assert @lex.tokenize(code) == [[:IDENTIFIER, "clap_your_hands"], ["(", "("],
|
||||||
|
|||||||
Reference in New Issue
Block a user