factbase 0.0.3 → 0.0.4

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
  SHA256:
3
- metadata.gz: 270354d428517846946f93bec8e2e5b4c134306ca2c2136d5d99eae6add60b65
4
- data.tar.gz: 170078b593a186785869f33fdb41e5152d8d34d5b7a47eb65a9b5756939592a0
3
+ metadata.gz: dc21a8440801909645bc06e200ca9b238ab899f6ecd5ef422eec4d082fdbbfaf
4
+ data.tar.gz: 05fb34e5249ec11dd0cddcfe74888d89a3343c44afada52926f720c102804321
5
5
  SHA512:
6
- metadata.gz: afecc5884796f2e67c8f95bab613c6b82f92291c424adbea1c883a19c7c3816475ea14897ea4079434da71e1a382ee40aa4e7e62347702116f999ea40de6d24a
7
- data.tar.gz: d14d86d5ea751d540dd3ed5d52b3aba747f70b6db9ec295717abdcb30af2d57793b2d2b0cc2607eb4e90d509a53f0a6d336865afddcbce658ed2fd7e9a3f07b0
6
+ metadata.gz: 6c72854f13afe4af7be6a232c12c60025cd9d286559940e96a0ebd9decf748e13e8db4993bffe64f8416d3c03cd20f9a491c06c55434bdba2f53c32edd7d7410
7
+ data.tar.gz: e44f1b7c3c83f207ead55e5aed42844eb98dd53ee6ef5241be0e0931cbe7670dc0c6103c002ce6a4a7cbc8818784de6c7f600a7102d91a20a2dcc732a62fc5d7
@@ -37,7 +37,9 @@ class Factbase::Syntax
37
37
  def to_term
38
38
  @tokens ||= to_tokens
39
39
  @ast ||= to_ast(@tokens, 0)
40
- @ast[0]
40
+ term = @ast[0]
41
+ raise 'No terms found' if term.nil?
42
+ term
41
43
  end
42
44
 
43
45
  private
@@ -50,6 +52,7 @@ class Factbase::Syntax
50
52
  # is the term/literal and the second one is the position where the
51
53
  # scanning should continue.
52
54
  def to_ast(tokens, at)
55
+ raise "Closing too soon at ##{at}" if tokens[at] == :close
53
56
  return [tokens[at], at + 1] unless tokens[at] == :open
54
57
  at += 1
55
58
  op = tokens[at]
@@ -57,8 +60,11 @@ class Factbase::Syntax
57
60
  operands = []
58
61
  at += 1
59
62
  loop do
63
+ raise "End of token stream at ##{at}" if tokens[at].nil?
60
64
  break if tokens[at] == :close
61
65
  (operand, at1) = to_ast(tokens, at)
66
+ raise "Stuck at position ##{at}" if at == at1
67
+ raise "Jump back at position ##{at}" if at1 < at
62
68
  at = at1
63
69
  operands << operand
64
70
  break if tokens[at] == :close
@@ -71,7 +77,13 @@ class Factbase::Syntax
71
77
  acc = ''
72
78
  string = false
73
79
  @query.to_s.chars.each do |c|
74
- string = !string if ['\'', '"'].include?(c)
80
+ if ['\'', '"'].include?(c)
81
+ if string && acc[acc.length - 1] == '\\'
82
+ acc = acc[0..-2]
83
+ else
84
+ string = !string
85
+ end
86
+ end
75
87
  if string
76
88
  acc += c
77
89
  next
@@ -91,6 +103,7 @@ class Factbase::Syntax
91
103
  acc += c
92
104
  end
93
105
  end
106
+ raise 'String not closed' if string
94
107
  list.map do |t|
95
108
  if t.is_a?(Symbol)
96
109
  t
data/lib/factbase/term.rb CHANGED
@@ -44,7 +44,7 @@ class Factbase::Term
44
44
  items << @op
45
45
  items += @operands.map do |o|
46
46
  if o.is_a?(String)
47
- "'#{o}'"
47
+ "'#{o.gsub("'", "\\\\'").gsub('"', '\\\\"')}'"
48
48
  else
49
49
  o.to_s
50
50
  end
data/lib/factbase.rb CHANGED
@@ -26,7 +26,7 @@
26
26
  # License:: MIT
27
27
  class Factbase
28
28
  # Current version of the library and of this class.
29
- VERSION = '0.0.3'
29
+ VERSION = '0.0.4'
30
30
 
31
31
  # Constructor.
32
32
  def initialize
@@ -57,6 +57,8 @@ class TestSyntax < Minitest::Test
57
57
  '(foo)',
58
58
  '(foo 7)',
59
59
  "(foo 7 'Dude')",
60
+ "(r 'Dude\\'s Friend')",
61
+ "(r 'I\\'m \\\"good\\\"')",
60
62
  '(foo x y z)',
61
63
  "(foo x y z t f 42 'Hi!' 33)",
62
64
  '(foo (x) y z)',
@@ -80,4 +82,24 @@ class TestSyntax < Minitest::Test
80
82
  assert_equal(v, Factbase::Syntax.new(k).to_term.matches?(m), k)
81
83
  end
82
84
  end
85
+
86
+ def test_broken_syntax
87
+ [
88
+ '',
89
+ '(foo',
90
+ '"hello, world!',
91
+ '(foo 7',
92
+ "(foo 7 'Dude'",
93
+ '(foo x y z (',
94
+ '(foo x y (z t (f 42 ',
95
+ ')foo ) y z)',
96
+ ")y 42 'Hey you)",
97
+ ')',
98
+ '"'
99
+ ].each do |q|
100
+ assert_raises do
101
+ Factbase::Syntax.new(q).to_term
102
+ end
103
+ end
104
+ end
83
105
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: factbase
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yegor Bugayenko