to_source 0.2.0 → 0.2.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.
- data/Changelog.md +5 -0
- data/Gemfile +0 -1
- data/Gemfile.devtools +0 -1
- data/README.md +3 -3
- data/TODO +2 -0
- data/lib/to_source/version.rb +1 -1
- data/lib/to_source/visitor.rb +58 -4
- data/spec/unit/to_source/visitor/class_methods/run_spec.rb +37 -1
- data/to_source.gemspec +1 -1
- metadata +4 -4
data/Changelog.md
CHANGED
data/Gemfile
CHANGED
data/Gemfile.devtools
CHANGED
data/README.md
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
to_source
|
2
2
|
=========
|
3
3
|
|
4
|
-
[](https://codeclimate.com/github/
|
4
|
+
[](http://travis-ci.org/txus/to_source)
|
5
|
+
[](https://gemnasium.com/txus/to_source)
|
6
|
+
[](https://codeclimate.com/github/txus/to_source)
|
7
7
|
|
8
8
|
Reverse parser to generate source code from the Rubinius AST. Also works well under MRI.
|
9
9
|
|
data/TODO
CHANGED
data/lib/to_source/version.rb
CHANGED
data/lib/to_source/visitor.rb
CHANGED
@@ -16,6 +16,28 @@ module ToSource
|
|
16
16
|
new(node).output
|
17
17
|
end
|
18
18
|
|
19
|
+
# Return handler registry
|
20
|
+
#
|
21
|
+
# @return [Hash]
|
22
|
+
#
|
23
|
+
# @api private
|
24
|
+
#
|
25
|
+
def self.registry
|
26
|
+
@registry ||= {}
|
27
|
+
end
|
28
|
+
|
29
|
+
# Register handler class
|
30
|
+
#
|
31
|
+
# @param [Class:Rubinius::AST::Node] node_klass
|
32
|
+
#
|
33
|
+
# @return [undefined]
|
34
|
+
#
|
35
|
+
# @api private
|
36
|
+
#
|
37
|
+
def self.register(node_klass)
|
38
|
+
registry[node_klass]=self
|
39
|
+
end
|
40
|
+
|
19
41
|
# Return the source code of AST
|
20
42
|
#
|
21
43
|
# @return [String]
|
@@ -51,9 +73,14 @@ module ToSource
|
|
51
73
|
# @api private
|
52
74
|
#
|
53
75
|
def dispatch(node)
|
54
|
-
|
55
|
-
|
56
|
-
|
76
|
+
handler = self.class.registry.fetch(node.class) do
|
77
|
+
name = node.node_name
|
78
|
+
name = "#{name}_def" if %w(class module).include?(name)
|
79
|
+
__send__(name, node)
|
80
|
+
nil
|
81
|
+
end
|
82
|
+
|
83
|
+
handler.run(self, node) if handler
|
57
84
|
end
|
58
85
|
|
59
86
|
# Emit file
|
@@ -1522,6 +1549,24 @@ module ToSource
|
|
1522
1549
|
kend
|
1523
1550
|
end
|
1524
1551
|
|
1552
|
+
# Emit pattern arguments
|
1553
|
+
#
|
1554
|
+
# @param [Rubinius::AST::PatternArguments] node
|
1555
|
+
#
|
1556
|
+
# @return [undefined]
|
1557
|
+
#
|
1558
|
+
# @api private
|
1559
|
+
#
|
1560
|
+
def pattern_arguments(node)
|
1561
|
+
emit('(')
|
1562
|
+
arguments = node.arguments.body
|
1563
|
+
arguments.each_with_index do |argument, index|
|
1564
|
+
emit(argument.name)
|
1565
|
+
emit(', ') unless index == arguments.size - 1
|
1566
|
+
end
|
1567
|
+
emit(')')
|
1568
|
+
end
|
1569
|
+
|
1525
1570
|
# Emit formal arguments as shared between ruby18 and ruby19 mode
|
1526
1571
|
#
|
1527
1572
|
# @param [Rubinius::AST::Node] node
|
@@ -1535,7 +1580,15 @@ module ToSource
|
|
1535
1580
|
required, defaults, splat = node.required, node.defaults, node.splat
|
1536
1581
|
|
1537
1582
|
emit(open)
|
1538
|
-
|
1583
|
+
|
1584
|
+
required.each_with_index do |node, index|
|
1585
|
+
if node.kind_of?(Rubinius::AST::Node)
|
1586
|
+
dispatch(node)
|
1587
|
+
else
|
1588
|
+
emit(node)
|
1589
|
+
end
|
1590
|
+
emit(', ') unless index == required.size-1
|
1591
|
+
end
|
1539
1592
|
|
1540
1593
|
empty = required.empty?
|
1541
1594
|
|
@@ -1547,6 +1600,7 @@ module ToSource
|
|
1547
1600
|
if splat
|
1548
1601
|
emit(', ') unless empty
|
1549
1602
|
emit('*')
|
1603
|
+
empty = false
|
1550
1604
|
unless splat == :@unnamed_splat
|
1551
1605
|
emit(splat)
|
1552
1606
|
end
|
@@ -20,7 +20,7 @@ describe ToSource::Visitor,'.run' do
|
|
20
20
|
end
|
21
21
|
|
22
22
|
def self.assert_source(source)
|
23
|
-
let(:node) { source.to_ast }
|
23
|
+
let(:node) { puts source; source.to_ast }
|
24
24
|
let(:source) { source }
|
25
25
|
let(:expected_source) { source }
|
26
26
|
|
@@ -398,6 +398,14 @@ describe ToSource::Visitor,'.run' do
|
|
398
398
|
end
|
399
399
|
end
|
400
400
|
|
401
|
+
context 'with block that takes pattern arguments' do
|
402
|
+
assert_source <<-RUBY
|
403
|
+
foo.bar do |(a, b), c|
|
404
|
+
d
|
405
|
+
end
|
406
|
+
RUBY
|
407
|
+
end
|
408
|
+
|
401
409
|
context 'with block that takes arguments' do
|
402
410
|
assert_source <<-RUBY
|
403
411
|
foo.bar do |a|
|
@@ -427,6 +435,13 @@ describe ToSource::Visitor,'.run' do
|
|
427
435
|
RUBY
|
428
436
|
end
|
429
437
|
|
438
|
+
context 'with splat and block argument' do
|
439
|
+
assert_source <<-RUBY
|
440
|
+
def foo.bar(*args, &block)
|
441
|
+
some_stuff
|
442
|
+
end
|
443
|
+
RUBY
|
444
|
+
end
|
430
445
|
|
431
446
|
context 'with passing block argument' do
|
432
447
|
assert_source 'foo.bar(&baz)'
|
@@ -524,6 +539,11 @@ describe ToSource::Visitor,'.run' do
|
|
524
539
|
context "on calls #{operator}" do
|
525
540
|
assert_source "a #{operator} b"
|
526
541
|
end
|
542
|
+
|
543
|
+
end
|
544
|
+
|
545
|
+
pending 'nested binary operators' do
|
546
|
+
assert_source "(a or b) || c"
|
527
547
|
end
|
528
548
|
end
|
529
549
|
|
@@ -565,6 +585,22 @@ describe ToSource::Visitor,'.run' do
|
|
565
585
|
context 'double negation' do
|
566
586
|
assert_source '!!1'
|
567
587
|
end
|
588
|
+
|
589
|
+
pending 'unary match' do
|
590
|
+
assert_source '~a'
|
591
|
+
end
|
592
|
+
|
593
|
+
pending 'unary match' do
|
594
|
+
assert_source '~a'
|
595
|
+
end
|
596
|
+
|
597
|
+
pending 'unary minus' do
|
598
|
+
assert_source '-a'
|
599
|
+
end
|
600
|
+
|
601
|
+
pending 'unary plus' do
|
602
|
+
assert_source '+a'
|
603
|
+
end
|
568
604
|
end
|
569
605
|
|
570
606
|
context 'if statement' do
|
data/to_source.gemspec
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: to_source
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2012-12-
|
13
|
+
date: 2012-12-08 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: adamantium
|
@@ -35,7 +35,7 @@ dependencies:
|
|
35
35
|
requirements:
|
36
36
|
- - ~>
|
37
37
|
- !ruby/object:Gem::Version
|
38
|
-
version: 2.0.
|
38
|
+
version: 2.0.3
|
39
39
|
type: :runtime
|
40
40
|
prerelease: false
|
41
41
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -43,7 +43,7 @@ dependencies:
|
|
43
43
|
requirements:
|
44
44
|
- - ~>
|
45
45
|
- !ruby/object:Gem::Version
|
46
|
-
version: 2.0.
|
46
|
+
version: 2.0.3
|
47
47
|
description: Transform your Rubinius AST nodes back to source code. Reverse parsing!
|
48
48
|
email:
|
49
49
|
- josep.m.bach@gmail.com
|