parser_node_ext 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/Gemfile.lock +2 -2
- data/README.md +13 -3
- data/lib/parser_node_ext/version.rb +1 -1
- data/lib/parser_node_ext.rb +38 -14
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9c326faea53723b13d4a991011fb260e874a0d86788308c4c4a919ede8d5680b
|
4
|
+
data.tar.gz: 1892e4c21ec1e1c8d4f2fc8c8b4cc78f502a8d20bcf4ee3cd823aa717415122e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 810a25e6c68cfc13791501a5bf1d7a3264a31a90b4af25ba4d109373bd18240fa88c577a194540ebb7835da756e0f1ad584ad19d6318165e802c68e49167b6eb
|
7
|
+
data.tar.gz: cba51e45acbf600057a22ac27fd21fe1a980fc6d7b8278312815c4b8c87dd89ab7b9b2b67af81994078f4dd4cf8f34e0755ef59357ee090cc0852a87740b1b26
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
parser_node_ext (1.
|
4
|
+
parser_node_ext (1.1.0)
|
5
5
|
parser
|
6
6
|
|
7
7
|
GEM
|
@@ -9,7 +9,7 @@ GEM
|
|
9
9
|
specs:
|
10
10
|
ast (2.4.2)
|
11
11
|
diff-lcs (1.5.0)
|
12
|
-
parser (3.2.1
|
12
|
+
parser (3.2.2.1)
|
13
13
|
ast (~> 2.4.1)
|
14
14
|
rake (13.0.6)
|
15
15
|
rspec (3.11.0)
|
data/README.md
CHANGED
@@ -1,8 +1,15 @@
|
|
1
1
|
# ParserNodeExt
|
2
2
|
|
3
|
-
|
3
|
+
It assigns names to the child nodes of the [parser](https://rubygems.org/gems/parser).
|
4
4
|
|
5
|
-
|
5
|
+
It also adds some helpers
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
# node is a hash node
|
9
|
+
node.foo_pair # get the pair node of hash foo key
|
10
|
+
node.foo_value # get the value node of the hash foo key
|
11
|
+
node.foo_source # get the source of the value node of the hash foo key
|
12
|
+
```
|
6
13
|
|
7
14
|
## Installation
|
8
15
|
|
@@ -22,7 +29,10 @@ Or install it yourself as:
|
|
22
29
|
|
23
30
|
## Usage
|
24
31
|
|
25
|
-
|
32
|
+
```ruby
|
33
|
+
require 'parser/current'
|
34
|
+
require 'parser_node_ext'
|
35
|
+
```
|
26
36
|
|
27
37
|
## Development
|
28
38
|
|
data/lib/parser_node_ext.rb
CHANGED
@@ -367,12 +367,27 @@ module ParserNodeExt
|
|
367
367
|
end
|
368
368
|
end
|
369
369
|
|
370
|
+
# Get :hash pair node according to specified key.
|
371
|
+
# @example
|
372
|
+
# node # s(:hash, s(:pair, s(:sym, :foo), s(:sym, :bar)))
|
373
|
+
# node.hash_pair(:foo) # s(:pair, s(:sym, :foo), s(:sym, :bar))
|
374
|
+
# @param [Symbol, String] key value.
|
375
|
+
# @return [Parser::AST::Node] hash pair node.
|
376
|
+
# @raise [MethodNotSupported] if calls on other node.
|
377
|
+
def hash_pair(key)
|
378
|
+
if :hash == type
|
379
|
+
children.find { |pair_node| pair_node.key.to_value == key }
|
380
|
+
else
|
381
|
+
raise MethodNotSupported, "hash_pair is not supported for #{self}"
|
382
|
+
end
|
383
|
+
end
|
384
|
+
|
370
385
|
# Get :hash value node according to specified key.
|
371
386
|
# @example
|
372
387
|
# node # s(:hash, s(:pair, s(:sym, :foo), s(:sym, :bar)))
|
373
388
|
# node.hash_value(:foo) # s(:sym, :bar)
|
374
389
|
# @param [Symbol, String] key value.
|
375
|
-
# @return [Parser::AST::Node] hash value
|
390
|
+
# @return [Parser::AST::Node] hash value node.
|
376
391
|
# @raise [MethodNotSupported] if calls on other node.
|
377
392
|
def hash_value(key)
|
378
393
|
if :hash == type
|
@@ -450,18 +465,24 @@ module ParserNodeExt
|
|
450
465
|
def method_missing(method_name, *args, &block)
|
451
466
|
if :args == type && children.respond_to?(method_name)
|
452
467
|
return children.send(method_name, *args, &block)
|
453
|
-
elsif :hash == type && method_name.to_s.
|
454
|
-
key = method_name.to_s
|
455
|
-
return
|
456
|
-
return
|
468
|
+
elsif :hash == type && method_name.to_s.end_with?('_pair')
|
469
|
+
key = method_name.to_s[0..-6]
|
470
|
+
return hash_pair(key.to_sym) if key?(key.to_sym)
|
471
|
+
return hash_pair(key.to_s) if key?(key.to_s)
|
457
472
|
|
458
473
|
return nil
|
459
|
-
elsif :hash == type && method_name.to_s.
|
460
|
-
key = method_name.to_s
|
474
|
+
elsif :hash == type && method_name.to_s.end_with?('_value')
|
475
|
+
key = method_name.to_s[0..-7]
|
476
|
+
return hash_value(key.to_sym) if key?(key.to_sym)
|
477
|
+
return hash_value(key.to_s) if key?(key.to_s)
|
478
|
+
|
479
|
+
return nil
|
480
|
+
elsif :hash == type && method_name.to_s.end_with?('_source')
|
481
|
+
key = method_name.to_s[0..-8]
|
461
482
|
return hash_value(key.to_sym)&.to_source if key?(key.to_sym)
|
462
483
|
return hash_value(key.to_s)&.to_source if key?(key.to_s)
|
463
484
|
|
464
|
-
return
|
485
|
+
return ''
|
465
486
|
end
|
466
487
|
|
467
488
|
super
|
@@ -470,12 +491,15 @@ module ParserNodeExt
|
|
470
491
|
def respond_to_missing?(method_name, *args)
|
471
492
|
if :args == type && children.respond_to?(method_name)
|
472
493
|
return true
|
473
|
-
elsif :hash == type && method_name.to_s.
|
474
|
-
key = method_name.to_s
|
475
|
-
return
|
476
|
-
elsif :hash == type && method_name.to_s.
|
477
|
-
key = method_name.to_s
|
478
|
-
return
|
494
|
+
elsif :hash == type && method_name.to_s.end_with?('_pair')
|
495
|
+
key = method_name.to_s[0..-6]
|
496
|
+
return key?(key.to_sym) || key?(key.to_s)
|
497
|
+
elsif :hash == type && method_name.to_s.end_with?('_value')
|
498
|
+
key = method_name.to_s[0..-7]
|
499
|
+
return key?(key.to_sym) || key?(key.to_s)
|
500
|
+
elsif :hash == type && method_name.to_s.end_with?('_source')
|
501
|
+
key = method_name.to_s[0..-8]
|
502
|
+
return key?(key.to_sym) || key?(key.to_s)
|
479
503
|
end
|
480
504
|
|
481
505
|
super
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: parser_node_ext
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Richard Huang
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-05-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: parser
|
@@ -63,7 +63,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
63
63
|
- !ruby/object:Gem::Version
|
64
64
|
version: '0'
|
65
65
|
requirements: []
|
66
|
-
rubygems_version: 3.4.
|
66
|
+
rubygems_version: 3.4.10
|
67
67
|
signing_key:
|
68
68
|
specification_version: 4
|
69
69
|
summary: extend parser node
|