cortexio-grammar 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8f96b4d4964fd0420114c961659f5685245049ff
4
+ data.tar.gz: 4857b0c4edc2f860bdb7407886203a293faea85d
5
+ SHA512:
6
+ metadata.gz: b35c8e4ce6fcd2022e9a7ef0400e77e7aaea97d6af8aa4934699ab82120f8fef2f0d19d3cd8ffe9afa35e6184048caa318aaaf2b04636bac31fde869766f6060
7
+ data.tar.gz: 17a34646381c6e0e227ec76160b8869f57680d12bdd71e98ca0afc9ee19cb2a0cf005cc530c0f94935b04f09bb29e187f8ab0d57d2c9779c11b04365e26c26ac
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ **/.DS_Store
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+ ruby '2.0.0'
3
+
4
+ gemspec
@@ -0,0 +1,12 @@
1
+ CortexIO Grammer
2
+ ====
3
+ A sample grammar using citrus. Run:
4
+
5
+ cql_grammar_console
6
+
7
+ ..for interactive grammar validadtion.
8
+
9
+
10
+ Credit
11
+ ----
12
+ Written by Preston Lee. @prestonism http://prestonlee.com
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+
4
+ require 'rake/testtask'
5
+
6
+ Rake::TestTask.new do |t|
7
+ t.libs << 'lib/cortexio-grammar'
8
+ t.test_files = FileList['test/lib/cortexio-grammar/*_test.rb']
9
+ t.verbose = true
10
+ end
11
+
12
+ task :default => :test
@@ -0,0 +1,35 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'citrus'
4
+
5
+ $LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib')
6
+ require 'cortexio-grammar' # Citrus.load 'grammar', :force => true
7
+
8
+ valid = true
9
+ invalid = false
10
+ prompt = '> '
11
+
12
+ puts "\n#{__FILE__}. Copyright (C) 2011 TGen. All rights reserved."
13
+ puts "Enter one statement per line."
14
+ puts "'#{valid}' will be returned iff the statment is syntactically and grammatically correct."
15
+ print "The grammar definition will be reloaded before parsing each statement.\n\n#{prompt}"
16
+
17
+ begin
18
+ while(line = STDIN.readline.chop)
19
+ break if line.match /(quit)|(exit)/
20
+ begin
21
+ CortexIOGrammar.parse line
22
+ puts valid
23
+ rescue Exception => e
24
+ puts e
25
+ puts invalid
26
+ end
27
+ print prompt
28
+ end
29
+ rescue EOFError => e
30
+ # Probably just hit CTRL-D or something. Regardless, time to go!
31
+ puts ""
32
+ end
33
+
34
+ puts "Later!"
35
+
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'cortexio-grammar/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "cortexio-grammar"
8
+ spec.version = CortexIO::Grammar::VERSION
9
+ spec.authors = ["Preston Lee"]
10
+ spec.email = ["preston.lee@prestonlee.com"]
11
+ spec.description = %q{A proof-of-concept CortexIO Query Language grammar using Ruby and citrus.}
12
+ spec.summary = %q{A proof-of-concept grammar.}
13
+ spec.homepage = "http://github.com/preston/cortexio-grammar"
14
+ spec.license = "Apache 2"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "citrus"
24
+ spec.add_development_dependency "minitest"
25
+ end
@@ -0,0 +1,7 @@
1
+ require 'citrus'
2
+ Citrus.load File.join(File.dirname(__FILE__), 'cortexio-grammar', 'parse', 'grammar'), :force => true
3
+
4
+ module CortexIO
5
+ module Grammar
6
+ end
7
+ end
@@ -0,0 +1,113 @@
1
+ grammar CortexIOGrammar
2
+
3
+ rule statement
4
+ space? (recover | create | modify | remove | context | expand) space?
5
+ end
6
+
7
+ rule context
8
+ `context` space (level space)? (uuid | uuid_set | `with` space expression)
9
+ end
10
+
11
+ rule expand
12
+ `expand` space (level space)? (uuid | uuid_set | `with` space expression)
13
+ end
14
+
15
+ rule recover
16
+ `recover` space (level space)? (uuid | uuid_set)
17
+ end
18
+
19
+ rule modify
20
+ `modify` # entity
21
+ end
22
+
23
+ rule remove
24
+ `remove` space `element` space (uuid | uuid_set)
25
+ end
26
+
27
+ rule set_external
28
+ `set(e)` space? '=' uuid_set
29
+ end
30
+
31
+ rule set_internal
32
+ `set(i)` space? '=' uuid_set
33
+ end
34
+
35
+ rule set_attribute
36
+ `set(a)` space? '=' attribute_set
37
+ end
38
+
39
+
40
+ rule attribute_set
41
+ space? '{' space? (attribute (',' space? attribute)* space?)? '}' space?
42
+ end
43
+
44
+ rule attribute
45
+ attribute_key space? '=' space? attribute_value space?
46
+ end
47
+
48
+ rule expression
49
+ expression_either (space? (`and` | `or`) space? expression_either)? space?
50
+ end
51
+
52
+ rule expression_either
53
+ (attribute | expression_precedent)
54
+ end
55
+
56
+
57
+ rule expression_precedent
58
+ '(' space? expression ')' space?
59
+ end
60
+
61
+ rule attribute_key
62
+ quoted_string
63
+ end
64
+
65
+ rule attribute_value
66
+ quoted_string
67
+ end
68
+
69
+ rule level
70
+ (numeric | `max`)
71
+ end
72
+
73
+ rule uuid_set
74
+ space? '{' space? (uuid (space? ',' space? uuid)* space?)? '}' space?
75
+ end
76
+
77
+ rule uuid
78
+ numeric
79
+ end
80
+
81
+ rule quote
82
+ "\""
83
+ end
84
+
85
+ rule quoted_string
86
+ /".*?"/
87
+ end
88
+
89
+ rule numeric
90
+ [0-9]+
91
+ end
92
+
93
+ rule alphanumeric
94
+ [a-zA-Z0-9]+
95
+ end
96
+
97
+ rule create
98
+ (create_element | create_attribute)
99
+ end
100
+
101
+ rule create_element
102
+ `create` space `element` space set_internal? space? set_external? space? set_attribute?
103
+ end
104
+
105
+ rule create_attribute
106
+ `create` space `attribute` space (attribute | attribute_set)
107
+ end
108
+
109
+ rule space
110
+ [ \t]+
111
+ end
112
+
113
+ end
@@ -0,0 +1,7 @@
1
+ module CortexIO
2
+
3
+ module Grammar
4
+ VERSION = '1.0.0'
5
+ end
6
+
7
+ end
@@ -0,0 +1,241 @@
1
+ # Copyright 2011 TGen. All rights reserved.
2
+ # Author: Preston Lee <plee@tgen.org>
3
+
4
+ require_relative '../../test_helper'
5
+
6
+
7
+ class GrammarTest < Minitest::Test
8
+
9
+ # Helper function for batch rule validation.
10
+ def assert_rule_with_data(rule = 'statement', good, bad)
11
+ good.each do |line|
12
+ refute_nil @g.parse line, :root => rule
13
+ end
14
+
15
+ bad.each do |line|
16
+ assert_raises(Citrus::ParseError) { @g.parse line, :root => rule }
17
+ end
18
+ end
19
+
20
+
21
+ def setup
22
+ # puts 'setup!!!!'
23
+ @g = CortexIOGrammar
24
+ end
25
+
26
+ def test_grammar_loaded
27
+ assert defined?(CortexIOGrammar), "Grammar was not preloaded!"
28
+ end
29
+
30
+
31
+
32
+ # create element set(i)={235, 452, 786} set(a)={"symbol"="BRCA1", "source"="ncbi"}
33
+ # create attribute "type"="node"
34
+ # create attribute {"type"="node", "source"="ncbi"}
35
+ # modify element 456 set(x)={578}
36
+ # modify element with {"symbol"="DLC1", "foo"="bar"} set(i)={345, 87, 673, 4829}
37
+ # remove element 6574
38
+ # remove element {157}
39
+ # remove element {157, 239842, 23}
40
+ # recover 9875
41
+ # recover 1 {235, 9875, 98}
42
+ # recover max {42}
43
+ # context 1 with "type"="equivalence" and "chromosome"="14"
44
+ # context max 345
45
+ # expand 1 element with "symbol" = "BRC*"
46
+ # expand 2 {578, 418}
47
+
48
+
49
+ def test_context
50
+ good = [
51
+ 'context 0 42',
52
+ 'context 1 1234',
53
+ 'context 42 12341234',
54
+ 'context 42 {}',
55
+ 'context 42 {1}',
56
+ 'context 42 {1,2}',
57
+ 'context 42 { 1 , 2 }',
58
+ 'context max 42',
59
+ 'context max with "type" = "equivalence" and "chromosome"="14" ',
60
+ 'Context Max WITH ("type" = "equivalence" AND "chromosome"="14")',
61
+ ' CONTEXT MAX WITH "type" = "equivalence" and ("foo"="bar" OR "chromosome"="14") ',
62
+ ]
63
+ bad = [
64
+ "context",
65
+ "context 1 {,}",
66
+ "context 1 aoeu",
67
+ "context 1 with"
68
+ ]
69
+ assert_rule_with_data('statement', good, bad)
70
+ end
71
+
72
+ def test_expand
73
+ good = [
74
+ 'expand 0 42',
75
+ 'expand 1 1234',
76
+ 'expand 42 12341234',
77
+ 'expand max 42',
78
+ 'expand max with "type" = "equivalence" and "chromosome"="14" ',
79
+ 'Expand Max WITH ("type" = "equivalence" AND "chromosome"="14")',
80
+ ' EXPAND MAX WITH "type" = "equivalence" and ("foo"="bar" OR "chromosome"="14") ',
81
+ ]
82
+ bad = [
83
+ "expand",
84
+ "expand 1 {,}",
85
+ "expand 1 aoeu",
86
+ "expand 1 with"
87
+ ]
88
+ assert_rule_with_data('statement', good, bad)
89
+ end
90
+
91
+
92
+ def test_create_attribute
93
+ good = [
94
+ 'create attribute "type"="node"',
95
+ 'create attribute {"type"="node"}',
96
+ 'create attribute {"type"="node", "name"="bob"}',
97
+ 'create attribute {}',
98
+ ' create attribute { } ',
99
+ 'CREATE ATTRIBUTE {"type"="node", "name"="bob"}',
100
+ 'Create Attribute {"type"="node", "name"="bob"}',
101
+ ]
102
+ bad = [
103
+ 'create attribute',
104
+ 'create attribute {,}',
105
+ 'create attribute {"bob"}',
106
+ 'create attribute {aoeu}',
107
+ 'create attribute {aoeu,}',
108
+ 'create attribute {, 1234}',
109
+ 'create attribute {1234 2345}'
110
+ ]
111
+ assert_rule_with_data('statement', good, bad)
112
+ end
113
+
114
+ def test_create_element
115
+ good = [
116
+ 'create element set(i)={235, 452, 786}',
117
+ 'create element set(e)={42}',
118
+ 'create element set(a)={"symbol"="BRCA1", "source"="ncbi"}',
119
+ 'create element set(i)={} set(a)={}',
120
+ 'create element SET(I)={235, 452, 786} SET(E)={42} SET(A)={"symbol"="BRCA1", "source"="ncbi"}',
121
+ ' CREATE ELEMENT set(e)={}',
122
+ 'Create ELEMENT set(a)={"type"="node", "name"="bob"}',
123
+ ]
124
+ bad = [
125
+ 'create element',
126
+ 'create element {,}',
127
+ 'create element {"bob"}',
128
+ 'create element {aoeu}',
129
+ 'create element {aoeu,}',
130
+ 'create element {, 1234}',
131
+ 'create element {1234 2345}'
132
+ ]
133
+ assert_rule_with_data('statement', good, bad)
134
+ end
135
+
136
+ def test_recover
137
+ good = [
138
+ 'recover 1234',
139
+ 'recover 0 1234',
140
+ 'recover 42 1234',
141
+ 'recover max 1234',
142
+ 'recover {}',
143
+ 'recover {1234}',
144
+ 'recover {1234, 1234}',
145
+ ' recover {1234, 2345} ',
146
+ ' recover {1234, 2345 }',
147
+ 'RECOVER 1 { 1234,2345 }',
148
+ "Recover 33 {\t 1234, 2345 }",
149
+ "rEcOveR MAX {1234, 2345\t}"
150
+ ]
151
+ bad = [
152
+ 'recover',
153
+ 'recover element {,}',
154
+ 'recover element {1234,}',
155
+ 'recover element {aoeu}',
156
+ 'recover element {aoeu,}',
157
+ 'recover element {, 1234}',
158
+ 'recover element {1234 2345}'
159
+ ]
160
+ assert_rule_with_data('statement', good, bad)
161
+ end
162
+
163
+ def test_remove_element
164
+ good = [
165
+ "remove element 1234",
166
+ "remove element {}",
167
+ "remove element {1234}",
168
+ "remove element {1234, 1234}",
169
+ " remove element {1234, 2345} ",
170
+ " remove element {1234, 2345 }",
171
+ "REMOVE ELEMENT { 1234,2345 }",
172
+ "Remove Element {\t 1234, 2345 }",
173
+ "REmoVE ELEMent {1234, 2345\t}"
174
+ ]
175
+ bad = [
176
+ "remove",
177
+ "remove element {,}",
178
+ "remove element {1234,}",
179
+ "remove element {aoeu}",
180
+ "remove element {aoeu,}",
181
+ "remove element {, 1234}",
182
+ "remove element {1234 2345}"
183
+ ]
184
+ assert_rule_with_data('statement', good, bad)
185
+ end
186
+
187
+ def test_uuid_set
188
+ good = [
189
+ "{}",
190
+ "{1234}",
191
+ "{1234, 2345}",
192
+ " {1234,2345} ",
193
+ "{1234, 2345, 3456}",
194
+ ]
195
+ bad = [
196
+ "",
197
+ "1234",
198
+ "{1234",
199
+ "1234}",
200
+ "{,}",
201
+ "{1234}}",
202
+ "{{1234}}",
203
+ ]
204
+ assert_rule_with_data('uuid_set', good, bad)
205
+ end
206
+
207
+ def test_expression
208
+ good = [
209
+ '"name"="alice"',
210
+ '"name" = "alice"',
211
+ '"name" ="alice"',
212
+ "\"name\"=\t\"alice\"",
213
+ '("name"="alice")',
214
+ '( ("name"="alice"))',
215
+ '( "name" = "alice" )',
216
+ '"name" = "alice" AND "type"="user" ',
217
+ '("name" = "alice" AND "type"="user")',
218
+ '("name" = "alice") AND ("type"="user")',
219
+ '"name" = "alice" AND ("type"="admin")',
220
+ '"name" = "alice" AND ("type"="admin" or "type"="manager")',
221
+ '("name" = "alice" OR "email"="alice@example.com") and ("type"="admin" or "type"="manager")',
222
+ '(("name" = "alice" OR "email"="alice@example.com") and ("type"="admin" or "type"="manager"))',
223
+ '( ( "name" = "alice" OR "email"="alice@example.com") and ("type"="admin" or "type"="manager" ) ) '
224
+ ]
225
+
226
+
227
+ bad = [
228
+ 'name',
229
+ '"name"',
230
+ '"name" =',
231
+ '"name"=',
232
+ '("name" = "alice" AND "type"="user" ',
233
+ '("name" = "alice" AND "type"="user") )'
234
+ ]
235
+ assert_rule_with_data('expression', good, bad)
236
+ end
237
+
238
+
239
+
240
+
241
+ end
@@ -0,0 +1,3 @@
1
+ require 'minitest/autorun'
2
+ require 'minitest/pride'
3
+ require File.expand_path('../../lib/cortexio-grammar.rb', __FILE__)
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cortexio-grammar
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Preston Lee
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-07-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: citrus
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: A proof-of-concept CortexIO Query Language grammar using Ruby and citrus.
70
+ email:
71
+ - preston.lee@prestonlee.com
72
+ executables:
73
+ - cql_grammar_console
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - .gitignore
78
+ - Gemfile
79
+ - README.md
80
+ - Rakefile
81
+ - bin/cql_grammar_console
82
+ - cortexio-grammar.gemspec
83
+ - lib/cortexio-grammar.rb
84
+ - lib/cortexio-grammar/parse/grammar.citrus
85
+ - lib/cortexio-grammar/version.rb
86
+ - test/lib/cortexio-grammar/grammar_test.rb
87
+ - test/test_helper.rb
88
+ homepage: http://github.com/preston/cortexio-grammar
89
+ licenses:
90
+ - Apache 2
91
+ metadata: {}
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - '>='
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - '>='
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 2.0.3
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: A proof-of-concept grammar.
112
+ test_files:
113
+ - test/lib/cortexio-grammar/grammar_test.rb
114
+ - test/test_helper.rb