Class: Masamune::LexNode

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

Overview

docs.ruby-lang.org/en/3.0/Ripper.html#method-c-lex

@location: Line number and starting point on the line. i.e. - [4, 7]. @type: The type of token. i.e. - :kw (is a keyword). @token: The raw string which represents the actual ruby code. i.e. - “do”. @state: Ripper::Lexer::State. i.e. - CMDARG.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(index, raw_lex_node, ast_id) ⇒ LexNode

Returns a new instance of LexNode.



12
13
14
15
16
17
18
19
20
# File 'lib/masamune/lex_node.rb', line 12

def initialize(index, raw_lex_node, ast_id)
  @index = index
  @location, @type, @token, @state = raw_lex_node
  @type = @type.to_s.gsub(/^[a-z]*_/, "").to_sym

  # Since the Abstract Syntax Tree can get very large,
  # We just save the id and reference it with `ast` below.
  @ast_id = ast_id
end

Instance Attribute Details

#ast_idObject

Returns the value of attribute ast_id.



9
10
11
# File 'lib/masamune/lex_node.rb', line 9

def ast_id
  @ast_id
end

#indexObject (readonly)

Returns the value of attribute index.



10
11
12
# File 'lib/masamune/lex_node.rb', line 10

def index
  @index
end

#locationObject

Returns the value of attribute location.



9
10
11
# File 'lib/masamune/lex_node.rb', line 9

def location
  @location
end

#stateObject

Returns the value of attribute state.



9
10
11
# File 'lib/masamune/lex_node.rb', line 9

def state
  @state
end

#tokenObject

Returns the value of attribute token.



9
10
11
# File 'lib/masamune/lex_node.rb', line 9

def token
  @token
end

#typeObject

Returns the value of attribute type.



9
10
11
# File 'lib/masamune/lex_node.rb', line 9

def type
  @type
end

Instance Method Details

#astObject



22
23
24
# File 'lib/masamune/lex_node.rb', line 22

def ast
  ObjectSpace._id2ref(@ast_id)
end

#identifier?Boolean

TODO: I’m not sure how I feel about checking @type against the symbol directly.

Returns:

  • (Boolean)


45
46
47
# File 'lib/masamune/lex_node.rb', line 45

def identifier?
  @type == :ident
end

#method?Boolean

Returns:

  • (Boolean)


39
40
41
42
# File 'lib/masamune/lex_node.rb', line 39

def method?
  return false unless identifier?
  method_definition? || method_call?
end

#method_call?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/masamune/lex_node.rb', line 35

def method_call?
  ast.method_calls(token_value: @token).any?
end

#method_definition?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/masamune/lex_node.rb', line 31

def method_definition?
  ast.method_definitions(token_value: @token).any?
end

#string?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/masamune/lex_node.rb', line 49

def string?
  @type == :tstring_content
end

#variable?Boolean

Returns:

  • (Boolean)


26
27
28
29
# File 'lib/masamune/lex_node.rb', line 26

def variable?
  return false unless identifier?
  ast.variables(token_value: @token).any?
end