Class: SyntaxTree::Word

Inherits:
Object
  • Object
show all
Defined in:
lib/syntax_tree.rb

Overview

Word represents an element within a special array literal that accepts interpolation.

%W[a#{b}c xyz]

In the example above, there would be two Word nodes within a parent Words node.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parts:, location:, comments: []) ⇒ Word

Returns a new instance of Word.



12745
12746
12747
12748
12749
# File 'lib/syntax_tree.rb', line 12745

def initialize(parts:, location:, comments: [])
  @parts = parts
  @location = location
  @comments = comments
end

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



12743
12744
12745
# File 'lib/syntax_tree.rb', line 12743

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



12740
12741
12742
# File 'lib/syntax_tree.rb', line 12740

def location
  @location
end

#partsObject (readonly)

Array[ StringEmbExpr | StringDVar | TStringContent ]

the parts of the

word



12737
12738
12739
# File 'lib/syntax_tree.rb', line 12737

def parts
  @parts
end

Instance Method Details

#child_nodesObject



12751
12752
12753
# File 'lib/syntax_tree.rb', line 12751

def child_nodes
  parts
end

#format(q) ⇒ Object



12755
12756
12757
# File 'lib/syntax_tree.rb', line 12755

def format(q)
  q.format_each(parts)
end

#pretty_print(q) ⇒ Object



12759
12760
12761
12762
12763
12764
12765
12766
12767
12768
# File 'lib/syntax_tree.rb', line 12759

def pretty_print(q)
  q.group(2, "(", ")") do
    q.text("word")

    q.breakable
    q.group(2, "(", ")") { q.seplist(parts) { |part| q.pp(part) } }

    q.pp(Comment::List.new(comments))
  end
end

#to_json(*opts) ⇒ Object



12770
12771
12772
12773
12774
# File 'lib/syntax_tree.rb', line 12770

def to_json(*opts)
  { type: :word, parts: parts, loc: location, cmts: comments }.to_json(
    *opts
  )
end