Class: Steep::Source
- Inherits:
-
Object
- Object
- Steep::Source
- Defined in:
- lib/steep/source.rb
Defined Under Namespace
Classes: Builder, LocatedAnnotation
Instance Attribute Summary collapse
-
#mapping ⇒ Object
readonly
Returns the value of attribute mapping.
-
#node ⇒ Object
readonly
Returns the value of attribute node.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Class Method Summary collapse
- .construct_mapping(node:, annotations:, mapping:, line_range: nil) ⇒ Object
- .each_child_node(node) ⇒ Object
- .parse(source_code, path:, labeling: ASTUtils::Labeling.new) ⇒ Object
- .parser ⇒ Object
Instance Method Summary collapse
- #annotations(block:, builder:, current_module:) ⇒ Object
- #each_annotation ⇒ Object
-
#initialize(path:, node:, mapping:) ⇒ Source
constructor
A new instance of Source.
Constructor Details
#initialize(path:, node:, mapping:) ⇒ Source
Returns a new instance of Source.
25 26 27 28 29 |
# File 'lib/steep/source.rb', line 25 def initialize(path:, node:, mapping:) @path = path @node = node @mapping = mapping end |
Instance Attribute Details
#mapping ⇒ Object (readonly)
Returns the value of attribute mapping.
23 24 25 |
# File 'lib/steep/source.rb', line 23 def mapping @mapping end |
#node ⇒ Object (readonly)
Returns the value of attribute node.
22 23 24 |
# File 'lib/steep/source.rb', line 22 def node @node end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
21 22 23 |
# File 'lib/steep/source.rb', line 21 def path @path end |
Class Method Details
.construct_mapping(node:, annotations:, mapping:, line_range: nil) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
# File 'lib/steep/source.rb', line 91 def self.construct_mapping(node:, annotations:, mapping:, line_range: nil) case node.type when :if if node.loc.is_a?(::Parser::Source::Map::Ternary) # Skip ternary operator each_child_node node do |child| construct_mapping(node: child, annotations: annotations, mapping: mapping, line_range: nil) end else if node.loc.expression.begin_pos == node.loc.keyword.begin_pos construct_mapping(node: node.children[0], annotations: annotations, mapping: mapping, line_range: nil) if node.children[1] if node.loc.keyword.source == "if" || node.loc.keyword.source == "elsif" then_start = node.loc.begin&.loc&.last_line || node.children[0].loc.last_line then_end = node.children[2] ? node.loc.else.line : node.loc.last_line else then_start = node.loc.else.last_line then_end = node.loc.last_line end construct_mapping(node: node.children[1], annotations: annotations, mapping: mapping, line_range: then_start...then_end) end if node.children[2] if node.loc.keyword.source == "if" || node.loc.keyword.source == "elsif" else_start = node.loc.else.last_line else_end = node.loc.last_line else else_start = node.loc.begin&.last_line || node.children[0].loc.last_line else_end = node.children[1] ? node.loc.else.line : node.loc.last_line end construct_mapping(node: node.children[2], annotations: annotations, mapping: mapping, line_range: else_start...else_end) end else # postfix if/unless each_child_node(node) do |child| construct_mapping(node: child, annotations: annotations, mapping: mapping, line_range: nil) end end end when :while, :until if node.loc.expression.begin_pos == node.loc.keyword.begin_pos construct_mapping(node: node.children[0], annotations: annotations, mapping: mapping, line_range: nil) if node.children[1] body_start = node.children[0].loc.last_line body_end = node.loc.end.line construct_mapping(node: node.children[1], annotations: annotations, mapping: mapping, line_range: body_start...body_end) end else # postfix while each_child_node(node) do |child| construct_mapping(node: child, annotations: annotations, mapping: mapping, line_range: nil) end end when :while_post, :until_post construct_mapping(node: node.children[0], annotations: annotations, mapping: mapping, line_range: nil) if node.children[1] body_start = node.loc.expression.line body_end = node.loc.keyword.line construct_mapping(node: node.children[1], annotations: annotations, mapping: mapping, line_range: body_start...body_end) end when :case if node.children[0] construct_mapping(node: node.children[0], annotations: annotations, mapping: mapping, line_range: nil) end if node.loc.else else_node = node.children.last else_start = node.loc.else.last_line else_end = node.loc.end.line construct_mapping(node: else_node, annotations: annotations, mapping: mapping, line_range: else_start...else_end) end node.children.drop(1).each do |child| if child&.type == :when construct_mapping(node: child, annotations: annotations, mapping: mapping, line_range: nil) end end when :when last_cond = node.children[-2] body = node.children.last node.children.take(node.children.size-1) do |child| construct_mapping(node: child, annotations: annotations, mapping: mapping, line_range: nil) end if body cond_end = last_cond.loc.last_line+1 body_end = body.loc.last_line construct_mapping(node: body, annotations: annotations, mapping: mapping, line_range: cond_end...body_end) end when :rescue if node.children.last else_node = node.children.last else_start = node.loc.else.last_line else_end = node.loc.last_line construct_mapping(node: else_node, annotations: annotations, mapping: mapping, line_range: else_start...else_end) end each_child_node(node) do |child| construct_mapping(node: child, annotations: annotations, mapping: mapping, line_range: nil) end else each_child_node(node) do |child| construct_mapping(node: child, annotations: annotations, mapping: mapping, line_range: nil) end end associated_annotations = annotations.select do |annot| case node.type when :def, :module, :class, :block, :ensure, :defs loc = node.loc loc.line <= annot.line && annot.line < loc.last_line when :resbody if node.loc.keyword.begin_pos == node.loc.expression.begin_pos # skip postfix rescue loc = node.loc loc.line <= annot.line && annot.line < loc.last_line end else if line_range line_range.begin <= annot.line && annot.line < line_range.end end end end associated_annotations.each do |annot| mapping[node.__id__] = [] unless mapping.key?(node.__id__) mapping[node.__id__] << annot.annotation annotations.delete annot end end |
.each_child_node(node) ⇒ Object
269 270 271 272 273 274 275 |
# File 'lib/steep/source.rb', line 269 def self.each_child_node(node) node.children.each do |child| if child.is_a?(::AST::Node) yield child end end end |
.parse(source_code, path:, labeling: ASTUtils::Labeling.new) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/steep/source.rb', line 47 def self.parse(source_code, path:, labeling: ASTUtils::Labeling.new) buffer = ::Parser::Source::Buffer.new(path.to_s, 1) buffer.source = source_code node = parser.parse(buffer).yield_self do |n| if n labeling.translate(n, {}) else return end end annotations = [] _, comments, _ = yield_self do buffer = ::Parser::Source::Buffer.new(path.to_s) buffer.source = source_code parser = ::Parser::Ruby25.new parser.tokenize(buffer) end buffer = AST::Buffer.new(name: path, content: source_code) comments.each do |comment| src = comment.text.gsub(/\A#/, '') annotation = Steep::Parser.parse_annotation_opt(src, buffer: buffer, offset: comment.location.expression.begin_pos+1) if annotation annotations << LocatedAnnotation.new(line: comment.location.line, source: src, annotation: annotation) end end mapping = {} construct_mapping(node: node, annotations: annotations, mapping: mapping) annotations.each do |annot| mapping[node.__id__] = [] unless mapping.key?(node.__id__) mapping[node.__id__] << annot.annotation end new(path: path, node: node, mapping: mapping) end |
Instance Method Details
#annotations(block:, builder:, current_module:) ⇒ Object
277 278 279 |
# File 'lib/steep/source.rb', line 277 def annotations(block:, builder:, current_module:) AST::Annotation::Collection.new(annotations: mapping[block.__id__] || [], builder: builder, current_module: current_module) end |
#each_annotation ⇒ Object
281 282 283 284 285 286 287 288 289 290 |
# File 'lib/steep/source.rb', line 281 def each_annotation if block_given? mapping.each_key do |id| node = ObjectSpace._id2ref(id) yield node, mapping[id] end else enum_for :each_annotation end end |