xr 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.envrc +2 -0
- data/.rubocop.yml +18 -0
- data/CHANGELOG.md +5 -0
- data/LICENSE +202 -0
- data/README.md +37 -0
- data/Rakefile +24 -0
- data/lib/xr.rb +128 -0
- data/manifest.scm +1 -0
- data/sig/xr.rbs +4 -0
- data/syntax.ja.rdoc +219 -0
- metadata +54 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 3167993ee746e27fb4c9325e259c94b615aaea66820eb8814571b716c936fc99
|
4
|
+
data.tar.gz: c171d8ffeb87b57843a325b4f3a25ba6725b5da3bf829be20c41bba63cd82987
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0d7c7d87bedaf7a63dacd75b123ae12ea075d3764bf9406eb26fece88f6075cbc1c66e24f9c6e76d47b3c7414acad3de7d87bb8a793124fed09f5386222d28af
|
7
|
+
data.tar.gz: cdccf42cda2d7dc82338bb4d4bfe640e4766b1a4d5c5fab483123d4cac6a0941c2bc0c86ad744aa4d5f737a8a5b991a6cab35c7bb8835f634d48ef9b1aae202b
|
data/.envrc
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require:
|
2
|
+
- rubocop-rake
|
3
|
+
|
4
|
+
AllCops:
|
5
|
+
TargetRubyVersion: 3.1
|
6
|
+
NewCops: enable
|
7
|
+
DisabledByDefault: true
|
8
|
+
|
9
|
+
Style/StringLiterals:
|
10
|
+
Enabled: true
|
11
|
+
EnforcedStyle: double_quotes
|
12
|
+
|
13
|
+
Style/StringLiteralsInInterpolation:
|
14
|
+
Enabled: true
|
15
|
+
EnforcedStyle: double_quotes
|
16
|
+
|
17
|
+
Layout/LineLength:
|
18
|
+
Max: 120
|
data/CHANGELOG.md
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,202 @@
|
|
1
|
+
|
2
|
+
Apache License
|
3
|
+
Version 2.0, January 2004
|
4
|
+
http://www.apache.org/licenses/
|
5
|
+
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
7
|
+
|
8
|
+
1. Definitions.
|
9
|
+
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
12
|
+
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
14
|
+
the copyright owner that is granting the License.
|
15
|
+
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
17
|
+
other entities that control, are controlled by, or are under common
|
18
|
+
control with that entity. For the purposes of this definition,
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
20
|
+
direction or management of such entity, whether by contract or
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
23
|
+
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
25
|
+
exercising permissions granted by this License.
|
26
|
+
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
28
|
+
including but not limited to software source code, documentation
|
29
|
+
source, and configuration files.
|
30
|
+
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
32
|
+
transformation or translation of a Source form, including but
|
33
|
+
not limited to compiled object code, generated documentation,
|
34
|
+
and conversions to other media types.
|
35
|
+
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
37
|
+
Object form, made available under the License, as indicated by a
|
38
|
+
copyright notice that is included in or attached to the work
|
39
|
+
(an example is provided in the Appendix below).
|
40
|
+
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
47
|
+
the Work and Derivative Works thereof.
|
48
|
+
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
50
|
+
the original version of the Work and any modifications or additions
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
62
|
+
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
65
|
+
subsequently incorporated within the Work.
|
66
|
+
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
73
|
+
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
79
|
+
where such license applies only to those patent claims licensable
|
80
|
+
by such Contributor that are necessarily infringed by their
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
83
|
+
institute patent litigation against any entity (including a
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
86
|
+
or contributory patent infringement, then any patent licenses
|
87
|
+
granted to You under this License for that Work shall terminate
|
88
|
+
as of the date such litigation is filed.
|
89
|
+
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
92
|
+
modifications, and in Source or Object form, provided that You
|
93
|
+
meet the following conditions:
|
94
|
+
|
95
|
+
(a) You must give any other recipients of the Work or
|
96
|
+
Derivative Works a copy of this License; and
|
97
|
+
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
99
|
+
stating that You changed the files; and
|
100
|
+
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
103
|
+
attribution notices from the Source form of the Work,
|
104
|
+
excluding those notices that do not pertain to any part of
|
105
|
+
the Derivative Works; and
|
106
|
+
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
109
|
+
include a readable copy of the attribution notices contained
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
112
|
+
of the following places: within a NOTICE text file distributed
|
113
|
+
as part of the Derivative Works; within the Source form or
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
115
|
+
within a display generated by the Derivative Works, if and
|
116
|
+
wherever such third-party notices normally appear. The contents
|
117
|
+
of the NOTICE file are for informational purposes only and
|
118
|
+
do not modify the License. You may add Your own attribution
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
121
|
+
that such additional attribution notices cannot be construed
|
122
|
+
as modifying the License.
|
123
|
+
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
125
|
+
may provide additional or different license terms and conditions
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
129
|
+
the conditions stated in this License.
|
130
|
+
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
134
|
+
this License, without any additional terms or conditions.
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
136
|
+
the terms of any separate license agreement you may have executed
|
137
|
+
with Licensor regarding such Contributions.
|
138
|
+
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
141
|
+
except as required for reasonable and customary use in describing the
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
143
|
+
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
153
|
+
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
159
|
+
incidental, or consequential damages of any character arising as a
|
160
|
+
result of this License or out of the use or inability to use the
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
163
|
+
other commercial damages or losses), even if such Contributor
|
164
|
+
has been advised of the possibility of such damages.
|
165
|
+
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
169
|
+
or other liability obligations and/or rights consistent with this
|
170
|
+
License. However, in accepting such obligations, You may act only
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
175
|
+
of your accepting any such warranty or additional liability.
|
176
|
+
|
177
|
+
END OF TERMS AND CONDITIONS
|
178
|
+
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
180
|
+
|
181
|
+
To apply the Apache License to your work, attach the following
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
183
|
+
replaced with your own identifying information. (Don't include
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
185
|
+
comment syntax for the file format. We also recommend that a
|
186
|
+
file or class name and description of purpose be included on the
|
187
|
+
same "printed page" as the copyright notice for easier
|
188
|
+
identification within third-party archives.
|
189
|
+
|
190
|
+
Copyright 2024 gemmaro
|
191
|
+
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
193
|
+
you may not use this file except in compliance with the License.
|
194
|
+
You may obtain a copy of the License at
|
195
|
+
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
197
|
+
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
201
|
+
See the License for the specific language governing permissions and
|
202
|
+
limitations under the License.
|
data/README.md
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# Xr
|
2
|
+
|
3
|
+
Domain specific language (DSL) for Ruby's regular expression.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Install the gem and add to the application's Gemfile by executing:
|
8
|
+
|
9
|
+
$ bundle add xr
|
10
|
+
|
11
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
12
|
+
|
13
|
+
$ gem install xr
|
14
|
+
|
15
|
+
## Usage
|
16
|
+
|
17
|
+
Please see [syntax.ja.rdoc](syntax.ja.rdoc) (Japanese) or tests.
|
18
|
+
|
19
|
+
For a really simple example, digits can be written as...
|
20
|
+
|
21
|
+
``` ruby
|
22
|
+
Xr { _ + /\d/ }
|
23
|
+
```
|
24
|
+
|
25
|
+
... which compiles to `\d+` or something like that.
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies.
|
30
|
+
Then, run `rake test` to run the tests.
|
31
|
+
You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
32
|
+
|
33
|
+
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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
34
|
+
|
35
|
+
## Contributing
|
36
|
+
|
37
|
+
Bug reports and pull requests are welcome.
|
data/Rakefile
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "bundler/gem_tasks"
|
4
|
+
require "rake/testtask"
|
5
|
+
|
6
|
+
Rake::TestTask.new(:test) do |t|
|
7
|
+
t.libs << "test"
|
8
|
+
t.libs << "lib"
|
9
|
+
t.test_files = FileList["test/**/*_test.rb"]
|
10
|
+
end
|
11
|
+
|
12
|
+
require "rubocop/rake_task"
|
13
|
+
|
14
|
+
RuboCop::RakeTask.new
|
15
|
+
|
16
|
+
# TODO: Add RuboCop.
|
17
|
+
task default: %i[test]
|
18
|
+
|
19
|
+
require "rdoc/task"
|
20
|
+
|
21
|
+
RDoc::Task.new do |rdoc|
|
22
|
+
rdoc.main = "REDME.md"
|
23
|
+
rdoc.rdoc_files.include("lib/**/*.rb", "README.md", "syntax.ja.rdoc")
|
24
|
+
end
|
data/lib/xr.rb
ADDED
@@ -0,0 +1,128 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Xr
|
4
|
+
VERSION = "0.1.0"
|
5
|
+
Error = Class.new(StandardError)
|
6
|
+
|
7
|
+
UNMATCHABLE = Regexp.union([]) # :nodoc:
|
8
|
+
|
9
|
+
def initialize(greedy: true)
|
10
|
+
@greedy = greedy
|
11
|
+
end
|
12
|
+
|
13
|
+
def unmatchable = UNMATCHABLE
|
14
|
+
|
15
|
+
def _ *exprs
|
16
|
+
case exprs
|
17
|
+
in [] then Xr.new
|
18
|
+
in Integer => num, *exprs
|
19
|
+
expr = Xr { exprs }
|
20
|
+
Regexp.new("(#{expr}){#{num}}")
|
21
|
+
in Range => range, *exprs
|
22
|
+
expr = Xr { exprs }
|
23
|
+
Regexp.new("(#{expr}){#{range.begin},#{range.end}}")
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def _? *exprs, &block
|
28
|
+
block_given? and return Xr.new(greedy: false).instance_eval(&block)
|
29
|
+
case exprs
|
30
|
+
in [] then Xr.new(greedy: false)
|
31
|
+
else
|
32
|
+
expr = try_to_s(Xr { exprs })
|
33
|
+
Regexp.new(@greedy ? "(#{expr})?" : "(#{expr})??")
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def | other
|
38
|
+
@expr = @expr ? Regexp.union([@expr, other].map { parse(_1) }) : parse(other)
|
39
|
+
self
|
40
|
+
end
|
41
|
+
|
42
|
+
def *(*exprs)
|
43
|
+
expr = try_to_s(Xr { exprs })
|
44
|
+
Regexp.new(@greedy ? "(#{expr})*" : "(#{expr})*?")
|
45
|
+
end
|
46
|
+
|
47
|
+
def +(*exprs)
|
48
|
+
expr = try_to_s(Xr { exprs })
|
49
|
+
Regexp.new(@greedy ? "(#{expr})+" : "(#{expr})+?")
|
50
|
+
end
|
51
|
+
|
52
|
+
def any(*sets)
|
53
|
+
set = sets.map { try_to_s(_1) }.join
|
54
|
+
Regexp.new("[#{set}]")
|
55
|
+
end
|
56
|
+
|
57
|
+
def except(*sets)
|
58
|
+
set = sets.map { try_to_s(_1) }.join
|
59
|
+
Regexp.new("[^#{set}]")
|
60
|
+
end
|
61
|
+
|
62
|
+
def group(name = nil, &block)
|
63
|
+
expr = Xr.new.instance_eval(&block)
|
64
|
+
if name
|
65
|
+
valid_name?(name) or raise Error, "invalid group name"
|
66
|
+
Regexp.new("(?<#{name}>#{expr})")
|
67
|
+
else Regexp.new("(#{expr})")
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def backref(name)
|
72
|
+
case name
|
73
|
+
in Integer then ->(pre) { Regexp.new("#{pre}\\#{name}") }
|
74
|
+
in Symbol | String
|
75
|
+
name = name.to_s
|
76
|
+
valid_name?(name) or raise Error, "invalid group name"
|
77
|
+
->(pre) { Regexp.new("#{pre}\\k<#{name}>") }
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def valid_name?(name) = name.match?(/ \A [a-zA-Z_][a-zA-Z0-9_]* \z /x) # :nodoc:
|
82
|
+
|
83
|
+
def try_to_s(expr) # :nodoc:
|
84
|
+
case expr
|
85
|
+
in String then Regexp.escape(expr)
|
86
|
+
in Regexp then expr.to_s
|
87
|
+
in Range then "#{expr.begin}-#{expr.end}"
|
88
|
+
in Proc then expr
|
89
|
+
in Symbol then parse(expr).to_s
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
def parse(expr) # :nodoc:
|
94
|
+
case expr
|
95
|
+
in String then Regexp.new(Regexp.escape(expr))
|
96
|
+
in Regexp | Proc then expr
|
97
|
+
in Xr then expr.instance_variable_get(:@expr)
|
98
|
+
in Array
|
99
|
+
pre, *exprs = *expr
|
100
|
+
pre = try_to_s(pre).to_s
|
101
|
+
exprs.each do |exp|
|
102
|
+
if exp.respond_to?(:call)
|
103
|
+
pre = exp.(pre).to_s
|
104
|
+
else
|
105
|
+
pre = "#{pre}#{try_to_s(exp)}"
|
106
|
+
end
|
107
|
+
end
|
108
|
+
Regexp.new(pre)
|
109
|
+
in :not_newline then Regexp.new(".")
|
110
|
+
in :anychar then Regexp.new('[^a-z]')
|
111
|
+
in :line_start then /^/
|
112
|
+
in :line_end then /$/
|
113
|
+
in :string_start then /\A/
|
114
|
+
in :string_end then /\z/
|
115
|
+
in :word_boundary then /\b/
|
116
|
+
in :alpha | :alnum | :digit | :xdigit | :cntrl | :blank | :space |
|
117
|
+
:lower | :upper |
|
118
|
+
:graph | :print | :punct | :word | :ascii
|
119
|
+
Regexp.new("[[:#{expr}:]]")
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
def Xr(&block)
|
125
|
+
xr = ::Xr.new
|
126
|
+
pat = xr.parse(xr.instance_eval(&block))
|
127
|
+
pat.respond_to?(:call) ? pat.() : pat
|
128
|
+
end
|
data/manifest.scm
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
(specifications->manifest (list "ruby@3.1" "ruby-rubocop" "ruby-rubocop-rake"))
|
data/sig/xr.rbs
ADDED
data/syntax.ja.rdoc
ADDED
@@ -0,0 +1,219 @@
|
|
1
|
+
= Xrの構文
|
2
|
+
|
3
|
+
Xrは正規表現を構築する領域特化言語です。
|
4
|
+
普通、 Xr メソッドを使います。
|
5
|
+
このメソッドはブロックを受け付け、その中で表現します。
|
6
|
+
|
7
|
+
構文の原則はいくつかあります。
|
8
|
+
|
9
|
+
まず、記号での表現と英語の単語があるとき、前者を優先します。
|
10
|
+
単語だと、利用者が使いたいものと衝突してしまうのではないか、という懸念からです。
|
11
|
+
これを、<i>記号優先の原則</i>とします。
|
12
|
+
|
13
|
+
次に、ある意味を表す記法は、基本的に1つ提供します。
|
14
|
+
様々には用意しません。
|
15
|
+
これは、既存の記法で席が埋まってしまい、将来、新しく意味を定義できなくなることを防ぐためです。
|
16
|
+
これを、<i>単一記法の原則</i>とします。
|
17
|
+
|
18
|
+
3つ目に、対の意味を持たせたい記法でも、一般に対とされる記号を使うとは限りません。
|
19
|
+
|
20
|
+
Xr により作成された正規表現は、できるだけ、評価の回数が少なくなるようにしてください。
|
21
|
+
理想的には、トップレベルの定数として定義し、1度だけ評価することです。
|
22
|
+
Xr は特に効率を意識していない実装であるため、毎回、表現を構築すると、時間が掛かります。
|
23
|
+
|
24
|
+
== 便覧
|
25
|
+
|
26
|
+
Xr のブロック内に何らかの直値があると、それを記法として解釈しようとします。
|
27
|
+
|
28
|
+
正規表現はその表現自体です。
|
29
|
+
|
30
|
+
Xr { /a/ }
|
31
|
+
|
32
|
+
文字列そのままは、それ自体の正規表現です。
|
33
|
+
つまり、正規表現で使われる、特殊な文字は効果がありません。
|
34
|
+
額面通りの文字列にするため、ブロックが文字列に評価されたときは、正規表現のエスケープを施します。
|
35
|
+
|
36
|
+
Xr { "何らかの文字列" }
|
37
|
+
|
38
|
+
しかし、最後の式だけが使われることに注意してください。
|
39
|
+
|
40
|
+
Xr { "これは無視される"; "これは使われる" }
|
41
|
+
|
42
|
+
配列は、要素を順番に連結したものです。
|
43
|
+
|
44
|
+
Xr { ["何らかの", "文字列"] }
|
45
|
+
|
46
|
+
+seq+ (+sequence+) というメソッドを提供することも考えました。
|
47
|
+
例えば、<tt>Xr { seq "何らかの", "文字列" }</tt>のようになります。
|
48
|
+
また、空であれば単に<tt>Xr { seq }</tt>です。
|
49
|
+
しかしこれは記号優先の原則により、取り下げました。
|
50
|
+
|
51
|
+
また、<tt>&</tt>を2項演算子にすることも考えました。
|
52
|
+
しかしこれは、単一記法の原則により、取り下げました。
|
53
|
+
|
54
|
+
Emacs Lispのrxパッケージ(以下 +rx.el+ とします)には、 +:+ や +and+ があります。
|
55
|
+
|
56
|
+
特定のシンボルは記法の中に書けます。
|
57
|
+
|
58
|
+
+:not_newline+ は改行でない任意の1文字です。
|
59
|
+
|
60
|
+
Xr { :not_newline }
|
61
|
+
|
62
|
+
以降のシンボルの例は省略します。
|
63
|
+
|
64
|
+
+nonl+ の追加は、単一記法の原則により行いません。
|
65
|
+
+rx.el+ では +not-newline+ があります。
|
66
|
+
|
67
|
+
+:anychar+ は任意の1文字です。
|
68
|
+
+anything+ の追加は、単一記法の原則により行いません。
|
69
|
+
+rx.el+ では +anychar+ があります。
|
70
|
+
|
71
|
+
+alpha+, +alnum+, +digit+, +xdigit+, +cntrl+, +blank+, +space+, +lower+, +upper+, +graph+, +print+, +punct+, +word+, +ascii+ は文字クラスです。
|
72
|
+
+rx.el+ にも同名のものがあります。
|
73
|
+
|
74
|
+
ゼロ幅断定として、 +line_start+, +line_end+, +string_start+, +string_end+, +word_boundary+ があります。
|
75
|
+
+rx.el+ には +line-start+, +line-end+, +string-start+, +string-end+, +word-boundary+ があります。
|
76
|
+
|
77
|
+
文字集合は +any+ で表現されます。
|
78
|
+
|
79
|
+
Xr { any 'ab' }
|
80
|
+
Xr { any 'a', 'b' }
|
81
|
+
|
82
|
+
含まれる文字はエスケープされるのでお気を付けください。
|
83
|
+
以下では、ハイフンを特別な意味に解釈しません。
|
84
|
+
|
85
|
+
Xr { any 'a-b' }
|
86
|
+
|
87
|
+
範囲も使えます。
|
88
|
+
|
89
|
+
Xr { any 'a'..'c' }
|
90
|
+
|
91
|
+
+char+ や +in+ は、統一記号の原則から取り下げました。
|
92
|
+
Set も考えられますが、逆に指定した文字集合以外の場合を考えて取り下げました。
|
93
|
+
|
94
|
+
+rx.el+ には +any+ があります。
|
95
|
+
ただし、そちらでは<tt>-</tt>を範囲として解釈します。
|
96
|
+
|
97
|
+
除外する文字集合は +except+ で表します。
|
98
|
+
|
99
|
+
Xr { except 'a' }
|
100
|
+
|
101
|
+
+rx.el+ では +not+ があります。
|
102
|
+
|
103
|
+
グループは +group+ で囲みます。
|
104
|
+
|
105
|
+
Xr { group { 'a' } }
|
106
|
+
|
107
|
+
また、グループ名を付けられます。
|
108
|
+
|
109
|
+
Xr { group(:b) { 'b' } }
|
110
|
+
|
111
|
+
後方参照は +backref+ です。
|
112
|
+
|
113
|
+
Xr { [group { 'a' }, backref(1)] }
|
114
|
+
Xr { [group(:b) { 'a' }, backref(:b)] }
|
115
|
+
|
116
|
+
=== 選択
|
117
|
+
|
118
|
+
選択は<tt>|</tt>で表します。
|
119
|
+
|
120
|
+
Xr { _ | 'a' | 'b' }
|
121
|
+
|
122
|
+
<tt>_ |</tt>は<tt>|</tt>で連結するためのおまじないと見れます。
|
123
|
+
空の選択は、合致不能を意味するのですが、それについては後述します。
|
124
|
+
|
125
|
+
+choice+ を使うことも考えられましたが、記号優先の原則により、取り下げました。
|
126
|
+
|
127
|
+
+rx.el+ には +or+ があります。
|
128
|
+
|
129
|
+
合致不能は +unmatchable+ です。
|
130
|
+
|
131
|
+
Xr { unmatchable }
|
132
|
+
|
133
|
+
+choice+ があれば、<tt>Xr { choice }</tt>と書けますが、意図が伝わりにくそうです。
|
134
|
+
+_!+ という記号も考えましたが、そこまで直感的でないように思われました。
|
135
|
+
そもそも、合致不能が頻用されるとは思えなかったため、記号への割り当てはしていません。
|
136
|
+
|
137
|
+
=== 1回か0回
|
138
|
+
|
139
|
+
1回か0回合致するには、<tt>_?</tt>とします。
|
140
|
+
対象のパターンは位置引数に渡します。
|
141
|
+
|
142
|
+
Xr { _? 'a' }
|
143
|
+
|
144
|
+
+zero_or_one+ や +optional+ や +opt+ も考えましたが、記号優先の原則により取り下げました。
|
145
|
+
|
146
|
+
最小合致で1回か0回合致するには、さらに<tt>_?</tt>のブロックで囲みます。
|
147
|
+
|
148
|
+
Xr { _? { _? 'a' } }
|
149
|
+
|
150
|
+
最小合致の記法は提供していますが、最大合致の記法は提供していません。
|
151
|
+
+maximum_match+ を提供することも考えています。
|
152
|
+
|
153
|
+
=== 0回以上
|
154
|
+
|
155
|
+
0回以上の繰り返しは<tt>_ *</tt>を使います。
|
156
|
+
|
157
|
+
Xr { _ * 'a' }
|
158
|
+
|
159
|
+
+zero_or_more+ は記号優先の原則により取り下げました。
|
160
|
+
|
161
|
+
+rx.el+ では<tt>0+</tt>があります。
|
162
|
+
|
163
|
+
最小合致の0回以上の繰り返しは、<tt>_? *</tt>とします。
|
164
|
+
|
165
|
+
Xr { _? * 'a' }
|
166
|
+
|
167
|
+
+minimal_match+ も考えましたが、記号優先の原則により取り下げました。
|
168
|
+
|
169
|
+
=== 1回以上
|
170
|
+
|
171
|
+
1回以上の繰り返しは<tt>_ +</tt>を使います。
|
172
|
+
|
173
|
+
Xr { _ + 'a' }
|
174
|
+
|
175
|
+
+one_or_more+ は記号優先の原則により取り下げました。
|
176
|
+
|
177
|
+
+rx.el+ では<tt>1+</tt>があります。
|
178
|
+
|
179
|
+
最小合致の1回以上の繰り返しは、<tt>_? +</tt>とします。
|
180
|
+
|
181
|
+
Xr { _? + 'a' }
|
182
|
+
|
183
|
+
=== 指定回数
|
184
|
+
|
185
|
+
指定回数の繰り返しは<tt>_ <i>自然数</i>, ...</tt>です。
|
186
|
+
|
187
|
+
Xr { _ 3, 'a' }
|
188
|
+
|
189
|
+
+repeat+ は記号優先の原則により取り下げました。
|
190
|
+
<tt>_<i>自然数</i> = <i>式</i></tt>は、式を返すのでうまくいきません。
|
191
|
+
|
192
|
+
+rx.el+ には<tt>= <i>自然数</i></tt>や<tt>**</tt>があります。
|
193
|
+
|
194
|
+
回数の上限も指定できます。
|
195
|
+
|
196
|
+
Xr { _ 3..5, 'a' }
|
197
|
+
|
198
|
+
<tt>Xr { _ 3, 5, 'a' }</tt>も考えられましたが、単一記法の原則により取り下げました。
|
199
|
+
|
200
|
+
下限を指定することもできます。
|
201
|
+
|
202
|
+
Xr { _ 3.., 'a' }
|
203
|
+
|
204
|
+
+or_more+ は記号優先の原則により取り下げました。
|
205
|
+
|
206
|
+
+rx.el+ には<tt>>=</tt>があります。
|
207
|
+
|
208
|
+
上限を指定することもできます。
|
209
|
+
|
210
|
+
Xr { _ (..3), 'a' }
|
211
|
+
|
212
|
+
== 実装予定の記法
|
213
|
+
|
214
|
+
+rx.el+ の +intersection+ は、文字集合の共通部分に合致するパターンです。
|
215
|
+
また、構文と分類がいろいろあります。
|
216
|
+
+word-start+ は単語の始めです。
|
217
|
+
+word-end+ は単語の終わりです。
|
218
|
+
+symbol-start+ はシンボルの始めです。
|
219
|
+
+symbol-end+ はシンボルの終わりです。
|
metadata
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: xr
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- gemmaro
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-09-21 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: xr gem is a domain specific language for the regular expression (Regexp)
|
14
|
+
like the rx package written in Emacs Lisp.
|
15
|
+
email:
|
16
|
+
- gemmaro.dev@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- ".envrc"
|
22
|
+
- ".rubocop.yml"
|
23
|
+
- CHANGELOG.md
|
24
|
+
- LICENSE
|
25
|
+
- README.md
|
26
|
+
- Rakefile
|
27
|
+
- lib/xr.rb
|
28
|
+
- manifest.scm
|
29
|
+
- sig/xr.rbs
|
30
|
+
- syntax.ja.rdoc
|
31
|
+
homepage:
|
32
|
+
licenses:
|
33
|
+
- Apache-2.0
|
34
|
+
metadata: {}
|
35
|
+
post_install_message:
|
36
|
+
rdoc_options: []
|
37
|
+
require_paths:
|
38
|
+
- lib
|
39
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '3.1'
|
44
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
requirements: []
|
50
|
+
rubygems_version: 3.3.26
|
51
|
+
signing_key:
|
52
|
+
specification_version: 4
|
53
|
+
summary: Regexp DSL
|
54
|
+
test_files: []
|