Class: Yard2steep::Gen
- Inherits:
-
Object
- Object
- Yard2steep::Gen
- Defined in:
- lib/yard2steep/gen.rb
Instance Method Summary collapse
- #emit!(s, off: 0) ⇒ void
- #gen(ast) ⇒ String
-
#gen_block_p!(p) ⇒ void
TODO(south37) Represents block param and return type separately.
- #gen_c!(c_node, off:) ⇒ void
- #gen_c_list!(c_node, off:) ⇒ void
- #gen_child!(c_node, off:) ⇒ void
- #gen_children!(c_node, off:) ⇒ void
-
#gen_ivar_list!(c_node, off:) ⇒ void
NOTE: In current impl, ivar’s type is declared as any.
- #gen_m!(m_node, off:) ⇒ void
- #gen_m_list!(c_node, off:) ⇒ void
- #gen_m_p!(p_node) ⇒ void
- #gen_p_list!(p_list) ⇒ void
-
#initialize ⇒ Gen
constructor
A new instance of Gen.
Constructor Details
#initialize ⇒ Gen
Returns a new instance of Gen.
5 6 7 |
# File 'lib/yard2steep/gen.rb', line 5 def initialize @out = StringIO.new # Output buffer end |
Instance Method Details
#emit!(s, off: 0) ⇒ void
This method returns an undefined value.
21 22 23 24 25 26 27 28 |
# File 'lib/yard2steep/gen.rb', line 21 def emit!(s, off: 0) Util.assert! { off >= 0 } if off == 0 @out.print(s) else @out.print("#{' ' * off}#{s}") end end |
#gen(ast) ⇒ String
11 12 13 14 15 16 |
# File 'lib/yard2steep/gen.rb', line 11 def gen(ast) gen_child!(ast, off: 0) @out.rewind @out.read end |
#gen_block_p!(p) ⇒ void
This method returns an undefined value.
TODO(south37) Represents block param and return type separately
141 142 143 |
# File 'lib/yard2steep/gen.rb', line 141 def gen_block_p!(p) emit! "#{p.type_node.p_type} " end |
#gen_c!(c_node, off:) ⇒ void
This method returns an undefined value.
94 95 96 97 98 |
# File 'lib/yard2steep/gen.rb', line 94 def gen_c!(c_node, off:) Util.assert! { c_node.is_a?(AST::ConstantNode) } # NOTE: Use any as constant type. emit! "#{c_node.long_name}: #{c_node.v_type}\n", off: off end |
#gen_c_list!(c_node, off:) ⇒ void
This method returns an undefined value.
76 77 78 79 80 |
# File 'lib/yard2steep/gen.rb', line 76 def gen_c_list!(c_node, off:) c_node.c_list.each do |c_node| gen_c!(c_node, off: off) end end |
#gen_child!(c_node, off:) ⇒ void
This method returns an undefined value.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/yard2steep/gen.rb', line 33 def gen_child!(c_node, off:) Util.assert! { c_node.is_a?(AST::ClassNode) } # TODO(south37) We should check `main` by class_node's type (not name). if c_node.c_name == 'main' # NOTE: In main, steep does not check method type, so we does not # generate type definition of methods. gen_children!(c_node, off: 0) else if (c_node.m_list.size > 0) || (c_node.ivar_list.size > 0) emit! "#{c_node.kind} #{c_node.long_name}#{c_node.super_c ? " <: #{c_node.long_super}" : nil}\n", off: off gen_ivar_list!(c_node, off: off + 2) gen_m_list!(c_node, off: off + 2) emit! "end\n", off: off end gen_c_list!(c_node, off: off) gen_children!(c_node, off: off) end end |
#gen_children!(c_node, off:) ⇒ void
This method returns an undefined value.
85 86 87 88 89 |
# File 'lib/yard2steep/gen.rb', line 85 def gen_children!(c_node, off:) c_node.children.each do |child| gen_child!(child, off: off) end end |
#gen_ivar_list!(c_node, off:) ⇒ void
This method returns an undefined value.
NOTE: In current impl, ivar’s type is declared as any
67 68 69 70 71 |
# File 'lib/yard2steep/gen.rb', line 67 def gen_ivar_list!(c_node, off:) c_node.ivar_list.each do |ivar_node| emit! "@#{ivar_node.name}: any\n", off: off end end |
#gen_m!(m_node, off:) ⇒ void
This method returns an undefined value.
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/yard2steep/gen.rb', line 103 def gen_m!(m_node, off:) Util.assert! { m_node.is_a?(AST::MethodNode) } emit! "def #{m_node.m_name}: ", off: off p_list = m_node.p_list # NOTE: Check presence of block variable at last. if p_list[-1] && (p_list[-1].type_node.kind == AST::PTypeNode::KIND[:block]) gen_p_list!(p_list[0..-2]) gen_block_p!(p_list[-1]) else gen_p_list!(p_list) end emit! "-> #{m_node.r_type}\n" end |
#gen_m_list!(c_node, off:) ⇒ void
This method returns an undefined value.
56 57 58 59 60 |
# File 'lib/yard2steep/gen.rb', line 56 def gen_m_list!(c_node, off:) c_node.m_list.each do |m_node| gen_m!(m_node, off: off) end end |
#gen_m_p!(p_node) ⇒ void
This method returns an undefined value.
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/yard2steep/gen.rb', line 147 def gen_m_p!(p_node) t = p_node.type_node case p_node.style when AST::PNode::STYLE[:normal] emit! t.p_type when AST::PNode::STYLE[:normal_with_default] emit! "?#{t.p_type}" when AST::PNode::STYLE[:keyword] emit! "#{t.p_name}: #{t.p_type}" when AST::PNode::STYLE[:keyword_with_default] emit! "?#{t.p_name}: #{t.p_type}" else raise "invalid style: #{p_node.style}" end end |
#gen_p_list!(p_list) ⇒ void
This method returns an undefined value.
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/yard2steep/gen.rb', line 121 def gen_p_list!(p_list) len = p_list.size if len > 0 emit! "(" p_list.each.with_index do |p, i| gen_m_p!(p) if i < (len - 1) emit!(", ") end end emit! ") " end end |