boolean-expression 0.0.1.1 → 0.0.1.2
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.
data/lib/boolean/expression.rb
CHANGED
@@ -23,39 +23,62 @@ end
|
|
23
23
|
|
24
24
|
class Boolean::Expression
|
25
25
|
def self.parse (text)
|
26
|
-
base
|
27
|
-
name
|
28
|
-
stack
|
29
|
-
logic
|
26
|
+
base = Group.new
|
27
|
+
name = nil
|
28
|
+
stack = [base]
|
29
|
+
logic = nil
|
30
|
+
string = false
|
31
|
+
quoted = false
|
30
32
|
|
31
33
|
text.to_s.chars.each_with_index {|char, index|
|
32
34
|
begin
|
33
|
-
if char == ')' && stack.length == 1
|
35
|
+
if !string && char == ')' && stack.length == 1
|
34
36
|
raise SyntaxError, 'closing an unopened parenthesis'
|
35
37
|
end
|
36
38
|
|
37
|
-
if char.match(/\s|\(|\)/) || (!logic && ['|', '&', '!'].member?(char))
|
38
|
-
if logic || (name && name.match(
|
39
|
+
if !string && (char.match(/\s|\(|\)/) || (!logic && ['|', '&', '!'].member?(char)))
|
40
|
+
if logic || (name && name.match(/^(and|or|not)$/i) && !quoted)
|
39
41
|
stack.last << Logic.new(logic || name)
|
40
42
|
logic = nil
|
41
43
|
name = nil
|
42
44
|
elsif name
|
45
|
+
if !stack.last.last.nil? && !stack.last.last.is_a?(Logic)
|
46
|
+
raise SyntaxError, 'you cannot put two names in a row'
|
47
|
+
end
|
48
|
+
|
43
49
|
stack.last << Name.new(name)
|
44
50
|
name = nil
|
51
|
+
quoted = false
|
45
52
|
end
|
46
53
|
end
|
47
54
|
|
48
|
-
if name
|
49
|
-
|
50
|
-
|
55
|
+
if name
|
56
|
+
if char == '"'
|
57
|
+
string = false
|
58
|
+
else
|
59
|
+
name << char
|
60
|
+
end
|
61
|
+
elsif logic
|
62
|
+
logic << char
|
51
63
|
else
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
64
|
+
if char == '"'
|
65
|
+
string = true
|
66
|
+
quoted = true
|
67
|
+
|
68
|
+
next
|
69
|
+
end
|
70
|
+
|
71
|
+
if string
|
72
|
+
name = char unless name
|
73
|
+
else
|
74
|
+
case char
|
75
|
+
when '(' then stack.push Group.new
|
76
|
+
when ')' then stack[-2] << stack.pop
|
77
|
+
when '|' then logic = '|'
|
78
|
+
when '&' then logic = '&'
|
79
|
+
when '!' then stack.last << Logic.new('!')
|
80
|
+
else name = char if !char.match(/\s/)
|
81
|
+
end
|
59
82
|
end
|
60
83
|
end
|
61
84
|
rescue SyntaxError => e
|
@@ -67,7 +90,13 @@ class Boolean::Expression
|
|
67
90
|
|
68
91
|
raise SyntaxError, 'the expression cannot end with a logic operator' if logic
|
69
92
|
|
70
|
-
|
93
|
+
if name
|
94
|
+
if !stack.last.last.nil? && !stack.last.last.is_a?(Logic)
|
95
|
+
raise SyntaxError, 'you cannot put two names in a row'
|
96
|
+
end
|
97
|
+
|
98
|
+
base << Name.new(name)
|
99
|
+
end
|
71
100
|
|
72
101
|
base = base.first if base.length == 1 && base.first.is_a?(Group)
|
73
102
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: boolean-expression
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.1.
|
4
|
+
version: 0.0.1.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-02-09 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description:
|
15
15
|
email: meh@paranoici.org
|
@@ -41,7 +41,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
41
41
|
version: '0'
|
42
42
|
requirements: []
|
43
43
|
rubyforge_project:
|
44
|
-
rubygems_version: 1.8.
|
44
|
+
rubygems_version: 1.8.15
|
45
45
|
signing_key:
|
46
46
|
specification_version: 3
|
47
47
|
summary: A simple library to evaluate boolean expressions.
|