rkelly-turbo 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.rdoc +38 -0
- data/Manifest.txt +203 -0
- data/README.rdoc +135 -0
- data/Rakefile +41 -0
- data/lib/parser.y +883 -0
- data/lib/rkelly.rb +14 -0
- data/lib/rkelly/char_pos.rb +39 -0
- data/lib/rkelly/char_range.rb +33 -0
- data/lib/rkelly/constants.rb +3 -0
- data/lib/rkelly/generated_parser.rb +3380 -0
- data/lib/rkelly/js.rb +14 -0
- data/lib/rkelly/js/array.rb +15 -0
- data/lib/rkelly/js/base.rb +91 -0
- data/lib/rkelly/js/boolean.rb +21 -0
- data/lib/rkelly/js/function.rb +39 -0
- data/lib/rkelly/js/function_prototype.rb +15 -0
- data/lib/rkelly/js/global_object.rb +52 -0
- data/lib/rkelly/js/math.rb +10 -0
- data/lib/rkelly/js/nan.rb +18 -0
- data/lib/rkelly/js/number.rb +22 -0
- data/lib/rkelly/js/object.rb +30 -0
- data/lib/rkelly/js/object_prototype.rb +14 -0
- data/lib/rkelly/js/property.rb +20 -0
- data/lib/rkelly/js/scope.rb +6 -0
- data/lib/rkelly/js/string.rb +21 -0
- data/lib/rkelly/lexeme.rb +18 -0
- data/lib/rkelly/nodes.rb +25 -0
- data/lib/rkelly/nodes/binary_node.rb +18 -0
- data/lib/rkelly/nodes/bracket_accessor_node.rb +11 -0
- data/lib/rkelly/nodes/case_clause_node.rb +11 -0
- data/lib/rkelly/nodes/comma_node.rb +11 -0
- data/lib/rkelly/nodes/conditional_node.rb +11 -0
- data/lib/rkelly/nodes/dot_accessor_node.rb +11 -0
- data/lib/rkelly/nodes/for_in_node.rb +12 -0
- data/lib/rkelly/nodes/for_node.rb +13 -0
- data/lib/rkelly/nodes/function_call_node.rb +16 -0
- data/lib/rkelly/nodes/function_decl_node.rb +6 -0
- data/lib/rkelly/nodes/function_expr_node.rb +12 -0
- data/lib/rkelly/nodes/if_node.rb +12 -0
- data/lib/rkelly/nodes/label_node.rb +11 -0
- data/lib/rkelly/nodes/new_expr_node.rb +11 -0
- data/lib/rkelly/nodes/node.rb +116 -0
- data/lib/rkelly/nodes/not_strict_equal_node.rb +6 -0
- data/lib/rkelly/nodes/op_equal_node.rb +16 -0
- data/lib/rkelly/nodes/postfix_node.rb +11 -0
- data/lib/rkelly/nodes/prefix_node.rb +6 -0
- data/lib/rkelly/nodes/property_node.rb +13 -0
- data/lib/rkelly/nodes/resolve_node.rb +19 -0
- data/lib/rkelly/nodes/strict_equal_node.rb +6 -0
- data/lib/rkelly/nodes/try_node.rb +13 -0
- data/lib/rkelly/nodes/var_decl_node.rb +15 -0
- data/lib/rkelly/parser.rb +106 -0
- data/lib/rkelly/runtime.rb +36 -0
- data/lib/rkelly/runtime/ruby_function.rb +13 -0
- data/lib/rkelly/runtime/scope_chain.rb +57 -0
- data/lib/rkelly/syntax_error.rb +4 -0
- data/lib/rkelly/token.rb +27 -0
- data/lib/rkelly/tokenizer.rb +255 -0
- data/lib/rkelly/visitable.rb +31 -0
- data/lib/rkelly/visitors.rb +4 -0
- data/lib/rkelly/visitors/dot_visitor.rb +228 -0
- data/lib/rkelly/visitors/ecma_visitor.rb +328 -0
- data/lib/rkelly/visitors/enumerable_visitor.rb +18 -0
- data/lib/rkelly/visitors/evaluation_visitor.rb +419 -0
- data/lib/rkelly/visitors/function_visitor.rb +46 -0
- data/lib/rkelly/visitors/pointcut_visitor.rb +31 -0
- data/lib/rkelly/visitors/real_sexp_visitor.rb +16 -0
- data/lib/rkelly/visitors/sexp_visitor.rb +373 -0
- data/lib/rkelly/visitors/visitor.rb +149 -0
- data/test/ecma_script_test_case.rb +21 -0
- data/test/execute_test_case.rb +16 -0
- data/test/execution_contexts/test_10_1_3-1.rb +32 -0
- data/test/expressions/test_11_3_1.rb +64 -0
- data/test/expressions/test_11_3_2.rb +64 -0
- data/test/expressions/test_11_4_2.rb +13 -0
- data/test/expressions/test_11_4_3.rb +52 -0
- data/test/expressions/test_11_4_4.rb +68 -0
- data/test/expressions/test_11_4_5.rb +69 -0
- data/test/expressions/test_11_4_6.rb +94 -0
- data/test/expressions/test_11_4_8.rb +28 -0
- data/test/expressions/test_11_4_9.rb +103 -0
- data/test/expressions/test_11_5_1.rb +51 -0
- data/test/expressions/test_11_5_2.rb +80 -0
- data/test/expressions/test_11_5_3.rb +88 -0
- data/test/expressions/test_11_6_1-1.rb +19 -0
- data/test/expressions/test_11_9_1.rb +19 -0
- data/test/function/test_15_3_1_1-1.rb +34 -0
- data/test/global_object/test_15_1_1_1.rb +29 -0
- data/test/global_object/test_15_1_1_2.rb +17 -0
- data/test/global_object/test_15_1_1_3.rb +9 -0
- data/test/helper.rb +5 -0
- data/test/node_test_case.rb +11 -0
- data/test/object/test_15_2_1_1.rb +257 -0
- data/test/object/test_15_2_1_2.rb +21 -0
- data/test/object/test_15_2_2_1.rb +52 -0
- data/test/statements/test_12_5-1.rb +27 -0
- data/test/test_add_node.rb +8 -0
- data/test/test_arguments_node.rb +8 -0
- data/test/test_array_node.rb +9 -0
- data/test/test_assign_expr_node.rb +8 -0
- data/test/test_automatic_semicolon_insertion.rb +137 -0
- data/test/test_bit_and_node.rb +8 -0
- data/test/test_bit_or_node.rb +8 -0
- data/test/test_bit_x_or_node.rb +8 -0
- data/test/test_bitwise_not_node.rb +8 -0
- data/test/test_block_node.rb +14 -0
- data/test/test_bracket_accessor_node.rb +16 -0
- data/test/test_break_node.rb +11 -0
- data/test/test_case_block_node.rb +11 -0
- data/test/test_case_clause_node.rb +15 -0
- data/test/test_char_pos.rb +39 -0
- data/test/test_char_range.rb +29 -0
- data/test/test_comma_node.rb +13 -0
- data/test/test_comments.rb +45 -0
- data/test/test_conditional_node.rb +17 -0
- data/test/test_const_statement_node.rb +14 -0
- data/test/test_continue_node.rb +11 -0
- data/test/test_delete_node.rb +8 -0
- data/test/test_divide_node.rb +8 -0
- data/test/test_do_while_node.rb +13 -0
- data/test/test_dot_accessor_node.rb +9 -0
- data/test/test_ecma_visitor.rb +213 -0
- data/test/test_element_node.rb +8 -0
- data/test/test_empty_statement_node.rb +8 -0
- data/test/test_equal_node.rb +8 -0
- data/test/test_evaluation_visitor.rb +66 -0
- data/test/test_expression_statement_node.rb +10 -0
- data/test/test_false_node.rb +8 -0
- data/test/test_for_in_node.rb +17 -0
- data/test/test_for_node.rb +24 -0
- data/test/test_function_body_node.rb +8 -0
- data/test/test_function_call_node.rb +10 -0
- data/test/test_function_decl_node.rb +16 -0
- data/test/test_function_expr_node.rb +16 -0
- data/test/test_function_visitor.rb +26 -0
- data/test/test_getter_property_node.rb +10 -0
- data/test/test_global_object.rb +49 -0
- data/test/test_greater_node.rb +8 -0
- data/test/test_greater_or_equal_node.rb +8 -0
- data/test/test_if_node.rb +17 -0
- data/test/test_in_node.rb +8 -0
- data/test/test_instance_of_node.rb +8 -0
- data/test/test_label_node.rb +13 -0
- data/test/test_left_shift_node.rb +8 -0
- data/test/test_less_node.rb +8 -0
- data/test/test_less_or_equal_node.rb +8 -0
- data/test/test_line_number.rb +81 -0
- data/test/test_logical_and_node.rb +8 -0
- data/test/test_logical_not_node.rb +8 -0
- data/test/test_logical_or_node.rb +8 -0
- data/test/test_modulus_node.rb +8 -0
- data/test/test_multiply_node.rb +8 -0
- data/test/test_new_expr_node.rb +9 -0
- data/test/test_not_equal_node.rb +8 -0
- data/test/test_not_strict_equal_node.rb +8 -0
- data/test/test_null_node.rb +8 -0
- data/test/test_number_node.rb +8 -0
- data/test/test_object_literal_node.rb +9 -0
- data/test/test_op_and_equal_node.rb +10 -0
- data/test/test_op_divide_equal_node.rb +10 -0
- data/test/test_op_equal_node.rb +10 -0
- data/test/test_op_l_shift_equal_node.rb +10 -0
- data/test/test_op_minus_equal_node.rb +10 -0
- data/test/test_op_mod_equal_node.rb +10 -0
- data/test/test_op_multiply_equal_node.rb +10 -0
- data/test/test_op_or_equal_node.rb +10 -0
- data/test/test_op_plus_equal_node.rb +10 -0
- data/test/test_op_r_shift_equal_node.rb +10 -0
- data/test/test_op_u_r_shift_equal_node.rb +10 -0
- data/test/test_op_x_or_equal_node.rb +10 -0
- data/test/test_parameter_node.rb +8 -0
- data/test/test_parser.rb +1434 -0
- data/test/test_pointcut_visitor.rb +34 -0
- data/test/test_postfix_node.rb +8 -0
- data/test/test_prefix_node.rb +8 -0
- data/test/test_property_node.rb +8 -0
- data/test/test_regexp_node.rb +8 -0
- data/test/test_resolve_node.rb +22 -0
- data/test/test_return_node.rb +11 -0
- data/test/test_right_shift_node.rb +8 -0
- data/test/test_rkelly.rb +19 -0
- data/test/test_runtime.rb +12 -0
- data/test/test_scope_chain.rb +50 -0
- data/test/test_setter_property_node.rb +10 -0
- data/test/test_source_elements.rb +9 -0
- data/test/test_strict_equal_node.rb +8 -0
- data/test/test_string_node.rb +8 -0
- data/test/test_subtract_node.rb +8 -0
- data/test/test_switch_node.rb +12 -0
- data/test/test_this_node.rb +8 -0
- data/test/test_throw_node.rb +7 -0
- data/test/test_tokenizer.rb +285 -0
- data/test/test_true_node.rb +8 -0
- data/test/test_try_node.rb +59 -0
- data/test/test_type_of_node.rb +8 -0
- data/test/test_unary_minus_node.rb +8 -0
- data/test/test_unary_plus_node.rb +8 -0
- data/test/test_unsigned_right_shift_node.rb +8 -0
- data/test/test_var_decl_node.rb +21 -0
- data/test/test_var_statement_node.rb +14 -0
- data/test/test_void_node.rb +8 -0
- data/test/test_while_node.rb +15 -0
- data/test/test_with_node.rb +8 -0
- metadata +293 -0
@@ -0,0 +1,34 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/helper"
|
2
|
+
|
3
|
+
class PointcutVisitorTest < Test::Unit::TestCase
|
4
|
+
include RKelly::Visitors
|
5
|
+
include RKelly::Nodes
|
6
|
+
|
7
|
+
def setup
|
8
|
+
@parser = RKelly::Parser.new
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_visit_NumberNode
|
12
|
+
ast = @parser.parse('Element.update(10, 10)')
|
13
|
+
assert_equal(2, ast.pointcut('10').matches.length)
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_visit_RegexpNode
|
17
|
+
ast = @parser.parse('Element.update(/asdf/, /asdf/)')
|
18
|
+
assert_equal(2, ast.pointcut('/asdf/').matches.length)
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_visit_ContinueNode
|
22
|
+
ast = @parser.parse('function foo() { continue; }')
|
23
|
+
cut = ast.pointcut('continue')
|
24
|
+
assert_equal(1, cut.matches.length)
|
25
|
+
assert cut.matches.first.is_a?(ContinueNode)
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_try_catch
|
29
|
+
ast = @parser.parse('try { Element.update(10, 10); } catch(e) { }')
|
30
|
+
assert_equal(1, ast.pointcut('Element.update(10, 10)').matches.length)
|
31
|
+
ast = @parser.parse('try { Element.update("foo", "bar"); } catch(e) { }')
|
32
|
+
assert_equal(1, ast.pointcut('Element.update(String, String)').matches.length)
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/helper"
|
2
|
+
|
3
|
+
class ResolveNodeTest < NodeTestCase
|
4
|
+
def test_to_sexp
|
5
|
+
node = ResolveNode.new('foo')
|
6
|
+
assert_sexp [:resolve, 'foo'], node
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_match
|
10
|
+
node = ResolveNode.new('foo')
|
11
|
+
node2 = ResolveNode.new('foo')
|
12
|
+
assert(node =~ node2)
|
13
|
+
|
14
|
+
assert(node !~ NumberNode.new(10))
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_is_a
|
18
|
+
node = ResolveNode.new('foo')
|
19
|
+
node3 = ResolveNode.new('String')
|
20
|
+
assert(node3 =~ node)
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/helper"
|
2
|
+
|
3
|
+
class ReturnNodeTest < NodeTestCase
|
4
|
+
def test_to_sexp
|
5
|
+
node = ReturnNode.new(nil)
|
6
|
+
assert_sexp([:return], node)
|
7
|
+
|
8
|
+
node = ReturnNode.new(NumberNode.new(10))
|
9
|
+
assert_sexp([:return, [:lit, 10]], node)
|
10
|
+
end
|
11
|
+
end
|
data/test/test_rkelly.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/helper"
|
2
|
+
|
3
|
+
class RKellyTest < Test::Unit::TestCase
|
4
|
+
def test_array_access
|
5
|
+
assert_sexp(
|
6
|
+
[
|
7
|
+
[:var,
|
8
|
+
[[:var_decl, :a,
|
9
|
+
[:assign, [:bracket_access, [:resolve, "foo"], [:lit, 10]]],
|
10
|
+
]]
|
11
|
+
]
|
12
|
+
],
|
13
|
+
RKelly.parse('var a = foo[10];'))
|
14
|
+
end
|
15
|
+
|
16
|
+
def assert_sexp(expected, node)
|
17
|
+
assert_equal(expected, node.to_sexp)
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/helper"
|
2
|
+
|
3
|
+
class RuntimeTest < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@runtime = RKelly::Runtime.new
|
6
|
+
end
|
7
|
+
|
8
|
+
def test_call_function
|
9
|
+
@runtime.execute("function foo(a) { return a + 2; }")
|
10
|
+
assert_equal(12, @runtime.call_function("foo", 10))
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/helper"
|
2
|
+
|
3
|
+
class TestScopeChain < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@scope_chain = RKelly::Runtime::ScopeChain.new
|
6
|
+
scope_1 = RKelly::JS::Scope.new
|
7
|
+
scope_1.properties[:foo] = 1
|
8
|
+
scope_2 = RKelly::JS::Scope.new
|
9
|
+
scope_2.properties[:bar] = 10
|
10
|
+
@scope_chain << scope_1
|
11
|
+
@scope_chain << scope_2
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_global_object_in_chain
|
15
|
+
assert @scope_chain.has_property?('NaN')
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_has_property
|
19
|
+
assert @scope_chain.has_property?(:foo)
|
20
|
+
assert @scope_chain.has_property?(:bar)
|
21
|
+
assert !@scope_chain.has_property?(:baz)
|
22
|
+
@scope_chain.pop
|
23
|
+
assert @scope_chain.has_property?(:bar).nil?
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_find_property
|
27
|
+
assert_equal(10, @scope_chain[:bar])
|
28
|
+
assert_equal(1, @scope_chain[:foo])
|
29
|
+
@scope_chain.pop
|
30
|
+
assert(!@scope_chain.has_property?(:bar))
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_add_property
|
34
|
+
assert !@scope_chain.has_property?(:baz)
|
35
|
+
@scope_chain[:baz] = 10
|
36
|
+
assert @scope_chain.has_property?(:baz)
|
37
|
+
@scope_chain.pop
|
38
|
+
assert !@scope_chain.has_property?(:baz)
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_chain_in_block
|
42
|
+
assert !@scope_chain.has_property?(:baz)
|
43
|
+
@scope_chain.new_scope { |chain|
|
44
|
+
chain[:baz] = 10
|
45
|
+
assert @scope_chain.has_property?(:baz)
|
46
|
+
assert chain.has_property?(:baz)
|
47
|
+
}
|
48
|
+
assert @scope_chain.has_property?(:baz).nil?
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/helper"
|
2
|
+
|
3
|
+
class SetterPropertyNodeTest < NodeTestCase
|
4
|
+
def test_to_sexp
|
5
|
+
body = FunctionBodyNode.new(SourceElementsNode.new([]))
|
6
|
+
function = FunctionExprNode.new(nil, body)
|
7
|
+
node = SetterPropertyNode.new('foo', function)
|
8
|
+
assert_sexp([:setter, :foo, [:func_expr, nil, [], [:func_body, []]]], node)
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/helper"
|
2
|
+
|
3
|
+
class SwitchNodeTest < NodeTestCase
|
4
|
+
def test_to_sexp
|
5
|
+
clause = CaseClauseNode.new( ResolveNode.new('foo'),
|
6
|
+
SourceElementsNode.new([ResolveNode.new('bar')]))
|
7
|
+
block = CaseBlockNode.new([clause])
|
8
|
+
node = SwitchNode.new(ResolveNode.new('o'), block)
|
9
|
+
assert_sexp([:switch, [:resolve, 'o'],[:case_block, [[:case, [:resolve, 'foo'], [[:resolve, 'bar']]]]]],
|
10
|
+
node)
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,285 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
require File.dirname(__FILE__) + "/helper"
|
3
|
+
|
4
|
+
class TokenizerTest < Test::Unit::TestCase
|
5
|
+
def setup
|
6
|
+
@tokenizer = RKelly::Tokenizer.new
|
7
|
+
end
|
8
|
+
|
9
|
+
{
|
10
|
+
:space => " ",
|
11
|
+
:tab => "\t",
|
12
|
+
:form_feed => "\f",
|
13
|
+
:vertical_tab => "\v",
|
14
|
+
:no_break_space => [0x00A0].pack("U"),
|
15
|
+
:ogham_space_mark => [0x1680].pack("U"),
|
16
|
+
:en_quad => [0x2000].pack("U"),
|
17
|
+
:em_quad => [0x2001].pack("U"),
|
18
|
+
:en_space => [0x2002].pack("U"),
|
19
|
+
:em_space => [0x2003].pack("U"),
|
20
|
+
:three_per_em_space => [0x2004].pack("U"),
|
21
|
+
:four_per_em_space => [0x2005].pack("U"),
|
22
|
+
:six_per_em_space => [0x2006].pack("U"),
|
23
|
+
:figure_space => [0x2007].pack("U"),
|
24
|
+
:punctuation_space => [0x2008].pack("U"),
|
25
|
+
:thin_space => [0x2009].pack("U"),
|
26
|
+
:hair_space => [0x200a].pack("U"),
|
27
|
+
:narrow_no_break_space => [0x202f].pack("U"),
|
28
|
+
:medium_mathematical_space => [0x205f].pack("U"),
|
29
|
+
:ideographic_space => [0x3000].pack("U"),
|
30
|
+
|
31
|
+
# Line terminators
|
32
|
+
:newline => "\n",
|
33
|
+
:carriage_return => "\r",
|
34
|
+
:line_separator => [0x2028].pack("U"),
|
35
|
+
:paragraph_separator => [0x2029].pack("U"),
|
36
|
+
}.each do |name, char|
|
37
|
+
define_method(:"test_whitespace_#{name}") do
|
38
|
+
assert_equal([[:S, char]], @tokenizer.tokenize(char))
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def assert_tokens(expected, actual)
|
43
|
+
assert_equal(expected, actual.select { |x| x[0] != :S })
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_comments
|
47
|
+
tokens = @tokenizer.tokenize("/** Fooo */")
|
48
|
+
assert_tokens([[:COMMENT, '/** Fooo */']], tokens)
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_string_single_quote
|
52
|
+
tokens = @tokenizer.tokenize("foo = 'hello world';")
|
53
|
+
assert_tokens([
|
54
|
+
[:IDENT, 'foo'],
|
55
|
+
['=', '='],
|
56
|
+
[:STRING, "'hello world'"],
|
57
|
+
[';', ';'],
|
58
|
+
], tokens)
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_string_double_quote
|
62
|
+
tokens = @tokenizer.tokenize('foo = "hello world";')
|
63
|
+
assert_tokens([
|
64
|
+
[:IDENT, 'foo'],
|
65
|
+
['=', '='],
|
66
|
+
[:STRING, '"hello world"'],
|
67
|
+
[';', ';'],
|
68
|
+
], tokens)
|
69
|
+
end
|
70
|
+
|
71
|
+
def test_number_parse
|
72
|
+
tokens = @tokenizer.tokenize('3.')
|
73
|
+
assert_tokens([[:NUMBER, 3.0]], tokens)
|
74
|
+
|
75
|
+
tokens = @tokenizer.tokenize('3.e1')
|
76
|
+
assert_tokens([[:NUMBER, 30]], tokens)
|
77
|
+
|
78
|
+
tokens = @tokenizer.tokenize('.001')
|
79
|
+
assert_tokens([[:NUMBER, 0.001]], tokens)
|
80
|
+
|
81
|
+
tokens = @tokenizer.tokenize('3.e-1')
|
82
|
+
assert_tokens([[:NUMBER, 0.30]], tokens)
|
83
|
+
end
|
84
|
+
|
85
|
+
def test_identifier
|
86
|
+
tokens = @tokenizer.tokenize("foo")
|
87
|
+
assert_tokens([[:IDENT, 'foo']], tokens)
|
88
|
+
end
|
89
|
+
|
90
|
+
def test_ignore_identifier
|
91
|
+
tokens = @tokenizer.tokenize("0foo")
|
92
|
+
assert_tokens([[:NUMBER, 0], [:IDENT, 'foo']], tokens)
|
93
|
+
end
|
94
|
+
|
95
|
+
def test_increment
|
96
|
+
tokens = @tokenizer.tokenize("foo += 1;")
|
97
|
+
assert_tokens([
|
98
|
+
[:IDENT, 'foo'],
|
99
|
+
[:PLUSEQUAL, '+='],
|
100
|
+
[:NUMBER, 1],
|
101
|
+
[';', ';'],
|
102
|
+
], tokens)
|
103
|
+
end
|
104
|
+
|
105
|
+
def test_regular_expression
|
106
|
+
tokens = @tokenizer.tokenize("foo = /=asdf/;")
|
107
|
+
assert_tokens([
|
108
|
+
[:IDENT, 'foo'],
|
109
|
+
['=', '='],
|
110
|
+
[:REGEXP, '/=asdf/'],
|
111
|
+
[';', ';'],
|
112
|
+
], tokens)
|
113
|
+
end
|
114
|
+
|
115
|
+
def test_regular_expression_invalid
|
116
|
+
tokens = @tokenizer.tokenize("foo = (1 / 2) / 3")
|
117
|
+
assert_tokens([[:IDENT, "foo"],
|
118
|
+
["=", "="],
|
119
|
+
["(", "("],
|
120
|
+
[:NUMBER, 1],
|
121
|
+
["/", "/"],
|
122
|
+
[:NUMBER, 2],
|
123
|
+
[")", ")"],
|
124
|
+
["/", "/"],
|
125
|
+
[:NUMBER, 3]
|
126
|
+
], tokens)
|
127
|
+
end
|
128
|
+
|
129
|
+
def test_regular_expression_escape
|
130
|
+
tokens = @tokenizer.tokenize('foo = /\/asdf/gi;')
|
131
|
+
assert_tokens([
|
132
|
+
[:IDENT, 'foo'],
|
133
|
+
['=', '='],
|
134
|
+
[:REGEXP, '/\/asdf/gi'],
|
135
|
+
[';', ';'],
|
136
|
+
], tokens)
|
137
|
+
end
|
138
|
+
|
139
|
+
def test_regular_expression_with_slash_inside_charset
|
140
|
+
tokens = @tokenizer.tokenize('foo = /[/]/;')
|
141
|
+
assert_tokens([
|
142
|
+
[:IDENT, 'foo'],
|
143
|
+
['=', '='],
|
144
|
+
[:REGEXP, '/[/]/'],
|
145
|
+
[';', ';'],
|
146
|
+
], tokens)
|
147
|
+
end
|
148
|
+
|
149
|
+
def test_regular_expression_is_not_found_if_prev_token_implies_division
|
150
|
+
{:IDENT => 'foo',
|
151
|
+
:TRUE => 'true',
|
152
|
+
:NUMBER => 1,
|
153
|
+
')' => ')',
|
154
|
+
']' => ']',
|
155
|
+
'}' => '}'}.each do |name, value|
|
156
|
+
tokens = @tokenizer.tokenize("#{value}/2/3")
|
157
|
+
assert_tokens([
|
158
|
+
[name, value],
|
159
|
+
["/", "/"],
|
160
|
+
[:NUMBER, 2],
|
161
|
+
["/", "/"],
|
162
|
+
[:NUMBER, 3],
|
163
|
+
], tokens)
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
167
|
+
def test_regular_expression_is_found_if_prev_token_is_non_literal_keyword
|
168
|
+
{:RETURN => 'return',
|
169
|
+
:THROW => 'throw'}.each do |name, value|
|
170
|
+
tokens = @tokenizer.tokenize("#{value}/2/")
|
171
|
+
assert_tokens([
|
172
|
+
[name, value],
|
173
|
+
[:REGEXP, "/2/"],
|
174
|
+
], tokens)
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
178
|
+
def test_regular_expression_is_not_found_if_block_comment_with_re_modifier
|
179
|
+
tokens = @tokenizer.tokenize("/**/i")
|
180
|
+
assert_tokens([
|
181
|
+
[:COMMENT, "/**/"],
|
182
|
+
[:IDENT, "i"]
|
183
|
+
], tokens)
|
184
|
+
end
|
185
|
+
|
186
|
+
def test_comment_assign
|
187
|
+
tokens = @tokenizer.tokenize("foo = /**/;")
|
188
|
+
assert_tokens([
|
189
|
+
[:IDENT, 'foo'],
|
190
|
+
['=', '='],
|
191
|
+
[:COMMENT, '/**/'],
|
192
|
+
[';', ';'],
|
193
|
+
], tokens)
|
194
|
+
|
195
|
+
tokens = @tokenizer.tokenize("foo = //;")
|
196
|
+
assert_tokens([
|
197
|
+
[:IDENT, 'foo'],
|
198
|
+
['=', '='],
|
199
|
+
[:COMMENT, '//;'],
|
200
|
+
], tokens)
|
201
|
+
end
|
202
|
+
|
203
|
+
def test_unicode_string
|
204
|
+
tokens = @tokenizer.tokenize("foo = 'öäüõ';")
|
205
|
+
assert_tokens([
|
206
|
+
[:IDENT, 'foo'],
|
207
|
+
['=', '='],
|
208
|
+
[:STRING, "'öäüõ'"],
|
209
|
+
[';', ';'],
|
210
|
+
], tokens)
|
211
|
+
end
|
212
|
+
|
213
|
+
def test_unicode_regex
|
214
|
+
tokens = @tokenizer.tokenize("foo = /öäüõ/;")
|
215
|
+
assert_tokens([
|
216
|
+
[:IDENT, 'foo'],
|
217
|
+
['=', '='],
|
218
|
+
[:REGEXP, "/öäüõ/"],
|
219
|
+
[';', ';'],
|
220
|
+
], tokens)
|
221
|
+
end
|
222
|
+
|
223
|
+
%w{
|
224
|
+
break case catch continue default delete do else finally for function
|
225
|
+
if in instanceof new return switch this throw try typeof var void while
|
226
|
+
with
|
227
|
+
|
228
|
+
const true false null debugger
|
229
|
+
}.each do |kw|
|
230
|
+
define_method(:"test_keyword_#{kw}") do
|
231
|
+
tokens = @tokenizer.tokenize(kw)
|
232
|
+
assert_equal 1, tokens.length
|
233
|
+
assert_equal([[kw.upcase.to_sym, kw]], tokens)
|
234
|
+
end
|
235
|
+
end
|
236
|
+
|
237
|
+
%w{
|
238
|
+
class enum extends super export import
|
239
|
+
}.each do |rw|
|
240
|
+
define_method(:"test_future_reserved_word_#{rw}_is_reserved") do
|
241
|
+
tokens = @tokenizer.tokenize(rw)
|
242
|
+
assert_equal 1, tokens.length
|
243
|
+
assert_equal([[:RESERVED, rw]], tokens)
|
244
|
+
end
|
245
|
+
end
|
246
|
+
|
247
|
+
%w{
|
248
|
+
implements let private public yield
|
249
|
+
interface package protected static
|
250
|
+
}.each do |rw|
|
251
|
+
define_method(:"test_future_reserved_word_#{rw}_is_identifier") do
|
252
|
+
tokens = @tokenizer.tokenize(rw)
|
253
|
+
assert_equal 1, tokens.length
|
254
|
+
assert_equal([[:IDENT, rw]], tokens)
|
255
|
+
end
|
256
|
+
end
|
257
|
+
|
258
|
+
{
|
259
|
+
'==' => :EQEQ,
|
260
|
+
'!=' => :NE,
|
261
|
+
'===' => :STREQ,
|
262
|
+
'!==' => :STRNEQ,
|
263
|
+
'<=' => :LE,
|
264
|
+
'>=' => :GE,
|
265
|
+
'||' => :OR,
|
266
|
+
'&&' => :AND,
|
267
|
+
'++' => :PLUSPLUS,
|
268
|
+
'--' => :MINUSMINUS,
|
269
|
+
'<<' => :LSHIFT,
|
270
|
+
'>>' => :RSHIFT,
|
271
|
+
'>>>' => :URSHIFT,
|
272
|
+
'+=' => :PLUSEQUAL,
|
273
|
+
'-=' => :MINUSEQUAL,
|
274
|
+
'*=' => :MULTEQUAL,
|
275
|
+
'null' => :NULL,
|
276
|
+
'true' => :TRUE,
|
277
|
+
'false' => :FALSE,
|
278
|
+
}.each do |punctuator, sym|
|
279
|
+
define_method(:"test_punctuator_#{sym}") do
|
280
|
+
tokens = @tokenizer.tokenize(punctuator)
|
281
|
+
assert_equal 1, tokens.length
|
282
|
+
assert_equal([[sym, punctuator]], tokens)
|
283
|
+
end
|
284
|
+
end
|
285
|
+
end
|