starlark_compiler 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.rubocop.yml +5 -0
- data/.travis.yml +7 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +9 -0
- data/Gemfile.lock +51 -0
- data/LICENSE.txt +21 -0
- data/README.md +72 -0
- data/Rakefile +10 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/lib/starlark_compiler.rb +10 -0
- data/lib/starlark_compiler/ast.rb +133 -0
- data/lib/starlark_compiler/version.rb +5 -0
- data/lib/starlark_compiler/writer.rb +185 -0
- data/starlark_compiler.gemspec +31 -0
- metadata +76 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: c67a3032b052e3a24e9d962651efb0a13b983ca082a6df86d620378164dedc6e
|
4
|
+
data.tar.gz: bcfaae939420a5976f25ac744d481374e0eb3000c0e909be07111465576c6d1b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4611b9f4ae5d7840dd40c387bd8b49e3caa83474bfee0fef3225cb6ec2591adcca4d5af7ab24e3989e0b773c85f6f12024ea9b9fae25830da9a1e3cb04c22164
|
7
|
+
data.tar.gz: 5569c58921d1e4fbc4ef4c9dd2a38547a6660d52700a3b26f6ef0f4450cfa9f5b34bc249948e5355a3a6089d680bfc4776a89f39e15c30d44e542c69f36210a6
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
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 segiddins@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,51 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
starlark_compiler (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
ast (2.4.0)
|
10
|
+
diff-lcs (1.3)
|
11
|
+
jaro_winkler (1.5.4)
|
12
|
+
parallel (1.19.1)
|
13
|
+
parser (2.7.0.1)
|
14
|
+
ast (~> 2.4.0)
|
15
|
+
rainbow (3.0.0)
|
16
|
+
rake (12.3.3)
|
17
|
+
rspec (3.9.0)
|
18
|
+
rspec-core (~> 3.9.0)
|
19
|
+
rspec-expectations (~> 3.9.0)
|
20
|
+
rspec-mocks (~> 3.9.0)
|
21
|
+
rspec-core (3.9.0)
|
22
|
+
rspec-support (~> 3.9.0)
|
23
|
+
rspec-expectations (3.9.0)
|
24
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
25
|
+
rspec-support (~> 3.9.0)
|
26
|
+
rspec-mocks (3.9.0)
|
27
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
28
|
+
rspec-support (~> 3.9.0)
|
29
|
+
rspec-support (3.9.0)
|
30
|
+
rubocop (0.79.0)
|
31
|
+
jaro_winkler (~> 1.5.1)
|
32
|
+
parallel (~> 1.10)
|
33
|
+
parser (>= 2.7.0.1)
|
34
|
+
rainbow (>= 2.2.2, < 4.0)
|
35
|
+
ruby-progressbar (~> 1.7)
|
36
|
+
unicode-display_width (>= 1.4.0, < 1.7)
|
37
|
+
ruby-progressbar (1.10.1)
|
38
|
+
unicode-display_width (1.6.0)
|
39
|
+
|
40
|
+
PLATFORMS
|
41
|
+
ruby
|
42
|
+
|
43
|
+
DEPENDENCIES
|
44
|
+
bundler (~> 2.0)
|
45
|
+
rake (~> 12.0)
|
46
|
+
rspec (~> 3.9)
|
47
|
+
rubocop (~> 0.79.0)
|
48
|
+
starlark_compiler!
|
49
|
+
|
50
|
+
BUNDLED WITH
|
51
|
+
2.1.2
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 Samuel Giddins
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
# Starlark Compiler
|
2
|
+
|
3
|
+
This is a gem for creating Starlark ASTs and serializing them into Starlark code.
|
4
|
+
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'starlark_compiler'
|
12
|
+
```
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install starlark_compiler
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
starlark_string = StarlarkCompiler::AST.build do
|
26
|
+
ast = StarlarkCompiler::AST.new(toplevel: [])
|
27
|
+
ast << function_call(
|
28
|
+
'load',
|
29
|
+
string('@bazel_build_rules_apple//rules:ios.bzl'),
|
30
|
+
string('ios_application'),
|
31
|
+
_ios_application: string('ios_application')
|
32
|
+
)
|
33
|
+
ast << function_call(
|
34
|
+
'load',
|
35
|
+
string('@bazel_build_rules_apple//rules:ios.bzl'),
|
36
|
+
string('ios_application')
|
37
|
+
)
|
38
|
+
ast << function_call(
|
39
|
+
'ios_library',
|
40
|
+
name: string('App_Objc'),
|
41
|
+
srcs: function_call('glob', array([string('Sources/**/*.swift')])) +
|
42
|
+
['A.swift']
|
43
|
+
)
|
44
|
+
ast << function_call(
|
45
|
+
'ios_application',
|
46
|
+
name: string('App'),
|
47
|
+
deps: array([
|
48
|
+
string(':App_Objc')
|
49
|
+
]),
|
50
|
+
entitlements: array([string(':App.entitlements')])
|
51
|
+
)
|
52
|
+
StarlarkCompiler::Writer.write(ast: ast, io: +'')
|
53
|
+
end
|
54
|
+
```
|
55
|
+
|
56
|
+
## Development
|
57
|
+
|
58
|
+
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.
|
59
|
+
|
60
|
+
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).
|
61
|
+
|
62
|
+
## Contributing
|
63
|
+
|
64
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/segiddins/starlark_compiler. 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.
|
65
|
+
|
66
|
+
## License
|
67
|
+
|
68
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
69
|
+
|
70
|
+
## Code of Conduct
|
71
|
+
|
72
|
+
Everyone interacting in the StarlarkCompiler project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/segiddins/starlark_compiler/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require 'bundler/setup'
|
5
|
+
require 'starlark_compiler'
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require 'irb'
|
15
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,133 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module StarlarkCompiler
|
4
|
+
class AST
|
5
|
+
attr_reader :toplevel
|
6
|
+
|
7
|
+
def initialize(toplevel:)
|
8
|
+
@toplevel = toplevel
|
9
|
+
end
|
10
|
+
|
11
|
+
def <<(node)
|
12
|
+
@toplevel << node
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.build(&blk)
|
16
|
+
Class.new do
|
17
|
+
def const_name_for(str)
|
18
|
+
str = str.to_s
|
19
|
+
return str if str !~ /_/ && str =~ /[A-Z]+.*/
|
20
|
+
|
21
|
+
str.split('_').map(&:capitalize).join.to_sym
|
22
|
+
end
|
23
|
+
|
24
|
+
def respond_to_missing?(name)
|
25
|
+
AST.constants.include?(const_name_for(name))
|
26
|
+
end
|
27
|
+
|
28
|
+
def method_missing(name, *args, **kwargs)
|
29
|
+
const_name = const_name_for(name)
|
30
|
+
begin
|
31
|
+
v = AST.const_get(const_name)
|
32
|
+
rescue NameError
|
33
|
+
super
|
34
|
+
else
|
35
|
+
if kwargs.empty?
|
36
|
+
v.new(*args)
|
37
|
+
else
|
38
|
+
v.new(*args, **kwargs)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end.new.instance_exec(&blk)
|
43
|
+
end
|
44
|
+
|
45
|
+
class Node
|
46
|
+
%i[- + / * % == < <= >= >].each do |op|
|
47
|
+
define_method(op) { |rhs| BinaryOperator.new(self, rhs, operator: op) }
|
48
|
+
end
|
49
|
+
|
50
|
+
# TODO: ==, eql?, hash
|
51
|
+
|
52
|
+
private
|
53
|
+
|
54
|
+
def node(obj)
|
55
|
+
case obj
|
56
|
+
when Node
|
57
|
+
obj
|
58
|
+
when ::String
|
59
|
+
String.new(obj)
|
60
|
+
when ::Array
|
61
|
+
Array.new(obj)
|
62
|
+
when ::Hash
|
63
|
+
Dictionary.new(obj)
|
64
|
+
when NilClass
|
65
|
+
None.new
|
66
|
+
when TrueClass
|
67
|
+
True.new
|
68
|
+
when FalseClass
|
69
|
+
False.new
|
70
|
+
else
|
71
|
+
raise Error, "#{obj.inspect} not convertible to Node"
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
class None < Node
|
77
|
+
end
|
78
|
+
|
79
|
+
class True < Node
|
80
|
+
end
|
81
|
+
|
82
|
+
class False < Node
|
83
|
+
end
|
84
|
+
|
85
|
+
class String < Node
|
86
|
+
attr_reader :str
|
87
|
+
def initialize(str)
|
88
|
+
@str = str
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
class Array < Node
|
93
|
+
attr_reader :elements
|
94
|
+
def initialize(elements)
|
95
|
+
@elements = elements.map(&method(:node))
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
class FunctionCall < Node
|
100
|
+
attr_reader :name, :args, :kwargs
|
101
|
+
def initialize(name, *args, **kwargs)
|
102
|
+
@name = name
|
103
|
+
@args = args.map(&method(:node))
|
104
|
+
@kwargs = kwargs.transform_values(&method(:node))
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
class MethodCall < Node
|
109
|
+
end
|
110
|
+
|
111
|
+
class Dictionary < Node
|
112
|
+
attr_reader :elements
|
113
|
+
def initialize(elements)
|
114
|
+
@elements = Hash[elements.map { |k, v| [node(k), node(v)] }]
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
class BinaryOperator < Node
|
119
|
+
attr_reader :lhs, :rhs, :operator
|
120
|
+
def initialize(lhs, rhs, operator:)
|
121
|
+
@lhs = node(lhs)
|
122
|
+
@rhs = node(rhs)
|
123
|
+
@operator = operator
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
class PlusOperator < BinaryOperator
|
128
|
+
def initialize(lhs, rhs)
|
129
|
+
super(lhs, rhs, operator: '+')
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
@@ -0,0 +1,185 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module StarlarkCompiler
|
4
|
+
class Writer
|
5
|
+
def self.write(ast:, io: nil)
|
6
|
+
if io.nil?
|
7
|
+
|
8
|
+
end
|
9
|
+
|
10
|
+
new(ast: ast, io: io).write
|
11
|
+
end
|
12
|
+
|
13
|
+
private_class_method :new
|
14
|
+
|
15
|
+
attr_accessor :ast, :io, :indent
|
16
|
+
|
17
|
+
def initialize(ast:, io:)
|
18
|
+
@ast = ast
|
19
|
+
@io = io
|
20
|
+
@indent = 0
|
21
|
+
end
|
22
|
+
|
23
|
+
def write
|
24
|
+
case ast
|
25
|
+
when AST
|
26
|
+
ast.toplevel.each_with_index do |o, i|
|
27
|
+
io << "\n\n" unless i.zero?
|
28
|
+
write_node(o)
|
29
|
+
end
|
30
|
+
when AST::Node
|
31
|
+
write_node(ast)
|
32
|
+
else
|
33
|
+
raise Error, "Trying to write unknown object #{ast.inspect}"
|
34
|
+
end
|
35
|
+
io << "\n"
|
36
|
+
end
|
37
|
+
|
38
|
+
def write_node(node, start_of_line: true)
|
39
|
+
write_start_of_line if start_of_line
|
40
|
+
delegate('write_%s', node)
|
41
|
+
end
|
42
|
+
|
43
|
+
def write_start_of_line
|
44
|
+
io << ' ' * indent
|
45
|
+
end
|
46
|
+
|
47
|
+
def write_string(str)
|
48
|
+
io << str.str.inspect
|
49
|
+
end
|
50
|
+
|
51
|
+
def delegate(template, obj)
|
52
|
+
snake_case = lambda do |str|
|
53
|
+
return str.downcase if str =~ /^[A-Z_]+$/
|
54
|
+
|
55
|
+
str.gsub(/\B[A-Z]/, '_\&').squeeze('_') =~ /_*(.*)/
|
56
|
+
$+.downcase
|
57
|
+
end
|
58
|
+
cls = obj.class
|
59
|
+
while cls && (cls != AST::Node)
|
60
|
+
name = cls.name.split('::').last
|
61
|
+
method = template % snake_case[name]
|
62
|
+
return send(method, obj) if respond_to?(method)
|
63
|
+
|
64
|
+
cls = cls.superclass
|
65
|
+
end
|
66
|
+
raise Error, "No #{template} for #{obj.class}"
|
67
|
+
end
|
68
|
+
|
69
|
+
def write_function_call(call)
|
70
|
+
single_line = single_line?(call)
|
71
|
+
io << call.name << '('
|
72
|
+
io << "\n" unless single_line
|
73
|
+
final_index = single_line && call.kwargs.empty? && call.args.size.pred
|
74
|
+
call.args.each_with_index do |arg, idx|
|
75
|
+
indented do
|
76
|
+
write_node(arg, start_of_line: !single_line)
|
77
|
+
unless final_index == idx
|
78
|
+
io << ','
|
79
|
+
io << (single_line ? ' ' : "\n")
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
final_index = single_line && call.kwargs.size.pred
|
84
|
+
call.kwargs.each_with_index do |(k, v), idx|
|
85
|
+
indented do
|
86
|
+
write_start_of_line unless single_line
|
87
|
+
io << "#{k} = "
|
88
|
+
write_node(v, start_of_line: false)
|
89
|
+
unless final_index == idx
|
90
|
+
io << ','
|
91
|
+
io << (single_line ? ' ' : "\n")
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
write_start_of_line unless single_line
|
96
|
+
io << ')'
|
97
|
+
end
|
98
|
+
|
99
|
+
def write_array(array)
|
100
|
+
single_line = single_line?(array)
|
101
|
+
io << '['
|
102
|
+
array.elements.each_with_index do |node, i|
|
103
|
+
unless i.zero?
|
104
|
+
io << ','
|
105
|
+
io << "\n" unless single_line
|
106
|
+
end
|
107
|
+
write_node(node, start_of_line: !single_line)
|
108
|
+
end
|
109
|
+
io << ']'
|
110
|
+
end
|
111
|
+
|
112
|
+
def write_dictionary(dictionary)
|
113
|
+
single_line = single_line?(dictionary)
|
114
|
+
io << '{'
|
115
|
+
dictionary.elements.each_with_index do |(key, value), i|
|
116
|
+
unless i.zero?
|
117
|
+
io << ','
|
118
|
+
io << "\n" unless single_line
|
119
|
+
end
|
120
|
+
write_node(key, start_of_line: !single_line)
|
121
|
+
io << ': '
|
122
|
+
write_node(value, start_of_line: false)
|
123
|
+
end
|
124
|
+
io << '}'
|
125
|
+
end
|
126
|
+
|
127
|
+
def write_binary_operator(operator)
|
128
|
+
write_node(operator.lhs, start_of_line: false)
|
129
|
+
io << " #{operator.operator} "
|
130
|
+
write_node(operator.rhs, start_of_line: false)
|
131
|
+
end
|
132
|
+
|
133
|
+
def write_none(_none)
|
134
|
+
io << 'None'
|
135
|
+
end
|
136
|
+
|
137
|
+
def write_true(_none)
|
138
|
+
io << 'True'
|
139
|
+
end
|
140
|
+
|
141
|
+
def write_false(_none)
|
142
|
+
io << 'True'
|
143
|
+
end
|
144
|
+
|
145
|
+
def indented
|
146
|
+
@indent += 1
|
147
|
+
yield
|
148
|
+
ensure
|
149
|
+
@indent -= 1
|
150
|
+
end
|
151
|
+
|
152
|
+
def single_line?(node)
|
153
|
+
return true unless node
|
154
|
+
|
155
|
+
delegate('single_line_%s?', node)
|
156
|
+
end
|
157
|
+
|
158
|
+
def single_line_string?(str)
|
159
|
+
str.str.size <= 50
|
160
|
+
end
|
161
|
+
|
162
|
+
def single_line_function_call?(call)
|
163
|
+
if call.args.empty?
|
164
|
+
call.kwargs.size <= 1 &&
|
165
|
+
call.kwargs.each_value.all? { |v| single_line?(v) }
|
166
|
+
elsif call.kwargs.empty?
|
167
|
+
call.args.size <= 2 && call.args.all? { |v| single_line?(v) } ||
|
168
|
+
(call.args.size == 1 && call.args.first.respond_to?(:elements))
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
172
|
+
def single_line_array?(array)
|
173
|
+
if array.elements.all? { |e| e.is_a?(AST::String) }
|
174
|
+
array.elements.sum { |s| s.str.length } < 50
|
175
|
+
else
|
176
|
+
array.elements.size <= 1 && array.elements.all?(&method(:single_line?))
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
180
|
+
def single_line_dictionary?(dictionary)
|
181
|
+
dictionary.elements.size <= 1 &&
|
182
|
+
dictionary.elements.each_value.all?(&method(:single_line?))
|
183
|
+
end
|
184
|
+
end
|
185
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path('lib', __dir__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require 'starlark_compiler/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = 'starlark_compiler'
|
9
|
+
spec.version = StarlarkCompiler::VERSION
|
10
|
+
spec.authors = ['Samuel Giddins']
|
11
|
+
spec.email = ['segiddins@segiddins.me']
|
12
|
+
|
13
|
+
spec.summary = 'A starlark gem'
|
14
|
+
spec.homepage = 'https://github.com/segiddins/starlark_compiler'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
18
|
+
spec.metadata['source_code_uri'] = spec.homepage
|
19
|
+
|
20
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
21
|
+
`git ls-files -z`.split("\x0")
|
22
|
+
.reject { |f| f.match(%r{^(test|spec|features)/}) }
|
23
|
+
end
|
24
|
+
spec.bindir = 'exe'
|
25
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
26
|
+
spec.require_paths = ['lib']
|
27
|
+
|
28
|
+
spec.required_ruby_version = '~> 2.6'
|
29
|
+
|
30
|
+
spec.add_development_dependency 'bundler', '~> 2.0'
|
31
|
+
end
|
metadata
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: starlark_compiler
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Samuel Giddins
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-01-09 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
|
+
description:
|
28
|
+
email:
|
29
|
+
- segiddins@segiddins.me
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- ".gitignore"
|
35
|
+
- ".rspec"
|
36
|
+
- ".rubocop.yml"
|
37
|
+
- ".travis.yml"
|
38
|
+
- CODE_OF_CONDUCT.md
|
39
|
+
- Gemfile
|
40
|
+
- Gemfile.lock
|
41
|
+
- LICENSE.txt
|
42
|
+
- README.md
|
43
|
+
- Rakefile
|
44
|
+
- bin/console
|
45
|
+
- bin/setup
|
46
|
+
- lib/starlark_compiler.rb
|
47
|
+
- lib/starlark_compiler/ast.rb
|
48
|
+
- lib/starlark_compiler/version.rb
|
49
|
+
- lib/starlark_compiler/writer.rb
|
50
|
+
- starlark_compiler.gemspec
|
51
|
+
homepage: https://github.com/segiddins/starlark_compiler
|
52
|
+
licenses:
|
53
|
+
- MIT
|
54
|
+
metadata:
|
55
|
+
homepage_uri: https://github.com/segiddins/starlark_compiler
|
56
|
+
source_code_uri: https://github.com/segiddins/starlark_compiler
|
57
|
+
post_install_message:
|
58
|
+
rdoc_options: []
|
59
|
+
require_paths:
|
60
|
+
- lib
|
61
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
62
|
+
requirements:
|
63
|
+
- - "~>"
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '2.6'
|
66
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
requirements: []
|
72
|
+
rubygems_version: 3.0.1
|
73
|
+
signing_key:
|
74
|
+
specification_version: 4
|
75
|
+
summary: A starlark gem
|
76
|
+
test_files: []
|