Class: RBS::Inline::AST::Tree
- Inherits:
-
Object
- Object
- RBS::Inline::AST::Tree
- Defined in:
- lib/rbs/inline/ast/tree.rb
Overview
@rbs!
type token = [Symbol, String]
type tree = token | Tree | Types::t | MethodType | nil
Instance Attribute Summary collapse
-
#non_trivia_trees ⇒ Object
readonly
Children but without ‘tWHITESPACE` tokens.
-
#trees ⇒ Object
readonly
: Array.
-
#type ⇒ Object
readonly
: Symbol.
Instance Method Summary collapse
- #<<(tok) ⇒ Object
-
#initialize(type) ⇒ Tree
constructor
A new instance of Tree.
-
#nth_method_type(index) ⇒ Object
Returns n-th method type from the children.
-
#nth_method_type!(index) ⇒ Object
Returns n-th method tree from the children.
-
#nth_method_type?(index) ⇒ Boolean
Returns n-th method type from the children.
-
#nth_token(index) ⇒ Object
Returns n-th token from the children.
-
#nth_token!(index) ⇒ Object
Returns n-th token from the children.
-
#nth_token?(index) ⇒ Boolean
Returns n-th token from the children.
-
#nth_tree(index) ⇒ Object
Returns n-th tree from the children.
-
#nth_tree!(index) ⇒ Object
Returns n-th tree from the children.
-
#nth_tree?(index) ⇒ Boolean
Returns n-th tree from the children.
-
#nth_type(index) ⇒ Object
Returns n-th type from the children.
-
#nth_type!(index) ⇒ Object
Returns n-th type from the children.
-
#nth_type?(index) ⇒ Boolean
Returns n-th type from the children.
-
#to_s ⇒ Object
Returns the source code associated to the tree.
-
#token?(type, at:) ⇒ Boolean
Returns ‘true` if token at the given index is of the given type.
-
#tree?(type, at:) ⇒ Boolean
Returns ‘true` if tree at the given index is of the given type.
Constructor Details
#initialize(type) ⇒ Tree
Returns a new instance of Tree.
19 20 21 22 23 |
# File 'lib/rbs/inline/ast/tree.rb', line 19 def initialize(type) #: void @type = type @trees = [] @non_trivia_trees = [] end |
Instance Attribute Details
#non_trivia_trees ⇒ Object (readonly)
Children but without ‘tWHITESPACE` tokens
16 17 18 |
# File 'lib/rbs/inline/ast/tree.rb', line 16 def non_trivia_trees @non_trivia_trees end |
#trees ⇒ Object (readonly)
: Array
12 13 14 |
# File 'lib/rbs/inline/ast/tree.rb', line 12 def trees @trees end |
#type ⇒ Object (readonly)
: Symbol
13 14 15 |
# File 'lib/rbs/inline/ast/tree.rb', line 13 def type @type end |
Instance Method Details
#<<(tok) ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/rbs/inline/ast/tree.rb', line 27 def <<(tok) trees << tok unless tok.is_a?(Array) && tok[0] == :tWHITESPACE non_trivia_trees << tok end self end |
#nth_method_type(index) ⇒ Object
Returns n-th method type from the children
Raises if the value is not a method type or ‘nil`.
202 203 204 205 206 207 208 209 210 |
# File 'lib/rbs/inline/ast/tree.rb', line 202 def nth_method_type(index) tok = non_trivia_trees[index] case tok when MethodType, nil tok else raise end end |
#nth_method_type!(index) ⇒ Object
Returns n-th method tree from the children
Raises if the value is not a method tree.
234 235 236 |
# File 'lib/rbs/inline/ast/tree.rb', line 234 def nth_method_type!(index) nth_method_type(index) || raise end |
#nth_method_type?(index) ⇒ Boolean
Returns n-th method type from the children
Returns ‘nil` if the value is not a method type.
218 219 220 221 222 223 224 225 226 |
# File 'lib/rbs/inline/ast/tree.rb', line 218 def nth_method_type?(index) tok = non_trivia_trees[index] case tok when MethodType tok else nil end end |
#nth_token(index) ⇒ Object
Returns n-th token from the children
Raises if the value is not a token or nil.
75 76 77 78 79 80 81 82 83 |
# File 'lib/rbs/inline/ast/tree.rb', line 75 def nth_token(index) tok = non_trivia_trees[index] case tok when Array, nil tok else raise end end |
#nth_token!(index) ⇒ Object
Returns n-th token from the children
Raises if the value is not token.
107 108 109 |
# File 'lib/rbs/inline/ast/tree.rb', line 107 def nth_token!(index) nth_token(index) || raise end |
#nth_token?(index) ⇒ Boolean
Returns n-th token from the children
Returns ‘nil` if the value is not a token.
91 92 93 94 95 96 97 98 99 |
# File 'lib/rbs/inline/ast/tree.rb', line 91 def nth_token?(index) tok = non_trivia_trees[index] case tok when Array tok else nil end end |
#nth_tree(index) ⇒ Object
Returns n-th tree from the children
Raises if the value is not a tree or nil.
117 118 119 120 121 122 123 124 125 |
# File 'lib/rbs/inline/ast/tree.rb', line 117 def nth_tree(index) tok = non_trivia_trees[index] case tok when Tree, nil tok else raise end end |
#nth_tree!(index) ⇒ Object
Returns n-th tree from the children
Raises if the value is not a tree.
149 150 151 |
# File 'lib/rbs/inline/ast/tree.rb', line 149 def nth_tree!(index) nth_tree(index) || raise end |
#nth_tree?(index) ⇒ Boolean
Returns n-th tree from the children
Returns ‘nil` if the value is not a tree or nil.
133 134 135 136 137 138 139 140 141 |
# File 'lib/rbs/inline/ast/tree.rb', line 133 def nth_tree?(index) tok = non_trivia_trees[index] case tok when Tree tok else nil end end |
#nth_type(index) ⇒ Object
Returns n-th type from the children
Raises if the value is not a type or nil.
160 161 162 163 164 165 166 167 168 |
# File 'lib/rbs/inline/ast/tree.rb', line 160 def nth_type(index) tok = non_trivia_trees[index] case tok when Array, Tree, MethodType raise else tok end end |
#nth_type!(index) ⇒ Object
Returns n-th type from the children
Raises if the value is not a type.
192 193 194 |
# File 'lib/rbs/inline/ast/tree.rb', line 192 def nth_type!(index) nth_type(index) || raise end |
#nth_type?(index) ⇒ Boolean
Returns n-th type from the children
Returns ‘nil` if the value is not a type.
176 177 178 179 180 181 182 183 184 |
# File 'lib/rbs/inline/ast/tree.rb', line 176 def nth_type?(index) tok = non_trivia_trees[index] case tok when Array, Tree, nil, MethodType nil else tok end end |
#to_s ⇒ Object
Returns the source code associated to the tree
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/rbs/inline/ast/tree.rb', line 36 def to_s #: String buf = +"" trees.each do |tree| case tree when Array buf << tree[1] when Tree buf << tree.to_s when nil else loc = tree.location or raise buf << loc.source end end buf end |
#token?(type, at:) ⇒ Boolean
Returns ‘true` if token at the given index is of the given type
56 57 58 59 60 |
# File 'lib/rbs/inline/ast/tree.rb', line 56 def token?(type, at:) if tok = nth_token?(at) tok[0] == type end end |
#tree?(type, at:) ⇒ Boolean
Returns ‘true` if tree at the given index is of the given type
63 64 65 66 67 |
# File 'lib/rbs/inline/ast/tree.rb', line 63 def tree?(type, at:) if tree = nth_tree?(at) tree.type == type end end |