ast_builder 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +13 -0
- data/.rspec +3 -0
- data/.travis.yml +7 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +87 -0
- data/Guardfile +16 -0
- data/LICENSE +201 -0
- data/README.md +244 -0
- data/Rakefile +6 -0
- data/ast_builder.gemspec +33 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/ast_builder.rb +8 -0
- data/lib/ast_builder/builder.rb +275 -0
- data/lib/ast_builder/literal_token.rb +27 -0
- data/lib/ast_builder/public_api.rb +22 -0
- data/lib/ast_builder/version.rb +3 -0
- metadata +146 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 2420c793e56d111977a664e767a347b92daa09e85c220e98c59dbe847c6408c8
|
4
|
+
data.tar.gz: bdc93ccd806db6ba16cdd4e5817685b147849affb6efa70579867c810edba533
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8fa76906fc56d1ac05d2557cd0f919e82a8b7ed3c189b52cc1dc229f1e8acd8eb38d08a4acdeda2f3e987927493384aa19e24beff3eaec8737bcc21d70cd9999
|
7
|
+
data.tar.gz: 015d6dc81facfc59d5b33164bbbf01ea45895a60e502229d60d7c629456e45dd8e3f0eb2a74e8560e508c1fd25693d357d1002be24c83170d0e5830d6df5a901
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at baweaver@squareup.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
ast_builder (0.0.1)
|
5
|
+
parser (~> 2.6.0)
|
6
|
+
rubocop
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
ast (2.4.0)
|
12
|
+
coderay (1.1.2)
|
13
|
+
diff-lcs (1.3)
|
14
|
+
ffi (1.11.3)
|
15
|
+
formatador (0.2.5)
|
16
|
+
guard (2.16.1)
|
17
|
+
formatador (>= 0.2.4)
|
18
|
+
listen (>= 2.7, < 4.0)
|
19
|
+
lumberjack (>= 1.0.12, < 2.0)
|
20
|
+
nenv (~> 0.1)
|
21
|
+
notiffany (~> 0.0)
|
22
|
+
pry (>= 0.9.12)
|
23
|
+
shellany (~> 0.0)
|
24
|
+
thor (>= 0.18.1)
|
25
|
+
guard-compat (1.2.1)
|
26
|
+
guard-rspec (4.7.3)
|
27
|
+
guard (~> 2.1)
|
28
|
+
guard-compat (~> 1.1)
|
29
|
+
rspec (>= 2.99.0, < 4.0)
|
30
|
+
jaro_winkler (1.5.4)
|
31
|
+
listen (3.2.0)
|
32
|
+
rb-fsevent (~> 0.10, >= 0.10.3)
|
33
|
+
rb-inotify (~> 0.9, >= 0.9.10)
|
34
|
+
lumberjack (1.0.13)
|
35
|
+
method_source (0.9.2)
|
36
|
+
nenv (0.3.0)
|
37
|
+
notiffany (0.1.3)
|
38
|
+
nenv (~> 0.1)
|
39
|
+
shellany (~> 0.0)
|
40
|
+
parallel (1.19.1)
|
41
|
+
parser (2.6.5.0)
|
42
|
+
ast (~> 2.4.0)
|
43
|
+
pry (0.12.2)
|
44
|
+
coderay (~> 1.1.0)
|
45
|
+
method_source (~> 0.9.0)
|
46
|
+
rainbow (3.0.0)
|
47
|
+
rake (10.5.0)
|
48
|
+
rb-fsevent (0.10.3)
|
49
|
+
rb-inotify (0.10.0)
|
50
|
+
ffi (~> 1.0)
|
51
|
+
rspec (3.9.0)
|
52
|
+
rspec-core (~> 3.9.0)
|
53
|
+
rspec-expectations (~> 3.9.0)
|
54
|
+
rspec-mocks (~> 3.9.0)
|
55
|
+
rspec-core (3.9.0)
|
56
|
+
rspec-support (~> 3.9.0)
|
57
|
+
rspec-expectations (3.9.0)
|
58
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
59
|
+
rspec-support (~> 3.9.0)
|
60
|
+
rspec-mocks (3.9.0)
|
61
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
62
|
+
rspec-support (~> 3.9.0)
|
63
|
+
rspec-support (3.9.0)
|
64
|
+
rubocop (0.77.0)
|
65
|
+
jaro_winkler (~> 1.5.1)
|
66
|
+
parallel (~> 1.10)
|
67
|
+
parser (>= 2.6)
|
68
|
+
rainbow (>= 2.2.2, < 4.0)
|
69
|
+
ruby-progressbar (~> 1.7)
|
70
|
+
unicode-display_width (>= 1.4.0, < 1.7)
|
71
|
+
ruby-progressbar (1.10.1)
|
72
|
+
shellany (0.0.1)
|
73
|
+
thor (0.20.3)
|
74
|
+
unicode-display_width (1.6.0)
|
75
|
+
|
76
|
+
PLATFORMS
|
77
|
+
ruby
|
78
|
+
|
79
|
+
DEPENDENCIES
|
80
|
+
ast_builder!
|
81
|
+
bundler (~> 2.0)
|
82
|
+
guard-rspec (~> 4.0)
|
83
|
+
rake (~> 10.0)
|
84
|
+
rspec (~> 3.0)
|
85
|
+
|
86
|
+
BUNDLED WITH
|
87
|
+
2.0.2
|
data/Guardfile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
guard :rspec, cmd: "bundle exec rspec" do
|
2
|
+
require "guard/rspec/dsl"
|
3
|
+
dsl = Guard::RSpec::Dsl.new(self)
|
4
|
+
|
5
|
+
# Feel free to open issues for suggestions and improvements
|
6
|
+
|
7
|
+
# RSpec files
|
8
|
+
rspec = dsl.rspec
|
9
|
+
watch(rspec.spec_helper) { rspec.spec_dir }
|
10
|
+
watch(rspec.spec_support) { rspec.spec_dir }
|
11
|
+
watch(rspec.spec_files)
|
12
|
+
|
13
|
+
# Ruby files
|
14
|
+
ruby = dsl.ruby
|
15
|
+
dsl.watch_spec_files_for(ruby.lib_files)
|
16
|
+
end
|
data/LICENSE
ADDED
@@ -0,0 +1,201 @@
|
|
1
|
+
Apache License
|
2
|
+
Version 2.0, January 2004
|
3
|
+
http://www.apache.org/licenses/
|
4
|
+
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
6
|
+
|
7
|
+
1. Definitions.
|
8
|
+
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
11
|
+
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
13
|
+
the copyright owner that is granting the License.
|
14
|
+
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
16
|
+
other entities that control, are controlled by, or are under common
|
17
|
+
control with that entity. For the purposes of this definition,
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
19
|
+
direction or management of such entity, whether by contract or
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
22
|
+
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
24
|
+
exercising permissions granted by this License.
|
25
|
+
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
27
|
+
including but not limited to software source code, documentation
|
28
|
+
source, and configuration files.
|
29
|
+
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
31
|
+
transformation or translation of a Source form, including but
|
32
|
+
not limited to compiled object code, generated documentation,
|
33
|
+
and conversions to other media types.
|
34
|
+
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
36
|
+
Object form, made available under the License, as indicated by a
|
37
|
+
copyright notice that is included in or attached to the work
|
38
|
+
(an example is provided in the Appendix below).
|
39
|
+
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
46
|
+
the Work and Derivative Works thereof.
|
47
|
+
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
49
|
+
the original version of the Work and any modifications or additions
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
61
|
+
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
64
|
+
subsequently incorporated within the Work.
|
65
|
+
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
72
|
+
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
78
|
+
where such license applies only to those patent claims licensable
|
79
|
+
by such Contributor that are necessarily infringed by their
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
82
|
+
institute patent litigation against any entity (including a
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
85
|
+
or contributory patent infringement, then any patent licenses
|
86
|
+
granted to You under this License for that Work shall terminate
|
87
|
+
as of the date such litigation is filed.
|
88
|
+
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
91
|
+
modifications, and in Source or Object form, provided that You
|
92
|
+
meet the following conditions:
|
93
|
+
|
94
|
+
(a) You must give any other recipients of the Work or
|
95
|
+
Derivative Works a copy of this License; and
|
96
|
+
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
98
|
+
stating that You changed the files; and
|
99
|
+
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
102
|
+
attribution notices from the Source form of the Work,
|
103
|
+
excluding those notices that do not pertain to any part of
|
104
|
+
the Derivative Works; and
|
105
|
+
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
108
|
+
include a readable copy of the attribution notices contained
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
111
|
+
of the following places: within a NOTICE text file distributed
|
112
|
+
as part of the Derivative Works; within the Source form or
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
114
|
+
within a display generated by the Derivative Works, if and
|
115
|
+
wherever such third-party notices normally appear. The contents
|
116
|
+
of the NOTICE file are for informational purposes only and
|
117
|
+
do not modify the License. You may add Your own attribution
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
120
|
+
that such additional attribution notices cannot be construed
|
121
|
+
as modifying the License.
|
122
|
+
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
124
|
+
may provide additional or different license terms and conditions
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
128
|
+
the conditions stated in this License.
|
129
|
+
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
133
|
+
this License, without any additional terms or conditions.
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
135
|
+
the terms of any separate license agreement you may have executed
|
136
|
+
with Licensor regarding such Contributions.
|
137
|
+
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
140
|
+
except as required for reasonable and customary use in describing the
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
142
|
+
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
152
|
+
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
158
|
+
incidental, or consequential damages of any character arising as a
|
159
|
+
result of this License or out of the use or inability to use the
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
162
|
+
other commercial damages or losses), even if such Contributor
|
163
|
+
has been advised of the possibility of such damages.
|
164
|
+
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
168
|
+
or other liability obligations and/or rights consistent with this
|
169
|
+
License. However, in accepting such obligations, You may act only
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
174
|
+
of your accepting any such warranty or additional liability.
|
175
|
+
|
176
|
+
END OF TERMS AND CONDITIONS
|
177
|
+
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
179
|
+
|
180
|
+
To apply the Apache License to your work, attach the following
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
182
|
+
replaced with your own identifying information. (Don't include
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
184
|
+
comment syntax for the file format. We also recommend that a
|
185
|
+
file or class name and description of purpose be included on the
|
186
|
+
same "printed page" as the copyright notice for easier
|
187
|
+
identification within third-party archives.
|
188
|
+
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
190
|
+
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
192
|
+
you may not use this file except in compliance with the License.
|
193
|
+
You may obtain a copy of the License at
|
194
|
+
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
196
|
+
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
200
|
+
See the License for the specific language governing permissions and
|
201
|
+
limitations under the License.
|
data/README.md
ADDED
@@ -0,0 +1,244 @@
|
|
1
|
+
# AstBuilder
|
2
|
+
|
3
|
+
AstBuilder (AST Amended) is a tool to make it easier to work with and compose S-Expressions and other AST related tasks in Ruby.
|
4
|
+
|
5
|
+
It relies heavily on RuboCop functionality, most notably of which the [`NodePattern`][0] meta-language for AST construction and matching.
|
6
|
+
|
7
|
+
## Usage
|
8
|
+
|
9
|
+
### Build
|
10
|
+
|
11
|
+
AstBuilder supports direct strings, which is mostly done to generate ASTs more quickly from static strings.
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
AstBuilder.build('1 + 1')
|
15
|
+
```
|
16
|
+
|
17
|
+
The more typical usage for AstBuilder involves passing it a block:
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
AstBuilder.build { s(:node_type, s(:child_node, '...')) }
|
21
|
+
=> #<AstBuilder::Builder:0x00007fe2fba18390 @ast=s(:node_type, s(:child_node, "..."))>
|
22
|
+
```
|
23
|
+
|
24
|
+
AstBuilder comes with several extensions to the standard `AST::Sexp` syntax's `s` method, as we'll be going over here.
|
25
|
+
|
26
|
+
### Expanded Nodes
|
27
|
+
|
28
|
+
Ruby constant strings and code that's mostly static can be a bit cumbersome to write out:
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
puts AstBuilder.build('A::B::C = 1')
|
32
|
+
(casgn
|
33
|
+
(const
|
34
|
+
(const nil :A) :B) :C
|
35
|
+
(int 1))
|
36
|
+
=> nil
|
37
|
+
```
|
38
|
+
|
39
|
+
If you wanted to actually capture or use wildcards from [`NodePattern`][0] in that statement, it becomes more difficult:
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
puts AstBuilder.build('A::B::C = ...')
|
43
|
+
AstBuilder::InvalidCode: The following node is invalid:
|
44
|
+
'A::B::C = ...'
|
45
|
+
from /Users/baweaver/Development/ast_builder/lib/ast_builder/builder.rb:189:in 'parse'
|
46
|
+
```
|
47
|
+
|
48
|
+
AstBuilder isn't quite smart enough to be able to tell the difference between a meta-character from [`NodePattern`][0] and a regular Ruby token. This is why the builder blocks are used, but in typical construction you would need to write out the expression by hand.
|
49
|
+
|
50
|
+
With AstBuilder you can keep those larger nodes as normal Ruby:
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
puts AstBuilder.build { s(:casgn, expand('A::B'), :C, expand('1')) }
|
54
|
+
(casgn
|
55
|
+
(const
|
56
|
+
(const nil :A) :B) :C
|
57
|
+
(int 1))
|
58
|
+
=> nil
|
59
|
+
```
|
60
|
+
|
61
|
+
### Literal Tokens
|
62
|
+
|
63
|
+
If [`NodePattern`][0] tokens aren't valid Ruby, how does one evaluate them into an s-expression tree? `RuboCop::AST::Node`, when coercing to a `String`, will call `inspect` on items it doesn't know how to coerce.
|
64
|
+
|
65
|
+
`AstBuilder::LiteralToken` defines this in such a way to not have quotation marks, allowing for a psuedo-interpolation of the meta-language:
|
66
|
+
|
67
|
+
```ruby
|
68
|
+
puts AstBuilder.build { s(:casgn, expand('A::B'), :C, literal('...')) }
|
69
|
+
(casgn
|
70
|
+
(const
|
71
|
+
(const nil :A) :B) :C ...)
|
72
|
+
=> nil
|
73
|
+
```
|
74
|
+
|
75
|
+
### Captures
|
76
|
+
|
77
|
+
If you want to capture a node, you would use `$` in NodePattern. In AstBuilder we use `capture` to simulate the same:
|
78
|
+
|
79
|
+
```ruby
|
80
|
+
puts AstBuilder.build { s(:casgn, expand('A::B'), :C, capture(literal('...'))) }
|
81
|
+
(casgn
|
82
|
+
(const
|
83
|
+
(const nil :A) :B) :C $...)
|
84
|
+
=> nil
|
85
|
+
```
|
86
|
+
|
87
|
+
There's the shorter version, `capture_children`, for this same task:
|
88
|
+
|
89
|
+
```ruby
|
90
|
+
puts AstBuilder.build { s(:casgn, expand('A::B'), :C, capture_children) }
|
91
|
+
(casgn
|
92
|
+
(const
|
93
|
+
(const nil :A) :B) :C $(...))
|
94
|
+
=> nil
|
95
|
+
```
|
96
|
+
|
97
|
+
### Matching
|
98
|
+
|
99
|
+
An `AstBuilder::Builder` can be coerced into a `RuboCop::NodePattern`, which can be used in the same fashion.
|
100
|
+
|
101
|
+
> Note: RuboCop has some slight inconsistencies with how it represents `nil`, which is why when
|
102
|
+
> coercing to a RuboCop::NodePattern syntax they're replaced with `nil?`.
|
103
|
+
|
104
|
+
```ruby
|
105
|
+
AstBuilder.build { s(:casgn, expand('A::B'), :C, capture_children) }.to_cop
|
106
|
+
=> #<RuboCop::NodePattern:0x00007fe2fca004d8>
|
107
|
+
```
|
108
|
+
|
109
|
+
This means that you can use `match` just the same as you would on a `NodePattern`, but AstBuilder surfaces this functionality as we'll see in the next section.
|
110
|
+
|
111
|
+
### match
|
112
|
+
|
113
|
+
AstBuilder can directly match by coercing its internal state into a NodePattern:
|
114
|
+
|
115
|
+
```ruby
|
116
|
+
ast_builder_build = AstBuilder.build { s(:casgn, expand('A::B'), :C, capture_children) }
|
117
|
+
|
118
|
+
# Using AstBuilder to build a quick AST mock:
|
119
|
+
match_data = ast_builder_build.match(AstBuilder.build('A::B::C = 1').to_ast)
|
120
|
+
=> s(:int, 1)
|
121
|
+
|
122
|
+
# Against a string:
|
123
|
+
ast_builder_build.match('A::B::C = 1')
|
124
|
+
=> s(:int, 1)
|
125
|
+
```
|
126
|
+
|
127
|
+
This can also be used against nodes in a RuboCop rule match:
|
128
|
+
|
129
|
+
```ruby
|
130
|
+
module RuboCop
|
131
|
+
module Cop
|
132
|
+
# Grouping name of the Cop
|
133
|
+
module Deprecations
|
134
|
+
# Name of the check
|
135
|
+
class AbcDeprecation < RuboCop::Cop::Cop
|
136
|
+
|
137
|
+
# RuboCop takes a default message for errors
|
138
|
+
MSG = '`A::B::C` is deprecated, use `D::E::F` instead.'
|
139
|
+
|
140
|
+
# Saving the matcher as a constant allows for easier reuse if you use
|
141
|
+
# autocorrect later, as well as giving a consistent theme across your
|
142
|
+
# matchers.
|
143
|
+
AST_MATCH = AstBuilder.build { s(:casgn, expand('A::B'), :C, capture_children) }
|
144
|
+
|
145
|
+
# [...]
|
146
|
+
|
147
|
+
# Node matches work with the node type you're planning to capture. In this
|
148
|
+
# case we're trying to capture a `casgn` from the top type of the expression:
|
149
|
+
#
|
150
|
+
# (casgn
|
151
|
+
# (const
|
152
|
+
# (const nil :A) :B) :C
|
153
|
+
# (int 1))
|
154
|
+
#
|
155
|
+
# RuboCop matchers are all on_(type of node) for method names.
|
156
|
+
def on_casgn(node)
|
157
|
+
# Unless our node matches that expression, bail out.
|
158
|
+
return false unless AST_MATCH.match(node)
|
159
|
+
|
160
|
+
# If it did, add an offense so RuboCop knows it's bad.
|
161
|
+
add_offense(node)
|
162
|
+
end
|
163
|
+
|
164
|
+
def autocorrect(node)
|
165
|
+
# RuboCop passes this function to a batch that runs all the given
|
166
|
+
# correctors for the code, hence returning a lambda here.
|
167
|
+
-> corrector {
|
168
|
+
matches = AST_MATCH.match(node)
|
169
|
+
|
170
|
+
# 1. Matches are wrapped in an Enumerator, as there can be multiple
|
171
|
+
# 2. Then the value you want may be in an S-Expression
|
172
|
+
# 3. An S-Expression can have multiple children, so it's returned as an Array
|
173
|
+
# 4. Getting the first value specifically gives us 1, the set value
|
174
|
+
#
|
175
|
+
# [s()] -> s() -> [1] -> 1
|
176
|
+
value = matches.first.children.first
|
177
|
+
|
178
|
+
# Now we could always use the actual s-expression from step 2 here
|
179
|
+
# with `matches.first.location.source`, which is handy when we're
|
180
|
+
# not dealing with only one integer.
|
181
|
+
new_code = "D::E::F = #{value}"
|
182
|
+
|
183
|
+
# You can use a few things here, like `insert_before` or after or
|
184
|
+
# other expressions. See the corrector source for more ideas:
|
185
|
+
#
|
186
|
+
# https://github.com/rubocop-hq/rubocop/blob/master/lib/rubocop/cop/corrector.rb
|
187
|
+
#
|
188
|
+
# In this case we're replacing the entire node where it's expression is,
|
189
|
+
# or rather the entire thing, with our new code.
|
190
|
+
corrector.replace(node.location.expression, new_code)
|
191
|
+
}
|
192
|
+
end
|
193
|
+
end
|
194
|
+
end
|
195
|
+
end
|
196
|
+
```
|
197
|
+
|
198
|
+
## Installation
|
199
|
+
|
200
|
+
Add this line to your application's Gemfile:
|
201
|
+
|
202
|
+
```ruby
|
203
|
+
gem 'ast_builder'
|
204
|
+
```
|
205
|
+
|
206
|
+
And then execute:
|
207
|
+
|
208
|
+
$ bundle
|
209
|
+
|
210
|
+
Or install it yourself as:
|
211
|
+
|
212
|
+
$ gem install ast_builder
|
213
|
+
|
214
|
+
## Development
|
215
|
+
|
216
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
217
|
+
|
218
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
219
|
+
|
220
|
+
## Contributing
|
221
|
+
|
222
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/square/ast_builder. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
223
|
+
|
224
|
+
## Code of Conduct
|
225
|
+
|
226
|
+
Everyone interacting in the AstBuilder project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/square/ast_builder/blob/master/CODE_OF_CONDUCT.md).
|
227
|
+
|
228
|
+
[0]: https://www.rubydoc.info/gems/rubocop/RuboCop/NodePattern "RuboCop NodePattern"
|
229
|
+
|
230
|
+
## License
|
231
|
+
|
232
|
+
Copyright 2019 Square, Inc.
|
233
|
+
|
234
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
235
|
+
you may not use this file except in compliance with the License.
|
236
|
+
You may obtain a copy of the License at
|
237
|
+
|
238
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
239
|
+
|
240
|
+
Unless required by applicable law or agreed to in writing, software
|
241
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
242
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
243
|
+
See the License for the specific language governing permissions and
|
244
|
+
limitations under the License.
|
data/Rakefile
ADDED
data/ast_builder.gemspec
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "ast_builder/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "ast_builder"
|
8
|
+
spec.version = AstBuilder::VERSION
|
9
|
+
spec.authors = ["Brandon Weaver"]
|
10
|
+
spec.email = ["baweaver@squareup.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{AstBuilder is an AST tool that makes it easier to build (and eventually manipulate) nodes}
|
13
|
+
spec.homepage = "https://www.github.com/baweaver/ast_builder"
|
14
|
+
|
15
|
+
spec.license = "Apache-2.0"
|
16
|
+
|
17
|
+
# Specify which files should be added to the gem when it is released.
|
18
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
19
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
20
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
21
|
+
end
|
22
|
+
spec.bindir = "exe"
|
23
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
24
|
+
spec.require_paths = ["lib"]
|
25
|
+
|
26
|
+
spec.add_development_dependency "bundler", "~> 2.0"
|
27
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
28
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
29
|
+
spec.add_development_dependency "guard-rspec", "~> 4.0"
|
30
|
+
|
31
|
+
spec.add_runtime_dependency "parser", '~> 2.6.0'
|
32
|
+
spec.add_runtime_dependency "rubocop"
|
33
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "ast_builder"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/lib/ast_builder.rb
ADDED
@@ -0,0 +1,275 @@
|
|
1
|
+
require 'rubocop'
|
2
|
+
|
3
|
+
module AstBuilder
|
4
|
+
class InvalidCode < StandardError; end
|
5
|
+
|
6
|
+
class Builder
|
7
|
+
extend RuboCop::NodePattern::Macros
|
8
|
+
|
9
|
+
attr_reader :meta_methods
|
10
|
+
|
11
|
+
# NodePattern won't allow numbers in meta-method calls, so
|
12
|
+
# we need to have alpha characters instead.
|
13
|
+
ALPHA = ('a'..'zz').to_a
|
14
|
+
|
15
|
+
# It can either work on a literal string or on a block
|
16
|
+
#
|
17
|
+
# @param s = nil [String]
|
18
|
+
# String to convert
|
19
|
+
#
|
20
|
+
# @param &fn [Proc]
|
21
|
+
# `instance_eval`'d function to allow for some nice Sexp-like
|
22
|
+
# tokens to be used in construction
|
23
|
+
#
|
24
|
+
# @return [AstBuilder]
|
25
|
+
def initialize(s = nil, &fn)
|
26
|
+
@meta_methods = {}
|
27
|
+
@ast = s ? parse(s) : instance_eval(&fn)
|
28
|
+
end
|
29
|
+
|
30
|
+
# Stand-in for the s-expression given from `AST::Sexp`
|
31
|
+
# to give us some of the `Node` level features that
|
32
|
+
# RuboCop's variant has.
|
33
|
+
#
|
34
|
+
# @param type [String, Symbol]
|
35
|
+
# Type of the node
|
36
|
+
#
|
37
|
+
# @param *children [Array[Node, respond_to?(:inspect)]]
|
38
|
+
# RuboCop compatible nodes, or meta-tokens defining
|
39
|
+
# `inspect` to allow for `NodePattern` interpolation
|
40
|
+
#
|
41
|
+
# @return [RuboCop::AST::Node]
|
42
|
+
def s_expression(type, *children)
|
43
|
+
RuboCop::AST::Node.new(type, children)
|
44
|
+
end
|
45
|
+
|
46
|
+
alias s s_expression
|
47
|
+
|
48
|
+
# A literal token. Think any of the node matchers from Rubocop's NodePattern:
|
49
|
+
#
|
50
|
+
# https://rubocop.readthedocs.io/en/latest/node_pattern/
|
51
|
+
#
|
52
|
+
# @param string [String]
|
53
|
+
# String to use as a literal token
|
54
|
+
#
|
55
|
+
# @return [LiteralToken]
|
56
|
+
def literal(string)
|
57
|
+
LiteralToken.new(string)
|
58
|
+
end
|
59
|
+
|
60
|
+
alias l literal
|
61
|
+
|
62
|
+
# Expands a token by parsing it instead of manually having to nest
|
63
|
+
# the thing 3-4 layers deep for constants and the like
|
64
|
+
#
|
65
|
+
# @param string [String]
|
66
|
+
# String to expand into AST Nodes
|
67
|
+
#
|
68
|
+
# @return [AST::Node]
|
69
|
+
def expand(*strings)
|
70
|
+
strings
|
71
|
+
.map { |s| s.is_a?(String) ? parse(s) : s }
|
72
|
+
.yield_self { |node, *children| node.concat(children) }
|
73
|
+
end
|
74
|
+
|
75
|
+
alias e expand
|
76
|
+
|
77
|
+
# Prepends a `$` to represent a captured node for matchers.
|
78
|
+
#
|
79
|
+
# @param string [String]
|
80
|
+
# String or AST (yeah yeah, names) to "capture"
|
81
|
+
#
|
82
|
+
# @return [LiteralToken]
|
83
|
+
def capture(string)
|
84
|
+
literal("$#{string}")
|
85
|
+
end
|
86
|
+
|
87
|
+
alias c capture
|
88
|
+
|
89
|
+
# Captures the children of a node. Convenience function
|
90
|
+
# combining a capture and a literal.
|
91
|
+
#
|
92
|
+
# @return [String]
|
93
|
+
def capture_children
|
94
|
+
capture literal '(...)'
|
95
|
+
end
|
96
|
+
|
97
|
+
# Top level method send for sexps that avoids having to type out
|
98
|
+
# the entire `(send nil? name (...))` bit.
|
99
|
+
#
|
100
|
+
# @param name [String]
|
101
|
+
# Name of the top level keyword
|
102
|
+
#
|
103
|
+
# @param *sexp [Array[String, AST::Node, LiteralToken]]
|
104
|
+
# Anything that looks vaguely like a Sexp
|
105
|
+
#
|
106
|
+
# @return [AST::Node]
|
107
|
+
def top_method_send(name, *sexp)
|
108
|
+
s(:send, nil, name, *sexp)
|
109
|
+
end
|
110
|
+
|
111
|
+
alias t top_method_send
|
112
|
+
|
113
|
+
# Regular method send for any level, normally used for things like
|
114
|
+
# constants and otherwise.
|
115
|
+
#
|
116
|
+
# @param name [String]
|
117
|
+
# Name of the method
|
118
|
+
#
|
119
|
+
# @param *sexp [[Array[String, AST::Node, LiteralToken]]
|
120
|
+
# Anything that looks vaguely like a Sexp
|
121
|
+
#
|
122
|
+
# @return [AST::Node]
|
123
|
+
def method_send(name, *sexp)
|
124
|
+
s(:send, name, *sexp)
|
125
|
+
end
|
126
|
+
|
127
|
+
alias m method_send
|
128
|
+
|
129
|
+
# Wraps a variable assignment for shorthand usage. It will try and tell
|
130
|
+
# the difference between instance
|
131
|
+
#
|
132
|
+
# @param variable [Symbol]
|
133
|
+
# Name of the variable
|
134
|
+
#
|
135
|
+
# @param value [Any]
|
136
|
+
# Value of the variable. Could be a NodePattern literal
|
137
|
+
#
|
138
|
+
# @return [AST::Node]
|
139
|
+
def assigns(variable, value)
|
140
|
+
# Constant assignment if we got a node
|
141
|
+
return s(:casgn, nil, variable, value) unless variable.respond_to?(:to_sym)
|
142
|
+
|
143
|
+
variable_name = variable.to_sym
|
144
|
+
|
145
|
+
case variable.to_s
|
146
|
+
when /^@@/
|
147
|
+
s(:cvasgn, variable_name, value)
|
148
|
+
when /^@/
|
149
|
+
s(:ivasgn, variable_name, value)
|
150
|
+
when /^\$/
|
151
|
+
s(:gvasgn, variable_name, value)
|
152
|
+
when /^[[:upper:]]/
|
153
|
+
s(:casgn, nil, variable_name, value)
|
154
|
+
else
|
155
|
+
s(:lvasgn, variable_name, value)
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
# Checks to see if a given value matches a meta-method.
|
160
|
+
#
|
161
|
+
# In a normal NodePattern, this is a method which exists in the parent context or on the NodePattern itself. As
|
162
|
+
# these methods are rarely used outside of this context, they can be defined instead as anonymous functions
|
163
|
+
# using the additional flexibility of AstBuilder's builder syntax:
|
164
|
+
#
|
165
|
+
# ```ruby
|
166
|
+
# assigns(:value, s(:str, matching(/abc/)))
|
167
|
+
# ```
|
168
|
+
#
|
169
|
+
# Now instead of having to specify these checks in an actual handler, or defining a method on the parent
|
170
|
+
# context, we can do so inline.
|
171
|
+
#
|
172
|
+
# These meta methods are then stored and defined on the generated NodePattern upon match time to ensure they're
|
173
|
+
# within scope.
|
174
|
+
#
|
175
|
+
# @param value = nil [#===]
|
176
|
+
# Any value that responds to `===`, used to build off of the flexibility of the Ruby `case`
|
177
|
+
# expression.
|
178
|
+
#
|
179
|
+
# @param &function [Proc]
|
180
|
+
# A function used to match against. Note that this function _must_ have the proper arity or NodePattern will
|
181
|
+
# reject it.
|
182
|
+
#
|
183
|
+
# @return [LiteralToken]
|
184
|
+
# This returns a literal token instead of a string, as NodePattern expects it to be a bare word.
|
185
|
+
def matching(value = nil, &function)
|
186
|
+
called_function = function ? function : -> x { value === x }
|
187
|
+
|
188
|
+
# NodePattern will not accept numbers, so we have to use letters instead.
|
189
|
+
meta_name = "_meta_method_#{ALPHA[@meta_methods.size]}"
|
190
|
+
|
191
|
+
@meta_methods[meta_name] = called_function
|
192
|
+
|
193
|
+
# These macros start with the `#` symbol, making this intentional
|
194
|
+
literal("##{meta_name}")
|
195
|
+
end
|
196
|
+
|
197
|
+
alias mm matching
|
198
|
+
|
199
|
+
# This method will both use anonymous functions or values to match against and then capture the output.
|
200
|
+
#
|
201
|
+
# @see #matching
|
202
|
+
#
|
203
|
+
# @param value = nil [#===]
|
204
|
+
# Any value that responds to `===`, used to build off of the flexibility of the Ruby `case`
|
205
|
+
# expression.
|
206
|
+
#
|
207
|
+
# @param &function [Proc]
|
208
|
+
# A function used to match against. Note that this function _must_ have the proper arity or NodePattern will
|
209
|
+
# reject it.
|
210
|
+
#
|
211
|
+
# @return [LiteralToken]
|
212
|
+
def capture_matching(value = nil, &function)
|
213
|
+
capture(matching(value, &function))
|
214
|
+
end
|
215
|
+
|
216
|
+
alias cm capture_matching
|
217
|
+
|
218
|
+
# Coerces the builder into a RuboCop NodePattern and attempts to match another value against it.
|
219
|
+
#
|
220
|
+
# @param other [String, AST]
|
221
|
+
# Either plaintext code or another AST to match against
|
222
|
+
#
|
223
|
+
# @return [nil]
|
224
|
+
# There was no match
|
225
|
+
#
|
226
|
+
# @return [String]
|
227
|
+
# The matched portion of the code
|
228
|
+
def match(other)
|
229
|
+
ast = other.is_a?(String) ? self.class.new(other).to_ast : other
|
230
|
+
self.to_cop.match(ast)
|
231
|
+
end
|
232
|
+
|
233
|
+
# Because RuboCop has... interesting ...formatting rules we have
|
234
|
+
# to hack around nil a bit and add a question mark.
|
235
|
+
#
|
236
|
+
# @return [RuboCop::NodePattern]
|
237
|
+
# RuboCop compatible Sexp
|
238
|
+
def to_cop
|
239
|
+
RuboCop::NodePattern.new(self.to_s.gsub(/\bnil\b/, 'nil?')).tap do |node_pattern|
|
240
|
+
# If there are any meta methods defined we bind them to the node pattern to match against
|
241
|
+
@meta_methods.each do |name, fn|
|
242
|
+
node_pattern.define_singleton_method(name, &fn)
|
243
|
+
end
|
244
|
+
end
|
245
|
+
end
|
246
|
+
|
247
|
+
# Returns the internal AST representation as-is
|
248
|
+
#
|
249
|
+
# @return [AST::Node]
|
250
|
+
def to_ast
|
251
|
+
@ast
|
252
|
+
end
|
253
|
+
|
254
|
+
# String version of the AST
|
255
|
+
#
|
256
|
+
# @return [String]
|
257
|
+
def to_s
|
258
|
+
@ast.to_s
|
259
|
+
end
|
260
|
+
|
261
|
+
# Parses a String to a Ruby AST
|
262
|
+
#
|
263
|
+
# @param string [String]
|
264
|
+
# String to convert
|
265
|
+
#
|
266
|
+
# @return [AST::Node]
|
267
|
+
private def parse(string)
|
268
|
+
ast_results = RuboCop::ProcessedSource.new(string, RUBY_VERSION.to_f).ast
|
269
|
+
|
270
|
+
raise InvalidCode, "The following node is invalid: \n '#{string}'" unless ast_results
|
271
|
+
|
272
|
+
ast_results
|
273
|
+
end
|
274
|
+
end
|
275
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module AstBuilder
|
2
|
+
# Represents a literal token entity to avoid any quotation marks on inspect.
|
3
|
+
# Parser::AST will recursively call nodes, using `inspect` for anything
|
4
|
+
# that's not a `Node` type, so we can cheat to get the macro language in
|
5
|
+
# here.
|
6
|
+
class LiteralToken
|
7
|
+
def initialize(string)
|
8
|
+
@string = string
|
9
|
+
end
|
10
|
+
|
11
|
+
# Converts to a string. If we happen to have gotten some
|
12
|
+
# extra fun we make sure it's a string representation instead
|
13
|
+
# of a node.
|
14
|
+
#
|
15
|
+
# @return [String]
|
16
|
+
def to_s
|
17
|
+
@string.to_s
|
18
|
+
end
|
19
|
+
|
20
|
+
# Won't show quotes around it, which we need for literal tokens
|
21
|
+
#
|
22
|
+
# @return [String]
|
23
|
+
def inspect
|
24
|
+
to_s
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'ast_builder/literal_token'
|
2
|
+
require 'ast_builder/builder'
|
3
|
+
|
4
|
+
module AstBuilder
|
5
|
+
module PublicApi
|
6
|
+
# Builds an AST Node from AstBuilder shorthand syntax
|
7
|
+
#
|
8
|
+
# @see AstBuilder::Builder
|
9
|
+
# For more notes on usage
|
10
|
+
#
|
11
|
+
# @param string = nil [String]
|
12
|
+
# Literal String to build
|
13
|
+
#
|
14
|
+
# @param &fn [Proc]
|
15
|
+
# `instance_eval`'d function used to build an s-expression
|
16
|
+
#
|
17
|
+
# @return [AstBuilder::Builder]
|
18
|
+
def build(string = nil, &fn)
|
19
|
+
Builder.new(string, &fn)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,146 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ast_builder
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Brandon Weaver
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-12-04 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: guard-rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '4.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '4.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: parser
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 2.6.0
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 2.6.0
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rubocop
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
description:
|
98
|
+
email:
|
99
|
+
- baweaver@squareup.com
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- ".gitignore"
|
105
|
+
- ".rspec"
|
106
|
+
- ".travis.yml"
|
107
|
+
- CODE_OF_CONDUCT.md
|
108
|
+
- Gemfile
|
109
|
+
- Gemfile.lock
|
110
|
+
- Guardfile
|
111
|
+
- LICENSE
|
112
|
+
- README.md
|
113
|
+
- Rakefile
|
114
|
+
- ast_builder.gemspec
|
115
|
+
- bin/console
|
116
|
+
- bin/setup
|
117
|
+
- lib/ast_builder.rb
|
118
|
+
- lib/ast_builder/builder.rb
|
119
|
+
- lib/ast_builder/literal_token.rb
|
120
|
+
- lib/ast_builder/public_api.rb
|
121
|
+
- lib/ast_builder/version.rb
|
122
|
+
homepage: https://www.github.com/baweaver/ast_builder
|
123
|
+
licenses:
|
124
|
+
- Apache-2.0
|
125
|
+
metadata: {}
|
126
|
+
post_install_message:
|
127
|
+
rdoc_options: []
|
128
|
+
require_paths:
|
129
|
+
- lib
|
130
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
131
|
+
requirements:
|
132
|
+
- - ">="
|
133
|
+
- !ruby/object:Gem::Version
|
134
|
+
version: '0'
|
135
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
136
|
+
requirements:
|
137
|
+
- - ">="
|
138
|
+
- !ruby/object:Gem::Version
|
139
|
+
version: '0'
|
140
|
+
requirements: []
|
141
|
+
rubygems_version: 3.0.3
|
142
|
+
signing_key:
|
143
|
+
specification_version: 4
|
144
|
+
summary: AstBuilder is an AST tool that makes it easier to build (and eventually manipulate)
|
145
|
+
nodes
|
146
|
+
test_files: []
|