lumb 0.1.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 (4) hide show
  1. checksums.yaml +7 -0
  2. data/bin/lumb +6 -0
  3. data/lib/lumb.rb +124 -0
  4. metadata +60 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d04469fb5b0430d7325228a3328c35e22f4135d7
4
+ data.tar.gz: 57278168ccc84c5b95e644819de2d64b53053c2e
5
+ SHA512:
6
+ metadata.gz: e36397b790b4292e8b17b7b5d76b5ec5f9a68a5cf5879df3dd10eef6ab20b885eacca7d9844b2eb555063024a1af16f65a3f8950a79213b15a0c7dfc42eb2993
7
+ data.tar.gz: d7df30d72062ae9b332def6994e07603eb8ec9328539f417eae298266f36b194fcb2be96b8fbafb3ffe8f40ed9093a2eb8371603a491691d4f1a2351cce9f68e
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'lumb'
4
+
5
+ print Lumb.parse(File.read(ARGV.first)).to_s
6
+
@@ -0,0 +1,124 @@
1
+ require 'parslet'
2
+
3
+ module Lumb
4
+ class Parser < Parslet::Parser
5
+ rule(:newlines) { match['\r\n'].repeat(1) }
6
+ rule(:space) { match[' \t\r\n'].repeat(1) }
7
+ rule(:hspace) { match[' \t'].repeat(1) }
8
+ rule(:delim_space) { hspace | newlines.present? }
9
+
10
+ rule(:identifier) { match['a-z'] >> match['a-z0-9_-'].repeat }
11
+ rule(:type) { match['A-Z'] >> match['a-z'].repeat }
12
+ rule(:number) { match['0-9'].repeat(1) }
13
+
14
+ rule(:struct_item) {
15
+ identifier.as(:field) >>
16
+ str('=') >>
17
+ type.as(:type) >>
18
+ delim_space.as(:ws)
19
+ }
20
+
21
+ rule(:entry_item) {
22
+ identifier.as(:field) >>
23
+ str('=') >>
24
+ number.as(:value) >>
25
+ delim_space.as(:ws)
26
+ }
27
+
28
+ rule(:struct) {
29
+ struct_item.repeat(1).as(:items) >>
30
+ newlines.as(:ws)
31
+ }
32
+
33
+ rule(:entry) {
34
+ entry_item.repeat(1).as(:items) >>
35
+ newlines.as(:ws)
36
+ }
37
+
38
+ rule(:file) {
39
+ space.maybe.as(:ws_pre) >>
40
+ struct.as(:struct) >>
41
+ space.maybe.as(:ws_mid) >>
42
+ entry.repeat.as(:entries) >>
43
+ space.maybe.as(:ws_post)
44
+ }
45
+
46
+ root(:file)
47
+ end
48
+
49
+ class Transform < Parslet::Transform
50
+ rule(:field => simple(:field), :type => simple(:type), :ws => simple(:ws)) {
51
+ StructItem.new(field, type, ws)
52
+ }
53
+ rule(:field => simple(:field), :value => simple(:value), :ws => simple(:ws)) {
54
+ EntryItem.new(field, value, ws)
55
+ }
56
+ rule(:items => sequence(:items), :ws => simple(:ws)) {
57
+ if items.first.is_a? StructItem
58
+ LogStruct.new(items, ws)
59
+ else
60
+ LogEntry.new(items, ws)
61
+ end
62
+ }
63
+ rule(:ws_pre => simple(:ws_pre), :struct => simple(:struct), :ws_mid => simple(:ws_mid), :entries => sequence(:entries), :ws_post => simple(:ws_post)) {
64
+ LogFile.new(struct, entries, ws_pre, ws_mid, ws_post)
65
+ }
66
+ end
67
+
68
+ class StructItem
69
+ def initialize(field, type, ws)
70
+ @field, @type, @ws = field, type, ws
71
+ end
72
+
73
+ def to_s
74
+ "#{@field}=#{@type}#{@ws}"
75
+ end
76
+ end
77
+
78
+ class EntryItem
79
+ def initialize(field, value, ws)
80
+ @field, @value, @ws = field, value, ws
81
+ end
82
+
83
+ def to_s
84
+ "#{@field}=#{@value}#{@ws}"
85
+ end
86
+ end
87
+
88
+ class LogStruct
89
+ def initialize(items, ws)
90
+ @items, @ws = items, ws
91
+ end
92
+
93
+ def to_s
94
+ "#{@items.join}#{@ws}"
95
+ end
96
+ end
97
+
98
+ class LogEntry
99
+ def initialize(items, ws)
100
+ @items, @ws = items, ws
101
+ end
102
+
103
+ def to_s
104
+ "#{@items.join}#{@ws}"
105
+ end
106
+ end
107
+
108
+ class LogFile
109
+ def initialize(struct, entries, ws_pre, ws_mid, ws_post)
110
+ @struct, @entries, @ws_pre, @ws_mid, @ws_post = struct, entries, ws_pre, ws_mid, ws_post
111
+ end
112
+
113
+ def to_s
114
+ "#{@ws_pre}#{@struct}#{@ws_mid}#{@entries.join}#{@ws_post}"
115
+ end
116
+ end
117
+
118
+ def self.parse(str)
119
+ Transform.new.apply(Parser.new.parse(str))
120
+ rescue Parslet::ParseFailed => failure
121
+ puts failure.cause.ascii_tree
122
+ end
123
+ end
124
+
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lumb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jeremy Ruten
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-09-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: parslet
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ description:
28
+ email: jeremy.ruten@gmail.com
29
+ executables:
30
+ - lumb
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - bin/lumb
35
+ - lib/lumb.rb
36
+ homepage: https://github.com/yjerem/lumb
37
+ licenses:
38
+ - MIT
39
+ metadata: {}
40
+ post_install_message:
41
+ rdoc_options: []
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ requirements: []
55
+ rubyforge_project:
56
+ rubygems_version: 2.5.1
57
+ signing_key:
58
+ specification_version: 4
59
+ summary: strongly-typed personal logging
60
+ test_files: []