minjs 0.1.2 → 0.1.3
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.
- checksums.yaml +4 -4
- data/lib/minjs/compressor.rb +87 -22
- data/lib/minjs/ctype.rb +5 -0
- data/lib/minjs/ecma262/base.rb +10 -0
- data/lib/minjs/ecma262/exp.rb +194 -27
- data/lib/minjs/ecma262/lit.rb +59 -108
- data/lib/minjs/ecma262/st.rb +93 -22
- data/lib/minjs/exceptions.rb +10 -2
- data/lib/minjs/expression.rb +15 -17
- data/lib/minjs/func.rb +11 -4
- data/lib/minjs/lex.rb +85 -36
- data/lib/minjs/program.rb +0 -4
- data/lib/minjs/statement.rb +11 -17
- data/lib/minjs/version.rb +1 -1
- metadata +2 -2
data/lib/minjs/lex.rb
CHANGED
@@ -115,7 +115,7 @@ module Minjs
|
|
115
115
|
lf = false
|
116
116
|
while (@codes[@pos] != 0x2a or @codes[@pos + 1] != 0x2f)
|
117
117
|
if @codes[@pos].nil?
|
118
|
-
raise ParseError.new("no `*/' at end of comment")
|
118
|
+
raise ParseError.new("no `*/' at end of comment", self)
|
119
119
|
end
|
120
120
|
if line_terminator?(@codes[@pos])
|
121
121
|
lf = true
|
@@ -159,21 +159,44 @@ module Minjs
|
|
159
159
|
ret
|
160
160
|
end
|
161
161
|
|
162
|
+
def unicode_escape?
|
163
|
+
if @codes[@pos] == 0x5c and
|
164
|
+
@codes[@pos+1] == 0x75 and
|
165
|
+
hex_number?(@codes[@pos+2]) and
|
166
|
+
hex_number?(@codes[@pos+3]) and
|
167
|
+
hex_number?(@codes[@pos+4]) and
|
168
|
+
hex_number?(@codes[@pos+5])
|
169
|
+
[@codes[@pos+2],@codes[@pos+3],@codes[@pos+4],@codes[@pos+5]].pack("U*").to_i(16)
|
170
|
+
else
|
171
|
+
false
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
162
175
|
def identifier_name
|
163
176
|
pos0 = @pos
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
177
|
+
return nil if @codes[@pos].nil?
|
178
|
+
|
179
|
+
chars = []
|
180
|
+
if u=unicode_escape? and identifier_start?(u)
|
181
|
+
chars.push(u)
|
182
|
+
@pos += 6
|
183
|
+
elsif identifier_start?(@codes[@pos])
|
184
|
+
chars.push(@codes[@pos])
|
185
|
+
@pos += 1
|
186
|
+
else
|
187
|
+
return nil
|
188
|
+
end
|
189
|
+
|
190
|
+
while true
|
191
|
+
code = @codes[@pos]
|
192
|
+
if u=unicode_escape? and (identifier_start?(u) || identifier_part?(u))
|
193
|
+
chars.push(u)
|
194
|
+
@pos += 6
|
195
|
+
elsif identifier_part?(@codes[@pos])
|
196
|
+
chars.push(@codes[@pos])
|
168
197
|
@pos += 1
|
169
|
-
|
170
|
-
|
171
|
-
break
|
172
|
-
elsif identifier_part?(code)
|
173
|
-
;#
|
174
|
-
else
|
175
|
-
return ECMA262::IdentifierName.new(nil, @codes[pos0...@pos].pack("U*").to_sym)
|
176
|
-
end
|
198
|
+
else
|
199
|
+
return ECMA262::IdentifierName.new(nil, chars.pack("U*").to_sym)
|
177
200
|
end
|
178
201
|
end
|
179
202
|
end
|
@@ -359,22 +382,22 @@ module Minjs
|
|
359
382
|
|
360
383
|
def regexp_body
|
361
384
|
if @codes[@pos] == 0x2a
|
362
|
-
raise ParseError.new("first character of regular expression is `*'")
|
385
|
+
raise ParseError.new("first character of regular expression is `*'", self)
|
363
386
|
end
|
364
387
|
pos0 = @pos
|
365
388
|
@pos += 1
|
366
389
|
while !(@codes[@pos] == 0x2f)
|
367
390
|
if @codes[@pos].nil?
|
368
|
-
raise ParseError.new("no `/' end of regular expression")
|
391
|
+
raise ParseError.new("no `/' end of regular expression", self)
|
369
392
|
end
|
370
393
|
if line_terminator?(@codes[@pos])
|
371
394
|
debug_lit
|
372
|
-
raise ParseError.new("regular expression has line terminator in body")
|
395
|
+
raise ParseError.new("regular expression has line terminator in body", self)
|
373
396
|
end
|
374
397
|
if @codes[@pos] == 0x5c # \
|
375
398
|
@pos += 1
|
376
399
|
if line_terminator?(@codes[@pos])
|
377
|
-
raise ParseError.new("regular expression has line terminator in body")
|
400
|
+
raise ParseError.new("regular expression has line terminator in body", self)
|
378
401
|
end
|
379
402
|
@pos += 1
|
380
403
|
elsif @codes[@pos] == 0x5b # [
|
@@ -389,20 +412,20 @@ module Minjs
|
|
389
412
|
|
390
413
|
def regexp_class
|
391
414
|
if @codes[@pos] != 0x5b
|
392
|
-
raise ParseError.new('bad regular expression')
|
415
|
+
raise ParseError.new('bad regular expression', self)
|
393
416
|
end
|
394
417
|
@pos += 1
|
395
418
|
while !(@codes[@pos] == 0x5d)
|
396
419
|
if @codes[@pos].nil?
|
397
|
-
raise ParseError.new("no `]' end of regular expression class")
|
420
|
+
raise ParseError.new("no `]' end of regular expression class", self)
|
398
421
|
end
|
399
422
|
if line_terminator?(@codes[@pos])
|
400
|
-
raise ParseError.new("regular expression has line terminator in body")
|
423
|
+
raise ParseError.new("regular expression has line terminator in body", self)
|
401
424
|
end
|
402
425
|
if @codes[@pos] == 0x5c # \
|
403
426
|
@pos += 1
|
404
427
|
if line_terminator?(@codes[@pos])
|
405
|
-
raise ParseError.new("regular expression has line terminator in body")
|
428
|
+
raise ParseError.new("regular expression has line terminator in body", self)
|
406
429
|
end
|
407
430
|
@pos += 1
|
408
431
|
else
|
@@ -437,9 +460,11 @@ module Minjs
|
|
437
460
|
while true
|
438
461
|
code = @codes[@pos]
|
439
462
|
if (code >= 0x30 and code <= 0x39) || (code >= 0x41 and code <= 0x4f) || (code >= 0x61 and code <= 0x6f)
|
463
|
+
;
|
464
|
+
elsif identifier_start?(code)
|
465
|
+
raise ParseError.new("The source character immediately following a NumericLiteral must not be an IdentifierStart or DecimalDigit", self)
|
440
466
|
else
|
441
|
-
|
442
|
-
return ECMA262::ECMA262Numeric.new(raw, @codes[(pos0+2)...@pos].pack("U*").to_i(16))
|
467
|
+
return ECMA262::ECMA262Numeric.new(@codes[(pos0+2)...@pos].pack("U*").to_i(16))
|
443
468
|
end
|
444
469
|
@pos += 1
|
445
470
|
end
|
@@ -462,27 +487,34 @@ module Minjs
|
|
462
487
|
@pos += 1
|
463
488
|
e = exp_part
|
464
489
|
end
|
465
|
-
|
466
|
-
|
467
|
-
|
468
|
-
|
490
|
+
if identifier_start?(@codes[@pos])
|
491
|
+
raise ParseError.new("The source character immediately following a NumericLiteral must not be an IdentifierStart or DecimalDigit", self)
|
492
|
+
end
|
493
|
+
|
494
|
+
return ECMA262::ECMA262Numeric.new(0, f, e)
|
469
495
|
end
|
496
|
+
|
470
497
|
if code >= 0x30 and code <= 0x39
|
471
498
|
i = decimal_digits
|
472
|
-
if @codes[@pos] == 0x2e
|
499
|
+
if @codes[@pos] == 0x2e #.
|
473
500
|
@pos += 1
|
474
501
|
f = decimal_digits
|
475
|
-
if @codes[@pos] == 0x65 || @codes[@pos] == 0x45
|
502
|
+
if @codes[@pos] == 0x65 || @codes[@pos] == 0x45 #e or E
|
476
503
|
@pos += 1
|
477
504
|
e = exp_part
|
478
505
|
end
|
479
|
-
elsif @codes[@pos] == 0x65 || @codes[@pos] == 0x45
|
506
|
+
elsif @codes[@pos] == 0x65 || @codes[@pos] == 0x45 #e or E
|
480
507
|
@pos += 1
|
481
508
|
e = exp_part
|
482
509
|
end
|
483
|
-
|
484
|
-
|
510
|
+
if identifier_start?(@codes[@pos])
|
511
|
+
raise ParseError.new("The source character immediately following a NumericLiteral must not be an IdentifierStart or DecimalDigit", self)
|
512
|
+
end
|
513
|
+
|
514
|
+
return ECMA262::ECMA262Numeric.new(i, f, e)
|
485
515
|
end
|
516
|
+
|
517
|
+
nil
|
486
518
|
end
|
487
519
|
|
488
520
|
def exp_part
|
@@ -493,7 +525,7 @@ module Minjs
|
|
493
525
|
neg = true
|
494
526
|
end
|
495
527
|
if neg
|
496
|
-
e =
|
528
|
+
e = "-#{decimal_digits}"
|
497
529
|
else
|
498
530
|
e = decimal_digits
|
499
531
|
end
|
@@ -510,7 +542,7 @@ module Minjs
|
|
510
542
|
if code >= 0x30 and code <= 0x39
|
511
543
|
@pos += 1
|
512
544
|
else
|
513
|
-
return @codes[pos0...@pos].pack("U*")
|
545
|
+
return @codes[pos0...@pos].pack("U*")
|
514
546
|
end
|
515
547
|
end
|
516
548
|
else
|
@@ -536,9 +568,9 @@ module Minjs
|
|
536
568
|
@pos += 1
|
537
569
|
code = @codes[@pos]
|
538
570
|
if code.nil?
|
539
|
-
raise ParseError.new("no `#{term}' at end of string")
|
571
|
+
raise ParseError.new("no `#{term}' at end of string", self)
|
540
572
|
elsif line_terminator?(code)
|
541
|
-
raise ParseError.new("string has line terminator in body")
|
573
|
+
raise ParseError.new("string has line terminator in body", self)
|
542
574
|
elsif code == 0x5c #\
|
543
575
|
@pos += 1
|
544
576
|
str << esc_string
|
@@ -741,5 +773,22 @@ module Minjs
|
|
741
773
|
end
|
742
774
|
end
|
743
775
|
end
|
776
|
+
|
777
|
+
def line_col(pos)
|
778
|
+
_pos = 0
|
779
|
+
row = 0
|
780
|
+
col = 1
|
781
|
+
@codes.each do |code|
|
782
|
+
break if _pos >= pos
|
783
|
+
if line_terminator?(code)
|
784
|
+
row += 1
|
785
|
+
row = 0
|
786
|
+
else
|
787
|
+
col += 1
|
788
|
+
end
|
789
|
+
_pos += 1
|
790
|
+
end
|
791
|
+
return [row+1, col]
|
792
|
+
end
|
744
793
|
end
|
745
794
|
end
|
data/lib/minjs/program.rb
CHANGED
data/lib/minjs/statement.rb
CHANGED
@@ -68,14 +68,7 @@ module Minjs
|
|
68
68
|
if s = statement_list(lex, context) and lex.match_lit(ECMA262::PUNC_RCURLYBRAC)
|
69
69
|
ECMA262::StBlock.new(s)
|
70
70
|
else
|
71
|
-
|
72
|
-
lex.debug_lit
|
73
|
-
puts lex.debug_code(pos0, lex.pos)
|
74
|
-
raise 'no "}" end of block'
|
75
|
-
else
|
76
|
-
lex.debug_lit
|
77
|
-
raise "bad block"
|
78
|
-
end
|
71
|
+
raise ParseError.new('no "}" end of block', lex)
|
79
72
|
end
|
80
73
|
}
|
81
74
|
end
|
@@ -98,7 +91,7 @@ module Minjs
|
|
98
91
|
# variable_statement
|
99
92
|
#
|
100
93
|
def var_statement(lex, context)
|
101
|
-
raise 'error' if context.nil?
|
94
|
+
raise 'internal error' if context.nil?
|
102
95
|
return nil unless lex.match_lit(ECMA262::ID_VAR)
|
103
96
|
lex.eval_lit {
|
104
97
|
if vl = var_decl_list(lex, context, {}) and semicolon(lex, context)
|
@@ -111,7 +104,7 @@ module Minjs
|
|
111
104
|
ECMA262::StVar.new(context, vl)
|
112
105
|
else
|
113
106
|
lex.debug_lit
|
114
|
-
raise Minjs::ParseError.new("var_statement")
|
107
|
+
raise Minjs::ParseError.new("var_statement", lex)
|
115
108
|
end
|
116
109
|
}
|
117
110
|
end
|
@@ -185,9 +178,10 @@ module Minjs
|
|
185
178
|
#12.5
|
186
179
|
#
|
187
180
|
def if_statement(lex, context)
|
181
|
+
return nil unless lex.match_lit(ECMA262::ID_IF)
|
188
182
|
lex.eval_lit {
|
189
|
-
unless lex.match_lit(ECMA262::
|
190
|
-
|
183
|
+
unless lex.match_lit(ECMA262::PUNC_LPARENTHESIS) and cond=exp(lex, context, {}) and lex.match_lit(ECMA262::PUNC_RPARENTHESIS) and s=statement(lex, context)
|
184
|
+
raise ParseError.new("bad statement", lex)
|
191
185
|
end
|
192
186
|
if lex.match_lit(ECMA262::ID_ELSE) and e=statement(lex, context)
|
193
187
|
ECMA262::StIf.new(cond, s, e)
|
@@ -209,7 +203,7 @@ module Minjs
|
|
209
203
|
ECMA262::StWhile.new(e, s)
|
210
204
|
else
|
211
205
|
lex.debug_lit
|
212
|
-
raise ParseError.new("while_statement")
|
206
|
+
raise ParseError.new("while_statement", lex)
|
213
207
|
end
|
214
208
|
end
|
215
209
|
|
@@ -219,7 +213,7 @@ module Minjs
|
|
219
213
|
ECMA262::StDoWhile.new(e, s)
|
220
214
|
else
|
221
215
|
lex.debug_lit
|
222
|
-
raise ParseError.new("do_while_statement")
|
216
|
+
raise ParseError.new("do_while_statement", lex)
|
223
217
|
end
|
224
218
|
end
|
225
219
|
|
@@ -324,7 +318,7 @@ module Minjs
|
|
324
318
|
ECMA262::StWith.new(e, s)
|
325
319
|
else
|
326
320
|
lex.debug_lit
|
327
|
-
raise ParseError.new("switch_statement")
|
321
|
+
raise ParseError.new("switch_statement", lex)
|
328
322
|
end
|
329
323
|
}
|
330
324
|
end
|
@@ -338,7 +332,7 @@ module Minjs
|
|
338
332
|
ECMA262::StSwitch.new(e, c)
|
339
333
|
else
|
340
334
|
lex.debug_lit
|
341
|
-
raise ParseError.new("switch_statement")
|
335
|
+
raise ParseError.new("switch_statement", lex)
|
342
336
|
end
|
343
337
|
}
|
344
338
|
end
|
@@ -384,7 +378,7 @@ module Minjs
|
|
384
378
|
ECMA262::StThrow.new(e)
|
385
379
|
else
|
386
380
|
lex.debug_lit
|
387
|
-
raise ParseError.new("throw_statement")
|
381
|
+
raise ParseError.new("throw_statement", lex)
|
388
382
|
end
|
389
383
|
}
|
390
384
|
end
|
data/lib/minjs/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: minjs
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Issei Numata
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-05-
|
11
|
+
date: 2015-05-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|