syntax_tree_ext 0.1.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +10 -0
- data/Gemfile.lock +1 -1
- data/README.md +16 -8
- data/lib/syntax_tree_ext/version.rb +1 -1
- data/lib/syntax_tree_ext.rb +76 -9
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: b6521c0c1d5ead0d51f0dce8810f7dcb8e080f4ebceb14a1a0c82a1ea9de5a44
|
|
4
|
+
data.tar.gz: 384efd9248ed1002836b9b481d1ca727d058120f6cb082eecd9b4190cac609a5
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 9fea364be6aa7bb2303d5932b050efdae247dd8cb8f6e84d1906759eb6aef3dd0acd245e7f16c2e50e897d32a90a9fef6f79d29a03482a73c79bdfc2b1e40adb
|
|
7
|
+
data.tar.gz: 49e7e190eaa3772b5bfb29e053bb366ebb00581ed28de823a8bb2aa1500c960d455519cb12ac64d13c395f31086e840c18341e348f5c07b896fac8f329bcb550
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
# CHANGELOG
|
|
2
2
|
|
|
3
|
+
## 0.3.0 (2023-05-15)
|
|
4
|
+
|
|
5
|
+
* Support `xxx_assoc` to get assoc node of the hash node
|
|
6
|
+
|
|
7
|
+
## 0.2.0 (2023-05-14)
|
|
8
|
+
|
|
9
|
+
* Rename `Node#parent` to `Node#parent_node`
|
|
10
|
+
* Add `Node#to_value` and `Node#to_source`
|
|
11
|
+
* Add `Node#method_missing` for hash `xxx_value` and `xxx_pair`
|
|
12
|
+
|
|
3
13
|
## 0.1.0 (2023-04-20)
|
|
4
14
|
|
|
5
15
|
* Add `SyntaxTree::Node#siblings`
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
|
@@ -1,24 +1,32 @@
|
|
|
1
1
|
# SyntaxTreeExt
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
It adds `parent_node` and `source` methods to the `ParserTree::Node`.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
It also adds some helpers
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
```ruby
|
|
8
|
+
# node is a HashLiteral node
|
|
9
|
+
node.foo_assoc # get the assoc 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
|
+
```
|
|
8
13
|
|
|
9
|
-
|
|
14
|
+
## Installation
|
|
10
15
|
|
|
11
16
|
Install the gem and add to the application's Gemfile by executing:
|
|
12
17
|
|
|
13
|
-
$ bundle add
|
|
18
|
+
$ bundle add syntax_tree_ext
|
|
14
19
|
|
|
15
20
|
If bundler is not being used to manage dependencies, install the gem by executing:
|
|
16
21
|
|
|
17
|
-
$ gem install
|
|
22
|
+
$ gem install syntax_tree_ext
|
|
18
23
|
|
|
19
24
|
## Usage
|
|
20
25
|
|
|
21
|
-
|
|
26
|
+
```ruby
|
|
27
|
+
require 'syntax_tree'
|
|
28
|
+
require 'syntax_tree_ext'
|
|
29
|
+
```
|
|
22
30
|
|
|
23
31
|
## Development
|
|
24
32
|
|
|
@@ -28,4 +36,4 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
|
28
36
|
|
|
29
37
|
## Contributing
|
|
30
38
|
|
|
31
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
|
39
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/xinminlabs/syntax_tree_ext.
|
data/lib/syntax_tree_ext.rb
CHANGED
|
@@ -15,31 +15,98 @@ module SyntaxTree
|
|
|
15
15
|
|
|
16
16
|
def parse
|
|
17
17
|
node = original_parse
|
|
18
|
-
node.
|
|
18
|
+
node.set_parent_node_and_source(source)
|
|
19
19
|
node
|
|
20
20
|
end
|
|
21
21
|
end
|
|
22
22
|
|
|
23
23
|
class Node
|
|
24
|
-
attr_accessor :
|
|
24
|
+
attr_accessor :parent_node, :source
|
|
25
25
|
|
|
26
|
-
def
|
|
26
|
+
def set_parent_node_and_source(source)
|
|
27
27
|
self.source = source
|
|
28
28
|
child_nodes.each do |child_node|
|
|
29
29
|
next unless child_node.is_a?(Node)
|
|
30
30
|
|
|
31
|
-
child_node.
|
|
32
|
-
child_node.
|
|
31
|
+
child_node.parent_node = self
|
|
32
|
+
child_node.set_parent_node_and_source(source)
|
|
33
33
|
end
|
|
34
34
|
end
|
|
35
35
|
|
|
36
36
|
def siblings
|
|
37
|
-
index =
|
|
38
|
-
|
|
37
|
+
index = parent_node.child_nodes.index(self)
|
|
38
|
+
parent_node.child_nodes[index + 1...]
|
|
39
39
|
end
|
|
40
40
|
|
|
41
|
-
def
|
|
42
|
-
|
|
41
|
+
def to_value
|
|
42
|
+
case self
|
|
43
|
+
when SymbolLiteral
|
|
44
|
+
value.value.to_sym
|
|
45
|
+
when StringLiteral
|
|
46
|
+
parts.map(&:to_value).join
|
|
47
|
+
when FloatLiteral
|
|
48
|
+
value.to_f
|
|
49
|
+
when Int
|
|
50
|
+
value.to_i
|
|
51
|
+
when Kw
|
|
52
|
+
value == 'true'
|
|
53
|
+
when VarRef
|
|
54
|
+
value.to_value
|
|
55
|
+
when Label, TStringContent
|
|
56
|
+
value
|
|
57
|
+
else
|
|
58
|
+
self
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def to_source
|
|
63
|
+
source[location.start_char...location.end_char]
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# Respond key value and source for hash node
|
|
67
|
+
def method_missing(method_name, *args, &block)
|
|
68
|
+
return super unless respond_to_assocs?
|
|
69
|
+
|
|
70
|
+
if method_name.to_s.end_with?('_assoc')
|
|
71
|
+
key = method_name.to_s[0..-7]
|
|
72
|
+
return assocs.find { |assoc| assoc_key_equal?(assoc, key) }
|
|
73
|
+
elsif method_name.to_s.end_with?('_value')
|
|
74
|
+
key = method_name.to_s[0..-7]
|
|
75
|
+
return assocs.find { |assoc| assoc_key_equal?(assoc, key) }&.value
|
|
76
|
+
elsif method_name.to_s.end_with?('_source')
|
|
77
|
+
key = method_name.to_s[0..-8]
|
|
78
|
+
return assocs.find { |assoc| assoc_key_equal?(assoc, key) }&.value&.to_source || ''
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
super
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def respond_to_missing?(method_name, *args)
|
|
85
|
+
return super unless respond_to_assocs?
|
|
86
|
+
|
|
87
|
+
if method_name.to_s.end_with?('_assoc')
|
|
88
|
+
key = method_name[0..-7]
|
|
89
|
+
return !!assocs.find { |assoc| assoc_key_equal?(assoc, key) }
|
|
90
|
+
elsif method_name.to_s.end_with?('_value')
|
|
91
|
+
key = method_name[0..-7]
|
|
92
|
+
return !!assocs.find { |assoc| assoc_key_equal?(assoc, key) }
|
|
93
|
+
elsif method_name.to_s.end_with?('_source')
|
|
94
|
+
key = method_name.to_s[0..-8]
|
|
95
|
+
return !!assocs.find { |assoc| assoc_key_equal?(assoc, key) }
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
super
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
private
|
|
102
|
+
|
|
103
|
+
def respond_to_assocs?
|
|
104
|
+
is_a?(HashLiteral) || is_a?(BareAssocHash)
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
def assoc_key_equal?(assoc, key)
|
|
108
|
+
assoc_key = assoc.key.to_value.to_s
|
|
109
|
+
assoc_key.end_with?(':') ? assoc_key == "#{key}:" : assoc_key == key
|
|
43
110
|
end
|
|
44
111
|
end
|
|
45
112
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: syntax_tree_ext
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.3.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: syntax_tree
|