red 3.5.0 → 4.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,47 +0,0 @@
1
- module Red
2
- class ConstantNode # :nodoc:
3
- class BreakNode < ConstantNode # :nodoc:
4
- def initialize(*args)
5
- raise(BuildError::NoBreakArguments, "Break can't take an argument in JavaScript") unless args.empty?
6
- end
7
-
8
- def compile_node(options = {})
9
- return "break"
10
- end
11
- end
12
-
13
- class FalseNode < ConstantNode # :nodoc:
14
- def compile_node(options = {})
15
- return "false"
16
- end
17
- end
18
-
19
- class NextNode < ConstantNode # :nodoc:
20
- def initialize(*args)
21
- raise(BuildError::NoNextArguments, "Next/continue can't take an argument in JavaScript") unless args.empty?
22
- end
23
-
24
- def compile_node(options = {})
25
- return "continue"
26
- end
27
- end
28
-
29
- class NilNode < ConstantNode # :nodoc:
30
- def compile_node(options = {})
31
- return "null"
32
- end
33
- end
34
-
35
- class SelfNode < ConstantNode # :nodoc:
36
- def compile_node(options = {})
37
- return "this"
38
- end
39
- end
40
-
41
- class TrueNode < ConstantNode # :nodoc:
42
- def compile_node(options = {})
43
- return "true"
44
- end
45
- end
46
- end
47
- end
@@ -1,129 +0,0 @@
1
- module Red
2
- class ControlNode # :nodoc:
3
- class BeginNode # :nodoc:
4
- def initialize(body)
5
- @@rescue_is_safe = (body.first == :rescue)
6
- @body = body.build_node
7
- end
8
-
9
- def compile_node(options = {})
10
- "%s" % [@body.compile_node]
11
- end
12
- end
13
-
14
- class EnsureNode # :nodoc:
15
- def initialize(attempted, ensure_body)
16
- @@rescue_is_safe = @ensure_from_rescue = attempted.first == :rescue
17
- @attempted, @ensured = [attempted, ensure_body].build_nodes
18
- end
19
-
20
- def compile_node(options = {})
21
- if @ensure_from_rescue
22
- "%s finally { %s; }"
23
- else
24
- "try { %s; } finally { %s; }"
25
- end % self.compile_internals
26
- end
27
-
28
- def compile_internals(options = {})
29
- return [@attempted, @ensured].compile_nodes
30
- end
31
- end
32
-
33
- class ForNode # :nodoc:
34
- def initialize(source, iterator, body)
35
- @properties_loop = (iterator.last == :property)
36
- @source, @iterator, @body = [source, iterator.last, body].build_nodes
37
- end
38
-
39
- def compile_node(options = {})
40
- source = @source.compile_node(:as_argument => true)
41
- iterator = @iterator.compile_node
42
- body = @body.compile_node
43
- if @properties_loop
44
- "for (var property in %s) { %s; }" % [source, body]
45
- else
46
- "for (var %s = 0; %s < %s.length; %s++) { %s; }" % [iterator, iterator, source, iterator, body]
47
- end
48
- end
49
- end
50
-
51
- class RescueNode # :nodoc:
52
- def initialize(attempted, rescue_body)
53
- raise(BuildError::NoArbitraryRescue, "JavaScript does not support arbitrary placement of rescue/try blocks") unless @@rescue_is_safe
54
- raise(BuildError::NoSpecificRescue, "JavaScript does not support rescuing of specific exception classes") unless !rescue_body[1]
55
- @@rescue_is_safe == false
56
- @attempted = attempted.build_node
57
- if (block = (rescue_body.assoc(:block) || [])[1..-1]) && block.first.last == [:gvar, %s($!)]
58
- exception_node = block.shift
59
- @exception_variable = exception_node[1].build_node
60
- @rescued = block.unshift(:block).build_node
61
- else
62
- @exception_variable = :e.build_node
63
- @rescued = rescue_body[2].build_node
64
- end
65
- end
66
-
67
- def compile_node(options = {})
68
- return "try { %s; } catch(%s) { %s; }" % self.compile_internals
69
- end
70
-
71
- def compile_internals(options = {})
72
- return [@attempted, @exception_variable, @rescued].compile_nodes
73
- end
74
- end
75
-
76
- class UntilNode # :nodoc:
77
- def initialize(condition, body, run_only_if_condition_met)
78
- @condition, @body = [condition, body].build_nodes
79
- @do_while_loop = !run_only_if_condition_met
80
- end
81
-
82
- def compile_node(options = {})
83
- if @do_while_loop
84
- return "do { %s; } while (!(%s))" % self.compile_internals.reverse
85
- else
86
- return "while (!(%s)) { %s; }" % self.compile_internals
87
- end
88
- end
89
-
90
- def compile_internals(options = {})
91
- condition = @condition.compile_node(:skip_var => true)
92
- body = @body.compile_node
93
- return [condition, body]
94
- end
95
- end
96
-
97
- class WhileNode # :nodoc:
98
- def initialize(condition, body, run_only_if_condition_met)
99
- @condition, @body = [condition, body].build_nodes
100
- @do_while_loop = !run_only_if_condition_met
101
- end
102
-
103
- def compile_node(options = {})
104
- if @do_while_loop
105
- return "do { %s; } while (%s)" % self.compile_internals.reverse
106
- else
107
- return "while (%s) { %s; }" % self.compile_internals
108
- end
109
- end
110
-
111
- def compile_internals(options = {})
112
- condition = @condition.compile_node(:skip_var => true)
113
- body = @body.compile_node
114
- return [condition, body]
115
- end
116
- end
117
-
118
- class LibraryNode # :nodoc:
119
- def initialize(library)
120
- @@red_library = @library = library
121
- end
122
-
123
- def compile_node(options = {})
124
- @@red_library = @library
125
- nil
126
- end
127
- end
128
- end
129
- end
@@ -1,93 +0,0 @@
1
- module Red
2
- class DataNode # :nodoc:
3
- attr_reader :value
4
-
5
- def initialize(value = nil)
6
- @value = value
7
- end
8
-
9
- def compile_node(options = {})
10
- return @value.inspect
11
- end
12
-
13
- def wrap_string(string, left_wrapper = "", right_wrapper = nil)
14
- return "%s%s%s" % [left_wrapper, string, right_wrapper || left_wrapper]
15
- end
16
-
17
- class ErrorNode < DataNode # :nodoc:
18
- def compile_node(options = {})
19
- "/* Error %s */" % [@value]
20
- end
21
-
22
- def data_type
23
- :error
24
- end
25
- end
26
-
27
- class NilNode < DataNode # :nodoc:
28
- def compile_node(options = {})
29
- ""
30
- end
31
- end
32
-
33
- class OtherNode < DataNode # :nodoc:
34
- end
35
-
36
- class RangeNode < DataNode # :nodoc:
37
- def initialize(*args)
38
- case @@red_library
39
- when :Prototype : super(*args)
40
- else raise(BuildError::NoRangeConstructor, "#{@@red_library} JavaScript library has no literal range constructor")
41
- end
42
- end
43
-
44
- def compile_node(options = {})
45
- case @@red_library
46
- when :Prototype : return "$R(%s, %s%s)" % self.compile_internals
47
- else return ""
48
- end
49
- end
50
-
51
- def compile_internals(options = {})
52
- exclusive = @value.exclude_end? ? ", true" : ""
53
- return [@value.begin, @value.end, exclusive]
54
- end
55
- end
56
-
57
- class StringNode < DataNode # :nodoc:
58
- def compile_node(options = {})
59
- options[:quotes] ||= "'"
60
- return wrap_string(@value, options[:quotes])
61
- end
62
-
63
- def data_type
64
- :string
65
- end
66
- end
67
-
68
- class SymbolNode < DataNode # :nodoc:
69
- def compile_node(options = {})
70
- self.camelize!(options[:not_camelized])
71
- return wrap_string(@value, options[:quotes])
72
- end
73
-
74
- def camelize!(disabled = false)
75
- return @value = @value.to_s unless camelize? && !disabled
76
- words = @value.to_s.gsub(/@|\$/,'').split(/_| /)
77
- underscore = words.shift if words.first.empty?
78
- @value = (underscore ? '_' : '') + words[0] + words[1..-1].map {|word| word == word.upcase ? word : word.capitalize }.join
79
- end
80
-
81
- def camelize?
82
- value = @value.to_s
83
- is_not_a_constant_name = value != value.upcase || value =~ (/@|\$/)
84
- is_not_a_js_special_attribute = @value.to_s[0..1] != '__'
85
- return is_not_a_constant_name && is_not_a_js_special_attribute
86
- end
87
-
88
- def data_type
89
- :symbol
90
- end
91
- end
92
- end
93
- end
@@ -1,177 +0,0 @@
1
- module Red
2
- class DefinitionNode # :nodoc:
3
- class ClassNode # :nodoc:
4
- def initialize(class_name, superclass, scope)
5
- raise(BuildError::NoClassInheritance, "Class inheritance is currently not supported#{" for the #{@@red_library} JavaScript library";''}") if superclass
6
- old_class = @@red_class
7
- @@red_class = @class = class_name
8
- @class_name, @superclass = [class_name, superclass].build_nodes
9
- block_node = scope.assoc(:block) || scope
10
- case @@red_library
11
- when :Prototype
12
- @initializer = block_node.rassoc(:initialize)
13
- @classes = block_node.select {|node| (node.first == :class) rescue false }.build_nodes
14
- @properties = block_node.select {|node| (node.first == :cvdecl) rescue false }.build_nodes
15
- @functions = block_node.select {|node| ![:block, :scope].include?(node) && ((node.first != :cvdecl) rescue false) }.build_nodes
16
- else
17
- if initializer_node = block_node.rassoc(:initialize)
18
- args_node = initializer_node.assoc(:scope).assoc(:block).assoc(:args)
19
- @arguments = (args_node[1..-1] || []).build_nodes
20
- @initializer = initializer_node.assoc(:scope).assoc(:block).reject {|node| node == args_node}.build_node
21
- end
22
- @classes = block_node.select {|node| (node.first == :class) rescue false }.build_nodes
23
- @properties = block_node.select {|node| (node.first == :cvdecl) rescue false }.build_nodes
24
- @functions = block_node.select {|node| (node != initializer_node) && ![:block, :scope].include?(node) && ((node.first != :cvdecl) rescue false) }.build_nodes
25
- end
26
- @@red_class = old_class
27
- end
28
-
29
- def compile_node(options = {})
30
- old_class = @@red_class
31
- @@red_class = @class
32
- if options[:as_prototype]
33
- output = self.compile_as_child_class
34
- elsif @initializer
35
- case @@red_library
36
- when :Prototype
37
- output = self.compile_as_prototype_class
38
- else
39
- output = self.compile_as_standard_class
40
- end
41
- else
42
- output = self.compile_as_virtual_class
43
- end
44
- @@red_class = old_class
45
- return output
46
- end
47
-
48
- def compile_as_child_class
49
- class_name = @class_name.compile_node
50
- slots = (@classes | @properties | @functions).compile_nodes(:as_prototype => true).compact.join(', ')
51
- return "%s: { %s }" % [class_name, slots]
52
- end
53
-
54
- def compile_as_prototype_class
55
- class_name = @class_name.compile_node
56
- functions = @functions.compile_nodes(:as_prototype => true).compact.join(', ')
57
- properties = @properties.compile_nodes(:as_prototype => true).compact.join(', ')
58
- return "%s%s = Class.create({ %s });Object.extend(%s, { %s })" % [self.var?, class_name, functions, class_name, properties]
59
- end
60
-
61
- def compile_as_standard_class(options = {})
62
- class_name = @class_name.compile_node
63
- arguments = @arguments.compile_nodes.join(', ')
64
- initializer = @initializer.compile_node
65
- functions = @functions.compile_nodes(:as_attribute => true).join('; ')
66
- properties = @properties.compile_nodes.join('; ')
67
- return "%s%s = function(%s) { %s;%s }; %s" % [self.var?, class_name, arguments, initializer, functions, properties]
68
- end
69
-
70
- def compile_as_virtual_class(options = {})
71
- class_name = @class_name.compile_node
72
- slots = (@properties | @functions).compile_nodes(:as_prototype => true).compact.join(', ')
73
- return "%s%s = { %s }" % [self.var?, class_name, slots]
74
- end
75
-
76
- def var?
77
- return "var " unless @class_name.is_a?(LiteralNode::NamespaceNode)
78
- end
79
- end
80
-
81
- class ClassMethodNode # :nodoc:
82
- def initialize(receiver, function_name, scope)
83
- @receiver, @function_name = [receiver, function_name].build_nodes
84
- @arguments = (scope.assoc(:block).assoc(:args)[1..-1] || []).build_nodes
85
- @lines = (scope.assoc(:block)[2..-1] || []).build_nodes
86
- end
87
-
88
- def compile_node(options = {})
89
- case false
90
- when options[:as_attribute].nil?
91
- "this.%s = function(%s) { %s; }"
92
- when options[:as_prototype].nil?
93
- "%s: function(%s) { %s; }"
94
- when !(options[:as_attribute].nil? && options[:as_prototype].nil?)
95
- "function %s(%s) { %s; }"
96
- end % self.compile_internals
97
- end
98
-
99
- def compile_internals(options = {})
100
- function_name = @function_name.compile_node
101
- arguments = @arguments.compile_nodes.join(', ')
102
- lines = @lines.compile_nodes.compact.join('; ')
103
- return [function_name, arguments, lines]
104
- end
105
- end
106
-
107
- class InstanceMethodNode # :nodoc:
108
- def initialize(function_name, scope)
109
- block = scope.assoc(:block)
110
- @@rescue_is_safe = (block[2].first == :rescue)
111
- @function_name = (function_name == :function ? nil : function_name).build_node
112
- @arguments = (block.assoc(:args)[1..-1] || []).build_nodes
113
- @lines = (block[2..-1] || []).build_nodes
114
- end
115
-
116
- def compile_node(options = {})
117
- case false
118
- when options[:as_attribute].nil?
119
- "this.%s = function(%s) { %s; }"
120
- when options[:as_prototype].nil?
121
- "%s: function(%s) { %s; }"
122
- when !(options[:as_attribute].nil? && options[:as_prototype].nil?)
123
- "function %s(%s) { %s; }"
124
- end % self.compile_internals
125
- end
126
-
127
- def compile_internals(options = {})
128
- function_name = @function_name.compile_node
129
- arguments = @arguments.compile_nodes.join(', ')
130
- lines = @lines.compile_nodes.compact.join('; ')
131
- return [function_name, arguments, lines]
132
- end
133
- end
134
-
135
- class ModuleNode # :nodoc:
136
- def initialize(module_name, scope)
137
- old_module = @@red_module
138
- @@red_module = module_name
139
- @module_name, @scope = [module_name, scope].build_nodes
140
- @@red_module = old_module
141
- end
142
-
143
- def compile_node(options = {})
144
- return "%s" % self.compile_internals
145
- end
146
-
147
- def compile_internals(options = {})
148
- old_module = @@red_module
149
- @@red_module = @module_name.compile_node
150
- scope = @scope.compile_node
151
- @@red_module = old_module
152
- return [scope]
153
- end
154
- end
155
-
156
-
157
- class ObjectLiteralNode # :nodoc:
158
- def initialize(receiver, scope)
159
- old_class = @@red_class
160
- @@red_class = @class = receiver.build_node.compile_node
161
- block_node = scope.assoc(:block) || scope
162
- properties = block_node.select {|node| (node.first == :cvdecl) rescue false }
163
- functions = block_node.select {|node| ![:block, :scope].include?(node) }
164
- @slots = (properties | functions).build_nodes
165
- @@red_class = old_class
166
- end
167
-
168
- def compile_node(options = {})
169
- old_class = @@red_class
170
- @@red_class = @class
171
- slots = @slots.compile_nodes(:as_prototype => true).compact.join(', ')
172
- @@red_class = old_class
173
- return "{ %s }" % [slots]
174
- end
175
- end
176
- end
177
- end
@@ -1,63 +0,0 @@
1
- module Red
2
- class IllegalNode # :nodoc:
3
- class BlockArgument < IllegalNode # :nodoc:
4
- def initialize(*args)
5
- raise(BuildError::NoBlockArguments, "JavaScript does not support block arguments")
6
- end
7
- end
8
-
9
- class FlipflopNode < IllegalNode # :nodoc:
10
- def initialize(*args)
11
- raise(BuildError::NoFlipflops, "JavaScript does not support flip-flop operators")
12
- end
13
- end
14
-
15
- class MatchNode < IllegalNode # :nodoc:
16
- def initialize(*args)
17
- raise(BuildError::NoArbitraryMatch, "JavaScript does not support arbitrary boolean matching")
18
- end
19
- end
20
-
21
- class MultipleAssignmentNode < IllegalNode # :nodoc:
22
- def initialize(*args)
23
- raise(BuildError::NoMultipleAssignment, "JavaScript does not support catalogued assignment using multiple comma-separated expressions")
24
- end
25
- end
26
-
27
- class PostexeNode < IllegalNode # :nodoc:
28
- def initialize(*args)
29
- raise(BuildError::NoBEGINorEND, "BEGIN and END blocks are not supported")
30
- end
31
- end
32
-
33
- class RedoNode < IllegalNode # :nodoc:
34
- def initialize(*args)
35
- raise(BuildError::NoDoOvers, "JavaScript has no redo keyword")
36
- end
37
- end
38
-
39
- class RegexEvaluationNode < IllegalNode # :nodoc:
40
- def initialize(*args)
41
- raise(BuildError::NoRegexEvaluation, "Construction of JavaScript regular expressions with evaluated content is not supported")
42
- end
43
- end
44
-
45
- class RetryNode < IllegalNode # :nodoc:
46
- def initialize(*args)
47
- raise(BuildError::NoDoOvers, "JavaScript has no retry keyword")
48
- end
49
- end
50
-
51
- class SymbolEvaluationNode < IllegalNode # :nodoc:
52
- def initialize(*args)
53
- raise(BuildError::NoSymbolEvaluation, "Construction of JavaScript identifiers through evaluated symbols is not supported")
54
- end
55
- end
56
-
57
- class UndefNode < IllegalNode # :nodoc:
58
- def initialize(*args)
59
- raise(BuildError::NoUndef, "JavaScript does not support undefinition of methods")
60
- end
61
- end
62
- end
63
- end
@@ -1,119 +0,0 @@
1
- module Red
2
- class LiteralNode # :nodoc:
3
- def initialize(*elements)
4
- @initial = elements.shift.build_node
5
- @subsequent = elements.build_nodes
6
- end
7
-
8
- def compile_node(options = {})
9
- content = @initial.compile_node(options)
10
- return "%s" % [content]
11
- end
12
-
13
- def data_type
14
- return :node unless @initial.is_a?(DataNode)
15
- case @initial
16
- when DataNode::NilNode : :nil
17
- when DataNode::RangeNode : :range
18
- when DataNode::StringNode : (@subsequent.empty? ? :string : :eval)
19
- when DataNode::SymbolNode : :symbol
20
- else @initial.value.is_a?(Numeric) ? :numeric : :regexp
21
- end
22
- end
23
-
24
- class ArrayNode < LiteralNode # :nodoc:
25
- def compile_node(options = {})
26
- elements = @subsequent.unshift(@initial).compile_nodes.reject {|element| element.empty? }.join(', ')
27
- return "[%s]" % [elements]
28
- end
29
- end
30
-
31
- class HashNode < LiteralNode # :nodoc:
32
- def initialize(*array)
33
- @hash = {}
34
- Hash[*array].each do |k,v|
35
- key, value = [k,v].build_nodes
36
- raise(BuildError::NoArbitraryHashKeys, "JavaScript does not support non-string objects as hash keys") unless [:string, :symbol].include?(key.data_type)
37
- @hash[key] = value
38
- end
39
- end
40
-
41
- def compile_node(options = {})
42
- pairs = @hash.map { |k,v| "%s: %s" % [k.compile_node(:quotes => "'"), v.compile_node(:as_argument => true)] }.join(', ')
43
- "{ %s }" % [pairs]
44
- end
45
- end
46
-
47
- class MultilineNode < LiteralNode # :nodoc:
48
- def compile_node(options = {})
49
- lines = @subsequent.unshift(@initial).compile_nodes(options).compact.join('; ')
50
- return "%s" % [lines]
51
- end
52
- end
53
-
54
- class NamespaceNode < LiteralNode # :nodoc:
55
- def compile_node(options = {})
56
- namespaces = @initial.compile_node
57
- class_name = @subsequent.first.compile_node
58
- return "%s.%s" % [namespaces, class_name]
59
- end
60
- end
61
-
62
- class OtherNode < LiteralNode # :nodoc:
63
- end
64
-
65
- class RangeNode < LiteralNode # :nodoc:
66
- def initialize(*args)
67
- case @@red_library
68
- when :Prototype : super(*args)
69
- else raise(BuildError::NoRangeConstructor, "#{@@red_library} JavaScript library has no literal range constructor")
70
- end
71
- end
72
-
73
- def compile_node(options = {})
74
- start = @initial.compile_node(:as_argument => true)
75
- finish = @subsequent.first.compile_node(:as_argument => true)
76
- case @@red_library
77
- when :Prototype : return "$R(%s, %s)" % [start, finish]
78
- else ""
79
- end
80
- end
81
-
82
- class ExclusiveNode < RangeNode # :nodoc:
83
- def compile_node(options = {})
84
- start = @initial.compile_node(:as_argument => true)
85
- finish = @subsequent.first.compile_node(:as_argument => true)
86
- case @@red_library
87
- when :Prototype : return "$R(%s, %s, true)" % [start, finish]
88
- else ""
89
- end
90
- end
91
- end
92
- end
93
-
94
- class SplatNode < LiteralNode # :nodoc:
95
- def initialize(*args)
96
- case @@red_library
97
- when :Prototype : super(*args)
98
- else raise(BuildError::NoSplatConstructor, "Splat array constructor not supported for #{@@red_library} JavaScript library")
99
- end
100
- end
101
-
102
- def compile_node(options = {})
103
- initial = @initial.compile_node
104
- case @@red_library
105
- when :Prototype : return "$A(%s)" % [initial]
106
- else ""
107
- end
108
- end
109
- end
110
-
111
- class StringNode < LiteralNode # :nodoc:
112
- def compile_node(options = {})
113
- initial = @initial.compile_node(options)
114
- subsequent = @subsequent.map {|element| " + %s" % [element.compile_node]}.join
115
- return "%s%s" % [initial, subsequent]
116
- end
117
- end
118
- end
119
- end
@@ -1,37 +0,0 @@
1
- module Red
2
- class VariableNode # :nodoc:
3
- def initialize(variable_name)
4
- @variable_name = variable_name.build_node
5
- end
6
-
7
- def compile_node(options = {})
8
- return "%s" % self.compile_internals
9
- end
10
-
11
- def compile_internals(options = {})
12
- return [@variable_name.compile_node]
13
- end
14
-
15
- class ClassVariableNode < VariableNode # :nodoc:
16
- def compile_node(options = {})
17
- return "%s.%s" % self.compile_internals
18
- end
19
-
20
- def compile_internals(options = {})
21
- return [@@red_class, @variable_name.compile_node]
22
- end
23
- end
24
-
25
- class InstanceVariableNode < VariableNode # :nodoc:
26
- def compile_node(options = {})
27
- return "this.%s" % self.compile_internals
28
- end
29
- end
30
-
31
- class GlobalVariableNode < VariableNode # :nodoc:
32
- end
33
-
34
- class OtherVariableNode < VariableNode # :nodoc:
35
- end
36
- end
37
- end