Class: Ripper::RubyBuilder::Token

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/ripper/ruby_builder/token.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type = nil, token = nil, position = nil) ⇒ Token

Returns a new instance of Token.



19
20
21
22
23
# File 'lib/ripper/ruby_builder/token.rb', line 19

def initialize(type = nil, token = nil, position = nil)
  @type = token_type(type, token)
  @token = token
  @position = position if position
end

Instance Attribute Details

#positionObject

Returns the value of attribute position.



17
18
19
# File 'lib/ripper/ruby_builder/token.rb', line 17

def position
  @position
end

#prologObject

Returns the value of attribute prolog.



17
18
19
# File 'lib/ripper/ruby_builder/token.rb', line 17

def prolog
  @prolog
end

#tokenObject

Returns the value of attribute token.



17
18
19
# File 'lib/ripper/ruby_builder/token.rb', line 17

def token
  @token
end

#typeObject

Returns the value of attribute type.



17
18
19
# File 'lib/ripper/ruby_builder/token.rb', line 17

def type
  @type
end

Instance Method Details

#<=>(other) ⇒ Object



73
74
75
# File 'lib/ripper/ruby_builder/token.rb', line 73

def <=>(other)
  position <=> (other.respond_to?(:position) ? other.position : other)
end

#comment?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/ripper/ruby_builder/token.rb', line 57

def comment?
  type == :@comment
end

#heredoc?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/ripper/ruby_builder/token.rb', line 61

def heredoc?
  type == :@heredoc
end

#keyword?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/ripper/ruby_builder/token.rb', line 37

def keyword?
  KEYWORDS.include?(type)
end

#known?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/ripper/ruby_builder/token.rb', line 53

def known?
  keyword? || operator? || opener? || whitespace? || [:@backtick].include?(type)
end

#newline?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/ripper/ruby_builder/token.rb', line 25

def newline?
  NEWLINE.include?(type)
end

#opener?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/ripper/ruby_builder/token.rb', line 33

def opener?
  OPENERS.include?(type)
end

#operator?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/ripper/ruby_builder/token.rb', line 41

def operator?
  OPERATORS.include?(type)
end

#prolog?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/ripper/ruby_builder/token.rb', line 49

def prolog?
  whitespace? or separator? or heredoc?
end

#separator?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/ripper/ruby_builder/token.rb', line 45

def separator?
  SEPARATORS.include?(type)
end

#to_identifierObject



69
70
71
# File 'lib/ripper/ruby_builder/token.rb', line 69

def to_identifier
  Ruby::Identifier.new(token, position, prolog)
end

#to_sexpObject



65
66
67
# File 'lib/ripper/ruby_builder/token.rb', line 65

def to_sexp
  [type, token, [row + 1, column]]
end

#whitespace?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/ripper/ruby_builder/token.rb', line 29

def whitespace?
  WHITESPACE.include?(type)
end