minjs 0.3.0 → 0.4.0

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,34 +1,49 @@
1
1
  # coding: utf-8
2
2
  module Minjs
3
+ # Ctype
3
4
  module Ctype
4
- # Annex B
5
+ # Tests character _code_ is OctalDigit or not.
6
+ #
7
+ # @param [Fixnum] code
8
+ # @return _true_ if code is OctalDigit, otherwise _false_
9
+ # @see http://www.ecma-international.org/ecma-262 ECMA262 Annex B.
5
10
  def octal_digit?(code)
6
11
  code >= 0x30 and code <= 0x37
7
12
  end
8
13
 
9
- # 7.8.3
14
+ # Tests character _code_ is DecimalDigit or not.
15
+ #
16
+ # @param [Fixnum] code
17
+ # @return _true_ if code is DecimalDigit, otherwise _false_
18
+ # @see http://www.ecma-international.org/ecma-262 ECMA262 7.8.3
10
19
  def decimal_digit?(code)
11
20
  code >= 0x30 and code <= 0x39
12
21
  end
13
22
 
14
- # 7.8.3
23
+ # Tests character _code_ is HexDigit or not.
24
+ #
25
+ # @param [Fixnum] code
26
+ # @return _true_ if code is HexlDigit, otherwise _false_.
27
+ # @see http://www.ecma-international.org/ecma-262 ECMA262 7.8.3
15
28
  def hex_digit?(code)
16
29
  code >= 0x30 && code <= 0x39 or
17
30
  code >= 0x41 && code <= 0x46 or
18
31
  code >= 0x61 && code <= 0x66
19
32
  end
20
33
 
21
- # 7.2
34
+ # Tests character _code_ is WhiteSpace or not.
22
35
  #
23
- # WhiteSpace ::
24
- # <TAB>
25
- # <VT>
26
- # <FF>
27
- # <SP>
28
- # <NBSP>
29
- # <BOM>
36
+ # WhiteSpace is <TAB>,
37
+ # <VT>,
38
+ # <FF>,
39
+ # <SP>,
40
+ # <NBSP>,
41
+ # <BOM> and
30
42
  # <USP> any character in the Unicode category Zs
31
43
  #
44
+ # @param [Fixnum] code
45
+ # @return _true_ if code is WhiteSpace, otherwise _false_.
46
+ # @see http://www.ecma-international.org/ecma-262 ECMA262 7.2
32
47
  def white_space?(code)
33
48
  code == 0x20 || code == 0x9 || code == 0xb || code == 0xc || code == 0xa0 || code == 0xfeff ||
34
49
  code == 0x1680 || # OGHAM SPACE MARK
@@ -38,32 +53,35 @@ module Minjs
38
53
  code == 0x3000 # IDEOGRAPHIC SPACE
39
54
  end
40
55
 
41
- # 7.3
42
- #
43
- # LineTerminator ::
44
- # <LF>
45
- # <CR>
46
- # <LS>
56
+ # Tests character _code_ is LineTerminator or not.
57
+ # LineTerminator is
58
+ # <LF>,
59
+ # <CR>,
60
+ # <LS> and
47
61
  # <PS>
48
62
  #
63
+ # @param [Fixnum] code
64
+ # @return _true_ if code is LineTerminator, otherwise _false_.
65
+ # @see http://www.ecma-international.org/ecma-262 ECMA262 7.3
49
66
  def line_terminator?(code)
50
67
  code == 0x0a || code == 0x0d || code == 0x2028 || code == 0x2029
51
68
  end
52
69
 
53
- # 7.6
70
+ # Test character _code_ is IdentifierStart or not.
54
71
  #
55
- # IdentifierStart ::
56
- # UnicodeLetter
57
- # $
58
- # _
59
- # \ UnicodeEscapeSequence
72
+ # IdentifierStart is any character in the Unicode categories
60
73
  #
61
- # UnicodeLetter ::
62
- # any character in the Unicode categories Uppercase letter
63
- # (Lu)”,Lowercase letter (Ll)”, “Titlecase letter (Lt)”,
64
- # “Modifier letter (Lm)”, “Other letter (Lo)”, or “Letter
65
- # number (Nl)”.
74
+ # * “Uppercase letter (Lu)
75
+ # *Lowercase letter (Ll)”
76
+ # * “Titlecase letter (Lt)
77
+ # * “Modifier letter (Lm)
78
+ # * “Other letter (Lo)
79
+ # * “Letter number (Nl)
66
80
  #
81
+ # @param [Fixnum] c
82
+ # @return _true_ if code is IdentifierStart, otherwise _false_.
83
+ # @see http://www.ecma-international.org/ecma-262 ECMA262 7.6
84
+
67
85
  def identifier_start?(c)
68
86
  return false if c.nil?
69
87
  # almost all characters are ascii
@@ -622,6 +640,28 @@ module Minjs
622
640
  return false
623
641
  end
624
642
  end
643
+ # Test character _code_ is IdentifierPart or not.
644
+ #
645
+ # IdentifierPart is any character in the Unicode categories
646
+ #
647
+ # * “Uppercase letter (Lu)”
648
+ # * “Lowercase letter (Ll)”
649
+ # * “Titlecase letter (Lt)”
650
+ # * “Modifier letter (Lm)”
651
+ # * “Other letter (Lo)”
652
+ # * “Letter number (Nl)
653
+ # * “Non-spacing mark (Mn)”
654
+ # * “Combining spacing mark (Mc)”
655
+ # * “Decimal number (Nd)”
656
+ # * “Connector punctuation (Pc)
657
+ # * <ZWNJ>
658
+ # * <ZWJ>
659
+ # * IdentifierStart
660
+ #
661
+ # @param [Fixnum] c
662
+ # @return _true_ if code is IdentifierStart, otherwise _false_.
663
+ # @see http://www.ecma-international.org/ecma-262 ECMA262 7.6
664
+ #
625
665
  def identifier_part?(c)
626
666
  return false if c.nil?
627
667
  if c == 0x24 ||
@@ -1267,6 +1307,9 @@ module Minjs
1267
1307
  end
1268
1308
  end
1269
1309
 
1310
+ # Tests _name_ is IdentifierName or not.
1311
+ #
1312
+ # See ECMA262 7.5 for more detail
1270
1313
  def idname?(name)
1271
1314
  return false if name.length == 0
1272
1315
  s = name.codepoints
@@ -1,6 +1,11 @@
1
+ module Minjs
2
+ #ECMA262
3
+ module ECMA262
4
+ end
5
+ end
1
6
  require "minjs/ecma262/base"
2
7
  require "minjs/ecma262/env"
3
- require "minjs/ecma262/exp"
4
- require "minjs/ecma262/lit"
5
- require "minjs/ecma262/punc"
6
- require "minjs/ecma262/st"
8
+ require "minjs/ecma262/expression"
9
+ require "minjs/ecma262/literal"
10
+ require "minjs/ecma262/punctuator"
11
+ require "minjs/ecma262/statement"
@@ -1,14 +1,24 @@
1
1
  module Minjs
2
2
  module ECMA262
3
+ #ECMA262 Elements
3
4
  class Base
5
+ # Returns a ECMAScript string containg the representation of element.
6
+ # @param options [Hash] options for Base#concat
7
+ # @return [String] ECMAScript string.
4
8
  def to_js(options = {})
5
9
  self.class.to_s + "??"
6
10
  end
7
11
 
12
+ # to string
8
13
  def to_s
9
14
  to_js({})
10
15
  end
11
16
 
17
+ # concatenate some of ECMA262 elements and convert it to ECMAScript
18
+ #
19
+ # @param args ECMA262 element
20
+ # @option options :debug [Boolean] if set, output is easy to read.
21
+ #
12
22
  def concat(options, *args)
13
23
  prev = nil
14
24
  j = []
@@ -24,12 +34,13 @@ module Minjs
24
34
  if prev.match(/[\w\$]\z/) and js.match(/\A[\w\$]/)
25
35
  sep = ' '
26
36
  end
27
- #';;' means 'empty statement' that must not be deleted
37
+ # ';;' means 'empty statement' or separator of 'for statement'
38
+ # that must not be deleted
28
39
  if prev.match(/;;\Z/)
29
40
  prev.sub!(/;;\Z/, ";")
30
41
  elsif prev.match(/;\Z/) and js == "}"
31
42
  prev.sub!(/;\Z/, "")
32
- elsif prev.match(/;\Z/) and js == ";" and !options[:for_args]
43
+ elsif prev.match(/;\Z/) and js == ";"
33
44
  prev.sub!(/;\Z/, "")
34
45
  elsif prev.match(/[\-]\Z/) and js.match(/^\-/)
35
46
  sep = ' '
@@ -40,10 +51,10 @@ module Minjs
40
51
  #for debug
41
52
  unless options[:no_debug]
42
53
  if (@logger and @logger.debug?) || options[:debug]
43
- if js.match(/;\z/) and !options[:for_args]
54
+ if js.match(/;\z/)
44
55
  nl = "\n"
45
56
  end
46
- if js.match(/}\z/) and !options[:for_args]
57
+ if js.match(/}\z/)
47
58
  nl = "\n"
48
59
  end
49
60
  end
@@ -55,19 +66,28 @@ module Minjs
55
66
  j.join("")
56
67
  end
57
68
 
69
+ # Replaces child (if own it) object
70
+ #
71
+ # @param from [Base] from
72
+ # @param to [Base] to
58
73
  def replace(from, to)
59
74
  puts "warning: #{self.class}: replace not implement"
60
75
  end
61
76
 
77
+ # duplicate object
78
+ #
79
+ # duplicate this object's children (if own) and itself.
62
80
  def deep_dup
63
81
  puts "warning: #{self.class}: deep_dup not implement"
64
82
  end
65
83
 
84
+ # compare object
66
85
  def ==(obj)
67
86
  puts "warning: #{self.class}: == not implement"
68
87
  raise "warning: #{self.class}: == not implement"
69
88
  end
70
89
 
90
+ # add / remove parenthesis if need
71
91
  def add_remove_paren(node = self)
72
92
  node.traverse(nil) {|st, parent|
73
93
  if st.respond_to? :remove_paren
@@ -77,8 +97,24 @@ module Minjs
77
97
  }
78
98
  node
79
99
  end
100
+
101
+ # Traverses this children and itself with given block.
102
+ #
103
+ # If this element has children, traverse children first,
104
+ # then yield block with parent and self.
105
+ #
106
+ # @param parent [Base] parent element.
107
+ # @yield [parent, self] parent and this element.
108
+ # @yieldparam [Base] self this element.
109
+ # @yieldparam [Base] parent parent element.
110
+ def traverse(parent, &block)
111
+
112
+ end
80
113
  end
81
114
 
115
+ # Class of ECMA262 Statement List
116
+ #
117
+ # @see http://www.ecma-international.org/ecma-262 ECMA262 12.1
82
118
  class StatementList < Base
83
119
  attr_reader :statement_list
84
120
 
@@ -86,6 +122,7 @@ module Minjs
86
122
  @statement_list = statement_list #array
87
123
  end
88
124
 
125
+ # Groups statements and reduce number of them as few as posibble.
89
126
  def grouping
90
127
  remove_empty_statement
91
128
  new_sl = []
@@ -149,10 +186,14 @@ module Minjs
149
186
  @statement_list = new_sl
150
187
  end
151
188
 
189
+ # duplicate object
190
+ # @see Base#deep_dup
152
191
  def deep_dup
153
192
  self.class.new(@statement_list.collect{|s| s.deep_dup})
154
193
  end
155
194
 
195
+ # Replaces children object
196
+ # @see Base#replace
156
197
  def replace(from, to)
157
198
  idx = @statement_list.index(from)
158
199
  if idx
@@ -160,36 +201,46 @@ module Minjs
160
201
  end
161
202
  end
162
203
 
204
+ # Removes statement from statement list
205
+ # @param st statement
163
206
  def remove(st)
164
207
  @statement_list.delete(st)
165
208
  end
166
209
 
210
+ # Removes empty statement in this statement list
167
211
  def remove_empty_statement
168
212
  @statement_list.reject!{|x|
169
213
  x.class == StEmpty
170
214
  }
171
215
  end
172
216
 
217
+ # Traverses this children and itself with given block.
218
+ # @see Base#traverse
173
219
  def traverse(parent, &block)
174
220
  _self = self
175
221
  @statement_list.each do|st|
176
222
  st.traverse(self, &block)
177
223
  end
178
- yield self, parent
224
+ yield parent, self
179
225
  end
180
226
 
227
+ # compare object
181
228
  def ==(obj)
182
229
  @statement_list == obj.statement_list
183
230
  end
184
231
 
232
+ # Returns a ECMAScript string containg the representation of element.
233
+ # @see Base#to_js
185
234
  def to_js(options = {})
186
235
  concat options, @statement_list
187
236
  end
188
237
 
238
+ # Returns number of the statements
189
239
  def length
190
240
  @statement_list.length
191
241
  end
192
242
 
243
+ # return true if this can convert to expression.
193
244
  def to_exp?
194
245
  @statement_list.each do |s|
195
246
  return false if s.to_exp? == false
@@ -197,6 +248,7 @@ module Minjs
197
248
  return true
198
249
  end
199
250
 
251
+ # Converts statement list to expression and returns it.
200
252
  def to_exp(options = {})
201
253
  return nil if to_exp? == false
202
254
  t = @statement_list[0].to_exp(options)
@@ -213,19 +265,31 @@ module Minjs
213
265
  @statement_list.each(&block)
214
266
  end
215
267
 
268
+ # Returns the statement at index
269
+ # @param i index
270
+ # @return [Statement] statement
216
271
  def [](i)
217
272
  @statement_list[i]
218
273
  end
219
274
 
220
- def []=(i, s)
221
- @statement_list[i] = s
275
+ # Sets the statement at index.
276
+ # @param i index
277
+ # @param st statement
278
+ def []=(i, st)
279
+ @statement_list[i] = st
222
280
  end
223
281
 
282
+ # Returns index of statement.
283
+ # @param st statement.
284
+ # @return [Fixnum] index of statement.
224
285
  def index(st)
225
286
  @statement_list.index(st)
226
287
  end
227
288
  end
228
289
 
290
+ # Class of ECMA262 Source Elements
291
+ #
292
+ # @see http://www.ecma-international.org/ecma-262 ECMA262 14
229
293
  class SourceElements < StatementList
230
294
  #
231
295
  # source_elements: [statement, statement, ...]
@@ -234,19 +298,27 @@ module Minjs
234
298
  @statement_list = source_elements
235
299
  end
236
300
 
301
+ # alias of statement_list
237
302
  def source_elements
238
303
  @statement_list
239
304
  end
240
305
 
306
+ # alias of statement_list=
241
307
  def source_elements=(source_elements)
242
308
  @statement_list = source_elements
243
309
  end
244
310
 
311
+ alias :source_elements :statement_list
312
+
313
+ # compare object
245
314
  def ==(obj)
246
315
  statement_list == obj.statement_list
247
316
  end
248
317
  end
249
318
 
319
+ # Class of ECMA262 Program
320
+ #
321
+ # @see http://www.ecma-international.org/ecma-262 ECMA262 14
250
322
  class Prog < Base
251
323
  attr_reader :source_elements
252
324
  attr_reader :context
@@ -256,25 +328,34 @@ module Minjs
256
328
  @context = context
257
329
  end
258
330
 
331
+ # duplicate object
332
+ # @see Base#deep_dup
259
333
  def deep_dup
260
334
  self.class.new(context, source_elements.deep_dup)
261
335
  end
262
336
 
337
+ # Replaces children object
338
+ # @see Base#replace
263
339
  def replace(from, to)
264
340
  if from == @source_elements
265
341
  @source_elements = to
266
342
  end
267
343
  end
268
344
 
345
+ # Traverses this children and itself with given block.
346
+ # @see Base#traverse
269
347
  def traverse(parent, &block)
270
348
  @source_elements.traverse(self, &block)
271
- yield self, parent
349
+ yield parent, self
272
350
  end
273
351
 
352
+ # compare object
274
353
  def ==(obj)
275
354
  self.class == obj.class and self.source_elements == obj.source_elements
276
355
  end
277
356
 
357
+ # Returns a ECMAScript string containg the representation of element.
358
+ # @see Base#to_js
278
359
  def to_js(options = {})
279
360
  concat options, @source_elements
280
361
  end
@@ -1,5 +1,8 @@
1
1
  module Minjs
2
2
  module ECMA262
3
+ # class of Environment Record
4
+ #
5
+ # @see http://www.ecma-international.org/ecma-262 ECMA262 10.2.1
3
6
  class EnvRecord
4
7
  attr_reader :binding
5
8
  attr_reader :options
@@ -9,6 +12,9 @@ module Minjs
9
12
  @options = {}
10
13
  end
11
14
 
15
+ # CreateMutableBinding(N, D)
16
+ #
17
+ # @see http://www.ecma-international.org/ecma-262 ECMA262 10.2.1
12
18
  def create_mutable_binding(n, d, options = {})
13
19
  if n.kind_of? IdentifierName
14
20
  n = n.val
@@ -16,6 +22,9 @@ module Minjs
16
22
  @binding[n] = {:value => nil}
17
23
  end
18
24
 
25
+ # SetMutableBinding(N, V, S)
26
+ #
27
+ # @see http://www.ecma-international.org/ecma-262 ECMA262 10.2.1
19
28
  def set_mutable_binding(n, v, s, options = {})
20
29
  if n.kind_of? IdentifierName
21
30
  n = n.val
@@ -25,19 +34,21 @@ module Minjs
25
34
  end
26
35
  end
27
36
 
37
+ # class of Declarative Environment Record
38
+ #
39
+ # @see http://www.ecma-international.org/ecma-262 ECMA262 10.2.1.1
28
40
  class DeclarativeEnvRecord < EnvRecord
29
41
  end
30
42
 
43
+ # class of Object Environment Record
44
+ #
45
+ # @see http://www.ecma-international.org/ecma-262 ECMA262 10.2.1.2
31
46
  class ObjectEnvRecord < EnvRecord
32
47
  end
33
48
 
34
- class ExObject
35
- def initialize(options = {})
36
- @attr = options[:attr] || {}
37
- @prop = options[:prop] || {}
38
- end
39
- end
40
-
49
+ # class of Lexical Environment
50
+ #
51
+ # @see http://www.ecma-international.org/ecma-262 ECMA262 10.2
41
52
  class LexEnv
42
53
  attr_reader :record
43
54
  attr_reader :outer
@@ -52,10 +63,16 @@ module Minjs
52
63
  end
53
64
  end
54
65
 
66
+ # NewDeclarativeEnvironment(E)
67
+ #
68
+ # @see http://www.ecma-international.org/ecma-262 ECMA262 10.2.2.2
55
69
  def new_declarative_env(outer = nil)
56
70
  e = LexEnv.new(outer: (outer || self), type: :declarative)
57
71
  end
58
72
 
73
+ # NewObjectEnvironment(O, E)
74
+ #
75
+ # @see http://www.ecma-international.org/ecma-262 ECMA262 10.2.2.3
59
76
  def new_object_env(object, outer = nil)#TODO
60
77
  raise 'TODO'
61
78
  e = LexEnv.new(outer: (outer || self), type: :object)
@@ -67,11 +84,15 @@ module Minjs
67
84
  end
68
85
  end
69
86
 
87
+ # debug
70
88
  def debug
71
89
  STDERR.puts @record.binding.keys.join(", ")
72
90
  end
73
91
  end
74
92
 
93
+ # Class of Execution Contexts
94
+ #
95
+ # @see http://www.ecma-international.org/ecma-262 ECMA262 10.3
75
96
  class Context
76
97
  attr_accessor :lex_env
77
98
  attr_accessor :var_env
@@ -81,17 +102,19 @@ module Minjs
81
102
  @var_env = LexEnv.new(options)
82
103
  @lex_env = LexEnv.new(options)
83
104
  #TODO
84
- @this_binding = ExObject.new(
85
- {
86
- attr: {
87
- writable: true,
88
- enumerable: false,
89
- configurable: true
90
- }
91
- }
92
- )
105
+ @this_binding = nil
106
+ # ExObject.new(
107
+ # {
108
+ # attr: {
109
+ # writable: true,
110
+ # enumerable: false,
111
+ # configurable: true
112
+ # }
113
+ # }
114
+ # )
93
115
  end
94
116
 
117
+ # debug
95
118
  def debug
96
119
  @var_env.debug
97
120
  end