Class: SyntaxTree::In
- Inherits:
-
Object
- Object
- SyntaxTree::In
- Defined in:
- lib/syntax_tree.rb
Overview
In represents using the in keyword within the Ruby 2.7+ pattern matching syntax.
case value
in pattern
end
Instance Attribute Summary collapse
-
#comments ⇒ Object
readonly
- Array[ Comment | EmbDoc ]
-
the comments attached to this node.
-
#consequent ⇒ Object
readonly
- nil | In | Else
-
the next clause in the chain.
-
#location ⇒ Object
readonly
- Location
-
the location of this node.
-
#pattern ⇒ Object
readonly
- untyped
-
the pattern to check against.
-
#statements ⇒ Object
readonly
- Statements
-
the expressions to execute if the pattern matched.
Instance Method Summary collapse
- #child_nodes ⇒ Object
- #format(q) ⇒ Object
-
#initialize(pattern:, statements:, consequent:, location:, comments: []) ⇒ In
constructor
A new instance of In.
- #pretty_print(q) ⇒ Object
- #to_json(*opts) ⇒ Object
Constructor Details
#initialize(pattern:, statements:, consequent:, location:, comments: []) ⇒ In
Returns a new instance of In.
7239 7240 7241 7242 7243 7244 7245 |
# File 'lib/syntax_tree.rb', line 7239 def initialize(pattern:, statements:, consequent:, location:, comments: []) @pattern = pattern @statements = statements @consequent = consequent @location = location @comments = comments end |
Instance Attribute Details
#comments ⇒ Object (readonly)
- Array[ Comment | EmbDoc ]
-
the comments attached to this node
7237 7238 7239 |
# File 'lib/syntax_tree.rb', line 7237 def comments @comments end |
#consequent ⇒ Object (readonly)
- nil | In | Else
-
the next clause in the chain
7231 7232 7233 |
# File 'lib/syntax_tree.rb', line 7231 def consequent @consequent end |
#location ⇒ Object (readonly)
- Location
-
the location of this node
7234 7235 7236 |
# File 'lib/syntax_tree.rb', line 7234 def location @location end |
#pattern ⇒ Object (readonly)
- untyped
-
the pattern to check against
7225 7226 7227 |
# File 'lib/syntax_tree.rb', line 7225 def pattern @pattern end |
#statements ⇒ Object (readonly)
- Statements
-
the expressions to execute if the pattern matched
7228 7229 7230 |
# File 'lib/syntax_tree.rb', line 7228 def statements @statements end |
Instance Method Details
#child_nodes ⇒ Object
7247 7248 7249 |
# File 'lib/syntax_tree.rb', line 7247 def child_nodes [pattern, statements, consequent] end |
#format(q) ⇒ Object
7251 7252 7253 7254 7255 7256 7257 7258 7259 7260 7261 7262 7263 7264 7265 7266 7267 7268 7269 7270 |
# File 'lib/syntax_tree.rb', line 7251 def format(q) keyword = "in " q.group do q.text(keyword) q.nest(keyword.length) { q.format(pattern) } unless statements.empty? q.indent do q.breakable(force: true) q.format(statements) end end if consequent q.breakable(force: true) q.format(consequent) end end end |
#pretty_print(q) ⇒ Object
7272 7273 7274 7275 7276 7277 7278 7279 7280 7281 7282 7283 7284 7285 7286 7287 7288 7289 |
# File 'lib/syntax_tree.rb', line 7272 def pretty_print(q) q.group(2, "(", ")") do q.text("in") q.breakable q.pp(pattern) q.breakable q.pp(statements) if consequent q.breakable q.pp(consequent) end q.pp(Comment::List.new(comments)) end end |
#to_json(*opts) ⇒ Object
7291 7292 7293 7294 7295 7296 7297 7298 7299 7300 |
# File 'lib/syntax_tree.rb', line 7291 def to_json(*opts) { type: :in, pattern: pattern, stmts: statements, cons: consequent, loc: location, cmts: comments }.to_json(*opts) end |