class SyntaxTree::Translation::Parser::HeredocBuilder
Heredocs are represented very differently in the parser gem from how they are represented in the Syntax Tree AST. This class is responsible for handling the translation.
Constants
- Line
Attributes
node[R]
segments[R]
Public Class Methods
new(node)
click to toggle source
# File lib/syntax_tree/translation/parser.rb, line 16 def initialize(node) @node = node @segments = [] end
Public Instance Methods
<<(segment)
click to toggle source
# File lib/syntax_tree/translation/parser.rb, line 21 def <<(segment) if segment.type == :str && segments.last && segments.last.type == :str && !segments.last.children.first.end_with?("\n") segments.last.children.first << segment.children.first else segments << segment end end
trim!()
click to toggle source
# File lib/syntax_tree/translation/parser.rb, line 31 def trim! return unless node.beginning.value[2] == "~" lines = [Line.new(+"", [])] segments.each do |segment| lines.last.segments << segment if segment.type == :str lines.last.value << segment.children.first lines << Line.new(+"", []) if lines.last.value.end_with?("\n") end end lines.pop if lines.last.value.empty? return if lines.empty? segments.clear lines.each do |line| remaining = node.dedent line.segments.each do |segment| if segment.type == :str if remaining > 0 whitespace = segment.children.first[/^\s{0,#{remaining}}/] segment.children.first.sub!(/^#{whitespace}/, "") remaining -= whitespace.length end if node.beginning.value[3] != "'" && segments.any? && segments.last.type == :str && segments.last.children.first.end_with?("\\\n") segments.last.children.first.gsub!(/\\\n\z/, "") segments.last.children.first.concat(segment.children.first) elsif !segment.children.first.empty? segments << segment end else segments << segment end end end end