Class: SyntaxTree::HshPtn
- Inherits:
-
Object
- Object
- SyntaxTree::HshPtn
- Defined in:
- lib/syntax_tree.rb
Overview
HshPtn represents matching against a hash pattern using the Ruby 2.7+ pattern matching syntax.
case value
in { key: }
end
Defined Under Namespace
Classes: KeywordFormatter, KeywordRestFormatter
Instance Attribute Summary collapse
-
#comments ⇒ Object
readonly
- Array[ Comment | EmbDoc ]
-
the comments attached to this node.
-
#constant ⇒ Object
readonly
- nil | untyped
-
the optional constant wrapper.
-
#keyword_rest ⇒ Object
readonly
- nil | VarField
-
an optional parameter to gather up all remaining keywords.
-
#keywords ⇒ Object
readonly
- Array[ [Label, untyped
-
]] the set of tuples representing the keywords that should be matched against in the pattern.
-
#location ⇒ Object
readonly
- Location
-
the location of this node.
Instance Method Summary collapse
- #child_nodes ⇒ Object
- #format(q) ⇒ Object
-
#initialize(constant:, keywords:, keyword_rest:, location:, comments: []) ⇒ HshPtn
constructor
A new instance of HshPtn.
- #pretty_print(q) ⇒ Object
- #to_json(*opts) ⇒ Object
Constructor Details
#initialize(constant:, keywords:, keyword_rest:, location:, comments: []) ⇒ HshPtn
Returns a new instance of HshPtn.
6270 6271 6272 6273 6274 6275 6276 |
# File 'lib/syntax_tree.rb', line 6270 def initialize(constant:, keywords:, keyword_rest:, location:, comments: []) @constant = constant @keywords = keywords @keyword_rest = keyword_rest @location = location @comments = comments end |
Instance Attribute Details
#comments ⇒ Object (readonly)
- Array[ Comment | EmbDoc ]
-
the comments attached to this node
6268 6269 6270 |
# File 'lib/syntax_tree.rb', line 6268 def comments @comments end |
#constant ⇒ Object (readonly)
- nil | untyped
-
the optional constant wrapper
6255 6256 6257 |
# File 'lib/syntax_tree.rb', line 6255 def constant @constant end |
#keyword_rest ⇒ Object (readonly)
- nil | VarField
-
an optional parameter to gather up all remaining keywords
6262 6263 6264 |
# File 'lib/syntax_tree.rb', line 6262 def keyword_rest @keyword_rest end |
#keywords ⇒ Object (readonly)
- Array[ [Label, untyped
-
]] the set of tuples representing the keywords
that should be matched against in the pattern
6259 6260 6261 |
# File 'lib/syntax_tree.rb', line 6259 def keywords @keywords end |
#location ⇒ Object (readonly)
- Location
-
the location of this node
6265 6266 6267 |
# File 'lib/syntax_tree.rb', line 6265 def location @location end |
Instance Method Details
#child_nodes ⇒ Object
6278 6279 6280 |
# File 'lib/syntax_tree.rb', line 6278 def child_nodes [constant, *keywords.flatten(1), keyword_rest] end |
#format(q) ⇒ Object
6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 |
# File 'lib/syntax_tree.rb', line 6282 def format(q) parts = keywords.map { |(key, value)| KeywordFormatter.new(key, value) } parts << KeywordRestFormatter.new(keyword_rest) if keyword_rest contents = -> { q.seplist(parts) { |part| q.format(part) } } if constant q.format(constant) q.text("[") contents.call q.text("]") return end parent = q.parent if PATTERNS.any? { |pattern| parent.is_a?(pattern) } q.text("{ ") contents.call q.text(" }") else contents.call end end |
#pretty_print(q) ⇒ Object
6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 |
# File 'lib/syntax_tree.rb', line 6305 def pretty_print(q) q.group(2, "(", ")") do q.text("hshptn") if constant q.breakable q.pp(constant) end if keywords.any? q.breakable q.group(2, "(", ")") do q.seplist(keywords) do |(key, value)| q.group(2, "(", ")") do q.pp(key) if value q.breakable q.pp(value) end end end end end if keyword_rest q.breakable q.pp(keyword_rest) end q.pp(Comment::List.new(comments)) end end |
#to_json(*opts) ⇒ Object
6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 |
# File 'lib/syntax_tree.rb', line 6339 def to_json(*opts) { type: :hshptn, constant: constant, keywords: keywords, kwrest: keyword_rest, loc: location, cmts: comments }.to_json(*opts) end |