syntax_tree_ext 0.1.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +14 -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 +78 -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: c1db7c0430ae86be8cd79200e3764505f54175fc38359137fb1e2f74c337acef
|
4
|
+
data.tar.gz: e5a2827ce03b9fbb54b61b3cc0a3e00116fc66fc870d19a64d66d2c77ba0e791
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 47ca6fe36cdef18f2b59189bb29fea107973ba8f41735b1c450de6d0ee307ce702d021f244a3509fa92e0659b4c4bc2b4a0c111d3aae969ca4173e8b85059d61
|
7
|
+
data.tar.gz: f28d7ce55a092aa5bffb3ae5747cea2d99859ecd25662e8f1366ddae6ff578677bd2b4384222ed9b062b1485203ca6020fb98503278c8616c1d9eb0fc5387ae4
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,19 @@
|
|
1
1
|
# CHANGELOG
|
2
2
|
|
3
|
+
## 0.3.1 (2023-05-18)
|
4
|
+
|
5
|
+
* Support `array` in `Node#to_value`
|
6
|
+
|
7
|
+
## 0.3.0 (2023-05-15)
|
8
|
+
|
9
|
+
* Support `xxx_assoc` to get assoc node of the hash node
|
10
|
+
|
11
|
+
## 0.2.0 (2023-05-14)
|
12
|
+
|
13
|
+
* Rename `Node#parent` to `Node#parent_node`
|
14
|
+
* Add `Node#to_value` and `Node#to_source`
|
15
|
+
* Add `Node#method_missing` for hash `xxx_value` and `xxx_pair`
|
16
|
+
|
3
17
|
## 0.1.0 (2023-04-20)
|
4
18
|
|
5
19
|
* 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,100 @@ 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, Ident
|
56
|
+
value
|
57
|
+
when ArrayLiteral
|
58
|
+
contents.parts.map { |part| part.to_value }
|
59
|
+
else
|
60
|
+
self
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def to_source
|
65
|
+
source[location.start_char...location.end_char]
|
66
|
+
end
|
67
|
+
|
68
|
+
# Respond key value and source for hash node
|
69
|
+
def method_missing(method_name, *args, &block)
|
70
|
+
return super unless respond_to_assocs?
|
71
|
+
|
72
|
+
if method_name.to_s.end_with?('_assoc')
|
73
|
+
key = method_name.to_s[0..-7]
|
74
|
+
return assocs.find { |assoc| assoc_key_equal?(assoc, key) }
|
75
|
+
elsif method_name.to_s.end_with?('_value')
|
76
|
+
key = method_name.to_s[0..-7]
|
77
|
+
return assocs.find { |assoc| assoc_key_equal?(assoc, key) }&.value
|
78
|
+
elsif method_name.to_s.end_with?('_source')
|
79
|
+
key = method_name.to_s[0..-8]
|
80
|
+
return assocs.find { |assoc| assoc_key_equal?(assoc, key) }&.value&.to_source || ''
|
81
|
+
end
|
82
|
+
|
83
|
+
super
|
84
|
+
end
|
85
|
+
|
86
|
+
def respond_to_missing?(method_name, *args)
|
87
|
+
return super unless respond_to_assocs?
|
88
|
+
|
89
|
+
if method_name.to_s.end_with?('_assoc')
|
90
|
+
key = method_name[0..-7]
|
91
|
+
return !!assocs.find { |assoc| assoc_key_equal?(assoc, key) }
|
92
|
+
elsif method_name.to_s.end_with?('_value')
|
93
|
+
key = method_name[0..-7]
|
94
|
+
return !!assocs.find { |assoc| assoc_key_equal?(assoc, key) }
|
95
|
+
elsif method_name.to_s.end_with?('_source')
|
96
|
+
key = method_name.to_s[0..-8]
|
97
|
+
return !!assocs.find { |assoc| assoc_key_equal?(assoc, key) }
|
98
|
+
end
|
99
|
+
|
100
|
+
super
|
101
|
+
end
|
102
|
+
|
103
|
+
private
|
104
|
+
|
105
|
+
def respond_to_assocs?
|
106
|
+
is_a?(HashLiteral) || is_a?(BareAssocHash)
|
107
|
+
end
|
108
|
+
|
109
|
+
def assoc_key_equal?(assoc, key)
|
110
|
+
assoc_key = assoc.key.to_value.to_s
|
111
|
+
assoc_key.end_with?(':') ? assoc_key == "#{key}:" : assoc_key == key
|
43
112
|
end
|
44
113
|
end
|
45
114
|
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.1
|
4
|
+
version: 0.3.1
|
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-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: syntax_tree
|