junoser 0.2.4 → 0.2.5

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
  SHA1:
3
- metadata.gz: c1712996f972e957b0606905fdad4a5e1888623d
4
- data.tar.gz: 05fe617f94ace5f76847da3da6e950b6661bda4f
3
+ metadata.gz: 2ab1a9524ecd186fab65751afc3848b778d5277c
4
+ data.tar.gz: ef24a5f3ecc523eac099d383abe46d3d63afbd6c
5
5
  SHA512:
6
- metadata.gz: 8055f567929e8499ceb8b2ace5415ab82526f82e76aee1bfd72fa56eca5972dda99374daf725c4de823c52b52faae1d7fcfd48523a6173f9536dcb35c7ec5c9b
7
- data.tar.gz: 98733286e456322ce034ad6be647cb2d33f4d4481ecab034a7224284aac3a3f6d05237d7b292ad2f44ee3855bd18894b52f82bd90842e0ed84d2b8a2db2d26ad
6
+ metadata.gz: 185e14132f29d80aacf8e9020ce185a1b202b9337fa00bde2b7363803aac0f5680cf1a39b7af2df352671371035843ee4e5480af6e06911c026632fe5b1f19d2
7
+ data.tar.gz: cced7a834060b3a786b607cc9c51b961fb3161c5bab457d4d085aa5bf26d19b31de7909f992205e062753b729b9b2c9a2c4460eb5156f03df1e07b77808d2921
data/lib/junoser/cli.rb CHANGED
@@ -34,18 +34,7 @@ module Junoser
34
34
 
35
35
  def commit_check_display_set(config)
36
36
  parser = Junoser::Parser.new
37
- passed = true
38
-
39
- config.split("\n").each do |line|
40
- begin
41
- parser.parse line
42
- rescue Parslet::ParseFailed
43
- $stderr.puts "Invalid syntax: #{line}"
44
- passed = false
45
- end
46
- end
47
-
48
- passed
37
+ parser.parse_lines(config)
49
38
  end
50
39
  end
51
40
  end
@@ -7,9 +7,12 @@ module Junoser
7
7
 
8
8
  OFFSET = ' '
9
9
 
10
+ attr_accessor :deactivated
11
+
10
12
  def initialize(depth=0)
11
13
  @hash = {}
12
14
  @depth = depth
15
+ @deactivated = false
13
16
  end
14
17
 
15
18
  def push(str)
@@ -22,10 +25,34 @@ module Junoser
22
25
  end
23
26
  alias << push
24
27
 
28
+ def deactivate(deactivated_line)
29
+ statement, store = matched(deactivated_line)
30
+
31
+ if statement
32
+ if statement == deactivated_line
33
+ store.deactivated = true
34
+ else
35
+ store.deactivate(deactivated_line.sub(/^#{statement} */, ''))
36
+ end
37
+ else
38
+ statement, store = inverse_matched(deactivated_line)
39
+ if statement
40
+ store.deactivated = true
41
+ end
42
+ end
43
+ end
44
+
45
+ def each_with_inactive(&block)
46
+ each do |k, v|
47
+ k = "inactive: #{k}" if v.deactivated
48
+ yield k, v
49
+ end
50
+ end
51
+
25
52
  def to_s
26
53
  str = ''
27
54
 
28
- each do |k, v|
55
+ each_with_inactive do |k, v|
29
56
  if v.empty?
30
57
  str << OFFSET*@depth << "#{k};\n"
31
58
  else
@@ -47,6 +74,24 @@ module Junoser
47
74
  str.gsub!(/arg\((.*)\)/) { "#$1" }
48
75
  str
49
76
  end
77
+
78
+ def matched(str)
79
+ each do |statement, store|
80
+ # NOTE: return the first object
81
+ return [statement, store] if str =~ /^#{statement}/
82
+ end
83
+
84
+ []
85
+ end
86
+
87
+ def inverse_matched(str)
88
+ each do |statement, store|
89
+ # NOTE: return the first object
90
+ return [statement, store] if statement =~ /^#{str}/
91
+ end
92
+
93
+ []
94
+ end
50
95
  end
51
96
  end
52
97
  end
@@ -19,25 +19,15 @@ module Junoser
19
19
  end
20
20
 
21
21
  def commit_check(&block)
22
- parser = Junoser::Parser.new
23
- passed = true
24
-
25
- process do |current_stack, str|
26
- config = transform_line(current_stack, str)
27
-
28
- begin
29
- parser.parse config
30
- rescue Parslet::ParseFailed
31
- $stderr.puts "Invalid syntax: #{config}"
32
- passed = false
33
- end
22
+ begin
23
+ lines = transform
24
+ rescue
25
+ $stderr.puts $!
26
+ return false
34
27
  end
35
28
 
36
- rescue
37
- $stderr.puts $!
38
- passed = false
39
- ensure
40
- return passed
29
+ parser = Junoser::Parser.new
30
+ parser.parse_lines(lines)
41
31
  end
42
32
 
43
33
 
@@ -65,17 +55,22 @@ module Junoser
65
55
  end
66
56
 
67
57
  def transform_line(current_stack, str)
68
- statement = if current_stack.empty?
69
- str
70
- else
71
- statement = "#{current_stack.join(' ')} #{str}"
72
- end
58
+ statements = []
59
+ current_statement = ''
73
60
 
74
- if statement.gsub!('inactive: ', '')
75
- "deactivate #{statement}"
76
- else
77
- "set #{statement}"
61
+ current_stack.each do |stack|
62
+ if stack.gsub!('inactive: ', '')
63
+ statements << "deactivate #{current_statement}#{stack}"
64
+ end
65
+ current_statement << "#{stack} "
78
66
  end
67
+
68
+ if str.gsub!('inactive: ', '')
69
+ statements << "deactivate #{current_statement}#{str}"
70
+ end
71
+
72
+ statements.unshift "set #{current_statement}#{str}"
73
+ statements.join("\n")
79
74
  end
80
75
 
81
76
  def struct(stack, statement, offset=2)
@@ -15,12 +15,19 @@ module Junoser
15
15
  parser = Junoser::Parser.new
16
16
  transform = Junoser::Transformer.new
17
17
 
18
- Junoser::Input.new(@input).read.split("\n").each do |line|
18
+ config = Junoser::Input.new(@input).read.split("\n")
19
+ deactivated_lines = config.grep(/^deactivate /).map {|l| l.sub(/^deactivate /, '') }
20
+
21
+ config.each do |line|
22
+ next if line =~ /^deactivate /
23
+
19
24
  transformed = transform.apply(parser.parse(line))
20
25
  raise "ERROR: parse failed" unless transformed.is_a?(String)
21
26
  @config << transformed
22
27
  end
23
28
 
29
+ deactivated_lines.each {|l| @config.deactivate l }
30
+
24
31
  @config.to_s
25
32
  end
26
33
  end
@@ -2,6 +2,29 @@ require 'parslet'
2
2
 
3
3
  module Junoser
4
4
  class Parser < Parslet::Parser
5
+ def parse_lines(config)
6
+ lines = config.split("\n")
7
+ lines_without_deactivate = lines.reject {|l| l =~ /^deactivate/ }
8
+
9
+ lines.inject(true) do |passed, line|
10
+ if line =~ /^deactivate/
11
+ if lines_without_deactivate.grep(/^#{line.sub(/^deactivate/, 'set')}/).empty?
12
+ next false
13
+ else
14
+ next passed
15
+ end
16
+ end
17
+
18
+ begin
19
+ parse line
20
+ passed
21
+ rescue Parslet::ParseFailed
22
+ $stderr.puts "Invalid syntax: #{line}"
23
+ false
24
+ end
25
+ end
26
+ end
27
+
5
28
  # block with children maybe
6
29
  def b(object, *children)
7
30
  children.inject(object) {|rule, child| rule.as(:label) >> (space >> child.as(:child) | eos) }
@@ -46,7 +69,7 @@ module Junoser
46
69
  rule(:prefix ) { address >> (str('/') >> match('[0-9]').repeat(1)).maybe }
47
70
 
48
71
  root(:set)
49
- rule(:set) { (str('set') | str('deactivate')) >> space >> configuration.as(:config) >> comment.maybe }
72
+ rule(:set) { str('set') >> space >> configuration.as(:config) >> comment.maybe }
50
73
 
51
74
  rule(:comment) { space.maybe >> (hash_comment | slash_asterisk) }
52
75
  rule(:hash_comment) { str('#') >> any.maybe }
data/lib/junoser/ruler.rb CHANGED
@@ -163,6 +163,29 @@ require 'parslet'
163
163
 
164
164
  module Junoser
165
165
  class Parser < Parslet::Parser
166
+ def parse_lines(config)
167
+ lines = config.split("\\n")
168
+ lines_without_deactivate = lines.reject {|l| l =~ /^deactivate/ }
169
+
170
+ lines.inject(true) do |passed, line|
171
+ if line =~ /^deactivate/
172
+ if lines_without_deactivate.grep(/^\#{line.sub(/^deactivate/, 'set')}/).empty?
173
+ next false
174
+ else
175
+ next passed
176
+ end
177
+ end
178
+
179
+ begin
180
+ parse line
181
+ passed
182
+ rescue Parslet::ParseFailed
183
+ $stderr.puts "Invalid syntax: \#{line}"
184
+ false
185
+ end
186
+ end
187
+ end
188
+
166
189
  # block with children maybe
167
190
  def b(object, *children)
168
191
  children.inject(object) {|rule, child| rule.as(:label) >> (space >> child.as(:child) | eos) }
@@ -207,7 +230,7 @@ module Junoser
207
230
  rule(:prefix ) { address >> (str('/') >> match('[0-9]').repeat(1)).maybe }
208
231
 
209
232
  root(:set)
210
- rule(:set) { (str('set') | str('deactivate')) >> space >> configuration.as(:config) >> comment.maybe }
233
+ rule(:set) { str('set') >> space >> configuration.as(:config) >> comment.maybe }
211
234
 
212
235
  rule(:comment) { space.maybe >> (hash_comment | slash_asterisk) }
213
236
  rule(:hash_comment) { str('#') >> any.maybe }
@@ -1,3 +1,3 @@
1
1
  module Junoser
2
- VERSION = "0.2.4"
2
+ VERSION = "0.2.5"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: junoser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shintaro Kojima
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-02-17 00:00:00.000000000 Z
11
+ date: 2017-04-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: parslet