rupkl 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: c8b6a7346123ff0c771a201f1218f0c2d92e3c34a16e4fe0f668724eb53726ee
4
+ data.tar.gz: 169990d7f909f37f72ea97ba2ff44c9634137fead23791ab6a80fddd9b3af109
5
+ SHA512:
6
+ metadata.gz: 50e6f855bec47026a3a6f4b8b61b9fd61ceafc7686bcd0a51f459a42ae88a97ee954fb362f3450c89d6499fe6e1d97f2b2350350a2820607c007bc7a8e8d8190
7
+ data.tar.gz: ad63c41fa79cc9b487d6d3c7f4b129febbf914e564f15d9921072cf4680442dcc77c605d8d7cc283aa15662f15247f427ccbd218c2a1856681ca402500e2810f
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at taichi730@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Taichi Ishitani
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,123 @@
1
+ [![Gem Version](https://badge.fury.io/rb/rupkl.svg)](https://badge.fury.io/rb/rupkl)
2
+ [![CI](https://github.com/taichi-ishitani/rupkl/actions/workflows/ci.yml/badge.svg)](https://github.com/taichi-ishitani/rupkl/actions/workflows/ci.yml)
3
+ [![Maintainability](https://api.codeclimate.com/v1/badges/ee9795f03af99994d139/maintainability)](https://codeclimate.com/github/taichi-ishitani/rupkl/maintainability)
4
+ [![codecov](https://codecov.io/github/taichi-ishitani/rupkl/graph/badge.svg?token=CrcaXQ9FjI)](https://codecov.io/github/taichi-ishitani/rupkl)
5
+
6
+ # RuPkl
7
+
8
+ A [Pkl](https://pkl-lang.org) parser for Ruby.
9
+
10
+ ## Installation
11
+
12
+ Install the gem and add to the application's Gemfile by executing:
13
+
14
+ $ bundle add rupkl
15
+
16
+ If bundler is not being used to manage dependencies, install the gem by executing:
17
+
18
+ $ gem install rupkl
19
+
20
+ ## Usage
21
+
22
+ You can use the methods below to load a Pkl code into a Ruby structure:
23
+
24
+ * `RuPkl.load`
25
+ * Load the given Pkl code into a Ruby structure
26
+ * `RuPkl.load_file`
27
+ * Load a Pkl code read from the given file path into a Ruby structure
28
+
29
+ ```ruby
30
+ require 'rupkl'
31
+
32
+ pkl = <<~'PKL'
33
+ // From:
34
+ // https://pkl-lang.org/main/current/language-tutorial/01_basic_config.html
35
+ name = "Pkl: Configure your Systems in New Ways"
36
+ attendants = 100
37
+ isInteractive = true
38
+ amountLearned = 13.37
39
+ PKL
40
+
41
+ RuPkl.load(pkl)
42
+ # =>
43
+ # {:name=>"Pkl:Configure your Systems in New Ways",
44
+ # :attendants=>100,
45
+ # :isInteractive=>true,
46
+ # :amountLearned=>13.37}
47
+
48
+ File.open('sample.pkl', 'w') do |f|
49
+ f.write(<<~'PKL')
50
+ // From:
51
+ // https://pkl-lang.org/main/current/language-tutorial/01_basic_config.html
52
+ bird {
53
+ name = "Common wood pigeon"
54
+ diet = "Seeds"
55
+ taxonomy {
56
+ species = "Columba palumbus"
57
+ }
58
+ }
59
+
60
+ exampleObjectWithJustIntElements {
61
+ 100
62
+ 42
63
+ }
64
+
65
+ exampleObjectWithMixedElements {
66
+ "Bird Breeder Conference"
67
+ (2000 + 23)
68
+ exampleObjectWithJustIntElements
69
+ }
70
+
71
+ pigeonShelter {
72
+ ["bird"] {
73
+ name = "Common wood pigeon"
74
+ diet = "Seeds"
75
+ taxonomy {
76
+ species = "Columba palumbus"
77
+ }
78
+ }
79
+ ["address"] = "355 Bird St."
80
+ }
81
+
82
+ birdCount {
83
+ [pigeonShelter] = 42
84
+ }
85
+ PKL
86
+ end
87
+
88
+ RuPkl.load_file('sample.pkl')
89
+ # =>
90
+ # {:bird=>{:name=>"Common wood pigeon", :diet=>"Seeds", :taxonomy=>{:species=>"Columba palumbus"}},
91
+ # :exampleObjectWithJustIntElements=>[100, 42],
92
+ # :exampleObjectWithMixedElements=>["Bird Breeder Conference", 2023, [100, 42]],
93
+ # :pigeonShelter=>
94
+ # {"bird"=>{:name=>"Common wood pigeon", :diet=>"Seeds", :taxonomy=>{:species=>"Columba palumbus"}},
95
+ # "address"=>"355 Bird St."},
96
+ # :birdCount=>
97
+ # {{"bird"=>{:name=>"Common wood pigeon", :diet=>"Seeds", :taxonomy=>{:species=>"Columba palumbus"}},
98
+ # "address"=>"355 Bird St."}=>42}}
99
+ ```
100
+
101
+ ## Contributing
102
+
103
+ Bug reports and pull requests are welcome on GitHub at https://github.com/taichi-ishitani/rupkl.
104
+
105
+ * [Issue Tracker](https://github.com/taichi-ishitani/rupkl/issues/new/choose)
106
+ * [Discussion](https://github.com/taichi-ishitani/rupkl/discussions/new/choose)
107
+ * [Pull Request](https://github.com/taichi-ishitani/rupkl/pulls)
108
+
109
+ ## Notice
110
+
111
+ Pkl code snippets used for RSpec examples are originaly from:
112
+
113
+ * https://pkl-lang.org/main/current/language-tutorial/index.html
114
+ * https://github.com/apple/pkl
115
+
116
+ ## Copyright & License
117
+
118
+ Copyright &copy; 2024 Taichi Ishitani.
119
+ RuPkl is licensed under the terms of the [MIT License](https://opensource.org/licenses/MIT), see [LICENSE](LICENSE) for further details.
120
+
121
+ ## Code of Conduct
122
+
123
+ Everyone interacting in the RuPkl project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/taichi-ishitani/rupkl/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuPkl
4
+ class RuPklError < StandardError
5
+ end
6
+
7
+ class ParseError < RuPklError
8
+ def initialize(message, position, cause)
9
+ super(message)
10
+ @position = position
11
+ @cause = cause
12
+ end
13
+
14
+ attr_reader :position
15
+ attr_reader :cause
16
+ end
17
+
18
+ class EvaluationError < RuPklError
19
+ def initialize(message, position)
20
+ super(message)
21
+ @position = position
22
+ end
23
+
24
+ attr_reader :position
25
+ end
26
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuPkl
4
+ module Node
5
+ class Boolean
6
+ include ValueCommon
7
+
8
+ def evaluate(_scopes)
9
+ self
10
+ end
11
+
12
+ def undefined_operator?(operator)
13
+ [:!, :==, :'!=', :'&&', :'||'].none?(operator)
14
+ end
15
+
16
+ def short_circuit?(operator)
17
+ [operator, value] in [:'&&', false] | [:'||', true]
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,99 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuPkl
4
+ module Node
5
+ class Dynamic
6
+ include StructCommon
7
+
8
+ def initialize(members, scopes, position)
9
+ @position = position
10
+ add_members(members, scopes)
11
+ end
12
+
13
+ attr_reader :properties
14
+ attr_reader :entries
15
+ attr_reader :elements
16
+ attr_reader :position
17
+
18
+ def evaluate(_scopes)
19
+ self
20
+ end
21
+
22
+ def to_ruby(_scopes)
23
+ create_pkl_object(nil, properties, entries, elements)
24
+ end
25
+
26
+ def to_pkl_string(_scopes)
27
+ to_pkl_string_object(*properties, *entries, *elements)
28
+ end
29
+
30
+ def merge!(other)
31
+ @properties = merge_hash_members(properties, other.properties, :name)
32
+ @entries = merge_hash_members(entries, other.entries, :key)
33
+ @elements = merge_array_members(elements, other.elements)
34
+ self
35
+ end
36
+
37
+ def ==(other)
38
+ other.instance_of?(self.class) &&
39
+ match_members?(properties, other.properties, false) &&
40
+ match_members?(entries, other.entries, false) &&
41
+ match_members?(elements, other.elements, true)
42
+ end
43
+
44
+ def undefined_operator?(operator)
45
+ [:[], :==, :'!='].none?(operator)
46
+ end
47
+
48
+ def find_by_key(key)
49
+ find_entry(key) || find_element(key)
50
+ end
51
+
52
+ private
53
+
54
+ def add_members(members, scopes)
55
+ return unless members
56
+
57
+ push_scope(scopes) do |s|
58
+ members.each { |m| add_member(m, s) }
59
+ end
60
+ end
61
+
62
+ def add_member(member, scopes)
63
+ member.evaluate(scopes).then do |m|
64
+ case member
65
+ when ObjectProperty then add_property(m)
66
+ when ObjectEntry then add_entry(m)
67
+ else add_element(m)
68
+ end
69
+ end
70
+ end
71
+
72
+ def add_property(member)
73
+ add_hash_member((@properties ||= []), member, :name)
74
+ end
75
+
76
+ def add_entry(member)
77
+ add_hash_member((@entries ||= []), member, :key)
78
+ end
79
+
80
+ def add_element(member)
81
+ add_array_member((@elements ||= []), member)
82
+ end
83
+
84
+ def find_entry(key)
85
+ entries
86
+ &.find { _1.key == key }
87
+ &.then(&:value)
88
+ end
89
+
90
+ def find_element(index)
91
+ return nil unless elements
92
+ return nil unless index.value.is_a?(::Integer)
93
+
94
+ elements
95
+ .find.with_index { |_, i| i == index.value }
96
+ end
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuPkl
4
+ module Node
5
+ class Identifier
6
+ def initialize(id, position)
7
+ @id = id
8
+ @position = position
9
+ end
10
+
11
+ def ==(other)
12
+ other.instance_of?(self.class) && id == other.id
13
+ end
14
+
15
+ attr_reader :id
16
+ attr_reader :position
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuPkl
4
+ module Node
5
+ class MemberReference
6
+ def initialize(receiver, member, position)
7
+ @receiver = receiver
8
+ @member = member
9
+ @position = position
10
+ end
11
+
12
+ attr_reader :receiver
13
+ attr_reader :member
14
+ attr_reader :position
15
+
16
+ def evaluate(scopes)
17
+ member_node =
18
+ if receiver
19
+ find_member([receiver.evaluate(scopes)])
20
+ else
21
+ find_member(scopes)
22
+ end
23
+ member_node.evaluate(scopes).value
24
+ end
25
+
26
+ def to_string(scopes)
27
+ evaluate(scopes).to_string(nil)
28
+ end
29
+
30
+ def to_pkl_string(scopes)
31
+ evaluate(scopes).to_pkl_string(nil)
32
+ end
33
+
34
+ private
35
+
36
+ def find_member(scopes)
37
+ scopes.reverse_each do |scope|
38
+ node = scope&.properties&.find { _1.name.id == member.id }
39
+ return node if node
40
+ end
41
+
42
+ raise EvaluationError.new("cannot find property '#{member.id}'", position)
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuPkl
4
+ module Node
5
+ class Number
6
+ include ValueCommon
7
+
8
+ def evaluate(_scopes)
9
+ self
10
+ end
11
+
12
+ def undefined_operator?(operator)
13
+ [:[], :!, :'&&', :'||'].include?(operator)
14
+ end
15
+
16
+ def invalid_r_operand?(operand)
17
+ !operand.is_a?(Number)
18
+ end
19
+
20
+ def coerce(operator, r_operand)
21
+ if force_float?(operator, r_operand)
22
+ [value.to_f, r_operand.value.to_f]
23
+ else
24
+ [value.to_i, r_operand.value.to_i]
25
+ end
26
+ end
27
+
28
+ def force_float?(operator, r_operand)
29
+ operator == :/ ||
30
+ operator != :'~/' && [self, r_operand].any?(Float)
31
+ end
32
+ end
33
+
34
+ class Integer < Number
35
+ end
36
+
37
+ class Float < Number
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,141 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuPkl
4
+ module Node
5
+ module PropertyEvaluator
6
+ private
7
+
8
+ def evaluate_property(value, objects, scopes)
9
+ value&.evaluate(scopes) || evaluate_objects(objects, scopes)
10
+ end
11
+
12
+ def property_to_ruby(value, objects, scopes)
13
+ if value
14
+ value.to_ruby(scopes)
15
+ else
16
+ evaluate_objects(objects, scopes).to_ruby(nil)
17
+ end
18
+ end
19
+
20
+ def property_to_pkl_string(value, objects, scopes)
21
+ if value
22
+ value.to_pkl_string(scopes)
23
+ else
24
+ evaluate_objects(objects, scopes).to_pkl_string(nil)
25
+ end
26
+ end
27
+
28
+ def evaluate_objects(objects, scopes)
29
+ objects
30
+ .map { _1.evaluate(scopes) }
31
+ .inject { |r, o| r.merge!(o) }
32
+ end
33
+ end
34
+
35
+ class ObjectProperty
36
+ include PropertyEvaluator
37
+
38
+ def initialize(name, value, objects, position)
39
+ @name = name
40
+ @value = value
41
+ @objects = objects
42
+ @position = position
43
+ end
44
+
45
+ attr_reader :name
46
+ attr_reader :value
47
+ attr_reader :objects
48
+ attr_reader :position
49
+
50
+ def evaluate(scopes)
51
+ v = evaluate_property(value, objects, scopes)
52
+ self.class.new(name, v, nil, position)
53
+ end
54
+
55
+ def to_ruby(scopes)
56
+ [name.id, property_to_ruby(value, objects, scopes)]
57
+ end
58
+
59
+ def to_pkl_string(scopes)
60
+ v = property_to_pkl_string(value, objects, scopes)
61
+ if v.start_with?('{')
62
+ "#{name.id} #{v}"
63
+ else
64
+ "#{name.id} = #{v}"
65
+ end
66
+ end
67
+
68
+ def ==(other)
69
+ name.id == other.name.id && value == other.value
70
+ end
71
+ end
72
+
73
+ class ObjectEntry
74
+ include PropertyEvaluator
75
+
76
+ def initialize(key, value, objects, position)
77
+ @key = key
78
+ @value = value
79
+ @objects = objects
80
+ @position = position
81
+ end
82
+
83
+ attr_reader :key
84
+ attr_reader :value
85
+ attr_reader :objects
86
+ attr_reader :position
87
+
88
+ def evaluate(scopes)
89
+ k = key.evaluate(scopes)
90
+ v = evaluate_property(value, objects, scopes)
91
+ self.class.new(k, v, nil, position)
92
+ end
93
+
94
+ def to_ruby(scopes)
95
+ k = key.to_ruby(scopes)
96
+ v = property_to_ruby(value, objects, scopes)
97
+ [k, v]
98
+ end
99
+
100
+ def to_pkl_string(scopes)
101
+ k = key.to_pkl_string(scopes)
102
+ v = property_to_pkl_string(value, objects, scopes)
103
+ if v.start_with?('{')
104
+ "[#{k}] #{v}"
105
+ else
106
+ "[#{k}] = #{v}"
107
+ end
108
+ end
109
+
110
+ def ==(other)
111
+ key == other.key && value == other.value
112
+ end
113
+ end
114
+
115
+ class UnresolvedObject
116
+ def initialize(members, position)
117
+ @members = members
118
+ @position = position
119
+ end
120
+
121
+ attr_reader :members
122
+ attr_reader :position
123
+
124
+ def evaluate(scopes)
125
+ Dynamic.new(members, scopes, position)
126
+ end
127
+
128
+ def to_ruby(scopes)
129
+ evaluate(scopes).to_ruby(nil)
130
+ end
131
+
132
+ def to_string(scopes)
133
+ evaluate(scopes).to_string(nil)
134
+ end
135
+
136
+ def to_pkl_string(scopes)
137
+ evaluate(scopes).to_pkl_string(nil)
138
+ end
139
+ end
140
+ end
141
+ end