piece 0.1.5 → 0.1.6

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: 4ab088dfe0e66ffc1450fc9eab486b2c10ca6d19
4
- data.tar.gz: a0ec38dcea8530a04e2088a8364e7a80e2552a06
3
+ metadata.gz: e3a6516260ed0eeb0086f80f53372acff8f53f01
4
+ data.tar.gz: 377b91696da2e2cbbb5c6a41961ee58f6665708c
5
5
  SHA512:
6
- metadata.gz: d54d5a7e9bcdb09df0264b9bb50bd52cdfc5b0ad5fd8cc7f4096a3b188595671e1bd85d06b39257f8e206a1b3f9181dca9c1ce66b1afaa14b1576b8769513e74
7
- data.tar.gz: e9ac20ef0b1a3643ddcf861e9a448ebc968d56a78f04b213de6f5eb8d264d3d8bcbb2dc9eb11a08a6964d710cb38c3946ca2c90d6aea33f31db46d416ea0160a
6
+ metadata.gz: 1827e0dee1f479b25c541502d175960b9def8660216c874e861d98a4847f872e81388dce6dcd225bba3c75e0323bfec246d993310a0a311cbc0178f45cade208
7
+ data.tar.gz: bf6c142b95cdef0a738f0ca8e34d40dc4e6781c5347409a4777a0c67bfeee36c23583d640f8e9d8bc635d654f8c03087a5e4d160c6fedae45a4ddee62298ad05
data/lib/piece/rules.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'piece/expression.tab'
2
+ require 'piece/seq'
2
3
 
3
4
  module Piece
4
5
  class RulesError < StandardError
@@ -25,7 +26,7 @@ module Piece
25
26
  end
26
27
  alias :add :<<
27
28
 
28
- def delete(*rule)
29
+ def delete(rule)
29
30
  return if rule.empty?
30
31
  parts = rule_parts(rule)
31
32
 
@@ -38,24 +39,26 @@ module Piece
38
39
  if group
39
40
  group.delete_if {|rule| rule == parts[-1]}
40
41
  if group.empty?
41
- self.delete(*(parts[0..-2]))
42
+ self.delete(parts[0..-2])
42
43
  end
43
44
  end
44
45
  end
45
46
 
46
47
  def match?(*action)
47
- self[*action][:match]
48
+ matching_seq(*action).match?
48
49
  end
49
50
 
50
51
  def [](*action)
51
- ret = []
52
- m = eval(@data, action_parts(action), ret)
53
- {:match => m == :match, :reason => ret}
52
+ matching_seq(*action).to_a
54
53
  end
55
54
 
56
55
  private
56
+ def matching_seq(*action)
57
+ eval(@data, action_parts(action))
58
+ end
59
+
57
60
  def action_parts(action)
58
- action.map{|part| part.to_s.split(':')}.flatten.map(&:strip).tap do |ret|
61
+ action.flatten.map{|part| part.to_s.split(':')}.flatten.map(&:strip).tap do |ret|
59
62
  if ret.any?{|part| part.include?('*')}
60
63
  raise InvalidAction, "Should not include '*' in an action"
61
64
  end
@@ -79,73 +82,82 @@ module Piece
79
82
  str =~ /^[\['"](.*)[\]'"]$/ ? $1 : str
80
83
  end
81
84
 
82
- def apply(group, actions, backtrace)
85
+ def apply(group, actions)
83
86
  case group
84
87
  when ExpressionParser::Exp
85
88
  validate_rule_names(group)
86
89
  case group.op
87
90
  when '+'
88
- sub = []
89
- if r = apply(group.left, actions, sub)
90
- backtrace.concat(sub)
91
- r
91
+ left = apply(group.left, actions)
92
+ if left.match?
93
+ Seq[left] + Seq.match
92
94
  else
93
- apply(group.right, actions, backtrace)
95
+ right = apply(group.right, actions)
96
+ if right.match?
97
+ Seq[right] + Seq.match
98
+ else
99
+ Seq[left] + Seq[right] + Seq.mismatch
100
+ end
94
101
  end
95
102
  when '-'
96
- sub1, sub2 = [], []
97
- if r = apply(group.left, actions, sub1)
98
- if apply(group.right, actions, sub2).nil?
99
- backtrace.concat(sub1)
100
- r
103
+ left = apply(group.left, actions)
104
+ if left.match?
105
+ right = apply(group.right, actions)
106
+ if right.match?
107
+ Seq[right] + Seq.mismatch
101
108
  else
102
- backtrace.concat(sub2)
103
- nil
109
+ Seq[left] + Seq[right] + Seq.match
104
110
  end
105
111
  else
106
- backtrace.concat(sub1)
107
- nil
112
+ Seq[left] + Seq.mismatch
108
113
  end
109
114
  else
110
115
  raise "Unknown operator: #{group.op}"
111
116
  end
112
117
  when ExpressionParser::Id
113
118
  if @data.has_key?(group.val)
114
- backtrace << group.val
115
- eval(@data[group.val], actions, backtrace)
119
+ Seq[group.val] + eval(@data[group.val], actions)
116
120
  else
117
- eval(group, actions, backtrace)
121
+ eval(group, actions)
118
122
  end
119
123
  else
120
124
  raise "Unknown type: #{group.inspect}"
121
125
  end
122
126
  end
123
127
 
124
- def eval(data, actions, backtrace=[])
125
- return data.nil? ? nil : :match if actions.empty?
126
- case data
127
- when Hash
128
- backtrace << actions.first
129
- eval(data[actions.first], actions[1..-1], backtrace)
130
- when Array
131
- backtrace << data
132
- eval(data.first, actions) || eval(data[1..-1], actions)
133
- when NilClass
134
- nil
135
- when ExpressionParser::Id
136
- _match_?(data.val, actions.first) ? :match : nil
137
- when String
138
- backtrace << data
139
- apply(ExpressionParser.new.parse(data), actions, backtrace)
128
+ def eval(data, actions)
129
+ if actions.empty?
130
+ Seq.match(!data.nil?)
140
131
  else
141
- raise "Unknown type: #{group.inspect}"
132
+ case data
133
+ when Hash
134
+ Seq[actions.first] + eval(data[actions.first], actions[1..-1])
135
+ when Array
136
+ data.each do |ac|
137
+ ret = eval(ac, actions)
138
+ if ret.match?
139
+ return ret
140
+ end
141
+ end
142
+ Seq[data] + Seq.mismatch
143
+ when NilClass
144
+ Seq.mismatch
145
+ when ExpressionParser::Id
146
+ if data.val == '*'
147
+ Seq.match
148
+ elsif actions.size == 1
149
+ Seq.match(data.val == actions[0])
150
+ else
151
+ Seq.mismatch
152
+ end
153
+ when String
154
+ Seq[data] + apply(ExpressionParser.new.parse(data), actions)
155
+ else
156
+ raise "Unknown type: #{group.inspect}"
157
+ end
142
158
  end
143
159
  end
144
160
 
145
- def _match_?(a, b)
146
- a == b || a.nil? || a == '*' || b == '*'
147
- end
148
-
149
161
  def validate_rule_names(exp)
150
162
  case exp
151
163
  when ExpressionParser::Exp
data/lib/piece/seq.rb ADDED
@@ -0,0 +1,42 @@
1
+ module Piece
2
+ class Seq
3
+ def self.[](*data)
4
+ new(*data)
5
+ end
6
+
7
+ def self.match(m=true)
8
+ m ? new(:match) : mismatch
9
+ end
10
+
11
+ def self.mismatch
12
+ new(:mismatch)
13
+ end
14
+
15
+ def initialize(*data)
16
+ @data = data
17
+ end
18
+
19
+ def match?
20
+ @data.last == :match
21
+ end
22
+
23
+ def mismatch?
24
+ @data.last == :mismatch
25
+ end
26
+
27
+ def +(seq)
28
+ Seq.new(*(to_a.concat(seq.to_a)))
29
+ end
30
+
31
+ def to_a
32
+ @data.map do |d|
33
+ case d
34
+ when Seq
35
+ d.to_a
36
+ else
37
+ d
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
data/lib/piece/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Piece
2
- VERSION = "0.1.5"
2
+ VERSION = "0.1.6"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: piece
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Xiao Li
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-06-06 00:00:00.000000000 Z
11
+ date: 2015-06-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -87,6 +87,7 @@ files:
87
87
  - lib/piece/expression.tab.rb
88
88
  - lib/piece/expression.y
89
89
  - lib/piece/rules.rb
90
+ - lib/piece/seq.rb
90
91
  - lib/piece/version.rb
91
92
  - piece.gemspec
92
93
  homepage: https://github.com/ThoughtWorksStudios/piece