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 +4 -4
- data/lib/factbase/syntax.rb +15 -2
- data/lib/factbase/term.rb +1 -1
- data/lib/factbase.rb +1 -1
- data/test/factbase/test_syntax.rb +22 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dc21a8440801909645bc06e200ca9b238ab899f6ecd5ef422eec4d082fdbbfaf
|
4
|
+
data.tar.gz: 05fb34e5249ec11dd0cddcfe74888d89a3343c44afada52926f720c102804321
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6c72854f13afe4af7be6a232c12c60025cd9d286559940e96a0ebd9decf748e13e8db4993bffe64f8416d3c03cd20f9a491c06c55434bdba2f53c32edd7d7410
|
7
|
+
data.tar.gz: e44f1b7c3c83f207ead55e5aed42844eb98dd53ee6ef5241be0e0931cbe7670dc0c6103c002ce6a4a7cbc8818784de6c7f600a7102d91a20a2dcc732a62fc5d7
|
data/lib/factbase/syntax.rb
CHANGED
@@ -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
|
-
|
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
data/lib/factbase.rb
CHANGED
@@ -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
|