trees 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/trees.rb +2 -2
- data/lib/trie.rb +47 -27
- data/lib/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 1ac49d1c7aaa58a8ff86b29db7c2b7915c470094de6b3c1af4fbd4ad45adc597
|
|
4
|
+
data.tar.gz: 4a02b57c93cec8b8b42c36f39b158d0cca4c6d2a907b2c72bfdbfb38c5b57a74
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: f37c39226cd76bfc232ef4933aa6221b8bf6069c90a0af11e2160dcbe62be6ba2072b63f7562c0b63e44f69eed949e380a59b58302cc52cbb2fa8d39e36429f7
|
|
7
|
+
data.tar.gz: 0b5c295977cb0b50c80c1c0fcbd271a5923f80943cc08efc0ded1ce333008723c21532c33c8e376adf6de5d718610b26b79f23d7fa3b0ee4b84d349fc0f0cd61
|
data/lib/trees.rb
CHANGED
data/lib/trie.rb
CHANGED
|
@@ -8,12 +8,13 @@ module Trees
|
|
|
8
8
|
include LowType
|
|
9
9
|
|
|
10
10
|
PARAM_DELIMITERS = [' ', ':'].freeze
|
|
11
|
-
ARG_DELIMITERS = [' '].freeze
|
|
12
11
|
|
|
13
12
|
attr_reader :root_node
|
|
14
13
|
|
|
15
14
|
Result = Data.define(:line, :params)
|
|
16
15
|
|
|
16
|
+
class DuplicatePathError < StandardError; end
|
|
17
|
+
|
|
17
18
|
def initialize
|
|
18
19
|
@root_node = TrieNode.new
|
|
19
20
|
end
|
|
@@ -33,35 +34,55 @@ module Trees
|
|
|
33
34
|
current_node = current_node.upsert_child(key:)
|
|
34
35
|
end
|
|
35
36
|
|
|
37
|
+
raise DuplicatePathError, "Path already defined: #{line.path}" if current_node.line
|
|
38
|
+
|
|
36
39
|
current_node.line = line
|
|
37
40
|
end
|
|
38
41
|
|
|
39
|
-
def match(
|
|
40
|
-
return [] if
|
|
42
|
+
def match(tokens:, current_node: @root_node, index: 0, offset: 0, params: {})
|
|
43
|
+
return [] if tokens.empty?
|
|
41
44
|
|
|
42
|
-
|
|
45
|
+
[
|
|
46
|
+
*match_static(tokens:, current_node:, index:, offset:, params:),
|
|
47
|
+
*match_dynamic(tokens:, current_node:, index:, offset:, params:)
|
|
48
|
+
]
|
|
49
|
+
end
|
|
43
50
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
51
|
+
private
|
|
52
|
+
|
|
53
|
+
# Match an input character with the trie or mimic the space character between tokens in the trie.
|
|
54
|
+
def match_static(tokens:, current_node:, index:, offset:, params:)
|
|
55
|
+
child_node = current_node.child(key: tokens[index][offset] || ' ')
|
|
56
|
+
return [] unless child_node
|
|
57
|
+
|
|
58
|
+
next_index, next_offset = advance(tokens:, index:, offset:)
|
|
59
|
+
result = end_match(child_node:, tokens:, index: next_index, offset: next_offset, params:)
|
|
60
|
+
|
|
61
|
+
[*result, *match(tokens:, current_node: child_node, index: next_index, offset: next_offset, params:)]
|
|
62
|
+
end
|
|
49
63
|
|
|
50
|
-
|
|
51
|
-
|
|
64
|
+
# Dynamic path segment: capture the remainder of the current token.
|
|
65
|
+
def match_dynamic(tokens:, current_node:, index:, offset:, params:)
|
|
66
|
+
current_node.params.flat_map do |param|
|
|
52
67
|
child_node = current_node.child(key: param)
|
|
68
|
+
arg, next_index, next_offset = capture_arg(tokens:, index:, offset:)
|
|
69
|
+
next_params = params.merge(param.delete_prefix(':').to_sym => arg)
|
|
53
70
|
|
|
54
|
-
|
|
55
|
-
params[param.delete_prefix(':').to_sym] = arg
|
|
71
|
+
result = end_match(child_node:, tokens:, index: next_index, offset: next_offset, params: next_params)
|
|
56
72
|
|
|
57
|
-
|
|
58
|
-
results = [*results, *match(path:, current_node: child_node, current_index: next_index, params:)]
|
|
73
|
+
[*result, *match(tokens:, current_node: child_node, index: next_index, offset: next_offset, params: next_params)]
|
|
59
74
|
end
|
|
60
|
-
|
|
61
|
-
results
|
|
62
75
|
end
|
|
63
76
|
|
|
64
|
-
|
|
77
|
+
# Match an end node.
|
|
78
|
+
# TODO: Match a mid node.
|
|
79
|
+
def end_match(child_node:, tokens:, index:, offset:, params:)
|
|
80
|
+
if child_node.line && tokens[index + 1].nil?
|
|
81
|
+
return [Result.new(line: child_node.line, params:)]
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
[]
|
|
85
|
+
end
|
|
65
86
|
|
|
66
87
|
def capture_param(path:)
|
|
67
88
|
param = [':']
|
|
@@ -76,17 +97,16 @@ module Trees
|
|
|
76
97
|
param.join
|
|
77
98
|
end
|
|
78
99
|
|
|
79
|
-
def
|
|
80
|
-
|
|
81
|
-
arg = []
|
|
100
|
+
def advance(tokens:, index:, offset:)
|
|
101
|
+
token = tokens[index]
|
|
82
102
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
103
|
+
offset < token.length ? [index, offset + 1] : [index + 1, 0]
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def capture_arg(tokens:, index:, offset:)
|
|
107
|
+
token = tokens[index]
|
|
88
108
|
|
|
89
|
-
[
|
|
109
|
+
[token[offset..], index, token.length]
|
|
90
110
|
end
|
|
91
111
|
end
|
|
92
112
|
end
|
data/lib/version.rb
CHANGED