parse_queue 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
+ SHA1:
3
+ metadata.gz: 2dbdae6528358e96af53ee8a1021337dd6d9c2d8
4
+ data.tar.gz: 90ce08195ef61d12475c4b66e7751e248e5a5452
5
+ SHA512:
6
+ metadata.gz: 2dc54cd963668ab878f3fe69f6d2ffe7731dab1e5fbc9a49af619035b9d8ebe8df4f4aa93690c98810ae97dc660455932e9f001ed7c3e8d76ac4ca8db1663d4a
7
+ data.tar.gz: 749792d40d3992d2bdac14b70aecfa7021422127c71a589a2599dfa7173622136eb0f1f1e0c1d27bac5877ffc0ab13ea8631dcb205896925418788af413f2937
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,74 @@
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, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ 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 peter.c.camilleri@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 [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in parse_queue.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 PeterCamilleri
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,143 @@
1
+ # ParseQueue
2
+
3
+ The parse queue is a component in the Ruby Compiler Toolkit Project (RCTP). Its
4
+ role is to facilitate the movement of language tokens from one compiler phase
5
+ (like the lexical analyzer) to the next one (like the parser). More than just a
6
+ simple queue, it supports backing up or falling back to earlier states allowing
7
+ the parser to try other paths in the syntax tree when one path runs into a
8
+ dead end.
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'parse_queue'
16
+ ```
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install parse_queue
25
+
26
+ ## Usage
27
+
28
+ The parse queue is designed to be used to bridge phases of the compile process.
29
+ In particular the lexical analyzer and the parser.
30
+
31
+ #### Creating a parse queue:
32
+
33
+ When creating a parse queue, an optional block parameter is passed in. This is
34
+ called whenever more queue items are required. For example:
35
+
36
+ ```ruby
37
+ pq = ParseQueue.new { lex.next }
38
+ ```
39
+ If this block is omitted, then items will have to added to the parse queue
40
+ using the add method. The add method accepts single items, multiple items or
41
+ an array of items.
42
+
43
+ #### Getting a queued item:
44
+
45
+ Getting an item from the queue is done with the get method. For example:
46
+
47
+ ```ruby
48
+ item = pq.get
49
+ ```
50
+ This method returns the next unread item from the queue. Note that if no items
51
+ are available, the exception **ParseQueueNoFwd** is raised.
52
+
53
+
54
+ #### Backtracking:
55
+
56
+ Sometimes while parsing, it is required to backtrack to an earlier point in the
57
+ token stream so that an alternative branch of the syntax tree may be processed.
58
+ This is often done with the try method.
59
+
60
+ ```ruby
61
+ pq.try {
62
+ do_stuff_with(pq.get)
63
+ # etc
64
+
65
+ success
66
+ }
67
+ ```
68
+ Note how the try block returns a value called success. If this value is false
69
+ or nil, the parse queue is rolled back to its condition at the start of the try
70
+ block. Otherwise, any changes to the parse queue are retained.
71
+
72
+ Manual control of backtracking is possible by setting the position property to
73
+ a value saved off at an earlier point of the processing. For example:
74
+
75
+ ```ruby
76
+ save_point = pq.position
77
+ do_stuff_with(pq.get)
78
+ # etc
79
+
80
+ pq.position = save_point unless success
81
+ ```
82
+
83
+ #### Shifting
84
+
85
+ So far, items have been retained in the queue, even when are done being
86
+ processed. For large files, this may use a large amount of memory. To avoid
87
+ this, used items need to be shifted out of the parse queue. This can be done as
88
+ follows:
89
+
90
+ ```ruby
91
+ pq.try! {
92
+ do_stuff_with(pq.get)
93
+ # etc
94
+
95
+ success
96
+ }
97
+ ```
98
+ Note how the try! block returns a value called success. If this value is false
99
+ or nil, the parse queue is rolled back to its condition at the start of the try
100
+ block. Otherwise, any changes to the parse queue are retained and processed
101
+ items are removed.
102
+
103
+ This too can be done manually as shown below:
104
+
105
+ ```ruby
106
+ save_point = pq.position
107
+ do_stuff_with(pq.get)
108
+ # etc
109
+
110
+ if success
111
+ pq.shift
112
+ else
113
+ pq.position = save_point
114
+ end
115
+ ```
116
+ Note that if an attempt is made to fall back to data that has been shifted out,
117
+ a **ParseQueueNoRev** exception is raised.
118
+
119
+ ## Contributing
120
+
121
+ #### Plan A
122
+
123
+ 1. Fork it ( https://github.com/PeterCamilleri/parse_queue_dup/fork )
124
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
125
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
126
+ 4. Push to the branch (`git push origin my-new-feature`)
127
+ 5. Create a new Pull Request
128
+
129
+ #### Plan B
130
+
131
+ Go to the GitHub repository and raise an issue calling attention to some
132
+ aspect that could use some TLC or a suggestion or an idea.
133
+
134
+ ## License
135
+
136
+ The gem is available as open source under the terms of the
137
+ [MIT License](http://opensource.org/licenses/MIT).
138
+
139
+ ## Code of Conduct
140
+
141
+ Everyone interacting in the ParseQueue project’s codebases, issue trackers,
142
+ chat rooms and mailing lists is expected to follow the
143
+ [code of conduct](https://github.com/PeterCamilleri/parse_queue/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,16 @@
1
+ # coding: utf-8
2
+
3
+ # Exception types for the parse queue.
4
+
5
+ # The base error class for the parse queue.
6
+ class ParseQueueError < StandardError
7
+ end
8
+
9
+ # Raised when no more new data is available.
10
+ class ParseQueueNoFwd < ParseQueueError
11
+ end
12
+
13
+ # Raised when no back up data exists.
14
+ class ParseQueueNoRev < ParseQueueError
15
+ end
16
+
@@ -0,0 +1,5 @@
1
+ # coding: utf-8
2
+
3
+ class ParseQueue
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,99 @@
1
+ # coding: utf-8
2
+
3
+ # The Ruby Compiler Toolkit Project - Parse Queue
4
+ # A queue for compiler objects between parser layers.
5
+
6
+ require_relative "parse_queue/exceptions"
7
+ require_relative "parse_queue/version"
8
+
9
+ class ParseQueue
10
+
11
+ # The current read point of the queue.
12
+ attr_reader :position
13
+
14
+ # The number of old items removed from the queue.
15
+ attr_reader :offset
16
+
17
+ # Set up the parser queue.
18
+ def initialize(&fetch)
19
+ @fetch = fetch || lambda { false }
20
+ @buffer = []
21
+ @offset = @position = 0
22
+ end
23
+
24
+ # How many unread items are in this parse queue?
25
+ def unread
26
+ @buffer.length - @position + @offset
27
+ end
28
+
29
+ # Manually add items to the buffer
30
+ def add(*items)
31
+ @buffer += items.flatten
32
+ end
33
+
34
+ # Get an item from the buffer.
35
+ def get
36
+ if @position >= (@buffer.length + @offset)
37
+ item = @fetch.call
38
+ fail ParseQueueNoFwd unless item
39
+ @buffer << item
40
+ end
41
+
42
+ result = @buffer[@position - @offset]
43
+ @position += 1
44
+ result
45
+ end
46
+
47
+ # Get all possible items
48
+ def read_all
49
+ loop do
50
+ item = @fetch.call
51
+ return unless item
52
+ @buffer << item
53
+ end
54
+ end
55
+
56
+ # Set the position
57
+ def position=(value)
58
+ @position = value
59
+ validate_position
60
+ end
61
+
62
+ # Undo the last get.
63
+ def back_up
64
+ @position -= 1
65
+ validate_position
66
+ end
67
+
68
+ # Release any items before the current item.
69
+ def shift
70
+ @buffer.shift(@position - @offset)
71
+ @offset = @position
72
+ end
73
+
74
+ # Try to process some items with roll back on failure.
75
+ def try(&block)
76
+ save = @position
77
+ @position = save unless block.call
78
+ validate_position
79
+ end
80
+
81
+ # Try to process some items with shift of success and roll back on failure.
82
+ def try!(&block)
83
+ save = @position
84
+
85
+ if block.call
86
+ shift
87
+ else
88
+ @position = save
89
+ validate_position
90
+ end
91
+ end
92
+
93
+ # Is this a valid position?
94
+ def validate_position
95
+ fail ParseQueueNoRev if @position < @offset
96
+ fail ParseQueueNoFwd if @position >= (@buffer.length + @offset)
97
+ end
98
+
99
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+
3
+ lib = File.expand_path("../lib", __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require "parse_queue/version"
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "parse_queue"
9
+ spec.version = ParseQueue::VERSION
10
+ spec.authors = ["PeterCamilleri"]
11
+ spec.email = ["peter.c.camilleri@gmail.com"]
12
+
13
+ spec.summary = "The RCTP object queue for connecting parser steps."
14
+ spec.description = "The RCTP object queue for moving compiler tokens with nestable backtrack capability."
15
+ spec.homepage = "https://github.com/PeterCamilleri/parse_queue"
16
+ spec.license = "MIT"
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test)/}) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.15"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "minitest", "~> 5.0"
24
+ spec.add_development_dependency 'minitest_visible', "~> 0.1"
25
+ end
data/rakefile.rb ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: parse_queue
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - PeterCamilleri
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-08-22 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.15'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.15'
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.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest_visible
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.1'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.1'
69
+ description: The RCTP object queue for moving compiler tokens with nestable backtrack
70
+ capability.
71
+ email:
72
+ - peter.c.camilleri@gmail.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - CODE_OF_CONDUCT.md
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - lib/parse_queue.rb
83
+ - lib/parse_queue/exceptions.rb
84
+ - lib/parse_queue/version.rb
85
+ - parse_queue.gemspec
86
+ - rakefile.rb
87
+ homepage: https://github.com/PeterCamilleri/parse_queue
88
+ licenses:
89
+ - MIT
90
+ metadata: {}
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubyforge_project:
107
+ rubygems_version: 2.5.2
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: The RCTP object queue for connecting parser steps.
111
+ test_files: []