bel_parser 1.0.0.alpha.13 → 1.0.0.alpha.14
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/VERSION +1 -1
- data/lib/bel_parser/language/apply_namespace_encoding.rb +5 -2
- data/lib/bel_parser/language/expression_validator.rb +25 -7
- data/lib/bel_parser/language/syntax/undefined_namespace_value.rb +5 -6
- data/lib/bel_parser/language.rb +8 -0
- data/lib/bel_parser/parsers/ast/node.rb +30 -3
- data/lib/bel_parser/parsers/bel_script/define_annotation.rb +1732 -1288
- data/lib/bel_parser/parsers/bel_script/set.rb +1424 -972
- data/lib/bel_parser/parsers/bel_script/set_document.rb +2735 -2100
- data/lib/bel_parser/parsers/common/list.rb +708 -462
- data/lib/bel_parser/parsers/common/list.rl +6 -4
- data/lib/bel_parser/resource/eager_reader.rb +9 -9
- data/lib/bel_parser/resource/http_cache.rb +26 -8
- data/lib/bel_parser/resource/resource_file_reader.rb +1 -0
- data/lib/bel_parser/resource/sparql_reader.rb +1 -0
- data/lib/bel_parser/script/state/annotation_definition.rb +62 -0
- data/lib/bel_parser/script/state/bel_version.rb +35 -0
- data/lib/bel_parser/script/state/document_property.rb +29 -0
- data/lib/bel_parser/script/state/namespace_definition.rb +32 -0
- data/lib/bel_parser/script/state/set.rb +57 -0
- data/lib/bel_parser/script/state/unset.rb +43 -0
- data/lib/bel_parser/script/state_function.rb +10 -0
- data/lib/bel_parser/script/syntax/expression_validation.rb +46 -0
- data/lib/bel_parser/script/syntax/invalid_regex_pattern.rb +49 -0
- data/lib/bel_parser/script/syntax/undefined_annotation.rb +60 -0
- data/lib/bel_parser/script/syntax/undefined_annotation_value.rb +83 -0
- data/lib/bel_parser/script/syntax/unresolvable_namespace.rb +55 -0
- data/lib/bel_parser/script/syntax/unsupported_bel_version.rb +58 -0
- data/lib/bel_parser/script/validator.rb +153 -0
- metadata +15 -1
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'bel_parser/language'
|
2
|
+
require 'bel_parser/language/syntax_function'
|
3
|
+
require 'bel_parser/language/syntax_error'
|
4
|
+
require 'bel_parser/quoting'
|
5
|
+
require 'bel_parser/parsers/ast/node'
|
6
|
+
require 'concurrent/hash'
|
7
|
+
|
8
|
+
module BELParser
|
9
|
+
module Script
|
10
|
+
module Syntax
|
11
|
+
class UnresolvableNamespace
|
12
|
+
extend BELParser::Language::Syntax::SyntaxFunction
|
13
|
+
extend BELParser::Quoting
|
14
|
+
|
15
|
+
TARGET_NODE = BELParser::Parsers::AST::NamespaceDefinition
|
16
|
+
|
17
|
+
def self.map(ast_node, script_context)
|
18
|
+
return nil unless ast_node.is_a?(TARGET_NODE)
|
19
|
+
resource_reader = script_context[:resource_reader]
|
20
|
+
|
21
|
+
keyword, domain = ast_node.children
|
22
|
+
if domain.url?
|
23
|
+
url = unquote(domain.child.string.string_literal)
|
24
|
+
puts resource_reader.retrieve_resource(url)
|
25
|
+
unless resource_reader.retrieve_resource(url)
|
26
|
+
prefix = keyword.identifier.string_literal
|
27
|
+
return UnresolvableNamespaceError.new(ast_node, prefix, url)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
nil
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
# UnresolvableNamespaceError indicates that a namespace resource could
|
36
|
+
# not be resolved.
|
37
|
+
class UnresolvableNamespaceError < BELParser::Language::Syntax::SyntaxError
|
38
|
+
# Gets the unresolvable namespace prefix.
|
39
|
+
attr_reader :prefix
|
40
|
+
# Gets the unresolvable namespace identifier.
|
41
|
+
attr_reader :identifier
|
42
|
+
|
43
|
+
def initialize(define_namespace_node, prefix, identifier)
|
44
|
+
super(define_namespace_node, nil)
|
45
|
+
@prefix = prefix
|
46
|
+
@identifier = identifier
|
47
|
+
end
|
48
|
+
|
49
|
+
def msg
|
50
|
+
%(Could not resolve "#@prefix" with identifier "#@identifier".)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'bel_parser/language'
|
2
|
+
require 'bel_parser/language/syntax_function'
|
3
|
+
require 'bel_parser/language/syntax_error'
|
4
|
+
require 'bel_parser/quoting'
|
5
|
+
require 'bel_parser/parsers/ast/node'
|
6
|
+
require 'concurrent/hash'
|
7
|
+
|
8
|
+
module BELParser
|
9
|
+
module Script
|
10
|
+
module Syntax
|
11
|
+
class UnsupportedBELVersion
|
12
|
+
extend BELParser::Language::Syntax::SyntaxFunction
|
13
|
+
extend BELParser::Quoting
|
14
|
+
|
15
|
+
TARGET_NODE = BELParser::Parsers::AST::DocumentProperty
|
16
|
+
BEL_VERSION_REGEX = /#{Regexp.escape('bel_version')}/i
|
17
|
+
|
18
|
+
def self.map(ast_node, script_context)
|
19
|
+
return nil unless ast_node.is_a?(TARGET_NODE)
|
20
|
+
name, value = ast_node.children
|
21
|
+
name_string = name.identifier.string_literal
|
22
|
+
return nil unless name_string =~ BEL_VERSION_REGEX
|
23
|
+
|
24
|
+
value_string = unquote(value.children[0].string_literal)
|
25
|
+
begin
|
26
|
+
BELParser::Language.specification(value_string)
|
27
|
+
nil
|
28
|
+
rescue ArgumentError
|
29
|
+
latest_version = BELParser::Language.latest_supported_version
|
30
|
+
latest_spec = BELParser::Language.specification(latest_version)
|
31
|
+
script_context[:specification] = latest_spec
|
32
|
+
UnsupportedBELVersionWarning.new(ast_node, value_string)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
# UnsupportedBELVersionError represents the use of an unsupported
|
38
|
+
# BEL version string.
|
39
|
+
class UnsupportedBELVersionWarning < BELParser::Language::Syntax::SyntaxWarning
|
40
|
+
# Gets the unsupported bel version.
|
41
|
+
attr_reader :unsupported_version
|
42
|
+
|
43
|
+
def initialize(document_property_node, unsupported_version)
|
44
|
+
super(document_property_node, nil)
|
45
|
+
@unsupported_version = unsupported_version
|
46
|
+
@latest = BELParser::Language.latest_supported_version
|
47
|
+
end
|
48
|
+
|
49
|
+
def msg
|
50
|
+
<<-MSG.gsub(/^ +/, '').delete("\n")
|
51
|
+
Unsupported BEL version "#@unsupported_version".
|
52
|
+
Setting BEL version to the latest supported version "#@latest".
|
53
|
+
MSG
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,153 @@
|
|
1
|
+
require 'concurrent/hash'
|
2
|
+
|
3
|
+
require_relative '../parsers/common'
|
4
|
+
require_relative '../parsers/expression'
|
5
|
+
require_relative '../parsers/bel_script'
|
6
|
+
|
7
|
+
require_relative '../ast_filter'
|
8
|
+
require_relative '../ast_generator'
|
9
|
+
|
10
|
+
require_relative 'state_function'
|
11
|
+
require_relative '../language/syntax_function'
|
12
|
+
|
13
|
+
module BELParser
|
14
|
+
module Script
|
15
|
+
class Validator
|
16
|
+
include BELParser::Parsers::Common
|
17
|
+
include BELParser::Parsers::Expression
|
18
|
+
include BELParser::Parsers::BELScript
|
19
|
+
|
20
|
+
FILTER = BELParser::ASTFilter.new(
|
21
|
+
:simple_statement,
|
22
|
+
:observed_term,
|
23
|
+
:nested_statement,
|
24
|
+
:annotation_definition,
|
25
|
+
:namespace_definition,
|
26
|
+
:set,
|
27
|
+
:document_property,
|
28
|
+
:unset,
|
29
|
+
:blank_line,
|
30
|
+
:comment_line
|
31
|
+
)
|
32
|
+
|
33
|
+
def initialize(options = {})
|
34
|
+
@script_context = Concurrent::Hash.new.merge(options)
|
35
|
+
|
36
|
+
Validator.require_script_path
|
37
|
+
@state_functions = Validator.state_constants(State)
|
38
|
+
@syntax_functions = Validator.syntax_constants(Syntax)
|
39
|
+
end
|
40
|
+
|
41
|
+
def each(io)
|
42
|
+
if block_given?
|
43
|
+
filtered_ast = FILTER.each(BELParser::ASTGenerator.new.each(io))
|
44
|
+
filtered_ast.each do |results|
|
45
|
+
line_number, line, ast_results = results
|
46
|
+
ast_node = ast_results.first
|
47
|
+
|
48
|
+
syntax_results =
|
49
|
+
ast_node.traverse.flat_map do |node|
|
50
|
+
@syntax_functions.flat_map do |func|
|
51
|
+
func.map(node, @script_context)
|
52
|
+
end
|
53
|
+
end.compact
|
54
|
+
|
55
|
+
if syntax_results.empty?
|
56
|
+
ast_node.traverse.each do |node|
|
57
|
+
@state_functions.each do |func|
|
58
|
+
func.consume(node, @script_context)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
yield [line_number, line, ast_node, syntax_results, @script_context]
|
64
|
+
end
|
65
|
+
else
|
66
|
+
enum_for(:each, io)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def self.require_script_path
|
71
|
+
base_path = File.expand_path(File.dirname(__FILE__)) + File::SEPARATOR
|
72
|
+
['state', 'syntax'].each do |set|
|
73
|
+
Dir[File.join(base_path, set, '*.rb')]
|
74
|
+
.each do |ruby_file|
|
75
|
+
ruby_file.sub!(/^#{Regexp.escape(base_path)}/, '')
|
76
|
+
require_relative ruby_file
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def self.state_constants(mod)
|
82
|
+
mod.constants.collect do |symbol|
|
83
|
+
const = mod.const_get(symbol)
|
84
|
+
const if const.respond_to?(:consume)
|
85
|
+
end.compact
|
86
|
+
end
|
87
|
+
|
88
|
+
def self.syntax_constants(mod)
|
89
|
+
mod.constants.collect do |symbol|
|
90
|
+
const = mod.const_get(symbol)
|
91
|
+
const if const.respond_to?(:map)
|
92
|
+
end.compact
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
if __FILE__ == $PROGRAM_NAME
|
99
|
+
$LOAD_PATH.unshift(
|
100
|
+
File.join(File.expand_path(File.dirname(__FILE__)), '..', '..'))
|
101
|
+
require 'bel_parser/language'
|
102
|
+
require 'bel_parser/resource/resource_file_reader'
|
103
|
+
initial_state = {
|
104
|
+
resource_reader: BELParser::Resource::ResourceFileReader.new,
|
105
|
+
specification: BELParser::Language.specification(
|
106
|
+
BELParser::Language.latest_supported_version
|
107
|
+
)
|
108
|
+
}
|
109
|
+
|
110
|
+
BELParser::Script::Validator
|
111
|
+
.new(initial_state)
|
112
|
+
.each($stdin) do |(line_number, line, ast, syntax_results, state)|
|
113
|
+
puts "#{line_number}: #{line}"
|
114
|
+
puts ast.to_s(1)
|
115
|
+
|
116
|
+
puts "Syntax errors:"
|
117
|
+
syntax_results
|
118
|
+
.select do |res|
|
119
|
+
res.is_a?(BELParser::Language::Syntax::SyntaxError)
|
120
|
+
end
|
121
|
+
.each do |res|
|
122
|
+
puts " #{res}"
|
123
|
+
end
|
124
|
+
|
125
|
+
puts "Syntax warnings:"
|
126
|
+
syntax_results
|
127
|
+
.select do |res|
|
128
|
+
res.is_a?(BELParser::Language::Syntax::SyntaxWarning)
|
129
|
+
end
|
130
|
+
.each do |res|
|
131
|
+
puts " #{res}"
|
132
|
+
end
|
133
|
+
|
134
|
+
puts "Semantics warnings:"
|
135
|
+
syntax_results
|
136
|
+
.select do |res|
|
137
|
+
res.is_a?(BELParser::Language::Semantics::SemanticsWarning)
|
138
|
+
end
|
139
|
+
.each do |res|
|
140
|
+
puts " #{res}"
|
141
|
+
end
|
142
|
+
|
143
|
+
puts "State:"
|
144
|
+
state.each do |key, value|
|
145
|
+
case value
|
146
|
+
when Hash
|
147
|
+
puts " #{key}: #{value.keys}"
|
148
|
+
else
|
149
|
+
puts " #{key}: #{value}"
|
150
|
+
end
|
151
|
+
end
|
152
|
+
end
|
153
|
+
end
|
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.
|
4
|
+
version: 1.0.0.alpha.14
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Anthony Bargnesi
|
@@ -311,6 +311,20 @@ files:
|
|
311
311
|
- lib/bel_parser/resource/sparql_reader.rb
|
312
312
|
- lib/bel_parser/resource/value.rb
|
313
313
|
- lib/bel_parser/script/parser.rb
|
314
|
+
- lib/bel_parser/script/state/annotation_definition.rb
|
315
|
+
- lib/bel_parser/script/state/bel_version.rb
|
316
|
+
- lib/bel_parser/script/state/document_property.rb
|
317
|
+
- lib/bel_parser/script/state/namespace_definition.rb
|
318
|
+
- lib/bel_parser/script/state/set.rb
|
319
|
+
- lib/bel_parser/script/state/unset.rb
|
320
|
+
- lib/bel_parser/script/state_function.rb
|
321
|
+
- lib/bel_parser/script/syntax/expression_validation.rb
|
322
|
+
- lib/bel_parser/script/syntax/invalid_regex_pattern.rb
|
323
|
+
- lib/bel_parser/script/syntax/undefined_annotation.rb
|
324
|
+
- lib/bel_parser/script/syntax/undefined_annotation_value.rb
|
325
|
+
- lib/bel_parser/script/syntax/unresolvable_namespace.rb
|
326
|
+
- lib/bel_parser/script/syntax/unsupported_bel_version.rb
|
327
|
+
- lib/bel_parser/script/validator.rb
|
314
328
|
- lib/bel_parser/vendor/ast.rb
|
315
329
|
- lib/bel_parser/vendor/ast/node.rb
|
316
330
|
- lib/bel_parser/vendor/ast/processor.rb
|