regular_expression 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: a27d72d3175c1d50948e9ea99493ccab342b9e7e9b84d87a109257b43c4eeec1
4
+ data.tar.gz: 3adc54f3f5f1a6adf830668d16d0938e5c7f6dc23c04992186fd53983fa53238
5
+ SHA512:
6
+ metadata.gz: fa40e81edf71099e1101f8a4c5111556c41eafde41c1b2f541540dc23ef019b40cc56d332f64632f1b688a7147a5a4bccb87d89dc42de8f9cc9e6eacee5c23da
7
+ data.tar.gz: '08f9ad4a3857a9aeace3507d3c8108fc811baa6deb9484faae73d51a69d4ceabb1505c20b3ac3c6a971b2b278ef242bedbec5426084d8706445bba82f15a9dca'
@@ -0,0 +1,6 @@
1
+ version: 2
2
+ updates:
3
+ - package-ecosystem: "bundler"
4
+ directory: "/"
5
+ schedule:
6
+ interval: "daily"
@@ -0,0 +1,36 @@
1
+ name: Main
2
+ on:
3
+ - push
4
+ - pull_request_target
5
+ jobs:
6
+ ci:
7
+ name: CI
8
+ runs-on: ubuntu-latest
9
+ env:
10
+ CI: true
11
+ steps:
12
+ - name: Install graphviz
13
+ run: sudo apt-get install graphviz
14
+ - uses: actions/checkout@master
15
+ - uses: ruby/setup-ruby@v1
16
+ with:
17
+ ruby-version: 3.0
18
+ bundler-cache: true
19
+ - name: Lint and test
20
+ run: |
21
+ bundle exec rake test
22
+ bundle exec rubocop --parallel
23
+ automerge:
24
+ name: AutoMerge
25
+ needs: ci
26
+ runs-on: ubuntu-latest
27
+ if: github.event_name == 'pull_request_target' && github.actor == 'dependabot[bot]'
28
+ steps:
29
+ - uses: actions/github-script@v3
30
+ with:
31
+ script: |
32
+ github.pulls.merge({
33
+ owner: context.payload.repository.owner.login,
34
+ repo: context.payload.repository.name,
35
+ pull_number: context.payload.pull_request.number
36
+ })
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ coverage/
data/.rubocop.yml ADDED
@@ -0,0 +1,42 @@
1
+ AllCops:
2
+ DisplayCopNames: true
3
+ DisplayStyleGuide: true
4
+ NewCops: enable
5
+ SuggestExtensions: false
6
+ TargetRubyVersion: 3.0
7
+ Exclude:
8
+ - "lib/regular_expression/parser.rb"
9
+ - "vendor/**/*"
10
+
11
+ Gemspec/RequiredRubyVersion:
12
+ Enabled: false
13
+
14
+ Metrics:
15
+ Enabled: false
16
+
17
+ Style/AccessorGrouping:
18
+ Enabled: false
19
+
20
+ Style/CombinableLoops:
21
+ Enabled: false
22
+
23
+ Style/Documentation:
24
+ Enabled: false
25
+
26
+ Style/HashEachMethods:
27
+ Enabled: false
28
+
29
+ Style/IfUnlessModifier:
30
+ Enabled: false
31
+
32
+ Style/PerlBackrefs:
33
+ Enabled: false
34
+
35
+ Style/SpecialGlobalVars:
36
+ Enabled: false
37
+
38
+ Style/StringLiterals:
39
+ EnforcedStyle: double_quotes
40
+
41
+ Style/StructInheritance:
42
+ Enabled: false
@@ -0,0 +1,76 @@
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, sex characteristics, gender identity and expression,
9
+ level of experience, education, socio-economic status, nationality, personal
10
+ appearance, race, religion, or sexual identity and 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 kddnewton@gmail.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 https://www.contributor-covenant.org/version/1/4/code-of-conduct.html
72
+
73
+ [homepage]: https://www.contributor-covenant.org
74
+
75
+ For answers to common questions about this code of conduct, see
76
+ https://www.contributor-covenant.org/faq
data/CONTRIBUTING.md ADDED
@@ -0,0 +1,76 @@
1
+ # Contributing
2
+
3
+ Thanks for your interest in contributing to `regular_expression`! Here are a couple of ways that you can contribute:
4
+
5
+ * A good place to start is looking at the existing [open issues](https://github.com/kddnewton/regular_expression/issues) on GitHub. Most of the time they will be labeled with an estimate of their size and scope.
6
+ * We could always use help with better documentation both in code, in this document, and in the README.
7
+ * If you've found a bug or other issue with the code, opening issues with reproduction steps is always appreciated!
8
+
9
+ Below is some background information to get you up and running on what is happening in this gem.
10
+
11
+ ## Background
12
+
13
+ In order to understand your regular expression patterns and compare them against input strings, the source string goes through a couple of transformations.
14
+
15
+ ### 1. Abstract syntax tree
16
+
17
+ First, it passes through `RegularExpression::Parser`, which is a class that is generated using the [racc](https://github.com/ruby/racc) gem. It is a parser that will take your input string and convert it into an abstract syntax tree. The root of that tree is a `RegularExpression::AST::Root`, and each of its children are also within the `RegularExpression::AST` module. The grammar that the AST follows is loosely described below:
18
+
19
+ ```
20
+ root: CARET? expression
21
+
22
+ expression:
23
+ | item+ PIPE expression
24
+ | item+
25
+
26
+ item: group | match | ANCHOR
27
+
28
+ group: LPAREN expression RPAREN quantifier?
29
+
30
+ match: match_item quantifier?
31
+
32
+ match_item:
33
+ | LBRACKET CARET? character_group_item+ RBRACKET
34
+ | CHAR_CLASS
35
+ | CHAR
36
+ | PERIOD
37
+
38
+ character_group_item:
39
+ | CHAR_CLASS
40
+ | CHAR DASH CHAR
41
+ | CHAR
42
+
43
+ quantifier:
44
+ | LBRACE INTEGER COMMA INTEGER RBRACE
45
+ | LBRACE INTEGER COMMA RBRACE
46
+ | LBRACE INTEGER RBRACE
47
+ | STAR
48
+ | PLUS
49
+ | QMARK
50
+ ```
51
+
52
+ ### 2. State machine
53
+
54
+ Second, we take the AST and transform it into a [nondeterministic finite automaton](https://en.wikipedia.org/wiki/Nondeterministic_finite_automaton) (which is an academic way of saying a state machine). Each transition between the states of the state machine consume either 0 or 1 characters from the input string. Everything regarding the state machine is defined under the `RegularExpression::NFA` module.
55
+
56
+ ### 3. Bytecode
57
+
58
+ Third, we take the state machine and convert it into bytecode. This involves walking through each of the state machine's states and generating instructions that our interpreter understands. The various bytecode instructions are defined under the `RegularExpression::Bytecode::Insns` module. For the most part they consist of:
59
+
60
+ * Instructions that push and pop the current string index from the stack to support backtracking.
61
+ * Instructions that function as "guards" in that they assert that things are true (e.g., `\A` saying we're at the beginning of the string).
62
+ * Instructions that conditionally jump (e.g., a pattern with a `[a-zA-Z]` range receiving an `x` character).
63
+ * Instructions that succeed or fail the entire match.
64
+
65
+ Once we have the bytecode in place, we can pass it to our interpreter to execute. At this point we have a functional matching algorithm that will work (albeit somewhat slowly).
66
+
67
+ ### 4. Control flow graph
68
+
69
+ Fourth, if asked we will take the bytecode and convert it into a control flow graph. This graph creates a list of basic blocks (a set of instructions where the first instruction is a jump target). This graph makes it easier to compile our abstract representation of the regular expression pattern because the flow of the matching algorithm becomes explicit on the block boundaries. The code for generating and managing this control flow graph is defined under the `RegularExpression::CFG` module.
70
+
71
+ ### 5. Compilation
72
+
73
+ Fifth, we can take the control flow graph and pass it to one of our compilers. These compilers are responsible for taking the control flow graph and transforming it into a proc that we can call with an input string. The compilers include:
74
+
75
+ * `RegularExpression::Compiler::Ruby` - takes the control flow graph and converts it into a ruby string. To convert the string to a proc, the compiled object calls `eval`.
76
+ * `RegularExpression::Compiler::X86` - takes the control flow graph and uses the `fisk` gem to generate `X86-64` assembly. The assembly returns a function pointer, and the compiled object knows how to convert that into a proc using the `Fiddle` library. The assembled code can be disassembled using the `crabstone` gem.
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "bundler"
6
+ gem "crabstone"
7
+ gem "graphviz"
8
+ gem "minitest"
9
+ gem "rake"
10
+ gem "rubocop"
11
+ gem "simplecov"
12
+
13
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,69 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ regular_expression (0.1.0)
5
+ fisk
6
+ racc
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ ast (2.4.2)
12
+ crabstone (4.0.3)
13
+ ffi
14
+ docile (1.4.0)
15
+ ffi (1.15.3)
16
+ fisk (2.0.0)
17
+ graphviz (1.2.1)
18
+ process-pipeline
19
+ minitest (5.14.4)
20
+ parallel (1.20.1)
21
+ parser (3.0.2.0)
22
+ ast (~> 2.4.1)
23
+ process-group (1.2.3)
24
+ process-terminal (~> 0.2.0)
25
+ process-pipeline (1.0.2)
26
+ process-group
27
+ process-terminal (0.2.0)
28
+ ffi
29
+ racc (1.5.2)
30
+ rainbow (3.0.0)
31
+ rake (13.0.6)
32
+ regexp_parser (2.1.1)
33
+ rexml (3.2.5)
34
+ rubocop (1.18.3)
35
+ parallel (~> 1.10)
36
+ parser (>= 3.0.0.0)
37
+ rainbow (>= 2.2.2, < 4.0)
38
+ regexp_parser (>= 1.8, < 3.0)
39
+ rexml
40
+ rubocop-ast (>= 1.7.0, < 2.0)
41
+ ruby-progressbar (~> 1.7)
42
+ unicode-display_width (>= 1.4.0, < 3.0)
43
+ rubocop-ast (1.7.0)
44
+ parser (>= 3.0.1.1)
45
+ ruby-progressbar (1.11.0)
46
+ simplecov (0.21.2)
47
+ docile (~> 1.1)
48
+ simplecov-html (~> 0.11)
49
+ simplecov_json_formatter (~> 0.1)
50
+ simplecov-html (0.12.3)
51
+ simplecov_json_formatter (0.1.3)
52
+ unicode-display_width (2.0.0)
53
+
54
+ PLATFORMS
55
+ x86_64-darwin-19
56
+ x86_64-linux
57
+
58
+ DEPENDENCIES
59
+ bundler
60
+ crabstone
61
+ graphviz
62
+ minitest
63
+ rake
64
+ regular_expression!
65
+ rubocop
66
+ simplecov
67
+
68
+ BUNDLED WITH
69
+ 2.2.16
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021-present Kevin Newton
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,51 @@
1
+ # RegularExpression
2
+
3
+ [![Build Status](https://github.com/kddeisz/regular_expression/workflows/Main/badge.svg)](https://github.com/kddeisz/regular_expression/actions)
4
+
5
+ A regular expression engine written in Ruby.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem "regular_expression"
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install regular_expression
22
+
23
+ ## Usage
24
+
25
+ To create a regular expression pattern, use:
26
+
27
+ ```ruby
28
+ pattern = RegularExpression::Pattern.new("ab?c")
29
+ ```
30
+
31
+ Patterns can be queried for whether or not they match a test string, as in:
32
+
33
+ ```ruby
34
+ pattern.match?("abc") # => true
35
+ pattern.match?("ac") # => true
36
+ pattern.match?("ab") # => false
37
+ ```
38
+
39
+ ## Development
40
+
41
+ After checking out the repo, run `bundle install` to install dependencies. Then, run `bundle exec rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
42
+
43
+ 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).
44
+
45
+ ## Contributing
46
+
47
+ Bug reports and pull requests are welcome on GitHub at https://github.com/kddeisz/regular_expression. For information about how to contribute to the development of this gem, see the [CONTRIBUTING.md](CONTRIBUTING.md) document.
48
+
49
+ ## License
50
+
51
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,18 @@
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
+ file "lib/regular_expression/parser.rb" => "lib/regular_expression/parser.y" do
13
+ `bundle exec racc lib/regular_expression/parser.y -o lib/regular_expression/parser.rb`
14
+ end
15
+
16
+ Rake::Task["test"].enhance(["lib/regular_expression/parser.rb"])
17
+
18
+ task default: :test
data/bin/console ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ $:.unshift(File.expand_path("../lib", __dir__))
5
+ require "regular_expression"
6
+
7
+ require "irb"
8
+ IRB.start