Module: BELParser::Expression::Model::Converters
- Includes:
- Parsers::AST, Quoting
- Included in:
- Parser
- Defined in:
- lib/bel_parser/expression/model/term.rb,
lib/bel_parser/expression/model/namespace.rb,
lib/bel_parser/expression/model/parameter.rb,
lib/bel_parser/expression/model/statement.rb
Constant Summary
Constants included
from Quoting
Quoting::KeywordMatcher, Quoting::Keywords, Quoting::LenientQuotedMatcher, Quoting::NonWordMatcher, Quoting::QuoteNotEscapedMatcher, Quoting::StrictQuotedMatcher
Instance Method Summary
collapse
-
#ast_to_namespace(ast, namespace_hash = {}) ⇒ Object
-
#ast_to_parameter(ast, namespace_hash = {}) ⇒ Object
-
#ast_to_statement(ast, spec, namespaces = {}) ⇒ Object
-
#ast_to_term(ast, spec, namespaces = {}) ⇒ Object
-
#convert_argument(spec, namespaces, argument) ⇒ Object
-
#convert_function(ast, spec) ⇒ Object
-
#convert_object(ast, spec, namespaces) ⇒ Object
-
#convert_relationship(ast, spec) ⇒ Object
assert_is_a
Methods included from Quoting
#identifier_value?, #quote, #quote_if_needed, #quoted?, #string_value?, #unquote, #unquoted?
Instance Method Details
#ast_to_namespace(ast, namespace_hash = {}) ⇒ Object
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
# File 'lib/bel_parser/expression/model/namespace.rb', line 139
def ast_to_namespace(ast, namespace_hash = {})
return nil if ast.nil?
case ast
when BELParser::Parsers::AST::Prefix
if ast.namespace
dataset = ast.namespace
case
when dataset.uri?
Namespace.new(dataset.keyword, dataset.identifier, nil)
when dataset.url?
Namespace.new(dataset.keyword, nil, dataset.identifier)
else
Namespace.new(dataset.keyword, nil, nil)
end
else
return nil unless ast.identifier
prefix_s = ast.identifier.string_literal
namespace_hash[prefix_s]
end
when BELParser::Parsers::AST::NamespaceDefinition
keyword, domain = ast.children
keyword_s = keyword.identifier.string_literal
case
when domain.uri?
uri = domain.child.string_literal
Namespace.new(keyword_s, uri, nil)
when domain.url?
url = domain.child.string_literal
Namespace.new(keyword_s, ast.uri, url)
end
end
end
|
#ast_to_parameter(ast, namespace_hash = {}) ⇒ Object
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
# File 'lib/bel_parser/expression/model/parameter.rb', line 87
def ast_to_parameter(ast, namespace_hash = {})
return nil if ast.nil? ||
!ast.is_a?(BELParser::Parsers::AST::Parameter)
namespace = ast_to_namespace(ast.prefix, namespace_hash)
value = ast.value.children[0].string_literal
if namespace
namespace[value]
else
BELParser::Expression::Model::Parameter.new(
nil, value,
nil )
end
end
|
#ast_to_statement(ast, spec, namespaces = {}) ⇒ Object
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
# File 'lib/bel_parser/expression/model/statement.rb', line 158
def ast_to_statement(ast, spec, namespaces = {})
statement =
case ast
when BELParser::Parsers::AST::Statement
ast
when ObservedTerm, SimpleStatement, NestedStatement
ast.statement
else
nil
end
return nil if statement.nil?
spec ||= BELParser::Language.default_specification
= statement. && statement..children[0]
Statement.new(
ast_to_term(statement.subject.term, spec, namespaces),
convert_relationship(statement.relationship, spec),
convert_object(statement.object, spec, namespaces),
)
end
|
#ast_to_term(ast, spec, namespaces = {}) ⇒ Object
117
118
119
120
121
122
123
124
125
126
|
# File 'lib/bel_parser/expression/model/term.rb', line 117
def ast_to_term(ast, spec, namespaces = {})
return nil if ast.nil? ||
!ast.is_a?(BELParser::Parsers::AST::Term)
spec ||= BELParser::Language.default_specification
convert = method(:convert_argument).to_proc.curry[spec][namespaces]
Term.new(
convert_function(ast.function, spec),
ast.arguments.map(&convert))
end
|
#convert_argument(spec, namespaces, argument) ⇒ Object
140
141
142
143
144
145
146
147
148
149
150
|
# File 'lib/bel_parser/expression/model/term.rb', line 140
def convert_argument(spec, namespaces, argument)
child = argument.child
case child
when BELParser::Parsers::AST::Parameter
ast_to_parameter(child, namespaces)
when BELParser::Parsers::AST::Term
ast_to_term(child, spec, namespaces)
else
nil
end
end
|
#convert_function(ast, spec) ⇒ Object
128
129
130
131
132
133
134
135
136
137
138
|
# File 'lib/bel_parser/expression/model/term.rb', line 128
def convert_function(ast, spec)
func = ast.identifier.string_literal
spec_func = spec.function(func.to_sym)
unless spec_func
raise(
ArgumentError,
%(ast has invalid function "#{func}" for BEL #{spec.version}))
end
spec_func
end
|
#convert_object(ast, spec, namespaces) ⇒ Object
185
186
187
188
189
190
191
192
193
194
|
# File 'lib/bel_parser/expression/model/statement.rb', line 185
def convert_object(ast, spec, namespaces)
case
when ast.term?
ast_to_term(ast.child, spec, namespaces)
when ast.statement?
ast_to_statement(ast.child, spec, namespaces)
else
nil
end
end
|
#convert_relationship(ast, spec) ⇒ Object
180
181
182
183
|
# File 'lib/bel_parser/expression/model/statement.rb', line 180
def convert_relationship(ast, spec)
relationship = ast.string_literal
relationship && spec.relationship(relationship.to_sym)
end
|