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.



13239
13240
13241
13242
13243
# File 'lib/syntax_tree.rb', line 13239

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



13237
13238
13239
# File 'lib/syntax_tree.rb', line 13237

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



13234
13235
13236
# File 'lib/syntax_tree.rb', line 13234

def location
  @location
end

#partsObject (readonly)

Array[ StringEmbExpr | StringDVar | TStringContent ]

the parts of the

word



13231
13232
13233
# File 'lib/syntax_tree.rb', line 13231

def parts
  @parts
end

Instance Method Details

#child_nodesObject



13249
13250
13251
# File 'lib/syntax_tree.rb', line 13249

def child_nodes
  parts
end

#format(q) ⇒ Object



13253
13254
13255
# File 'lib/syntax_tree.rb', line 13253

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

#match?(pattern) ⇒ Boolean

Returns:

  • (Boolean)


13245
13246
13247
# File 'lib/syntax_tree.rb', line 13245

def match?(pattern)
  parts.any? { |part| part.is_a?(TStringContent) && part.match?(pattern) }
end

#pretty_print(q) ⇒ Object



13257
13258
13259
13260
13261
13262
13263
13264
13265
13266
# File 'lib/syntax_tree.rb', line 13257

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



13268
13269
13270
13271
13272
# File 'lib/syntax_tree.rb', line 13268

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