arugula 0.2.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/.gitignore +11 -0
- data/.rspec +3 -0
- data/.rubocop.yml +18 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +49 -0
- data/Gemfile +8 -0
- data/Gemfile.lock +47 -0
- data/LICENSE.txt +21 -0
- data/README.md +41 -0
- data/Rakefile +10 -0
- data/arugula.gemspec +26 -0
- data/bin/console +14 -0
- data/bin/rake +16 -0
- data/bin/rspec +16 -0
- data/bin/rubocop +16 -0
- data/bin/setup +8 -0
- data/lib/arugula.rb +24 -0
- data/lib/arugula/parser.rb +89 -0
- data/lib/arugula/parts.rb +206 -0
- data/lib/arugula/version.rb +4 -0
- metadata +106 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d54840cc707cfd54c866d94b902c4a6a3ac1ec01
|
4
|
+
data.tar.gz: 918b25030d62dbbff5081f04dd71adbfb3d9222c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e7a0b0c985aa8c3b100df17b3cbc2edfcecf767fb8d1e675705324bfbcc2ae38de0e9cc0044931ffc2b65a4340c9d2c517650d0aea641c96eb35d588ea2c6f9f
|
7
|
+
data.tar.gz: e2960e17711fb417fef5b5b46065639cde59297b60c824cf2068b15b9b72041b699616be45e68b4814814fcb788c24eff91888e6ef4a357e85e39fa1ed2bfe43
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# Style
|
2
|
+
|
3
|
+
Style/Documentation:
|
4
|
+
Enabled: false
|
5
|
+
|
6
|
+
Style/TrailingCommaInLiteral:
|
7
|
+
EnforcedStyleForMultiline: comma
|
8
|
+
|
9
|
+
# Metrics
|
10
|
+
|
11
|
+
Metrics/AbcSize:
|
12
|
+
Enabled: false
|
13
|
+
Metrics/CyclomaticComplexity:
|
14
|
+
Enabled: false
|
15
|
+
Metrics/MethodLength:
|
16
|
+
Enabled: false
|
17
|
+
Metrics/PerceivedComplexity:
|
18
|
+
Enabled: false
|
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, and in the interest of
|
4
|
+
fostering an open and welcoming community, we pledge to respect all people who
|
5
|
+
contribute through reporting issues, posting feature requests, updating
|
6
|
+
documentation, submitting pull requests or patches, and other activities.
|
7
|
+
|
8
|
+
We are committed to making participation in this project a harassment-free
|
9
|
+
experience for everyone, regardless of level of experience, gender, gender
|
10
|
+
identity and expression, sexual orientation, disability, personal appearance,
|
11
|
+
body size, race, ethnicity, age, religion, or nationality.
|
12
|
+
|
13
|
+
Examples of unacceptable behavior by participants include:
|
14
|
+
|
15
|
+
* The use of sexualized language or imagery
|
16
|
+
* Personal attacks
|
17
|
+
* Trolling or insulting/derogatory comments
|
18
|
+
* Public or private harassment
|
19
|
+
* Publishing other's private information, such as physical or electronic
|
20
|
+
addresses, without explicit permission
|
21
|
+
* Other unethical or unprofessional conduct
|
22
|
+
|
23
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
24
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
25
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
26
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
27
|
+
threatening, offensive, or harmful.
|
28
|
+
|
29
|
+
By adopting this Code of Conduct, project maintainers commit themselves to
|
30
|
+
fairly and consistently applying these principles to every aspect of managing
|
31
|
+
this project. Project maintainers who do not follow or enforce the Code of
|
32
|
+
Conduct may be permanently removed from the project team.
|
33
|
+
|
34
|
+
This code of conduct applies both within project spaces and in public spaces
|
35
|
+
when an individual is representing the project or its community.
|
36
|
+
|
37
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
38
|
+
reported by contacting a project maintainer at segiddins@segiddins.me. All
|
39
|
+
complaints will be reviewed and investigated and will result in a response that
|
40
|
+
is deemed necessary and appropriate to the circumstances. Maintainers are
|
41
|
+
obligated to maintain confidentiality with regard to the reporter of an
|
42
|
+
incident.
|
43
|
+
|
44
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
45
|
+
version 1.3.0, available at
|
46
|
+
[http://contributor-covenant.org/version/1/3/0/][version]
|
47
|
+
|
48
|
+
[homepage]: http://contributor-covenant.org
|
49
|
+
[version]: http://contributor-covenant.org/version/1/3/0/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
arugula (0.2.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
ast (2.2.0)
|
10
|
+
diff-lcs (1.2.5)
|
11
|
+
parser (2.3.0.2)
|
12
|
+
ast (~> 2.2)
|
13
|
+
powerpack (0.1.1)
|
14
|
+
rainbow (2.1.0)
|
15
|
+
rake (10.5.0)
|
16
|
+
rspec (3.4.0)
|
17
|
+
rspec-core (~> 3.4.0)
|
18
|
+
rspec-expectations (~> 3.4.0)
|
19
|
+
rspec-mocks (~> 3.4.0)
|
20
|
+
rspec-core (3.4.2)
|
21
|
+
rspec-support (~> 3.4.0)
|
22
|
+
rspec-expectations (3.4.0)
|
23
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
24
|
+
rspec-support (~> 3.4.0)
|
25
|
+
rspec-mocks (3.4.1)
|
26
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
27
|
+
rspec-support (~> 3.4.0)
|
28
|
+
rspec-support (3.4.1)
|
29
|
+
rubocop (0.36.0)
|
30
|
+
parser (>= 2.3.0.0, < 3.0)
|
31
|
+
powerpack (~> 0.1)
|
32
|
+
rainbow (>= 1.99.1, < 3.0)
|
33
|
+
ruby-progressbar (~> 1.7)
|
34
|
+
ruby-progressbar (1.7.5)
|
35
|
+
|
36
|
+
PLATFORMS
|
37
|
+
ruby
|
38
|
+
|
39
|
+
DEPENDENCIES
|
40
|
+
arugula!
|
41
|
+
bundler (~> 1.11)
|
42
|
+
rake (~> 10.5)
|
43
|
+
rspec (~> 3.4)
|
44
|
+
rubocop
|
45
|
+
|
46
|
+
BUNDLED WITH
|
47
|
+
1.11.2
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 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,41 @@
|
|
1
|
+
# Arugula
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/arugula`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'arugula'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install arugula
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
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.
|
30
|
+
|
31
|
+
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).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/arugula. 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.
|
36
|
+
|
37
|
+
|
38
|
+
## License
|
39
|
+
|
40
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
41
|
+
|
data/Rakefile
ADDED
data/arugula.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'arugula/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'arugula'
|
8
|
+
spec.version = Arugula::VERSION
|
9
|
+
spec.authors = ['Samuel Giddins']
|
10
|
+
spec.email = ['segiddins@segiddins.me']
|
11
|
+
|
12
|
+
spec.summary = 'A naïve regular expression implementation'
|
13
|
+
spec.homepage = 'https://github.com/segiddins/arugula'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`
|
17
|
+
.split("\x0")
|
18
|
+
.reject { |f| f.match(%r{^(test|spec|features)/}) }
|
19
|
+
spec.bindir = 'exe'
|
20
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
21
|
+
spec.require_paths = ['lib']
|
22
|
+
|
23
|
+
spec.add_development_dependency 'bundler', '~> 1.11'
|
24
|
+
spec.add_development_dependency 'rake', '~> 10.5'
|
25
|
+
spec.add_development_dependency 'rspec', '~> 3.4'
|
26
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'arugula'
|
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
|
data/bin/rake
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#
|
3
|
+
# This file was generated by Bundler.
|
4
|
+
#
|
5
|
+
# The application 'rake' is installed as part of a gem, and
|
6
|
+
# this file is here to facilitate running it.
|
7
|
+
#
|
8
|
+
|
9
|
+
require 'pathname'
|
10
|
+
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile',
|
11
|
+
Pathname.new(__FILE__).realpath)
|
12
|
+
|
13
|
+
require 'rubygems'
|
14
|
+
require 'bundler/setup'
|
15
|
+
|
16
|
+
load Gem.bin_path('rake', 'rake')
|
data/bin/rspec
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#
|
3
|
+
# This file was generated by Bundler.
|
4
|
+
#
|
5
|
+
# The application 'rspec' is installed as part of a gem, and
|
6
|
+
# this file is here to facilitate running it.
|
7
|
+
#
|
8
|
+
|
9
|
+
require 'pathname'
|
10
|
+
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile',
|
11
|
+
Pathname.new(__FILE__).realpath)
|
12
|
+
|
13
|
+
require 'rubygems'
|
14
|
+
require 'bundler/setup'
|
15
|
+
|
16
|
+
load Gem.bin_path('rspec-core', 'rspec')
|
data/bin/rubocop
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#
|
3
|
+
# This file was generated by Bundler.
|
4
|
+
#
|
5
|
+
# The application 'rubocop' is installed as part of a gem, and
|
6
|
+
# this file is here to facilitate running it.
|
7
|
+
#
|
8
|
+
|
9
|
+
require 'pathname'
|
10
|
+
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile',
|
11
|
+
Pathname.new(__FILE__).realpath)
|
12
|
+
|
13
|
+
require 'rubygems'
|
14
|
+
require 'bundler/setup'
|
15
|
+
|
16
|
+
load Gem.bin_path('rubocop', 'rubocop')
|
data/bin/setup
ADDED
data/lib/arugula.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
class Arugula
|
3
|
+
require 'arugula/version'
|
4
|
+
|
5
|
+
autoload :Parser, 'arugula/parser'
|
6
|
+
|
7
|
+
def initialize(pattern)
|
8
|
+
@root = Parser.new(pattern).parse!
|
9
|
+
end
|
10
|
+
|
11
|
+
def match?(str)
|
12
|
+
index = 0
|
13
|
+
loop do
|
14
|
+
match, = @root.match(str, index)
|
15
|
+
return index if match
|
16
|
+
index += 1
|
17
|
+
return if index > str.size
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def to_s
|
22
|
+
"/#{@root}/"
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
require 'arugula/parts'
|
2
|
+
|
3
|
+
class Arugula
|
4
|
+
class Parser
|
5
|
+
attr_reader :pattern
|
6
|
+
def initialize(str)
|
7
|
+
@pattern = str.dup
|
8
|
+
@states = [AndPart.new]
|
9
|
+
end
|
10
|
+
|
11
|
+
def state
|
12
|
+
@states.reverse_each.find { |s| s.respond_to?(:parts) }
|
13
|
+
end
|
14
|
+
|
15
|
+
def parse!
|
16
|
+
consume until pattern.empty?
|
17
|
+
@states.first
|
18
|
+
end
|
19
|
+
|
20
|
+
Part.all.each do |part|
|
21
|
+
type = part.type
|
22
|
+
define_method(:"#{type}_type?") do
|
23
|
+
state && state.class.type == type
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def consume
|
28
|
+
tok = pattern.slice!(0)
|
29
|
+
peek = pattern.chr
|
30
|
+
if tok.nil?
|
31
|
+
fail 'shouldnt happen'
|
32
|
+
elsif tok == '['
|
33
|
+
push_part(:characterclass)
|
34
|
+
elsif tok == '-' &&
|
35
|
+
characterclass_type? &&
|
36
|
+
state.parts.last.class.type == :literal &&
|
37
|
+
peek != ']'
|
38
|
+
literal = state.parts.pop
|
39
|
+
push_part(:range, literal.literal, peek)
|
40
|
+
pattern.slice!(0)
|
41
|
+
elsif tok == ']' && characterclass_type?
|
42
|
+
pop_part
|
43
|
+
elsif tok == '$'
|
44
|
+
push_part(:eol)
|
45
|
+
elsif tok == '^'
|
46
|
+
push_part(:sol)
|
47
|
+
elsif tok == '\\'
|
48
|
+
tok = pattern.slice!(0)
|
49
|
+
case tok
|
50
|
+
when nil
|
51
|
+
fail 'unterminated escape sequence'
|
52
|
+
when *MetacharacterPart::MATCHERS.keys.map(&:to_s)
|
53
|
+
push_part(:metacharacter, tok)
|
54
|
+
else
|
55
|
+
push_part(:literal, tok)
|
56
|
+
end
|
57
|
+
elsif characterclass_type?
|
58
|
+
push_part(:literal, tok)
|
59
|
+
elsif tok == '.'
|
60
|
+
push_part(:dot)
|
61
|
+
elsif tok == '*'
|
62
|
+
wrap_state(:star)
|
63
|
+
elsif tok == '+'
|
64
|
+
wrap_state(:plus)
|
65
|
+
else
|
66
|
+
push_part(:literal, tok)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def push_part(name, *content)
|
71
|
+
part_class = Part.all.find { |p| p.type == name }
|
72
|
+
fail "Unknown part type #{name}" unless part_class
|
73
|
+
part = part_class.new(*content)
|
74
|
+
state.parts << part
|
75
|
+
@states << part unless name == :literal
|
76
|
+
end
|
77
|
+
|
78
|
+
def wrap_state(name)
|
79
|
+
wrapped = Part.all.find { |p| p.type == name }.new(state.parts.pop)
|
80
|
+
state.parts << wrapped
|
81
|
+
@states << wrapped
|
82
|
+
end
|
83
|
+
|
84
|
+
def pop_part
|
85
|
+
@states.pop until @states.last.respond_to?(:parts)
|
86
|
+
@states.pop
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
@@ -0,0 +1,206 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
class Arugula
|
3
|
+
class Part
|
4
|
+
def self.all
|
5
|
+
@all ||= []
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.inherited(subclass)
|
9
|
+
all << subclass
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.type
|
13
|
+
@type ||= name.split('::').last.downcase.chomp('part').to_sym
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
class LiteralPart < Part
|
18
|
+
attr_accessor :literal
|
19
|
+
def initialize(literal)
|
20
|
+
@literal = literal
|
21
|
+
end
|
22
|
+
|
23
|
+
def to_s
|
24
|
+
literal.gsub('\\', '\\\\')
|
25
|
+
end
|
26
|
+
|
27
|
+
def match(str, index)
|
28
|
+
length = literal.size
|
29
|
+
matches = str[index, length] == literal
|
30
|
+
[matches, index + (matches ? length : 0)]
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
class AndPart < Part
|
35
|
+
attr_accessor :parts
|
36
|
+
def initialize
|
37
|
+
@parts = []
|
38
|
+
end
|
39
|
+
|
40
|
+
def to_s
|
41
|
+
parts.join
|
42
|
+
end
|
43
|
+
|
44
|
+
def match(str, index)
|
45
|
+
parts.each do |part|
|
46
|
+
match, index = part.match(str, index)
|
47
|
+
return false, index unless match
|
48
|
+
end
|
49
|
+
[true, index]
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
module MatchAny
|
54
|
+
attr_accessor :parts
|
55
|
+
def initialize
|
56
|
+
@parts = []
|
57
|
+
end
|
58
|
+
|
59
|
+
def match(str, index)
|
60
|
+
parts.each do |part|
|
61
|
+
match, match_index = part.match(str, index)
|
62
|
+
return true, match_index if match
|
63
|
+
end
|
64
|
+
[false, index]
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
class OrPart < Part
|
69
|
+
include MatchAny
|
70
|
+
def to_s
|
71
|
+
parts.join '|'
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
class CharacterClassPart < Part
|
76
|
+
include MatchAny
|
77
|
+
def to_s
|
78
|
+
"[#{parts.join}]"
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
class RangePart < Part
|
83
|
+
def initialize(start, final)
|
84
|
+
@range = start..final
|
85
|
+
end
|
86
|
+
|
87
|
+
def to_s
|
88
|
+
"#{@range.begin}-#{@range.end}"
|
89
|
+
end
|
90
|
+
|
91
|
+
def match(str, index)
|
92
|
+
matches = @range.member?(str[index])
|
93
|
+
[matches, index + (matches ? 1 : 0)]
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
class MetacharacterPart < Part
|
98
|
+
MATCHERS = {
|
99
|
+
A: ->(_str, index) { index == 0 },
|
100
|
+
d: ->(str, index) { ('0'..'9').member?(str[index]) },
|
101
|
+
s: ->(str, index) { [' ', "\t"].include?(str[index]) },
|
102
|
+
S: ->(str, index) { ![' ', "\t"].include?(str[index]) },
|
103
|
+
}.freeze
|
104
|
+
|
105
|
+
OFFSETS = begin
|
106
|
+
offsets = {
|
107
|
+
A: 0,
|
108
|
+
}
|
109
|
+
offsets.default = 1
|
110
|
+
offsets.freeze
|
111
|
+
end
|
112
|
+
|
113
|
+
def initialize(metachar)
|
114
|
+
@metachar = metachar.to_sym
|
115
|
+
end
|
116
|
+
|
117
|
+
def match(str, index)
|
118
|
+
[MATCHERS[@metachar][str, index], index + OFFSETS[@metachar]]
|
119
|
+
end
|
120
|
+
|
121
|
+
def to_s
|
122
|
+
"\\#{@metachar}"
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
class MatchPart < Part
|
127
|
+
attr_accessor :parts
|
128
|
+
def initialize
|
129
|
+
@parts = AndPart.new
|
130
|
+
end
|
131
|
+
|
132
|
+
def to_s
|
133
|
+
"(#{parts.join})"
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
class EOLPart < Part
|
138
|
+
def to_s
|
139
|
+
'$'
|
140
|
+
end
|
141
|
+
|
142
|
+
def match(str, index)
|
143
|
+
matches = str[index] == "\n" || index == str.size
|
144
|
+
return true, index + 1 if matches
|
145
|
+
[false, index]
|
146
|
+
end
|
147
|
+
end
|
148
|
+
class SOLPart < Part
|
149
|
+
def to_s
|
150
|
+
'^'
|
151
|
+
end
|
152
|
+
|
153
|
+
def match(str, index)
|
154
|
+
matches = (index == 0) || (str[index - 1] == "\n")
|
155
|
+
[matches, index]
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
module Wrapping
|
160
|
+
attr_reader :wrapped
|
161
|
+
def initialize(wrapped)
|
162
|
+
@wrapped = wrapped
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
class StarPart < Part
|
167
|
+
include Wrapping
|
168
|
+
def to_s
|
169
|
+
"#{wrapped}*"
|
170
|
+
end
|
171
|
+
|
172
|
+
def match(str, index)
|
173
|
+
loop do
|
174
|
+
matches, index = wrapped.match(str, index)
|
175
|
+
return true, index unless matches
|
176
|
+
end
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
180
|
+
class PlusPart < Part
|
181
|
+
include Wrapping
|
182
|
+
def to_s
|
183
|
+
"#{wrapped}+"
|
184
|
+
end
|
185
|
+
|
186
|
+
def match(str, index)
|
187
|
+
has_matched = false
|
188
|
+
loop do
|
189
|
+
matches, index = wrapped.match(str, index)
|
190
|
+
has_matched = true if matches
|
191
|
+
return has_matched, index unless matches
|
192
|
+
end
|
193
|
+
end
|
194
|
+
end
|
195
|
+
|
196
|
+
class DotPart < Part
|
197
|
+
def to_s
|
198
|
+
'.'
|
199
|
+
end
|
200
|
+
|
201
|
+
def match(str, index)
|
202
|
+
matches = index < str.size
|
203
|
+
[matches, index + (matches ? 1 : 0)]
|
204
|
+
end
|
205
|
+
end
|
206
|
+
end
|
metadata
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: arugula
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Samuel Giddins
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-01-30 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: '1.11'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.11'
|
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.5'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.5'
|
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.4'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.4'
|
55
|
+
description:
|
56
|
+
email:
|
57
|
+
- segiddins@segiddins.me
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- ".rubocop.yml"
|
65
|
+
- ".travis.yml"
|
66
|
+
- CODE_OF_CONDUCT.md
|
67
|
+
- Gemfile
|
68
|
+
- Gemfile.lock
|
69
|
+
- LICENSE.txt
|
70
|
+
- README.md
|
71
|
+
- Rakefile
|
72
|
+
- arugula.gemspec
|
73
|
+
- bin/console
|
74
|
+
- bin/rake
|
75
|
+
- bin/rspec
|
76
|
+
- bin/rubocop
|
77
|
+
- bin/setup
|
78
|
+
- lib/arugula.rb
|
79
|
+
- lib/arugula/parser.rb
|
80
|
+
- lib/arugula/parts.rb
|
81
|
+
- lib/arugula/version.rb
|
82
|
+
homepage: https://github.com/segiddins/arugula
|
83
|
+
licenses:
|
84
|
+
- MIT
|
85
|
+
metadata: {}
|
86
|
+
post_install_message:
|
87
|
+
rdoc_options: []
|
88
|
+
require_paths:
|
89
|
+
- lib
|
90
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
96
|
+
requirements:
|
97
|
+
- - ">="
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: '0'
|
100
|
+
requirements: []
|
101
|
+
rubyforge_project:
|
102
|
+
rubygems_version: 2.5.1
|
103
|
+
signing_key:
|
104
|
+
specification_version: 4
|
105
|
+
summary: A naïve regular expression implementation
|
106
|
+
test_files: []
|