parse_queue 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2dbdae6528358e96af53ee8a1021337dd6d9c2d8
4
- data.tar.gz: 90ce08195ef61d12475c4b66e7751e248e5a5452
3
+ metadata.gz: 383a2388d540d992e658a3b6e3eff0a04ffcaab4
4
+ data.tar.gz: 63aac7b0b20544d1e596a58d8e1e88d8f6c012b0
5
5
  SHA512:
6
- metadata.gz: 2dc54cd963668ab878f3fe69f6d2ffe7731dab1e5fbc9a49af619035b9d8ebe8df4f4aa93690c98810ae97dc660455932e9f001ed7c3e8d76ac4ca8db1663d4a
7
- data.tar.gz: 749792d40d3992d2bdac14b70aecfa7021422127c71a589a2599dfa7173622136eb0f1f1e0c1d27bac5877ffc0ab13ea8631dcb205896925418788af413f2937
6
+ metadata.gz: 0f7a07e5d590f807e8d9097ac5ea0b9fd567f9366fef702a5d5e0cf2be17d07b22b8127107db635ea828964ca2044fcc9267a22c8554b57500265d0d35baa72e
7
+ data.tar.gz: 37fa8202815532981dfd6d5327117e6e83fc5618d9be63f2317ebcb2bdde2a9c059140823d5c1ce7eb48c072ba175adf576cbc08c768f2b1539c52fee6ff01e2
data/README.md CHANGED
@@ -34,11 +34,21 @@ When creating a parse queue, an optional block parameter is passed in. This is
34
34
  called whenever more queue items are required. For example:
35
35
 
36
36
  ```ruby
37
- pq = ParseQueue.new { lex.next }
37
+ def open_file_tokenized(name)
38
+ txt = IO.readlines(name, nil)[0]
39
+ lex = LexicalAnalayzer.new(text: txt, rules: LEXICAL_RULES)
40
+ ParseQueue.new { lex.get }
41
+ end
38
42
  ```
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.
43
+ This example above is a method that reads in the named file, creates an
44
+ analyzer on it (see the
45
+ [lexical_analyzer](https://rubygems.org/gems/lexical_analyzer)
46
+ gem for more details) and then uses that as the source for the parse queue.
47
+ The queue is returned for use by the compiler's parser.
48
+
49
+ Note: If the fetch block is omitted, then items will have to added to the parse
50
+ queue using the add method. The add method accepts single items, multiple items
51
+ or an array of items.
42
52
 
43
53
  #### Getting a queued item:
44
54
 
@@ -120,7 +130,7 @@ a **ParseQueueNoRev** exception is raised.
120
130
 
121
131
  #### Plan A
122
132
 
123
- 1. Fork it ( https://github.com/PeterCamilleri/parse_queue_dup/fork )
133
+ 1. Fork it ( https://github.com/PeterCamilleri/parse_queue/fork )
124
134
  2. Create your feature branch (`git checkout -b my-new-feature`)
125
135
  3. Commit your changes (`git commit -am 'Add some feature'`)
126
136
  4. Push to the branch (`git push origin my-new-feature`)
@@ -134,10 +144,10 @@ aspect that could use some TLC or a suggestion or an idea.
134
144
  ## License
135
145
 
136
146
  The gem is available as open source under the terms of the
137
- [MIT License](http://opensource.org/licenses/MIT).
147
+ [MIT License](./LICENSE.txt).
138
148
 
139
149
  ## Code of Conduct
140
150
 
141
151
  Everyone interacting in the ParseQueue project’s codebases, issue trackers,
142
152
  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).
153
+ [code of conduct](./CODE_OF_CONDUCT.md).
@@ -1,5 +1,3 @@
1
- # coding: utf-8
2
-
3
1
  # Exception types for the parse queue.
4
2
 
5
3
  # The base error class for the parse queue.
@@ -1,5 +1,3 @@
1
- # coding: utf-8
2
-
3
1
  class ParseQueue
4
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
5
3
  end
data/lib/parse_queue.rb CHANGED
@@ -1,5 +1,3 @@
1
- # coding: utf-8
2
-
3
1
  # The Ruby Compiler Toolkit Project - Parse Queue
4
2
  # A queue for compiler objects between parser layers.
5
3
 
@@ -11,44 +9,54 @@ class ParseQueue
11
9
  # The current read point of the queue.
12
10
  attr_reader :position
13
11
 
14
- # The number of old items removed from the queue.
15
- attr_reader :offset
12
+ #The default fetch block
13
+ DFB = Proc.new { false }
16
14
 
17
15
  # Set up the parser queue.
18
16
  def initialize(&fetch)
19
- @fetch = fetch || lambda { false }
17
+ @fetch = fetch || DFB
20
18
  @buffer = []
21
19
  @offset = @position = 0
22
20
  end
23
21
 
24
22
  # How many unread items are in this parse queue?
25
- def unread
26
- @buffer.length - @position + @offset
23
+ def fwd_count
24
+ index_limit - @position
27
25
  end
28
26
 
29
- # Manually add items to the buffer
30
- def add(*items)
31
- @buffer += items.flatten
27
+ # How many already read items are still in this parse queue?
28
+ def rev_count
29
+ @position - @offset
32
30
  end
33
31
 
34
32
  # Get an item from the buffer.
35
33
  def get
36
- if @position >= (@buffer.length + @offset)
34
+ if @position == index_limit
37
35
  item = @fetch.call
38
- fail ParseQueueNoFwd unless item
36
+
37
+ unless item
38
+ @fetch = DFB
39
+ fail ParseQueueNoFwd
40
+ end
41
+
39
42
  @buffer << item
40
43
  end
41
44
 
42
- result = @buffer[@position - @offset]
45
+ result = @buffer[rev_count]
43
46
  @position += 1
44
47
  result
45
48
  end
46
49
 
47
- # Get all possible items
48
- def read_all
50
+ # Fetch all possible items.
51
+ def fetch_all
49
52
  loop do
50
53
  item = @fetch.call
51
- return unless item
54
+
55
+ unless item
56
+ @fetch = DFB
57
+ return
58
+ end
59
+
52
60
  @buffer << item
53
61
  end
54
62
  end
@@ -60,40 +68,40 @@ class ParseQueue
60
68
  end
61
69
 
62
70
  # Undo the last get.
63
- def back_up
64
- @position -= 1
71
+ def unget(count=1)
72
+ @position -= count
65
73
  validate_position
66
74
  end
67
75
 
68
76
  # Release any items before the current item.
69
77
  def shift
70
- @buffer.shift(@position - @offset)
78
+ @buffer.shift(rev_count)
71
79
  @offset = @position
72
80
  end
73
81
 
74
82
  # Try to process some items with roll back on failure.
75
83
  def try(&block)
76
84
  save = @position
77
- @position = save unless block.call
78
- validate_position
85
+ self.position = save unless (result = block.call)
86
+ result
79
87
  end
80
88
 
81
- # Try to process some items with shift of success and roll back on failure.
89
+ # Process some items with a shift on success and a roll back on failure.
82
90
  def try!(&block)
83
- save = @position
84
-
85
- if block.call
86
- shift
87
- else
88
- @position = save
89
- validate_position
90
- end
91
+ shift if (result = try(&block))
92
+ result
91
93
  end
92
94
 
95
+ private
96
+
93
97
  # Is this a valid position?
94
98
  def validate_position
95
99
  fail ParseQueueNoRev if @position < @offset
96
- fail ParseQueueNoFwd if @position >= (@buffer.length + @offset)
100
+ fail ParseQueueNoFwd if @position >= index_limit
97
101
  end
98
102
 
103
+ # The first index past the end of the array
104
+ def index_limit
105
+ @buffer.length + @offset
106
+ end
99
107
  end
data/parse_queue.gemspec CHANGED
@@ -18,6 +18,8 @@ Gem::Specification.new do |spec|
18
18
  spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test)/}) }
19
19
  spec.require_paths = ["lib"]
20
20
 
21
+ spec.required_ruby_version = '>=2.3.0'
22
+
21
23
  spec.add_development_dependency "bundler", "~> 1.15"
22
24
  spec.add_development_dependency "rake", "~> 10.0"
23
25
  spec.add_development_dependency "minitest", "~> 5.0"
data/rakefile.rb CHANGED
@@ -8,3 +8,9 @@ Rake::TestTask.new(:test) do |t|
8
8
  end
9
9
 
10
10
  task :default => :test
11
+
12
+ desc "What version of parse queue is this?"
13
+ task :vers do |t|
14
+ puts
15
+ puts "parse_queue version = #{ParseQueue::VERSION}"
16
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: parse_queue
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - PeterCamilleri
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-08-22 00:00:00.000000000 Z
11
+ date: 2018-08-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -96,7 +96,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
96
96
  requirements:
97
97
  - - ">="
98
98
  - !ruby/object:Gem::Version
99
- version: '0'
99
+ version: 2.3.0
100
100
  required_rubygems_version: !ruby/object:Gem::Requirement
101
101
  requirements:
102
102
  - - ">="