bel_parser 1.0.0.alpha.19 → 1.0.0.alpha.20

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
  SHA1:
3
- metadata.gz: 64448dc49f150b92a0dd2571bfd9f97eff32eda9
4
- data.tar.gz: 674d470b18c5195e21570cf3ae28ef75d111a0ff
3
+ metadata.gz: 5f5c683930e9f6671b818ec184fc5a8a35231984
4
+ data.tar.gz: 04cb98ee13e3c6f57f2c9ea3a6b2188aba861918
5
5
  SHA512:
6
- metadata.gz: a8854636248e1dd75114b5131920a227bbfacd2d3f2deea6cdc0cd4873d2771b4751c6d4de792e644d671ccfa9213fcc7660d61ecf3ad6f04e90750595e8c1b9
7
- data.tar.gz: 880b9595a4daa995594861f84891085762d6ae2b80de61573ac23db73ddb21bb5f5546fbd72e3e397789da559dca7381c4daf76a7f7be1a1e74eb13c2c753551
6
+ metadata.gz: 362bc4fd38cb8cb60fcbf3c6a975671192258a9717101b53dd536e68e912d4af15f9c4620506f9782e2cefcab5fbf450586fc988a422e2710a1e8b6e273d41c1
7
+ data.tar.gz: 6034289be41223dcda961ddafccc4079b62502ab3d4c8376eddee9bc7af109344cc16c2b442a9c4a6aac37186948f530875d3a7275da21bdc1408e5f1d8a5fa3
data/.gemspec CHANGED
@@ -11,7 +11,7 @@ Gem::Specification.new do |spec|
11
11
  'Anthony Bargnesi',
12
12
  'Nick Bargnesi',
13
13
  ]
14
- spec.date = %q{2016-04-29}
14
+ spec.date = %q{2016-05-01}
15
15
  spec.email = %q{abargnesi@selventa.com}
16
16
  spec.files = [
17
17
  Dir.glob('lib/**/*.{rb,rl}'),
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.0.alpha.19
1
+ 1.0.0.alpha.20
@@ -55,7 +55,6 @@ io =
55
55
  $stdin
56
56
  end
57
57
 
58
-
59
58
  rr = BELParser::Resource::ResourceURLReader.new(options[:reuse])
60
59
  namespaces = Hash[
61
60
  ARGV.map do |ns|
@@ -30,9 +30,12 @@ end
30
30
  if __FILE__ == $PROGRAM_NAME
31
31
  require_relative 'ast_generator'
32
32
 
33
- types = ARGV.map(&:to_sym)
34
- ast = BELParser::ASTGenerator.new.each($stdin)
35
- BELParser::ASTFilter.new(*types).each(ast) do |(line_number, line, results)|
33
+ types = ARGV.map(&:to_sym)
34
+ generator = BELParser::ASTGenerator.new($stdin)
35
+ BELParser::ASTFilter.new(
36
+ generator,
37
+ *types
38
+ ).each do |(line_number, line, results)|
36
39
  puts "#{line_number}: #{line}"
37
40
  results.each do |result_ast|
38
41
  puts result_ast.to_s(1)
@@ -73,7 +73,7 @@ module BELParser
73
73
  end
74
74
 
75
75
  if __FILE__ == $PROGRAM_NAME
76
- BELParser::ASTGenerator.new.each($stdin) do |line_results|
76
+ BELParser::ASTGenerator.new($stdin).each do |line_results|
77
77
  line_number, line, ast_results = line_results
78
78
  puts "#{line_number}: #{line}"
79
79
  ast_results.each do |ast|
@@ -11,18 +11,20 @@ module BELParser
11
11
  include BELParser::Parsers::Common
12
12
  include BELParser::Parsers::Expression
13
13
 
14
- FILTER = BELParser::ASTFilter.new(
14
+ TYPES = [
15
15
  :parameter,
16
16
  :term,
17
17
  :simple_statement,
18
18
  :observed_term,
19
19
  :nested_statement
20
- )
20
+ ]
21
21
 
22
22
  def each(io)
23
23
  if block_given?
24
- filtered_ast = FILTER.each(BELParser::ASTGenerator.new.each(io))
25
- filtered_ast.each do |results|
24
+ filter = BELParser::ASTFilter.new(
25
+ BELParser::ASTGenerator.new(io),
26
+ *TYPES)
27
+ filter.each do |results|
26
28
  yield results
27
29
  end
28
30
  else
@@ -11,12 +11,13 @@ module BELParser
11
11
  include BELParser::Parsers::Common
12
12
  include BELParser::Parsers::Expression
13
13
 
14
- FILTER = BELParser::ASTFilter.new(
14
+ TYPES = [
15
15
  :parameter,
16
16
  :term,
17
- :observed_term,
18
17
  :simple_statement,
19
- :nested_statement)
18
+ :observed_term,
19
+ :nested_statement
20
+ ]
20
21
 
21
22
  def initialize(specification_version, namespaces, resource_reader)
22
23
  @spec = BELParser::Language.specification(specification_version)
@@ -26,10 +27,12 @@ module BELParser
26
27
 
27
28
  def each(io)
28
29
  if block_given?
29
- filtered_ast = FILTER.each(BELParser::ASTGenerator.new.each(io))
30
- filtered_ast.each do |(line_number, line, ast_results)|
31
- ast_results.each do |ast|
32
- yield [line_number, line, ast, @validator.validate(ast)]
30
+ filter = BELParser::ASTFilter.new(
31
+ BELParser::ASTGenerator.new(io),
32
+ *TYPES)
33
+ filter.each do |(num, line, results)|
34
+ results.each do |ast|
35
+ yield [num, line, ast, @validator.validate(ast)]
33
36
  end
34
37
  end
35
38
  else
@@ -0,0 +1,205 @@
1
+ require 'bel_parser/vendor/ast'
2
+
3
+ module BELParser
4
+ module Parsers
5
+
6
+ def serialize(ast_node)
7
+ serializer = Serializer.new
8
+ serializer.process(ast_node)
9
+ serializer.string
10
+ end
11
+
12
+ # Defines an {AST::Processor::Mixin AST processor} that serializes
13
+ # all {AST::Node AST node} to a string.
14
+ class Serializer
15
+ include ::AST::Processor::Mixin
16
+
17
+ attr_reader :string
18
+
19
+ def initialize
20
+ @string = ''
21
+ end
22
+
23
+ def on_annotation_definition(annotation_definition_node)
24
+ @string << 'DEFINE ANNOTATION '
25
+ process(annotation_definition_node.keyword)
26
+ @string << ' AS '
27
+ process(annotation_definition_node.domain)
28
+ end
29
+
30
+ def on_argument(argument_node)
31
+ @string << ', ' if @separate_by_comma
32
+
33
+ process(argument_node.child)
34
+ @separate_by_comma = true
35
+ end
36
+
37
+ def on_blank_line(blank_line_node)
38
+ @string << ''
39
+ end
40
+
41
+ def on_comment_line(comment_line_node)
42
+ @string << %(##{comment_line_node.children[0]})
43
+ end
44
+
45
+ def on_document_property(document_property_node)
46
+ name = document_property_node.name.identifier.string_literal
47
+ value = document_property_node.value.children[0].string_literal
48
+ @string << %(SET DOCUMENT #{name} = #{value})
49
+ end
50
+
51
+ def on_domain(domain_node)
52
+ process(domain_node.child)
53
+ end
54
+
55
+ def on_function(function_node)
56
+ @string << function_node.identifier.string_literal
57
+ end
58
+
59
+ def on_identifier(identifier_node)
60
+ @string << identifier_node.string_literal
61
+ end
62
+
63
+ def on_keyword(keyword_node)
64
+ process(keyword_node.identifier)
65
+ end
66
+
67
+ def on_list(list_node)
68
+ items = list_node.list_items
69
+ if !items || items.empty?
70
+ @string << '{}'
71
+ else
72
+ @string << '{ '
73
+ process(items[0])
74
+ items[1..-1].each do |rest_item|
75
+ @string << ', '
76
+ process(rest_item)
77
+ end
78
+ @string << ' }'
79
+ end
80
+ end
81
+
82
+ def on_list_item(list_item_node)
83
+ process(list_item_node.children[0])
84
+ end
85
+
86
+ def on_name(name_node)
87
+ process(name_node.identifier)
88
+ end
89
+
90
+ def on_namespace_definition(namespace_definition_node)
91
+ @string << 'DEFINE NAMESPACE '
92
+ process(namespace_definition_node.keyword)
93
+ @string << ' AS '
94
+ process(namespace_definition_node.domain)
95
+ end
96
+
97
+ def on_nested_statement(nested_statement_node)
98
+ process(nested_statement_node.statement)
99
+ end
100
+
101
+ def on_object(object_node)
102
+ process(object_node.child)
103
+ end
104
+
105
+ def on_observed_term(observed_term_node)
106
+ process(observed_term_node.statement)
107
+ end
108
+
109
+ def on_parameter(param_node)
110
+ process(param_node.prefix)
111
+ process(param_node.value)
112
+ end
113
+
114
+ def on_pattern(pattern_node)
115
+ @string << 'PATTERN '
116
+ process(pattern_node.string)
117
+ end
118
+
119
+ def on_prefix(prefix_node)
120
+ prefix = prefix_node.identifier
121
+ @string << "#{prefix.string_literal}:" unless prefix.nil?
122
+ end
123
+
124
+ def on_relationship(relationship_node)
125
+ @string << " #{relationship_node.string_literal} "
126
+ end
127
+
128
+ def on_set(set_node)
129
+ @string << 'SET '
130
+ process(set_node.name)
131
+ @string << ' = '
132
+ process(set_node.value)
133
+ end
134
+
135
+ def on_simple_statement(simple_statement_node)
136
+ process(simple_statement_node.statement)
137
+ end
138
+
139
+ def on_statement(statement_node)
140
+ process(statement_node.subject)
141
+ return if statement_node.object.child.nil?
142
+
143
+ process(statement_node.relationship)
144
+ object_node = statement_node.object
145
+ if object_node.statement?
146
+ @string << '('
147
+ process(object_node)
148
+ @string << ')'
149
+ else
150
+ process(object_node)
151
+ end
152
+ end
153
+
154
+ def on_string(string_node)
155
+ @string << string_node.string_literal
156
+ end
157
+
158
+ def on_subject(subject_node)
159
+ process(subject_node.term)
160
+ end
161
+
162
+ # Called when visiting nodes of type +term+.
163
+ def on_term(term_node)
164
+ @separate_by_comma = false
165
+ process(term_node.function)
166
+ @string << '('
167
+ term_node.arguments.each { |arg| process(arg) }
168
+ @string << ')'
169
+ end
170
+
171
+ def on_unset(unset_node)
172
+ @string << 'UNSET '
173
+ process(unset_node.name)
174
+ end
175
+
176
+ def on_url(url_node)
177
+ @string << 'URL '
178
+ process(url_node.string)
179
+ end
180
+
181
+ def on_value(value_node)
182
+ process(value_node.children[0])
183
+ end
184
+ end
185
+ end
186
+ end
187
+
188
+ if __FILE__ == $PROGRAM_NAME
189
+ $LOAD_PATH.unshift(
190
+ File.join(
191
+ File.expand_path(File.dirname(__FILE__)),
192
+ '..',
193
+ '..',
194
+ '..',
195
+ 'lib'))
196
+ require 'bel_parser'
197
+
198
+ types = ARGV.map(&:to_sym)
199
+ generator = BELParser::ASTGenerator.new($stdin)
200
+ BELParser::ASTFilter.new(generator, *types).each do |(num, line, results)|
201
+ serializer = BELParser::Parsers::Serializer.new
202
+ serializer.process(results.first)
203
+ puts serializer.string
204
+ end
205
+ end
@@ -1,8 +1,68 @@
1
1
  module BELParser
2
2
  module Script
3
+ # NanopubMapper maps BEL Script AST nodes to an aggregated nanopub.
3
4
  class NanopubMapper
5
+ STATEMENT_TYPES = [
6
+ :simple_statement,
7
+ :nested_statement,
8
+ :observed_term
9
+ ]
4
10
 
11
+ def initialize(ast_enum)
12
+ @ast_enum = ast_enum
13
+ end
5
14
 
15
+ def each
16
+ if block_given?
17
+ @ast_enum.each do |(line_nunber, line, ast_node, state)|
18
+ next unless STATEMENT_TYPES.include?(ast_node.type)
19
+
20
+ # TODO Add StateFunction for capturing :citation.
21
+ # TODO Add BELSerializer AST processor. Serialize ast_node.
22
+ # TODO One time: Map annotation and namespace definitions.
23
+
24
+ # TODO Collect annotations.
25
+
26
+ # TODO Combine into hash. Yield.
27
+ end
28
+ else
29
+ enum_for(:each)
30
+ end
31
+ end
6
32
  end
7
33
  end
8
34
  end
35
+
36
+ if __FILE__ == $PROGRAM_NAME
37
+ $LOAD_PATH.unshift(
38
+ File.join(File.expand_path(File.dirname(__FILE__)), '..', '..', '..', 'lib'))
39
+
40
+ require 'json'
41
+ require 'bel_parser/language'
42
+ require 'bel_parser/script'
43
+ require 'bel_parser/resource/resource_url_reader'
44
+ include BELParser::Script
45
+
46
+ rr = BELParser::Resource::ResourceURLReader.new(true)
47
+ namespaces = Hash[
48
+ ARGV.map do |ns|
49
+ prefix, identifier = ns.split('=')
50
+ dataset = rr.retrieve_resource(identifier)
51
+ dataset ? [prefix, dataset] : nil
52
+ end.compact
53
+ ]
54
+
55
+ initial_state = {
56
+ resource_reader: rr,
57
+ specification: BELParser::Language.specification('2.0'),
58
+ namespace_definitions: namespaces
59
+ }
60
+
61
+ NanopubMapper.new(
62
+ Validator.new(
63
+ StateAggregator.new(
64
+ FirstNode.new(Filter.new(BELParser::ASTGenerator.new(File.open(ARGV.first)))),
65
+ initial_state))).each do |nanopub|
66
+ JSON.dump(nanopub)
67
+ end
68
+ end
@@ -12,7 +12,7 @@ module BELParser
12
12
  include BELParser::Parsers::Expression
13
13
  include BELParser::Parsers::BELScript
14
14
 
15
- FILTER = BELParser::ASTFilter.new(
15
+ TYPES = [
16
16
  :simple_statement,
17
17
  :observed_term,
18
18
  :nested_statement,
@@ -23,12 +23,14 @@ module BELParser
23
23
  :unset,
24
24
  :blank_line,
25
25
  :comment_line
26
- )
26
+ ]
27
27
 
28
28
  def each(io)
29
29
  if block_given?
30
- filtered_ast = FILTER.each(BELParser::ASTGenerator.new.each(io))
31
- filtered_ast.each do |results|
30
+ filter = BELParser::ASTFilter.new(
31
+ BELParser::ASTGenerator.new(io),
32
+ *TYPES)
33
+ filter.each do |results|
32
34
  yield results
33
35
  end
34
36
  else
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bel_parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0.alpha.19
4
+ version: 1.0.0.alpha.20
5
5
  platform: ruby
6
6
  authors:
7
7
  - Anthony Bargnesi
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-04-29 00:00:00.000000000 Z
12
+ date: 2016-05-01 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Implements language versions 1.0 and 2.0.
15
15
  email: abargnesi@selventa.com
@@ -299,6 +299,7 @@ files:
299
299
  - lib/bel_parser/parsers/line_parser.rb
300
300
  - lib/bel_parser/parsers/mixin/buffer.rb
301
301
  - lib/bel_parser/parsers/nonblocking_io_wrapper.rb
302
+ - lib/bel_parser/parsers/serializer.rb
302
303
  - lib/bel_parser/quoting.rb
303
304
  - lib/bel_parser/resource/concept.rb
304
305
  - lib/bel_parser/resource/concept_scheme.rb