Module: IRB::NestingParser
- Defined in:
- lib/irb/nesting_parser.rb
Defined Under Namespace
Classes: NestingElem, NestingVisitor
Class Method Summary collapse
-
.open_nestings(parse_lex_result) ⇒ Object
Return a list of open nesting elements at EOF position.
-
.parse_by_line(parse_lex_result) ⇒ Object
Calculates nesting information [prev_opens, next_opens, min_depth] for each line.
Class Method Details
.open_nestings(parse_lex_result) ⇒ Object
Return a list of open nesting elements at EOF position
345 346 347 |
# File 'lib/irb/nesting_parser.rb', line 345 def open_nestings(parse_lex_result) parse_by_line(parse_lex_result).last[1] end |
.parse_by_line(parse_lex_result) ⇒ Object
Calculates nesting information [prev_opens, next_opens, min_depth] for each line. Example code
["hello
world"+(
First line
prev_opens: []
next_opens: [lbracket, tstring_beg]
min_depth: 0 (minimum at beginning of line)
Second line
prev_opens: [lbracket, tstring_beg]
next_opens: [lbracket, lparen]
min_depth: 1 (minimum just after tstring_end)
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 |
# File 'lib/irb/nesting_parser.rb', line 362 def parse_by_line(parse_lex_result) visitor = NestingVisitor.new node, tokens = parse_lex_result.value node.accept(visitor) tokens.each do |token,| case token.type when :EMBDOC_BEGIN visitor.open_location(token.location, :on_embdoc_beg, '=begin') when :EMBDOC_END visitor.close_location_start(token.location) end end nestings = visitor.nestings last_nesting = nestings.last || [] num_lines = parse_lex_result.source.source.lines.size num_lines.times.map do |i| prev_opens = i == 0 ? [] : nestings[i - 1] || last_nesting opens = nestings[i] || last_nesting min_depth = prev_opens.zip(opens).take_while { |s, e| s == e }.size [prev_opens, opens, min_depth] end end |