Class: Y2R::AST::YCP::Type

Inherits:
Object
  • Object
show all
Defined in:
lib/y2r/ast/ycp.rb

Overview

Represents a YCP type.

Constant Summary collapse

BOOLEAN =
Type.new("boolean")
INTEGER =
Type.new("integer")
SYMBOL =
Type.new("symbol")
STRING =
Type.new("string")
PATH =
Type.new("path")
IMMUTABLE_TYPES =
[BOOLEAN, INTEGER, SYMBOL, STRING, PATH]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type) ⇒ Type

Returns a new instance of Type.



84
85
86
# File 'lib/y2r/ast/ycp.rb', line 84

def initialize(type)
  @type = type
end

Instance Attribute Details

#typeObject (readonly)

Returns the value of attribute type.



82
83
84
# File 'lib/y2r/ast/ycp.rb', line 82

def type
  @type
end

Instance Method Details

#==(other) ⇒ Object



88
89
90
# File 'lib/y2r/ast/ycp.rb', line 88

def ==(other)
  other.instance_of?(Type) && other.type == @type
end

#arg_typesObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/y2r/ast/ycp.rb', line 108

def arg_types
  nesting_level = 0

  # First, extract content of the parens with arguments. This is a bit
  # tricky, as they don't have to be the first parens in the type
  # specification. For example, a type of function returning a reference
  # to a function returning integer looks like this:
  #
  #   integer()()
  #
  in_parens = ""
  @type.each_char do |ch|
    case ch
      when '('
        in_parens = "" if nesting_level == 0
        nesting_level += 1
      when ')'
        nesting_level -= 1
      else
        in_parens += ch
    end
  end

  types = []
  type = ""
  in_parens.each_char do |ch|
    case ch
      when ","
        if nesting_level == 0
          types << type
          type = ""
        else
          type += ch
        end

      when "(", "<"
        nesting_level += 1
        type += ch

      when ")", ">"
        nesting_level -= 1
        type += ch

      else
        type += ch
    end
  end
  types << type unless type.empty?

  types.map { |t| Type.new(t.strip) }
end

#needs_copy?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/y2r/ast/ycp.rb', line 104

def needs_copy?
  !IMMUTABLE_TYPES.include?(no_const) && !reference?
end

#no_constObject



100
101
102
# File 'lib/y2r/ast/ycp.rb', line 100

def no_const
  @type =~ /^const / ? Type.new(@type.sub(/^const /, "")) : self
end

#reference?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/y2r/ast/ycp.rb', line 96

def reference?
  @type =~ /&$/
end

#to_sObject



92
93
94
# File 'lib/y2r/ast/ycp.rb', line 92

def to_s
  @type
end