Permalink
Browse files

Parser now has let support. Thanks Massimiliano! I also added to_ecma…

…, each, and to_dot support for let
  • Loading branch information...
1 parent 6e6f9f5 commit 515fe4cf0737ef319451102f4ad0b36337a2102f @tenderlove tenderlove committed Oct 28, 2008
@@ -13,6 +13,7 @@ module Nodes
AssignExpr
BracketAccess
DotAccessor
+ LexicalScope
Equal
GetterProperty
Label
@@ -3,6 +3,7 @@ module Nodes
LIST_NODES = %w{
SourceElements
VarStatement
+ LetStatement
Comma
ObjectLiteral
ArrayLiteral
@@ -11,6 +11,7 @@ def accept(visitor)
:tok_colon => :visit_Label,
:tok_name => :visit_AssignExpr,
:tok_dot => :visit_DotAccessor,
+ :tok_lexicalscope => :visit_LexicalScope
}[pn_type]
raise "Unknown type #{pn_type}" unless m
visitor.send(m, self)
@@ -112,6 +113,7 @@ def handle_list(visitor)
m = {
:tok_lc => :visit_SourceElements,
:tok_var => :visit_VarStatement,
+ :tok_let => :visit_LetStatement,
:tok_comma => :visit_Comma,
:tok_rc => :visit_ObjectLiteral,
:tok_rb => :visit_ArrayLiteral,
@@ -41,6 +41,14 @@ def visit_Function(ro_node)
ro_node.function_body.accept(self) )
end
+ def visit_LexicalScope(ro_node)
+ LexicalScope.new(
+ ro_node.line,
+ ro_node.index,
+ Name.new(ro_node.line, ro_node.index, "unnamed"), # lexical scope nodes don't hold a name
+ ro_node.pn_expr.accept(self))
+ end
+
%w{ Label AssignExpr DotAccessor }.each do |type|
define_method(:"visit_#{type}") do |ro_node|
Nodes.const_get(type).new(
@@ -55,6 +63,7 @@ def visit_Function(ro_node)
%w{
SourceElements
VarStatement
+ LetStatement
Comma
ObjectLiteral
ArrayLiteral
@@ -52,6 +52,7 @@ def visit_SourceElements(o)
%w{
VarStatement
+ LetStatement
Comma
ObjectLiteral
ArrayLiteral
@@ -147,6 +148,7 @@ def visit_Function(o)
OpModEqual OpMultiply OpMultiplyEqual OpRShift OpRShiftEqual
OpSubtract OpSubtractEqual OpURShift OpURShiftEqual Or Property
SetterProperty StrictEqual StrictNotEqual Switch While With
+ LexicalScope
}.each do |node|
define_method(:"visit_#{node}") do |o|
@nodes << Node.new(o.object_id, node, [])
@@ -42,6 +42,10 @@ def visit_VarStatement(o)
"var #{o.value.map { |x| x.accept(self) }.join(', ')}"
end
+ def visit_LetStatement(o)
+ "let #{o.value.map { |x| x.accept(self) }.join(', ')}"
+ end
+
def visit_ArrayLiteral(o)
"[#{o.value.map { |x| x.accept(self) }.join(', ')}]"
end
@@ -113,6 +117,10 @@ def visit_String(o)
def visit_BracketAccess(o)
"#{o.left.accept(self)}[#{o.right.accept(self)}]"
end
+
+ def visit_LexicalScope(o)
+ "#{o.right.accept(self)}"
+ end
def visit_DoWhile(o)
semi = o.left.is_a?(Nodes::SourceElements) ? '' : ';'
@@ -12,9 +12,15 @@ def visit_SourceElements(o)
self
end
+ def visit_LexicalScope(o)
+ block.call(o)
+ o.right.accept(self)
+ self
+ end
+
%w{
ArrayLiteral Comma Export FunctionCall Import New ObjectLiteral
- VarStatement
+ VarStatement LetStatement
}.each do |type|
define_method(:"visit_#{type}") do |o|
block.call(o)
@@ -7,6 +7,7 @@ def visit_SourceElements(o)
{
'VarStatement' => :var,
+ 'LetStatement' => :let,
'Comma' => :comma,
'ObjectLiteral' => :object,
'ArrayLiteral' => :array,
@@ -151,6 +152,7 @@ def visit_Function(o)
'AssignExpr' => :assign,
'BracketAccess' => :bracket_access,
'DotAccessor' => :dot_accessor,
+ 'LexicalScope' => :lexical_scope,
'Equal' => :equal,
'NotEqual' => :not_equal,
'Or' => :or,
@@ -0,0 +1,31 @@
+require File.expand_path(File.join(File.dirname(__FILE__), "/../../helper"))
+
+class LetNodeTest < Johnson::NodeTestCase
+ def test_let_to_sexp
+ assert_sexp(
+ [[:lexical_scope, [:name, "unnamed"], [[:let, [[:assign, [:name, "a"], [:lit, 1],
+]]]]]],
+ @parser.parse('if(true) { let a = 1; }')
+ )
+
+ end
+
+ def test_let_to_ecma
+ assert_ecma(
+ "{\n let a = 1;\n};",
+ @parser.parse('if(true) { let a = 1; }')
+ )
+ end
+
+ def test_enumerating_visitor
+ count = 0
+ @parser.parse('if(true) { let a = 1; }').each do |node|
+ count += 1
+ end
+ assert_equal 7, count
+ end
+
+ def test_dot_visitor
+ @parser.parse('if(true) { let a = 1; }').to_dot
+ end
+end
View
@@ -22,12 +22,6 @@ def test_exception_raised
}
end
- def test_let
- doc = @parser.parse(<<-eojs)
- let a = 1;
- eojs
- end
-
def test_exception_has_info
begin
@parser.parse('if(')

0 comments on commit 515fe4c

Please sign in to comment.