piece 0.1.4 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +0 -2
- data/TODO +0 -1
- data/lib/piece/rules.rb +58 -14
- data/lib/piece/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4ab088dfe0e66ffc1450fc9eab486b2c10ca6d19
|
4
|
+
data.tar.gz: a0ec38dcea8530a04e2088a8364e7a80e2552a06
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d54d5a7e9bcdb09df0264b9bb50bd52cdfc5b0ad5fd8cc7f4096a3b188595671e1bd85d06b39257f8e206a1b3f9181dca9c1ce66b1afaa14b1576b8769513e74
|
7
|
+
data.tar.gz: e9ac20ef0b1a3643ddcf861e9a448ebc968d56a78f04b213de6f5eb8d264d3d8bcbb2dc9eb11a08a6964d710cb38c3946ca2c90d6aea33f31db46d416ea0160a
|
data/.travis.yml
CHANGED
data/TODO
CHANGED
data/lib/piece/rules.rb
CHANGED
@@ -1,7 +1,13 @@
|
|
1
1
|
require 'piece/expression.tab'
|
2
2
|
|
3
3
|
module Piece
|
4
|
-
class
|
4
|
+
class RulesError < StandardError
|
5
|
+
end
|
6
|
+
|
7
|
+
class InvalidAction < RulesError
|
8
|
+
end
|
9
|
+
|
10
|
+
class UnknownRuleGroupName < RulesError
|
5
11
|
end
|
6
12
|
|
7
13
|
class Rules
|
@@ -38,11 +44,13 @@ module Piece
|
|
38
44
|
end
|
39
45
|
|
40
46
|
def match?(*action)
|
41
|
-
|
47
|
+
self[*action][:match]
|
42
48
|
end
|
43
49
|
|
44
50
|
def [](*action)
|
45
|
-
|
51
|
+
ret = []
|
52
|
+
m = eval(@data, action_parts(action), ret)
|
53
|
+
{:match => m == :match, :reason => ret}
|
46
54
|
end
|
47
55
|
|
48
56
|
private
|
@@ -71,41 +79,64 @@ module Piece
|
|
71
79
|
str =~ /^[\['"](.*)[\]'"]$/ ? $1 : str
|
72
80
|
end
|
73
81
|
|
74
|
-
def apply(group, actions)
|
82
|
+
def apply(group, actions, backtrace)
|
75
83
|
case group
|
76
84
|
when ExpressionParser::Exp
|
85
|
+
validate_rule_names(group)
|
77
86
|
case group.op
|
78
87
|
when '+'
|
79
|
-
|
88
|
+
sub = []
|
89
|
+
if r = apply(group.left, actions, sub)
|
90
|
+
backtrace.concat(sub)
|
91
|
+
r
|
92
|
+
else
|
93
|
+
apply(group.right, actions, backtrace)
|
94
|
+
end
|
80
95
|
when '-'
|
81
|
-
|
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
|
101
|
+
else
|
102
|
+
backtrace.concat(sub2)
|
103
|
+
nil
|
104
|
+
end
|
105
|
+
else
|
106
|
+
backtrace.concat(sub1)
|
107
|
+
nil
|
108
|
+
end
|
82
109
|
else
|
83
110
|
raise "Unknown operator: #{group.op}"
|
84
111
|
end
|
85
112
|
when ExpressionParser::Id
|
86
113
|
if @data.has_key?(group.val)
|
87
|
-
|
114
|
+
backtrace << group.val
|
115
|
+
eval(@data[group.val], actions, backtrace)
|
88
116
|
else
|
89
|
-
|
117
|
+
eval(group, actions, backtrace)
|
90
118
|
end
|
91
119
|
else
|
92
120
|
raise "Unknown type: #{group.inspect}"
|
93
121
|
end
|
94
122
|
end
|
95
123
|
|
96
|
-
def
|
97
|
-
return data.nil? ? nil :
|
124
|
+
def eval(data, actions, backtrace=[])
|
125
|
+
return data.nil? ? nil : :match if actions.empty?
|
98
126
|
case data
|
99
127
|
when Hash
|
100
|
-
|
128
|
+
backtrace << actions.first
|
129
|
+
eval(data[actions.first], actions[1..-1], backtrace)
|
101
130
|
when Array
|
102
|
-
|
131
|
+
backtrace << data
|
132
|
+
eval(data.first, actions) || eval(data[1..-1], actions)
|
103
133
|
when NilClass
|
104
134
|
nil
|
105
135
|
when ExpressionParser::Id
|
106
|
-
_match_?(data.val, actions.first) ?
|
136
|
+
_match_?(data.val, actions.first) ? :match : nil
|
107
137
|
when String
|
108
|
-
|
138
|
+
backtrace << data
|
139
|
+
apply(ExpressionParser.new.parse(data), actions, backtrace)
|
109
140
|
else
|
110
141
|
raise "Unknown type: #{group.inspect}"
|
111
142
|
end
|
@@ -114,5 +145,18 @@ module Piece
|
|
114
145
|
def _match_?(a, b)
|
115
146
|
a == b || a.nil? || a == '*' || b == '*'
|
116
147
|
end
|
148
|
+
|
149
|
+
def validate_rule_names(exp)
|
150
|
+
case exp
|
151
|
+
when ExpressionParser::Exp
|
152
|
+
validate_rule_names(exp.left)
|
153
|
+
validate_rule_names(exp.right)
|
154
|
+
when ExpressionParser::Id
|
155
|
+
if exp.val != '*' && !@data.has_key?(exp.val)
|
156
|
+
raise UnknownRuleGroupName,
|
157
|
+
"Expecting '#{exp.val}' in expression to be a root rule group name(root group names: #{@data.keys.inspect}) you defined. "
|
158
|
+
end
|
159
|
+
end
|
160
|
+
end
|
117
161
|
end
|
118
162
|
end
|
data/lib/piece/version.rb
CHANGED
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.
|
4
|
+
version: 0.1.5
|
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-
|
11
|
+
date: 2015-06-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|