rprec 1.0.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: 7885e7e40956ceeb7b03804fb2ed1daa7ab84d97928d7e0e6db52694a07fad82
4
+ data.tar.gz: 75e46f416c7ad459e163a6767a296ef410456b0d163a7bcf064308a993d6c6b0
5
+ SHA512:
6
+ metadata.gz: 76118cd37c656b7752f54987adc4b5b79e9589bf2351268fc3c91e39636fe16847d4ee3cec3510626401f8b14e750b817413163564365239cf55985913cbfa9e
7
+ data.tar.gz: 5296f56324c9710579e9db088ef939b82b0c53e8cd50728fa321e9ee6113a974907c72f01e156859bc6704c5488b955944ea6690334ce50b6a80cb720268977c
data/.rubocop.yml ADDED
@@ -0,0 +1,31 @@
1
+ require:
2
+ - rubocop-minitest
3
+ - rubocop-rake
4
+
5
+ AllCops:
6
+ NewCops: enable
7
+ TargetRubyVersion: 3.2
8
+
9
+ Metrics/AbcSize:
10
+ Max: 50
11
+
12
+ Metrics/BlockLength:
13
+ Max: 39
14
+
15
+ Metrics/ClassLength:
16
+ Max: 157
17
+
18
+ Metrics/CyclomaticComplexity:
19
+ Max: 14
20
+
21
+ Metrics/MethodLength:
22
+ Max: 49
23
+
24
+ Metrics/PerceivedComplexity:
25
+ Max: 13
26
+
27
+ Layout/LineLength:
28
+ Max: 125
29
+
30
+ Style/AccessModifierDeclarations:
31
+ EnforcedStyle: inline
data/.yardopts ADDED
@@ -0,0 +1,5 @@
1
+ --protected
2
+ --no-private
3
+ --markup markdown
4
+ -
5
+ CHANGELOG.md
data/CHANGELOG.md ADDED
@@ -0,0 +1,10 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [1.0.0]
9
+
10
+ [1.0.0]: https://github.com/olivierlacan/keep-a-changelog/releases/tag/v1.0.0
@@ -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 make.just.on@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) 2023 Hiroya Fujinami (a.k.a. TSUYUSATO "MakeNowJust" Kitsune)
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,19 @@
1
+ # RPrec
2
+
3
+ > Operator-precedence parsing implementation in Ruby
4
+
5
+ ## Usage
6
+
7
+ <!-- TODO: write here. -->
8
+
9
+ ## Contributing
10
+
11
+ Bug reports and pull requests are welcome on GitHub at https://github.com/MakeNowJust/rprec. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/makenowjust/rprec/blob/main/CODE_OF_CONDUCT.md).
12
+
13
+ ## License
14
+
15
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
16
+
17
+ ## Code of Conduct
18
+
19
+ Everyone interacting in the Rprec project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/makenowjust/rprec/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rake/testtask'
5
+ require 'yard'
6
+
7
+ Rake::TestTask.new(:test) do |t|
8
+ t.libs << 'test'
9
+ t.libs << 'lib'
10
+ t.test_files = FileList['test/**/test_*.rb']
11
+ end
12
+
13
+ YARD::Rake::YardocTask.new do |t|
14
+ t.files = ['lib/**/*.rb']
15
+ end
16
+
17
+ task default: :test
data/Steepfile ADDED
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ D = Steep::Diagnostic
4
+
5
+ target :lib do
6
+ signature 'sig'
7
+ signature 'test/sig'
8
+
9
+ check 'lib'
10
+ check 'test'
11
+ ignore 'test/test_helper.rb'
12
+
13
+ configure_code_diagnostics(D::Ruby.default)
14
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'stream'
4
+
5
+ module RPrec
6
+ # `ArrayStream` is a simple implementation of `RPrec::Stream` with an array.
7
+ class ArrayStream < Stream
8
+ # @param tokens [Array<RPrec::Token>]
9
+ def initialize(tokens)
10
+ super()
11
+ @tokens = tokens
12
+ @index = 0
13
+ end
14
+
15
+ # @return [RPrec::Token]
16
+ def current
17
+ return Token.new('EOF') if eof?
18
+
19
+ @tokens[@index]
20
+ end
21
+
22
+ # @return [void]
23
+ def next
24
+ super
25
+ @index += 1
26
+ end
27
+
28
+ # @return [Boolean]
29
+ def eof?
30
+ @index >= @tokens.size
31
+ end
32
+ end
33
+ end
data/lib/rprec/dsl.rb ADDED
@@ -0,0 +1,140 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RPrec
4
+ # `DSL` provides a DSL to construct `RPrec::Grammar` objects.
5
+ # It is also a context of blocks of the `RPrec::DSL.build` method.
6
+ class DSL
7
+ # @param main [Symbol]
8
+ # @param block [Proc]
9
+ # @return [RPrec::Grammar]
10
+ def self.build(main = :main, &)
11
+ context = DSL.new
12
+ context.instance_eval(&)
13
+ grammar = Grammar.new(main, context.precs)
14
+ grammar.setup
15
+ grammar
16
+ end
17
+
18
+ # @api private
19
+ def initialize
20
+ @precs = {}
21
+ end
22
+
23
+ # @dynamic precs
24
+
25
+ # @api private
26
+ # @return [Hash{Symbol => RPrec::Prec}]
27
+ attr_reader :precs
28
+
29
+ # @param name_and_succs [Symbol, Hash{Symbol => Symbol}, Hash{Symbol => Array<Symbol>}]
30
+ # @return [void]
31
+ def prec(name_and_succs, &)
32
+ # @type var name: Symbol
33
+ # @type var succs: Array[Symbol]
34
+ name, succs =
35
+ if name_and_succs.is_a?(Hash)
36
+ name_first, succs_first = name_and_succs.first
37
+ if name_and_succs.size != 1 || !name_first || !succs_first
38
+ raise ArgumentError, '`prec` should be `prec name => succs` form'
39
+ end
40
+
41
+ [name_first, succs_first.is_a?(Array) ? succs_first : [succs_first]]
42
+ else
43
+ [name_and_succs, []]
44
+ end
45
+
46
+ raise ArgumentError, "A prec '#{name}' is already defined" if @precs.include?(name)
47
+
48
+ @precs[name] = PrecDSL.build(name, succs, &)
49
+ end
50
+
51
+ # `PrecDSL` is a context of blocks of the `RPrec::DSL#prec` method.
52
+ class PrecDSL
53
+ # @api private
54
+ # @param name [Symbol]
55
+ # @param succs [Array<Symbol>]
56
+ # @param block [Proc]
57
+ # @return [RPrec::]
58
+ def self.build(name, succs, &block)
59
+ context = PrecDSL.new
60
+ context.instance_eval(&block) if block
61
+ Prec.new(name, succs, context.ops)
62
+ end
63
+
64
+ # @api private
65
+ def initialize
66
+ @ops = []
67
+ end
68
+
69
+ # @dynamic ops
70
+
71
+ # @api private
72
+ # @return [Array<RPrec::Op>]
73
+ attr_reader :ops
74
+
75
+ # @param key [String]
76
+ # @param parts [Array<String, Symbol>]
77
+ # @param build [Proc]
78
+ # @return [void]
79
+ def prefix(key, *parts, &build)
80
+ @ops << Op.new(:prefix, key, parts, build)
81
+ end
82
+
83
+ # @param key [String]
84
+ # @param parts [Array<String, Symbol>]
85
+ # @param build [Proc]
86
+ # @return [void]
87
+ def postfix(key, *parts, &build)
88
+ @ops << Op.new(:postfix, key, parts, build)
89
+ end
90
+
91
+ # @param key [String]
92
+ # @param parts [Array<String, Symbol>]
93
+ # @param build [Proc]
94
+ # @return [void]
95
+ def non_assoc_prefix(key, *parts, &build)
96
+ @ops << Op.new(:non_assoc_prefix, key, parts, build)
97
+ end
98
+
99
+ # @param key [String]
100
+ # @param parts [Array<String, Symbol>]
101
+ # @param build [Proc]
102
+ # @return [void]
103
+ def non_assoc_postfix(key, *parts, &build)
104
+ @ops << Op.new(:non_assoc_postfix, key, parts, build)
105
+ end
106
+
107
+ # @param key [String]
108
+ # @param parts [Array<String, Symbol>]
109
+ # @param build [Proc]
110
+ # @return [void]
111
+ def closed(key, *parts, &build)
112
+ @ops << Op.new(:closed, key, parts, build)
113
+ end
114
+
115
+ # @param key [String]
116
+ # @param parts [Array<String, Symbol>]
117
+ # @param build [Proc]
118
+ # @return [void]
119
+ def left_assoc(key, *parts, &build)
120
+ @ops << Op.new(:left_assoc, key, parts, build)
121
+ end
122
+
123
+ # @param key [String]
124
+ # @param parts [Array<String, Symbol>]
125
+ # @param build [Proc]
126
+ # @return [void]
127
+ def right_assoc(key, *parts, &build)
128
+ @ops << Op.new(:right_assoc, key, parts, build)
129
+ end
130
+
131
+ # @param key [String]
132
+ # @param parts [Array<String, Symbol>]
133
+ # @param build [Proc]
134
+ # @return [void]
135
+ def non_assoc(key, *parts, &build)
136
+ @ops << Op.new(:non_assoc, key, parts, build)
137
+ end
138
+ end
139
+ end
140
+ end
@@ -0,0 +1,102 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RPrec
4
+ # `Grammar` is a grammar defined by operator precedences.
5
+ class Grammar
6
+ # @param main [Symbol]
7
+ # @param precs [Hash{Symbol => RPrec::Prec}]
8
+ def initialize(main, precs)
9
+ @main = main
10
+ @precs = precs
11
+ end
12
+
13
+ # @param name [Symbol]
14
+ # @return [RPrec::Prec]
15
+ def [](name)
16
+ prec = @precs[name]
17
+ raise KeyError, "A prec '#{name}' is undefined" unless prec
18
+
19
+ prec
20
+ end
21
+
22
+ # @api private
23
+ # @param names [Array<Symbol>]
24
+ # @return [void]
25
+ def setup(names = [@main])
26
+ names.each do |name|
27
+ prec = self[name]
28
+ prec.setup(self)
29
+ end
30
+ end
31
+
32
+ # @param stream [RPrec::Stream]
33
+ # @param check_eof [Boolean]
34
+ # @return [Object]
35
+ def parse(stream, check_eof: true)
36
+ result = parse_prec(@main, stream)
37
+ stream.unexpected unless result
38
+
39
+ if check_eof
40
+ stream.expected(['EOF'])
41
+ stream.unexpected unless stream.eof?
42
+ end
43
+
44
+ result
45
+ end
46
+
47
+ # @api private
48
+ # @param names [Array<Symbol>]
49
+ # @param stream [RPrec::Stream]
50
+ # @return [Object]
51
+ def parse_precs(names, stream)
52
+ names.each do |name|
53
+ result = parse_prec(name, stream)
54
+ return result if result
55
+ end
56
+ stream.unexpected
57
+ end
58
+
59
+ # @api private
60
+ # @param name [Symbol]
61
+ # @param stream [RPrec::Stream]
62
+ # @return [Object, nil]
63
+ def parse_prec(name, stream)
64
+ prec = self[name]
65
+ prec.parse(self, stream)
66
+ end
67
+
68
+ # @api private
69
+ # @param parts [Array<String, Symbol>]
70
+ # @param stream [RPrec::Stream]
71
+ # @return [Array<RPrec::Token, Object>]
72
+ def parse_parts(parts, stream)
73
+ parts.map do |part|
74
+ if part.is_a?(Symbol)
75
+ result = parse_prec(part, stream)
76
+ stream.unexpected unless result
77
+ result
78
+ else
79
+ token = stream.current
80
+ unless token.type == part
81
+ stream.expected([part])
82
+ stream.unexpected
83
+ end
84
+ stream.next
85
+ token
86
+ end
87
+ end
88
+ end
89
+
90
+ # @return [String]
91
+ def inspect
92
+ result = "DSL.build #{@main.inspect} do\n"
93
+ @precs.each do |(_, prec)|
94
+ prec.inspect.lines(chomp: true).each do |line|
95
+ result << " #{line}\n"
96
+ end
97
+ end
98
+ result << 'end'
99
+ result
100
+ end
101
+ end
102
+ end
data/lib/rprec/op.rb ADDED
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RPrec
4
+ # `Op` is an operator.
5
+ class Op
6
+ # @param type [:prefix, :postfix, :non_assoc_prefix, :non_assoc_postfix, :closed, :left_assoc, :right_assoc, :non_assoc]
7
+ # @param key [String]
8
+ # @param parts [Array<String, Symbol>]
9
+ # @param build [Proc]
10
+ def initialize(type, key, parts, build)
11
+ @type = type
12
+ @key = key
13
+ @parts = parts
14
+ @build = build
15
+ end
16
+
17
+ # @dynamic type, key, parts
18
+
19
+ # @return [:prefix, :postfix, :non_assoc_prefix, :non_assoc_postfix, :closed, :left_assoc, :right_assoc, :non_assoc]
20
+ attr_reader :type
21
+ # @return [String]
22
+ attr_reader :key
23
+ # @return [Array<String, Symbol>]
24
+ attr_reader :parts
25
+
26
+ # @param args [Array<RPrec::Token, Object>]
27
+ # @return [Object]
28
+ def build(*)
29
+ @build.call(*)
30
+ end
31
+
32
+ # @return [String]
33
+ def inspect
34
+ "#{type} #{([key] + parts).map(&:inspect).join(', ')}"
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RPrec
4
+ # `ParseError` is an error on parsing.
5
+ class ParseError < Error
6
+ # @param message [String]
7
+ # @param loc [Range, nil]
8
+ def initialize(message, loc: nil)
9
+ full_message = loc ? "#{message} at #{loc}" : message
10
+ @loc = loc
11
+ super(full_message)
12
+ end
13
+
14
+ # @dynamic loc
15
+
16
+ # @return [Range, nil]
17
+ attr_reader :loc
18
+ end
19
+ end