yaparc 0.2.3 → 0.3.0

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/test/test_prolog.rb DELETED
@@ -1,287 +0,0 @@
1
- require 'lib/yaparc.rb'
2
- require 'test/unit'
3
-
4
- =begin
5
-
6
- <prog> ::= <clause> [ <clause> ]*
7
- <clause> ::= <fact> | <rule> | <query>
8
-
9
- <fact> ::= <compoundterm> .
10
- <compoundterm> ::= <atom> [ ( <arglist> ) ]*
11
-
12
- <rule> ::= <head> :- <body> .
13
- <query> ::= ?- <body> .
14
- <head> ::= <compoundterm>
15
- <body> ::= <goal> [, <goal>]*
16
- <goal> ::= <compoundterm> | !
17
-
18
- <arglist> := <arg>[, <arg>]*
19
- <arg> ::= <atom> | <var> | <list>
20
-
21
- <atom> ::= /[a-zA-Z][a-zA-Z0-9]*/
22
- | '/[a-zA-Z0-9]+/'
23
-
24
- <var> ::= ?/[a-zA-Z0-9]+/
25
-
26
- <list> ::= <leftbracket> <rightbracket>
27
- <list> ::= <leftbracket> <listelems> <rightbracket>
28
- <listelems> ::= <arglist>
29
- <listelems> ::= <arglist> <barsymbol> <list>
30
- <listelems> ::= <arglist> <barsymbol> <var>
31
-
32
- =end
33
-
34
- module PrologParser
35
- # KEYWORDS = %w{}
36
- # <prog> ::= <clause> [ <clause> ]*
37
- class Program
38
- include Yaparc::Parsable
39
-
40
- def initialize
41
- @parser = lambda do |input|
42
- Yaparc::Seq.new(Yaparc::ManyOne.new(Clause.new,[])) do |clauses|
43
- #LogRuby::Program.new(*clauses)
44
- end
45
- end
46
- end
47
- end
48
-
49
- # <clause> ::= <fact> | <rule> | <query>
50
- class Clause
51
- include Yaparc::Parsable
52
- def initialize
53
-
54
- @parser = lambda do |input|
55
- Yaparc::Alt.new(
56
- PrologParser::Fact.new,
57
- PrologParser::Rule.new,
58
- PrologParser::Query.new
59
- )
60
- end
61
- end
62
- end
63
-
64
- # <fact> ::= <compoundterm> .
65
- class Fact
66
- include Yaparc::Parsable
67
- def initialize
68
- @parser = lambda do |input|
69
- Yaparc::Seq.new(
70
- PrologParser::CompoundTerm.new,
71
- Yaparc::Literal.new('.')) do |compoundterm,_|
72
- #LogRuby::Fact.new(compoundterm)
73
- end
74
- end
75
- end
76
- end
77
-
78
- # <compoundterm> ::= <atom> [ ( <arglist> ) ]*
79
- class CompoundTerm
80
- include Yaparc::Parsable
81
- def initialize
82
- @parser = lambda do |input|
83
- Yaparc::Seq.new(
84
- PrologParser::Atom.new,
85
- Yaparc::ManyOne.new(
86
- Yaparc::Seq.new(
87
- Yaparc::Literal.new('('),
88
- PrologParser::ArgList.new,
89
- Yaparc::Literal.new(')')) do |_,arglist,_|
90
- arglist
91
- end) do ||
92
- end) do |head, tail|
93
- #LogRuby::CompoundTerm.new(head,*tail)
94
- end
95
- end
96
- end
97
- end
98
-
99
-
100
- # <rule> ::= <head> :- <body> .
101
- class Rule
102
- include Yaparc::Parsable
103
- def initialize
104
- @parser = lambda do |input|
105
- Yaparc::Seq.new(
106
- PrologParser::Head.new,
107
- Yaparc::Literal.new(':-'),
108
- PrologParser::Body.new,
109
- Yaparc::Literal.new('.')) do |head,_,body,_|
110
- #LogRuby::Rule.new(head,*body)
111
- end
112
- end
113
- end
114
- end
115
-
116
- # <query> ::= ?- <body> .
117
- class Query
118
- include Yaparc::Parsable
119
- def initialize
120
- @parser = lambda do |input|
121
- Yaparc::Seq.new(
122
- Yaparc::Literal.new('?-'),
123
- PrologParser::Body.new,
124
- Yaparc::Literal.new('.')) do |_,body,_|
125
- # LogRuby::Query.new(*body)
126
- end
127
- end
128
- end
129
- end
130
-
131
- # <head> ::= <compoundterm>
132
- class Head
133
- include Yaparc::Parsable
134
- def initialize
135
- @parser = lambda do |input|
136
- PrologParser::CompoundTerm.new
137
- end
138
- end
139
- end
140
-
141
- # <body> ::= <goal> [, <goal>]*
142
- class Body
143
- include Yaparc::Parsable
144
- def initialize
145
- @parser = lambda do |input|
146
- Yaparc::ManyOne.new(PrologParser::Goal.new,[])
147
- end
148
- end
149
- end
150
-
151
- # <goal> ::= <compoundterm> | !
152
- class Goal
153
- include Yaparc::Parsable
154
- def initialize
155
- @parser = lambda do |input|
156
- Yaparc::Alt.new(
157
- PrologParser::CompoundTerm.new,
158
- Yaparc::Literal.new('!'))
159
- end
160
- end
161
- end
162
-
163
- # <arglist> := <arg>[, <arg>]*
164
- class ArgList
165
- include Yaparc::Parsable
166
- def initialize
167
- @parser = lambda do |input|
168
- Yaparc::Seq.new(
169
- PrologParser::Arg.new,
170
- Yaparc::Many.new(
171
- Yaparc::Seq.new(Yaparc::Literal.new(','), PrologParser::Arg.new),[])) do |head,tail|
172
- [head] + tail
173
- end
174
- end
175
- end
176
- end
177
-
178
- # <arg> ::= <atom> | <var> | <list>
179
- class Arg
180
- include Yaparc::Parsable
181
- def initialize
182
- @parser = lambda do |input|
183
- Yaparc::Alt.new(
184
- PrologParser::Variable.new,
185
- PrologParser::Atom.new,
186
- PrologParser::List.new)
187
- end
188
- end
189
- end
190
-
191
- # <atom> ::= /[a-zA-Z][a-zA-Z0-9]*/
192
- # | '/[a-zA-Z0-9]+/'
193
- class Atom
194
- include Yaparc::Parsable
195
- def initialize
196
- @parser = lambda do |input|
197
- Yaparc::Seq.new(Yaparc::Regex.new(/[a-zA-Z][a-zA-Z0-9]*/)) do |ide|
198
- #LogRuby::Atom.new(ide)
199
- end
200
- end
201
- end
202
- end
203
-
204
- # <var> ::= ?/[a-zA-Z0-9]+/
205
- class Variable
206
- include Yaparc::Parsable
207
- def initialize
208
- @parser = lambda do |input|
209
- Yaparc::Seq.new(
210
- Yaparc::Literal.new("?"),
211
- Yaparc::Regex.new(/[a-zA-Z0-9]+/)) do |_,var|
212
- # LogRuby::Variable.new(var)
213
- end
214
- end
215
- end
216
- end
217
-
218
- # <list> ::= <leftbracket> <rightbracket>
219
- class List
220
- include Yaparc::Parsable
221
- def initialize
222
- @parser = lambda do |input|
223
- Yaparc::Seq.new(
224
- Yaparc::Literal.new("["),
225
- Yaparc::Literal.new("]"))
226
- end
227
- end
228
- end
229
- end # of PrologParser
230
-
231
-
232
- class PrologTest < Test::Unit::TestCase
233
- include ::Yaparc
234
-
235
-
236
- def test_variable
237
- var = PrologParser::Variable.new
238
- result = var.parse("?Var")
239
- assert_instance_of Result::OK, result
240
- # assert_equal LogRuby::Variable.new("Var"), result.value
241
- end
242
-
243
- def test_atom
244
- atom = PrologParser::Atom.new
245
- result = atom.parse("atom")
246
- assert_instance_of Result::OK, result
247
- # assert_equal LogRuby::Atom.new("atom"), result.value
248
- end
249
-
250
- def test_arglist
251
- arglist = PrologParser::ArgList.new
252
- result = arglist.parse("term1,term2,term3")
253
- assert_instance_of Result::OK, result
254
- # assert_equal LogRuby::Atom.new("atom"), result.value
255
- end
256
-
257
- def test_compoundterm
258
- compoundterm = PrologParser::CompoundTerm.new
259
- result = compoundterm.parse("functor(term1,term2,term3)")
260
- assert_instance_of Result::OK, result
261
- # assert_equal LogRuby::CompoundTerm.new(LogRuby::Atom.new("functor"),
262
- # LogRuby::Atom.new("term1"),LogRuby::Atom.new("term2"),LogRuby::Atom.new("term3")), result.value
263
- end
264
-
265
- def test_query
266
- query = PrologParser::Query.new
267
- result = query.parse("?- functor(?var,term).")
268
- assert_instance_of Result::OK, result
269
- # assert_equal LogRuby::Query.new(LogRuby::CompoundTerm.new(LogRuby::Atom.new('functor'),LogRuby::Variable.new('var'), LogRuby::Atom.new('term'))), result.value
270
- end
271
-
272
- def test_fact
273
- fact = PrologParser::Fact.new
274
- result = fact.parse("functor(term).")
275
- assert_instance_of Result::OK, result
276
- # assert_equal LogRuby::Fact.new(LogRuby::CompoundTerm.new(LogRuby::Atom.new('functor'),LogRuby::Atom.new('term'))), result.value
277
- end
278
-
279
- def test_rule
280
- # likes(X, P) :- based(P,Y), likes(X,Y).
281
- rule_parser = PrologParser::Rule.new
282
- result = rule_parser.parse("likes(?X, ?P) :- based(?P,?Y), likes(?X,?Y).")
283
- assert_instance_of Result::OK, result
284
- # assert_equal rule, result.value
285
- end
286
-
287
- end
data/test/test_sql.rb DELETED
@@ -1,317 +0,0 @@
1
- require 'lib/yaparc.rb'
2
- require 'test/unit'
3
-
4
- ### c.f. http://www11.plala.or.jp/sotsuken/db/sql.html
5
- ###
6
- ### query_body := select_expression from_expression where_expression
7
- ### select_expression := 'select' term_sequence
8
- ### term_sequence := term[,term_sequence]*
9
- ### from_expression := 'from' path_expression[, path_expression]*
10
- ### path_expression := '/' term [path_expression]*
11
- ### where_expression :=
12
- ### | search_cond
13
- ### search_cond := term op term
14
- ### | ( NOT search_cond )
15
- ### | ( search_cond AND search_cond )
16
- ### | ( search_cond OR search_cond )
17
- ### | EXISTS ( query_body )
18
- ### | term op ANY ( query_body )
19
- ### | term op ALL ( query_body )
20
- ### | term IN ( query_body )
21
- ### term := any_characters
22
- ### op := <> | = | < | > | <= | >=
23
-
24
- module SQL
25
-
26
- KEYWORDS = %w{NOT AND OR ALL IN select from where EXISTS}
27
-
28
- ### query_body := select_expression from_expression where_expression
29
- class QueryBody
30
- include Yaparc::Parsable
31
-
32
- def initialize
33
- @parser = lambda do |input|
34
- Yaparc::Seq.new(SelectExpression.new,
35
- FromExpression.new,
36
- WhereExpression.new) do |select, from, where|
37
- {:select => select, :from => from, :where => where }
38
- end
39
- end
40
- end
41
- end
42
-
43
- ### select_expression := 'select' term_sequence
44
- class SelectExpression
45
- include Yaparc::Parsable
46
-
47
- def initialize
48
- @parser = lambda do |input|
49
- Yaparc::Seq.new(
50
- Yaparc::Symbol.new('select'),
51
- TermSequence.new) do |_,terms|
52
- terms
53
- end
54
- end
55
- end
56
- end
57
-
58
- ### term_sequence := term[,term_sequence]*
59
- class TermSequence
60
- include Yaparc::Parsable
61
-
62
- def initialize
63
- @parser = lambda do |input|
64
- Yaparc::Seq.new(
65
- Term.new,
66
- Yaparc::Many.new(
67
- Yaparc::Seq.new(
68
- Yaparc::Symbol.new(','),
69
- TermSequence.new) do |_, terms|
70
- terms
71
- end)
72
- ) do |term,terms|
73
- [term] + terms
74
- end
75
- end
76
- end
77
- end
78
-
79
- ### from_expression := 'from' path_expression[, path_expression]*
80
- class FromExpression
81
- include Yaparc::Parsable
82
-
83
- def initialize
84
- @parser = lambda do |input|
85
- Yaparc::Seq.new(
86
- Yaparc::Symbol.new('from'),
87
- PathExpression.new,
88
- Yaparc::Many.new(
89
- Yaparc::Seq.new(
90
- Yaparc::Symbol.new(','),
91
- PathExpression.new) do |_, path|
92
- [path]
93
- end)
94
- ) do |_,path,paths|
95
- path + paths
96
- end
97
- end
98
- end
99
- end
100
-
101
-
102
- ### path_expression := '/' term [path_expression]*
103
- class PathExpression
104
- include Yaparc::Parsable
105
-
106
- def initialize
107
- @parser = lambda do |input|
108
- Yaparc::Seq.new(
109
- Yaparc::Symbol.new('/'),
110
- Term.new,
111
- Yaparc::Many.new(
112
- PathExpression.new do |path|
113
- path
114
- end)
115
- ) do |_,term, paths|
116
- [term] + paths
117
- end
118
- end
119
- end
120
- end
121
-
122
- ### where_expression :=
123
- ### | search_cond
124
- class WhereExpression
125
- include Yaparc::Parsable
126
-
127
- def initialize
128
- @parser = lambda do |input|
129
- Yaparc::Many.new(SearchCond.new)
130
- end
131
- end
132
- end
133
-
134
- ### term := any_characters
135
- class Term
136
- include Yaparc::Parsable
137
-
138
- def initialize#(*keywords)
139
- @parser = lambda do |input|
140
- # Yaparc::Identifier.new(*KEYWORDS)
141
- Yaparc::Identifier.new(:exclude => KEYWORDS)
142
- end
143
- end
144
- end
145
-
146
- ### search_cond := term op term
147
- ### | ( NOT search_cond )
148
- ### | ( search_cond AND search_cond )
149
- ### | ( search_cond OR search_cond )
150
- ### | EXISTS ( query_body )
151
- ### | term op ANY ( query_body )
152
- ### | term op ALL ( query_body )
153
- ### | term IN ( query_body )
154
-
155
- class SearchCond
156
- include Yaparc::Parsable
157
-
158
- def initialize
159
- @parser = lambda do |input|
160
- Yaparc::Alt.new(
161
- Yaparc::Seq.new(Term.new,
162
- Op.new,
163
- Term.new) do |term1, op, term2|
164
- {:operator => op, :args => [term1, term2]}
165
- end,
166
- Yaparc::Seq.new(Yaparc::Symbol.new('('),
167
- Yaparc::Symbol.new('NOT'),
168
- SearchCond.new,
169
- Yaparc::Symbol.new(')')) do |_, _, cond, _|
170
- {:logic => :not, :conditions => [cond]}
171
- end,
172
- Yaparc::Seq.new(Yaparc::Symbol.new('('),
173
- SearchCond.new,
174
- Yaparc::Symbol.new('AND'),
175
- SearchCond.new,
176
- Yaparc::Symbol.new(')')) do |_, cond1, _,cond2,_|
177
- {:logic => :and, :conditions => [cond1, cond2]}
178
- end,
179
- Yaparc::Seq.new(Yaparc::Symbol.new('('),
180
- SearchCond.new,
181
- Yaparc::Symbol.new('OR'),
182
- SearchCond.new,
183
- Yaparc::Symbol.new(')')) do |_, cond1, _,cond2,_|
184
- {:logic => :or, :conditions => [cond1,cond2]}
185
- end,
186
- Yaparc::Seq.new(Yaparc::Symbol.new('EXISTS'),
187
- Yaparc::Symbol.new('('),
188
- QueryBody.new,
189
- Yaparc::Symbol.new(')')) do |_, _, body,_|
190
- {:logic => :exits, :condition => body}
191
- end,
192
- # term op ANY ( query_body )
193
- Yaparc::Seq.new(Term.new,
194
- Op.new,
195
- Yaparc::Symbol.new('ANY'),
196
- Yaparc::Symbol.new('('),
197
- QueryBody.new,
198
- Yaparc::Symbol.new(')')) do |term, op, _, _, body,_|
199
- {:operator => op,
200
- :term1 => term,
201
- :term2 => {:logic => :any, :condition => body}
202
- }
203
- end,
204
- # term op ALL ( query_body )
205
- Yaparc::Seq.new(Term.new,
206
- Op.new,
207
- Yaparc::Symbol.new('ALL'),
208
- Yaparc::Symbol.new('('),
209
- QueryBody.new,
210
- Yaparc::Symbol.new(')')) do |term, op, _, _, body,_|
211
- {:operator => op,
212
- :term1 => term,
213
- :term2 => {:logic => :ALL, :condition => body}
214
- }
215
- end,
216
- # term IN ( query_body )
217
- Yaparc::Seq.new(Term.new,
218
- Op.new,
219
- Yaparc::Symbol.new('IN'),
220
- Yaparc::Symbol.new('('),
221
- QueryBody.new,
222
- Yaparc::Symbol.new(')')) do |term, op, _, _, body,_|
223
- {:operator => op,
224
- :term1 => term,
225
- :term2 => {:logic => :IN, :condition => body}
226
- }
227
- end)
228
- end
229
- end
230
- end
231
-
232
- ### op := <> | = | < | > | <= | >=
233
- class Op
234
- include Yaparc::Parsable
235
-
236
- def initialize
237
- @parser = lambda do |input|
238
- Yaparc::Alt.new(
239
- Yaparc::Apply.new(Yaparc::Symbol.new('<>')) {|_| :not},
240
- Yaparc::Apply.new(Yaparc::Symbol.new('<=')) {|_| :lesser_eq },
241
- Yaparc::Apply.new(Yaparc::Symbol.new('>=')) {|_| :greater_eq },
242
- Yaparc::Apply.new(Yaparc::Symbol.new('<')) {|_| :lesser},
243
- Yaparc::Apply.new(Yaparc::Symbol.new('>')) {|_| :greater })
244
- end
245
- end
246
- end
247
- end # of SQL
248
-
249
-
250
- class YaparcQueryTest < Test::Unit::TestCase
251
- include ::Yaparc
252
-
253
- def test_op
254
- op = SQL::Op.new
255
- result = op.parse("<")
256
- assert_instance_of Result::OK, result
257
- assert_equal :lesser, result.value
258
- result = op.parse("<=")
259
- assert_instance_of Result::OK, result
260
- assert_equal :lesser_eq, result.value
261
- end
262
-
263
- def test_term
264
- term = SQL::Term.new
265
- result = term.parse("abc")
266
- assert_instance_of Result::OK, result
267
- assert_equal "abc", result.value
268
- end
269
-
270
- def test_path_expression
271
- path = SQL::PathExpression.new
272
- result = path.parse("/xyz")
273
- assert_instance_of Result::OK, result
274
- assert_equal ["xyz"], result.value
275
- result = path.parse("/abc/def")
276
- assert_instance_of Result::OK, result
277
- assert_equal ["abc", "def"], result.value
278
- end
279
-
280
- def test_from_expression
281
- path = SQL::FromExpression.new
282
- result = path.parse("from /xyz")
283
- assert_instance_of Result::OK, result
284
- assert_equal ["xyz"], result.value
285
- result = path.parse("from /abc/def")
286
- assert_instance_of Result::OK, result
287
- assert_equal ["abc", "def"], result.value
288
- end
289
-
290
- def test_search_cond
291
- search = SQL::SearchCond.new
292
- result = search.parse("abc <> xyz")
293
- assert_instance_of Result::OK, result
294
- assert_equal Hash[:operator=> :not, :args=>["abc", "xyz"]], result.value
295
-
296
- result = search.parse("(NOT abc <> xyz)")
297
- assert_instance_of Result::OK, result
298
- assert_equal Hash[:logic=>:not, :conditions=>[{:operator=>:not, :args=>["abc", "xyz"]}]], result.value
299
- result = search.parse("(abc <> xyz AND abc > xyz)")
300
- assert_instance_of Result::OK, result
301
- assert_equal Hash[
302
- :logic=>:and, :conditions=> [{:operator=>:not, :args=>["abc", "xyz"]},
303
- {:operator=>:greater, :args=>["abc", "xyz"]}]
304
- ], result.value
305
- end
306
-
307
- def test_query_body
308
- query = SQL::QueryBody.new
309
- result = query.parse("select abc from /xyz")
310
- assert_instance_of Result::OK, result
311
- assert_equal Hash[:from=>["xyz"], :where=>[], :select=>["abc"]], result.value
312
- result = query.parse("select abc from /xyz/fgh")
313
- assert_instance_of Result::OK, result
314
- assert_equal Hash[:from=>["xyz","fgh"], :where=>[], :select=>["abc"]], result.value
315
- end
316
-
317
- end