Class: SyntaxTree::CHAR

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

Overview

CHAR irepresents a single codepoint in the script encoding.

?a

In the example above, the CHAR node represents the string literal “a”. You can use control characters with this as well, as in ?C-a.

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Constructor Details

#initialize(value:, location:, comments: []) ⇒ CHAR

Returns a new instance of CHAR.



173
174
175
176
177
# File 'lib/syntax_tree/node.rb', line 173

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



171
172
173
# File 'lib/syntax_tree/node.rb', line 171

def comments
  @comments
end

#valueObject (readonly)

String

the value of the character literal



168
169
170
# File 'lib/syntax_tree/node.rb', line 168

def value
  @value
end

Instance Method Details

#child_nodesObject Also known as: deconstruct



179
180
181
# File 'lib/syntax_tree/node.rb', line 179

def child_nodes
  []
end

#deconstruct_keys(keys) ⇒ Object



185
186
187
# File 'lib/syntax_tree/node.rb', line 185

def deconstruct_keys(keys)
  { value: value, location: location, comments: comments }
end

#format(q) ⇒ Object



189
190
191
192
193
194
195
196
197
# File 'lib/syntax_tree/node.rb', line 189

def format(q)
  if value.length != 2
    q.text(value)
  else
    q.text(q.quote)
    q.text(value[1] == "\"" ? "\\\"" : value[1])
    q.text(q.quote)
  end
end

#pretty_print(q) ⇒ Object



199
200
201
202
203
204
205
206
207
208
# File 'lib/syntax_tree/node.rb', line 199

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

    q.breakable
    q.pp(value)

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

#to_json(*opts) ⇒ Object



210
211
212
213
214
# File 'lib/syntax_tree/node.rb', line 210

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