air-parse 0.0.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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/air/parse.rb +96 -0
  3. metadata +43 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ee7d1317a87267453ede632b1fa266544b885bc90ea23f2a73aacbdaf490dc28
4
+ data.tar.gz: a6cbade7050bc05165a527b31b3968a64b7a1a847ef54aabe1d12c8f425e7676
5
+ SHA512:
6
+ metadata.gz: 1c824cfd77d202e7727ca6cfe5fda89f983ff5ec28b98b2dbcb7add9313f4560adcc1e30dddfe50631bba7a630dcae2f0531eb991e3bc0fcde45b2bf792fcbbc
7
+ data.tar.gz: fcf6ade3c61341500f362501b887c626f762108bd11ffd27226032223d6405737d21059706971663cbbad26c549f63a6e983bfb1d707467f90e476d432cbd298
data/lib/air/parse.rb ADDED
@@ -0,0 +1,96 @@
1
+ module Air
2
+ module Parse
3
+ module SpacePrefix
4
+ refine String do
5
+ def space_prefix
6
+ prefix = 0
7
+ prefix += 1 until self[prefix] != ' '
8
+ prefix
9
+ end
10
+ end
11
+ end
12
+
13
+ module ::Air
14
+ def self.parse_file path
15
+ parse IO.read path
16
+ end
17
+
18
+ def self.parse source
19
+ Parse[source]
20
+ end
21
+ end
22
+
23
+ def self.[] source
24
+ stairs = []
25
+
26
+ lines, cursor = source.lines, 0
27
+ endpoint = lines.size
28
+
29
+ until cursor == endpoint
30
+ stair = Stair.from lines, at: cursor
31
+ stairs << stair
32
+
33
+ cursor = stair.end_at + 1
34
+ end
35
+
36
+ stairs
37
+ end
38
+
39
+ class Stair
40
+ module From
41
+ def from lines, at: 0
42
+ at += 1 until lines[at] != "\n"
43
+ new lines, at: at
44
+ end
45
+ end
46
+
47
+ module Public
48
+ using SpacePrefix
49
+
50
+ attr_reader :start_at, :end_at
51
+
52
+ def space_prefix
53
+ @space_prefix ||= @lines[@start_at].space_prefix
54
+ end
55
+
56
+ def stairs
57
+ @stairs ||= []
58
+ end
59
+
60
+ def line
61
+ @line ||= @lines[@start_at].chomp.strip
62
+ end
63
+
64
+ def leaf?
65
+ stairs.empty?
66
+ end
67
+ end
68
+
69
+ using SpacePrefix
70
+ include Public
71
+ extend From
72
+
73
+ def initialize lines, at: 0
74
+ @start_at, @lines = at, lines
75
+
76
+ next_index = at + 1
77
+ next_line = lines[next_index]
78
+
79
+ if next_line && (next_line.space_prefix - space_prefix) == 2
80
+ until next_line.space_prefix <= space_prefix
81
+ stair = Stair.from lines, at: next_index
82
+ stairs << stair
83
+
84
+ next_index = stair.end_at + 1
85
+ next_line = lines[next_index]
86
+ break unless next_line
87
+ end
88
+
89
+ @end_at = stairs.last.end_at
90
+ else
91
+ @end_at = at
92
+ end
93
+ end
94
+ end
95
+ end
96
+ end
metadata ADDED
@@ -0,0 +1,43 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: air-parse
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Anatoly Chernov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-08-18 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - chertoly@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/air/parse.rb
21
+ homepage:
22
+ licenses: []
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubygems_version: 3.4.18
40
+ signing_key:
41
+ specification_version: 4
42
+ summary: To parse air.
43
+ test_files: []