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.
@@ -23,39 +23,62 @@ end
23
23
 
24
24
  class Boolean::Expression
25
25
  def self.parse (text)
26
- base = Group.new
27
- name = nil
28
- stack = [base]
29
- logic = nil
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(/(and|or|not)/i))
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 || logic
49
- name << char if name
50
- logic << char if logic
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
- case char
53
- when '(' then stack.push Group.new
54
- when ')' then stack[-2] << stack.pop
55
- when '|' then logic = '|'
56
- when '&' then logic = '&'
57
- when '!' then stack.last << Logic.new('!')
58
- else name = char if !char.match(/\s/)
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
- base << Name.new(name) if name
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
 
@@ -18,7 +18,9 @@
18
18
  #++
19
19
 
20
20
  class Boolean::Expression::Group < Array
21
- def inspect
21
+ def to_s
22
22
  '(' + map { |e| e.inspect }.join(' ') + ')'
23
23
  end
24
+
25
+ alias inspect to_s
24
26
  end
@@ -37,7 +37,9 @@ class Boolean::Expression::Logic
37
37
  end
38
38
  end
39
39
 
40
- def inspect
40
+ def to_s
41
41
  type.to_s.upcase
42
42
  end
43
+
44
+ alias inspect to_s
43
45
  end
@@ -19,6 +19,8 @@
19
19
 
20
20
  class Boolean::Expression::Name < String
21
21
  def inspect
22
- to_s.downcase
22
+ (match(/[\s!&|]/) || %w(and or not).include?(downcase)) ? super : self
23
23
  end
24
+
25
+ alias to_s inspect
24
26
  end
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.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-01-28 00:00:00.000000000 Z
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.10
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.