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 +4 -4
- data/lib/oppen/mixins.rb +53 -0
- data/lib/oppen/printer.rb +8 -4
- data/lib/oppen/scan_stack.rb +15 -2
- data/lib/oppen/version.rb +1 -1
- data/lib/oppen.rb +3 -0
- metadata +3 -3
- data/lib/oppen/utils.rb +0 -19
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fdf36c265fd8c6c3d7335fe79b016ac1e6d8014a23f1b027fc4d956d05b442ab
|
4
|
+
data.tar.gz: d75772336cb34c8af929e95d5c4520f0388718739f7d6df722496f6f2f96a82a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1d745467bc343f1262dbf36f1206a1a37336840014b6785badab602712fe167457b45598b4d2f10e4d438cb49935472c8fe9aea1e21109f349c6f52524831e8e
|
7
|
+
data.tar.gz: ad6279e7c29f9b6764fd19ddcdfc8fe006272eb481632d0a3f4f7fa9aad7a5b873e624ea5e829b89ee97401c474779034cfa956ff202e438a27afa22e6bd9151
|
data/lib/oppen/mixins.rb
ADDED
@@ -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 '
|
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) %
|
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
|
-
@
|
236
|
-
@
|
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
|
data/lib/oppen/scan_stack.rb
CHANGED
@@ -1,11 +1,13 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require_relative '
|
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 =
|
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
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.
|
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-
|
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
|