Class: Herb::AST::ERBBeginNode

Inherits:
Node
  • Object
show all
Includes:
Colors
Defined in:
lib/herb/ast/nodes.rb,
ext/herb/nodes.c

Overview

: type serialized_erb_begin_node = { | tag_opening: Herb::Token?, | content: Herb::Token?, | tag_closing: Herb::Token?, | prism_node: String?, | statements: Array, | rescue_clause: Herb::AST::ERBRescueNode?, | else_clause: Herb::AST::ERBElseNode?, | ensure_clause: Herb::AST::ERBEnsureNode?, | end_node: Herb::AST::ERBEndNode?, | }

Constant Summary

Constants included from Colors

Colors::CLEAR_SCREEN, Colors::HIDE_CURSOR, Colors::SHOW_CURSOR

Instance Attribute Summary collapse

Attributes inherited from Node

#errors, #location, #source, #type

Instance Method Summary collapse

Methods included from Colors

bold, bright_magenta, cyan, dimmed, enabled?, fg, fg_bg, green, magenta, red, white, yellow

Methods inherited from Node

#class_name, #inspect_array, #inspect_errors, #node_name, #recursive_errors, #to_json

Constructor Details

#initialize(type, location, errors, tag_opening, content, tag_closing, prism_node, statements, rescue_clause, else_clause, ensure_clause, end_node) ⇒ ERBBeginNode

: (String, Location, Array, Herb::Token, Herb::Token, Herb::Token, String, Array, Herb::AST::ERBRescueNode, Herb::AST::ERBElseNode, Herb::AST::ERBEnsureNode, Herb::AST::ERBEndNode) -> void



3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
# File 'lib/herb/ast/nodes.rb', line 3234

def initialize(type, location, errors, tag_opening, content, tag_closing, prism_node, statements, rescue_clause, else_clause, ensure_clause, end_node)
  super(type, location, errors)
  @tag_opening = tag_opening
  @content = content
  @tag_closing = tag_closing
  @prism_node = prism_node
  @statements = statements
  @rescue_clause = rescue_clause
  @else_clause = else_clause
  @ensure_clause = ensure_clause
  @end_node = end_node
end

Instance Attribute Details

#contentObject (readonly)

: Herb::Token?



3224
3225
3226
# File 'lib/herb/ast/nodes.rb', line 3224

def content
  @content
end

#else_clauseObject (readonly)

: Herb::AST::ERBElseNode?



3229
3230
3231
# File 'lib/herb/ast/nodes.rb', line 3229

def else_clause
  @else_clause
end

#end_nodeObject (readonly)

: Herb::AST::ERBEndNode?



3231
3232
3233
# File 'lib/herb/ast/nodes.rb', line 3231

def end_node
  @end_node
end

#ensure_clauseObject (readonly)

: Herb::AST::ERBEnsureNode?



3230
3231
3232
# File 'lib/herb/ast/nodes.rb', line 3230

def ensure_clause
  @ensure_clause
end

#prism_nodeObject (readonly)

: String?



3226
3227
3228
# File 'lib/herb/ast/nodes.rb', line 3226

def prism_node
  @prism_node
end

#rescue_clauseObject (readonly)

: Herb::AST::ERBRescueNode?



3228
3229
3230
# File 'lib/herb/ast/nodes.rb', line 3228

def rescue_clause
  @rescue_clause
end

#statementsObject (readonly)

: Array



3227
3228
3229
# File 'lib/herb/ast/nodes.rb', line 3227

def statements
  @statements
end

#tag_closingObject (readonly)

: Herb::Token?



3225
3226
3227
# File 'lib/herb/ast/nodes.rb', line 3225

def tag_closing
  @tag_closing
end

#tag_openingObject (readonly)

: Herb::Token?



3223
3224
3225
# File 'lib/herb/ast/nodes.rb', line 3223

def tag_opening
  @tag_opening
end

Instance Method Details

#accept(visitor) ⇒ Object

: (Visitor) -> void



3279
3280
3281
# File 'lib/herb/ast/nodes.rb', line 3279

def accept(visitor)
  visitor.visit_erb_begin_node(self)
end

#child_nodesObject

: () -> Array



3284
3285
3286
# File 'lib/herb/ast/nodes.rb', line 3284

def child_nodes
  [*(statements || []), rescue_clause, else_clause, ensure_clause, end_node]
end

#compact_child_nodesObject

: () -> Array



3289
3290
3291
# File 'lib/herb/ast/nodes.rb', line 3289

def compact_child_nodes
  child_nodes.compact
end

#deserialized_prism_nodeObject

: () -> Prism::node?



3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
# File 'lib/herb/ast/nodes.rb', line 3248

def deserialized_prism_node
  prism_node = @prism_node
  return nil unless prism_node
  return nil unless source

  begin
    require "prism"
  rescue LoadError
    warn "The 'prism' gem is required to deserialize Prism nodes. Add it to your Gemfile or install it with: gem install prism"
    return nil
  end

  Prism.load(source, prism_node).value
end

#inspectObject

: () -> String



3294
3295
3296
# File 'lib/herb/ast/nodes.rb', line 3294

def inspect
  tree_inspect.rstrip.gsub(/\s+$/, "")
end

#to_hashObject

: () -> serialized_erb_begin_node



3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
# File 'lib/herb/ast/nodes.rb', line 3264

def to_hash
  super.merge({
    tag_opening: tag_opening,
    content: content,
    tag_closing: tag_closing,
    prism_node: prism_node,
    statements: statements,
    rescue_clause: rescue_clause,
    else_clause: else_clause,
    ensure_clause: ensure_clause,
    end_node: end_node,
  }) #: Herb::serialized_erb_begin_node
end

#tree_inspect(indent: 0, depth: 0, depth_limit: 10) ⇒ Object

: (?indent: Integer, ?depth: Integer, ?depth_limit: Integer) -> String



3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
# File 'lib/herb/ast/nodes.rb', line 3299

def tree_inspect(indent: 0, depth: 0, depth_limit: 10)
  output = +""

  output += white("@ #{bold(yellow(node_name.to_s))} #{dimmed("(location: #{location.tree_inspect})")}")
  output += "\n"

  if depth >= depth_limit
    output += dimmed("└── [depth limit reached ...]\n\n")

    return output.gsub(/^/, "    " * indent)
  end

  output += inspect_errors(prefix: "")

  output += white("├── tag_opening: ")
  output += tag_opening ? tag_opening.tree_inspect : magenta("")
  output += "\n"
  output += white("├── content: ")
  output += content ? content.tree_inspect : magenta("")
  output += "\n"
  output += white("├── tag_closing: ")
  output += tag_closing ? tag_closing.tree_inspect : magenta("")
  output += "\n"
  if prism_node && source
    output += white("├── prism_node: ")
    output += Herb::PrismInspect.inspect_prism_serialized(prism_node, source, "")
    output += "\n"
  end
  output += white("├── statements: ")
  output += inspect_array(statements, prefix: "", indent: indent, depth: depth + 1, depth_limit: depth_limit)
  output += white("├── rescue_clause: ")
  if rescue_clause
    output += "\n"
    output += "│   └── "
    output += rescue_clause.tree_inspect(indent: indent, depth: depth + 1, depth_limit: depth_limit).gsub(/^/, "    " * (indent + 1)).lstrip.gsub(/^/, "").delete_prefix("")
  else
    output += magenta("∅\n")
  end
  output += white("├── else_clause: ")
  if else_clause
    output += "\n"
    output += "│   └── "
    output += else_clause.tree_inspect(indent: indent, depth: depth + 1, depth_limit: depth_limit).gsub(/^/, "    " * (indent + 1)).lstrip.gsub(/^/, "").delete_prefix("")
  else
    output += magenta("∅\n")
  end
  output += white("├── ensure_clause: ")
  if ensure_clause
    output += "\n"
    output += "│   └── "
    output += ensure_clause.tree_inspect(indent: indent, depth: depth + 1, depth_limit: depth_limit).gsub(/^/, "    " * (indent + 1)).lstrip.gsub(/^/, "").delete_prefix("")
  else
    output += magenta("∅\n")
  end
  output += white("└── end_node: ")
  if end_node
    output += "\n"
    output += "    └── "
    output += end_node.tree_inspect(indent: indent, depth: depth + 1, depth_limit: depth_limit).gsub(/^/, "    " * (indent + 1)).lstrip.gsub(/^/, "    ").delete_prefix("    ")
  else
    output += magenta("∅\n")
  end
  output += "\n"

  output.gsub(/^/, "    " * indent)
end