rbelly 2.0.3 → 2.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,10 +1,11 @@
1
1
  module RBelly
2
2
  module Nodes
3
3
  class ClassNode < Node
4
- attr_reader :class_body
5
- def initialize(name, body)
4
+ attr_reader :class_body, :parent
5
+ def initialize(name, body, parent = nil)
6
6
  super(name)
7
7
  @class_body = body
8
+ @parent = parent
8
9
  end
9
10
  end
10
11
  end
data/lib/rbelly/nodes.rb CHANGED
@@ -27,3 +27,6 @@ require 'rbelly/nodes/var_decl_node'
27
27
  #Bellejs
28
28
  require 'rbelly/nodes/bellejs/import_node'
29
29
  require 'rbelly/nodes/bellejs/class_node'
30
+ require 'rbelly/nodes/bellejs/extends_node'
31
+ require 'rbelly/nodes/bellejs/bellejs_var_statement_node'
32
+ require 'rbelly/nodes/bellejs/bellejs_func_statement_node'
@@ -11,7 +11,7 @@ module RBelly
11
11
  }
12
12
 
13
13
  BELLEJS_KEYWORDS = %w{
14
- import class
14
+ import class extends public private
15
15
  }
16
16
 
17
17
  RESERVED = %w{
@@ -46,7 +46,7 @@ module RBelly
46
46
  %w{
47
47
  AssignExprNode BitwiseNotNode BlockNode DeleteNode ElementNode
48
48
  ExpressionStatementNode FunctionBodyNode ClassBodyNode LogicalNotNode ReturnNode
49
- ThrowNode TypeOfNode UnaryMinusNode UnaryPlusNode VoidNode ImportNode
49
+ ThrowNode TypeOfNode UnaryMinusNode UnaryPlusNode VoidNode ImportNode ExtendsNode
50
50
  }.each do |type|
51
51
  define_method(:"visit_#{type}") do |o|
52
52
  node = Node.new(@node_index += 1, [type])
@@ -101,7 +101,7 @@ module RBelly
101
101
 
102
102
  # Name and Value Nodes
103
103
  %w{
104
- LabelNode PropertyNode GetterPropertyNode SetterPropertyNode VarDeclNode
104
+ LabelNode PropertyNode GetterPropertyNode SetterPropertyNode VarDeclNode BellejsVar
105
105
  }.each do |type|
106
106
  define_method(:"visit_#{type}") do |o|
107
107
  node = Node.new(@node_index += 1, [type, o.name || 'NULL'])
@@ -94,9 +94,20 @@ module RBelly
94
94
 
95
95
  def visit_ClassNode(o)
96
96
  "#{indent}class #{o.value}" +
97
+ (o.parent ? " extends #{o.parent.value.to_s}" : "") +
97
98
  "#{o.class_body.accept(self)}"
98
99
  end
99
100
 
101
+ def visit_BellejsVarStatementNode(o)
102
+ "#{o.visibility} " +
103
+ "#{o.var_statement.accept(self)}"
104
+ end
105
+
106
+ def visit_BellejsFuncStatementNode(o)
107
+ "#{o.visibility} " +
108
+ "#{o.func_statement.accept(self)}"
109
+ end
110
+
100
111
  def visit_ParameterNode(o)
101
112
  o.value
102
113
  end
@@ -179,6 +190,10 @@ module RBelly
179
190
  "import #{o.value.accept(self)};"
180
191
  end
181
192
 
193
+ def visit_ExtendsNode(o)
194
+ "extends #{o.value.accept(self)}"
195
+ end
196
+
182
197
  def visit_ThrowNode(o)
183
198
  "throw #{o.value.accept(self)};"
184
199
  end
@@ -214,6 +214,10 @@ module RBelly
214
214
  o.value.each { |x| x.accept(self) }
215
215
  end
216
216
 
217
+ def visit_ExtendsNode(o)
218
+ o.value.each { |x| x.accept(self) }
219
+ end
220
+
217
221
  def visit_BitwiseNotNode(o)
218
222
  orig = o.value.accept(self)
219
223
  number = to_int_32(orig)
@@ -42,7 +42,7 @@ module RBelly
42
42
  SetterPropertyNode StrictEqualNode StringNode
43
43
  SubtractNode SwitchNode ThisNode ThrowNode TrueNode TryNode TypeOfNode
44
44
  UnaryMinusNode UnaryPlusNode UnsignedRightShiftNode VarDeclNode
45
- VarStatementNode VoidNode WhileNode WithNode ImportNode
45
+ VarStatementNode VoidNode WhileNode WithNode ImportNode ExtendsNode
46
46
  }.each do |type|
47
47
  define_method(:"visit_#{type}") do |o|
48
48
  end
@@ -341,6 +341,10 @@ module RBelly
341
341
  [:import, *super]
342
342
  end
343
343
 
344
+ def visit_ExtendsNode(o)
345
+ [:extends, *super]
346
+ end
347
+
344
348
  def visit_FunctionExprNode(o)
345
349
  [ :func_expr, *super]
346
350
  end
@@ -3,11 +3,11 @@ module RBelly
3
3
  class Visitor
4
4
  TERMINAL_NODES = %w{
5
5
  Break Continue EmptyStatement False Null Number Parameter Regexp Resolve
6
- String This True
6
+ String This True Public Private
7
7
  }
8
8
  SINGLE_VALUE_NODES = %w{
9
9
  Parenthetical AssignExpr BitwiseNot Block Delete Element ExpressionStatement
10
- FunctionBody ClassBody LogicalNot Return Throw TypeOf UnaryMinus UnaryPlus Void Import
10
+ FunctionBody ClassBody LogicalNot Return Throw TypeOf UnaryMinus UnaryPlus Void Import Extends
11
11
  }
12
12
  BINARY_NODES = %w{
13
13
  Add BitAnd BitOr BitXOr CaseClause Comma Divide DoWhile Equal Greater
@@ -30,10 +30,12 @@ module RBelly
30
30
  FUNC_CALL_NODES = %w{ NewExpr FunctionCall }
31
31
  FUNC_DECL_NODES = %w{ FunctionExpr FunctionDecl }
32
32
  CLASS_NODES = %w{ Class }
33
+ BELLEJS_VAR_NODES = %w{ BellejsVarStatement }
34
+ BELLEJS_FUNC_NODES = %w{ BellejsFuncStatement }
33
35
  ALL_NODES = %w{ For ForIn Try BracketAccessor DotAccessor } +
34
36
  TERMINAL_NODES + SINGLE_VALUE_NODES + BINARY_NODES + ARRAY_VALUE_NODES +
35
37
  NAME_VALUE_NODES + PREFIX_POSTFIX_NODES + CONDITIONAL_NODES +
36
- FUNC_CALL_NODES + FUNC_DECL_NODES + CLASS_NODES
38
+ FUNC_CALL_NODES + FUNC_DECL_NODES + CLASS_NODES + BELLEJS_VAR_NODES
37
39
 
38
40
  def accept(target)
39
41
  target.accept(self)
@@ -105,6 +107,26 @@ module RBelly
105
107
  end
106
108
  end
107
109
 
110
+ BELLEJS_VAR_NODES.each do |type|
111
+ define_method(:"visit_#{type}Node") do |o|
112
+ [
113
+ o.value ? o.value : nil,
114
+ o.visibility ? o.visibility : nil,
115
+ o.var_statement ? o.var_statement : nil
116
+ ]
117
+ end
118
+ end
119
+
120
+ BELLEJS_FUNC_NODES.each do |type|
121
+ define_method(:"visit_#{type}Node") do |o|
122
+ [
123
+ o.value ? o.value : nil,
124
+ o.visibility ? o.visibility : nil,
125
+ o.func_statement ? o.func_statement : nil
126
+ ]
127
+ end
128
+ end
129
+
108
130
  def visit_ForNode(o)
109
131
  [
110
132
  o.init ? o.init.accept(self) : nil,
data/test/test_bellejs.rb CHANGED
@@ -2,9 +2,17 @@ require File.dirname(__FILE__) + "/helper"
2
2
 
3
3
  class BellejsTest < Test::Unit::TestCase
4
4
 
5
+ PRIVATE_VAR_DECL = "private var cat = 9;"
6
+ PUBLIC_VAR_DECL = "public var dog = 'large mammal with a tail';"
7
+ PRIVATE_FUNC_DECL = "private function doStuff(){
8
+
9
+ }"
5
10
  BELLEJS_IMPORT = "import 'fakeJSFileOne.bellejs';"
6
11
  BELLEJS_CLASS = "class Dingo{
7
12
 
13
+ }"
14
+ BELLEJS_CLASS_WITH_PARENT = "class Dingo extends Animal{
15
+
8
16
  }"
9
17
 
10
18
  def test_import_tokenizer
@@ -31,4 +39,38 @@ class BellejsTest < Test::Unit::TestCase
31
39
  assert_equal result.to_ecma, BELLEJS_CLASS
32
40
  end
33
41
 
42
+ def test_class_parent_tokenizer
43
+ tokenizer = RBelly::Tokenizer.new
44
+ result = tokenizer.tokenize "extends"
45
+ assert_equal result, [[:EXTENDS, "extends"]]
46
+ end
47
+
48
+ def test_class_with_parent
49
+ parser = RBelly::Parser.new
50
+ result = parser.parse(BELLEJS_CLASS_WITH_PARENT)
51
+ assert_equal result.to_ecma, BELLEJS_CLASS_WITH_PARENT
52
+ end
53
+
54
+ def test_public_private_tokenizer
55
+ tokenizer = RBelly::Tokenizer.new
56
+ result = tokenizer.tokenize "public"
57
+ result2 = tokenizer.tokenize "private"
58
+ assert_equal result, [[:PUBLIC, "public"]]
59
+ assert_equal result2, [[:PRIVATE, "private"]]
60
+ end
61
+
62
+ def test_public_private_vars
63
+ parser = RBelly::Parser.new
64
+ result = parser.parse(PRIVATE_VAR_DECL)
65
+ result2 = parser.parse(PUBLIC_VAR_DECL)
66
+ assert_equal result.to_ecma, PRIVATE_VAR_DECL
67
+ assert_equal result2.to_ecma, PUBLIC_VAR_DECL
68
+ end
69
+
70
+ def test_public_private_funcs
71
+ parser = RBelly::Parser.new
72
+ result = parser.parse(PRIVATE_FUNC_DECL)
73
+ assert_equal result.to_ecma, PRIVATE_FUNC_DECL
74
+ end
75
+
34
76
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rbelly
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.3
4
+ version: 2.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-04-29 00:00:00.000000000 Z
12
+ date: 2013-04-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rdoc