Class: Prism::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/masamune/abstract_syntax_tree/prism/node_extensions.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.node_predicate(name, body = -> { true }) ⇒ Object



5
6
7
8
# File 'lib/masamune/abstract_syntax_tree/prism/node_extensions.rb', line 5

def self.node_predicate(name, body = -> { true })
  define_method(name, &body)
  Node.define_method(name) { false } unless Node.respond_to?(name, false)
end

Instance Method Details

#comment?Boolean

Returns:

  • (Boolean)


3
# File 'lib/masamune/abstract_syntax_tree/prism/node_extensions.rb', line 3

def comment? = false

#line_numberObject



10
11
12
# File 'lib/masamune/abstract_syntax_tree/prism/node_extensions.rb', line 10

def line_number
  token_location.start_line
end

#token_locationObject

#location provides helpful information for the source code of a node as a whole, but in Masamune we generally want the token value itself, so we primarily get the token value’s location.



16
17
18
# File 'lib/masamune/abstract_syntax_tree/prism/node_extensions.rb', line 16

def token_location
  location
end

#token_valueObject

The source code of Prism nodes can be retrieved by calling #slice. However, the output tends to vary from node to node, and other methods like ‘name`, `message`, etc. are available in Prism nodes which means you have to know exactly which method to call the get the results you want.

The ‘token` method simplifies all of this, and just gives you the variable, method name, etc. in String form by just calling `token_value`.



27
28
29
# File 'lib/masamune/abstract_syntax_tree/prism/node_extensions.rb', line 27

def token_value
  content
end