oppen 0.9.1 → 0.9.2

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: fdf36c265fd8c6c3d7335fe79b016ac1e6d8014a23f1b027fc4d956d05b442ab
4
+ data.tar.gz: d75772336cb34c8af929e95d5c4520f0388718739f7d6df722496f6f2f96a82a
5
5
  SHA512:
6
- metadata.gz: e6a37e27b916fc83323a4b4b387c3990265094ac0c326b5770328ce8d43977b421c8cc0b2092b3c8a877d619ee56578abee1c0f5b2af9390c12f696211eb6446
7
- data.tar.gz: 2548a7c7cd71299b8779714537916cf603f72a57bf2d02cf0ebcad163bafe7d53299e833cae07ada8a8cc30ddf2ba4e7ef19df6556656c2c920a90478c734ce4
6
+ metadata.gz: 1d745467bc343f1262dbf36f1206a1a37336840014b6785badab602712fe167457b45598b4d2f10e4d438cb49935472c8fe9aea1e21109f349c6f52524831e8e
7
+ data.tar.gz: ad6279e7c29f9b6764fd19ddcdfc8fe006272eb481632d0a3f4f7fa9aad7a5b873e624ea5e829b89ee97401c474779034cfa956ff202e438a27afa22e6bd9151
@@ -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.2' # 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]
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.2
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