oppen 0.9.1 → 0.9.3

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
  SHA256:
3
- metadata.gz: 81c1832fc106f4f4622738ad55ea231c66bdb40a96523d9913b3426781aed296
4
- data.tar.gz: 9c5797979a36d529317bf31cdc85a158309c26f5a0c298f8cd1e577a83f4386a
3
+ metadata.gz: f4bd96d5a824c1ad4de7d5d1e041365ba41a0ee56acf4ace9b95fcaa94600c6a
4
+ data.tar.gz: 911d25db3ee42855425bc8dc29bc4f2ff6af231a29b949de684d02ad6f28418d
5
5
  SHA512:
6
- metadata.gz: e6a37e27b916fc83323a4b4b387c3990265094ac0c326b5770328ce8d43977b421c8cc0b2092b3c8a877d619ee56578abee1c0f5b2af9390c12f696211eb6446
7
- data.tar.gz: 2548a7c7cd71299b8779714537916cf603f72a57bf2d02cf0ebcad163bafe7d53299e833cae07ada8a8cc30ddf2ba4e7ef19df6556656c2c920a90478c734ce4
6
+ metadata.gz: 61dc218e4918ee44bbe66d5926859153d3052547669943512e4d65844959cab13c9e180e1406be1e7c31328e0025533ee632a025d59173abb9264f80df5b6ca6
7
+ data.tar.gz: e79d9269ab5fd74f4726edf6e834d8f95cece52499d4730040df5e90dd1a365a4691262e780bc2a1efca0209e319d82a082416f727ed1a3ec193a1f353fe7e6d
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Oppen
4
+ # Mixins.
5
+ module Mixins
6
+ # Rotates circular array and triples its size.
7
+ # This method is not for public use.
8
+ #
9
+ # @param arr [Array]
10
+ # @param offset [Integer] Rotation amount
11
+ #
12
+ # @return [Array<Array, Integer, Integer>] upsized array, lhs, rhs
13
+ def upsize_circular_array(arr, offset)
14
+ size = arr.size
15
+ arr = arr.rotate(offset)
16
+ arr.fill(nil, size, 2 * size)
17
+ [arr, 0, size]
18
+ end
19
+
20
+ # Convert a list of tokens to its wadler representation.
21
+ #
22
+ # @param tokens [Array[Token]]
23
+ # @param base_indent [Integer]
24
+ #
25
+ # @return [String]
26
+ def tokens_to_wadler(tokens, base_indent = 4)
27
+ out = StringIO.new
28
+ write = ->(txt, nb_spaces) {
29
+ out.write("#{' ' * nb_spaces}#{txt}\n")
30
+ }
31
+ nb_spaces = base_indent
32
+ tokens.each do |token|
33
+ case token
34
+ in Token::String
35
+ write.call("out.text '#{token}'", nb_spaces)
36
+ in Token::LineBreak
37
+ write.call('out.break', nb_spaces)
38
+ in Token::Break
39
+ write.call('out.breakable', nb_spaces)
40
+ in Token::Begin
41
+ write.call('out.group {', nb_spaces)
42
+ nb_spaces += 2
43
+ in Token::End
44
+ nb_spaces -= 2
45
+ write.call('}', nb_spaces)
46
+ in Token::EOF
47
+ write.call('', nb_spaces) # new line
48
+ end
49
+ end
50
+ out.string
51
+ end
52
+ end
53
+ end
data/lib/oppen/printer.rb CHANGED
@@ -4,12 +4,14 @@ require 'stringio'
4
4
 
5
5
  require_relative 'scan_stack'
6
6
  require_relative 'print_stack'
7
- require_relative 'utils'
7
+ require_relative 'mixins'
8
8
 
9
9
  # Oppen.
10
10
  module Oppen
11
11
  # Oppen pretty-printer.
12
12
  class Printer
13
+ extend Mixins
14
+
13
15
  attr_reader :config
14
16
  # Ring buffer left index.
15
17
  #
@@ -227,13 +229,15 @@ module Oppen
227
229
  #
228
230
  # @return [Nil]
229
231
  def advance_right
230
- @right = (right + 1) % scan_stack.length
232
+ @right = (right + 1) % @size.length
233
+
231
234
  return if right != left
232
235
 
233
236
  raise 'Token queue full' if !config.upsize_stack?
234
237
 
235
- @size, = Utils.upsize_circular_array(@size, @left)
236
- @tokens, @left, @right = Utils.upsize_circular_array(@tokens, @left)
238
+ @scan_stack.update_indexes(-@left)
239
+ @size, _left, _right = ScanStack.upsize_circular_array(@size, @left)
240
+ @tokens, @left, @right = ScanStack.upsize_circular_array(@tokens, @left)
237
241
  end
238
242
 
239
243
  # Advances the `left` pointer and lets the print stack
@@ -1,11 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative 'utils'
3
+ require_relative 'mixins'
4
4
 
5
5
  # Oppen.
6
6
  module Oppen
7
7
  # A fixed-size stack that can be popped from top and bottom.
8
8
  class ScanStack
9
+ extend Mixins
10
+
9
11
  def initialize(size, config)
10
12
  @bottom = 0
11
13
  @config = config
@@ -73,7 +75,7 @@ module Oppen
73
75
  if @top == @bottom
74
76
  raise 'Stack full' if !@config.upsize_stack?
75
77
 
76
- @stack, @bottom, @top = Utils.upsize_circular_array(@stack, @bottom)
78
+ @stack, @bottom, @top = ScanStack.upsize_circular_array(@stack, @bottom)
77
79
  end
78
80
  end
79
81
  @stack[@top] = value
@@ -112,5 +114,16 @@ module Oppen
112
114
  end
113
115
  res
114
116
  end
117
+
118
+ # Offset the values of the stack.
119
+ #
120
+ # @param offset [Integer]
121
+ #
122
+ # @return [Array[Integer]]
123
+ def update_indexes(offset)
124
+ @stack = @stack.map { |val|
125
+ (val + offset) % length if val
126
+ }
127
+ end
115
128
  end
116
129
  end
data/lib/oppen/version.rb CHANGED
@@ -5,5 +5,5 @@ module Oppen
5
5
  # Oppen version
6
6
  #
7
7
  # @return [String] current version
8
- VERSION = '0.9.1' # managed by release.sh
8
+ VERSION = '0.9.3' # managed by release.sh
9
9
  end
data/lib/oppen.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'oppen/mixins'
3
4
  require_relative 'oppen/printer'
4
5
  require_relative 'oppen/print_stack'
5
6
  require_relative 'oppen/scan_stack'
@@ -9,6 +10,8 @@ require_relative 'wadler/print'
9
10
 
10
11
  # Oppen.
11
12
  module Oppen
13
+ extend Mixins
14
+
12
15
  # Entry point of the pretty printer.
13
16
  #
14
17
  # @param config [Config]
data/lib/wadler/print.rb CHANGED
@@ -35,6 +35,10 @@ module Oppen
35
35
 
36
36
  # @return [String]
37
37
  def output
38
+ if !tokens.first.is_a? Token::Begin
39
+ tokens.unshift Oppen.begin_consistent(offset: 0)
40
+ tokens << Oppen.end
41
+ end
38
42
  if !tokens.last.is_a? Oppen::Token::EOF
39
43
  tokens << Oppen.eof
40
44
  end
@@ -128,5 +132,72 @@ module Oppen
128
132
  def break(line_continuation: '')
129
133
  tokens << Oppen.line_break(line_continuation:, offset: current_indent)
130
134
  end
135
+
136
+ # @!group Helpers
137
+
138
+ # Set a base indenetaion level to the printer.
139
+ #
140
+ # @param indent [Integer]
141
+ #
142
+ # @return [Nil]
143
+ def base_indent(indent = 0)
144
+ @current_indent = indent if !indent.nil?
145
+ end
146
+
147
+ # Open a consistent group.
148
+ #
149
+ # @param indent [Integer]
150
+ #
151
+ # @return [Nil]
152
+ def group_open(indent: 0)
153
+ tokens << Oppen.begin_consistent(offset: indent)
154
+ end
155
+
156
+ # Close a group.
157
+ #
158
+ # @return [Nil]
159
+ def group_close(_)
160
+ tokens << Oppen.end
161
+ end
162
+
163
+ # Open a consistent group with indent.
164
+ #
165
+ # @param indent [Integer]
166
+ #
167
+ # @return [Nil]
168
+ def indent_open(indent)
169
+ @current_indent += indent
170
+ group_open
171
+ end
172
+
173
+ # Close a group with indent.
174
+ #
175
+ # @param indent [Integer]
176
+ #
177
+ # @return [Nil]
178
+ def indent_close(group, indent)
179
+ @current_indent -= indent
180
+ group_close(group)
181
+ end
182
+
183
+ # Open a nest by indent.
184
+ #
185
+ # @param indent [Integer]
186
+ #
187
+ # @return [Nil]
188
+ def nest_open(indent)
189
+ @current_indent += indent
190
+ end
191
+
192
+ # Close a nest by indent.
193
+ #
194
+ # @param indent [Integer]
195
+ #
196
+ # @return [Nil]
197
+ def nest_close(indent)
198
+ @current_indent -= indent
199
+ end
200
+
201
+ # @!endgroup
131
202
  end
132
203
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: oppen
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.1
4
+ version: 0.9.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Amine Mike El Maalouf <amine.el-maalouf@epita.fr>
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2024-10-22 00:00:00.000000000 Z
12
+ date: 2024-10-25 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Implementation of the Oppen's pretty printing algorithm
15
15
  email:
@@ -20,11 +20,11 @@ files:
20
20
  - LICENSE
21
21
  - README.md
22
22
  - lib/oppen.rb
23
+ - lib/oppen/mixins.rb
23
24
  - lib/oppen/print_stack.rb
24
25
  - lib/oppen/printer.rb
25
26
  - lib/oppen/scan_stack.rb
26
27
  - lib/oppen/token.rb
27
- - lib/oppen/utils.rb
28
28
  - lib/oppen/version.rb
29
29
  - lib/wadler/print.rb
30
30
  homepage: http://github.com/Faveod/oppen-ruby
data/lib/oppen/utils.rb DELETED
@@ -1,19 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # Oppen.
4
- module Oppen
5
- # Utils.
6
- module Utils
7
- # Rotates circular array and triples its size.
8
- # @param arr [Array]
9
- # @param offset [Integer] Rotation amount
10
- #
11
- # @return [Array(Array, Integer, Integer)] upsized array, lhs, rhs
12
- def self.upsize_circular_array(arr, offset)
13
- size = arr.size
14
- arr = arr.rotate(offset)
15
- arr.fill(nil, size, 2 * size)
16
- [arr, 0, size]
17
- end
18
- end
19
- end