Class: BELParser::Language::ExpressionValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/bel_parser/language/expression_validator.rb

Overview

ExpressionValidator validates the syntax and semantics of BEL expressions when supplied a Specification and Hash of namespaces.

Defined Under Namespace

Modules: Result Classes: NestedStatementResult, ObservedTermResult, ParameterResult, SimpleStatementResult, TermResult

Instance Method Summary collapse

Constructor Details

#initialize(spec, namespaces, uri_reader, url_reader, will_match_partial = false) ⇒ ExpressionValidator

Returns a new instance of ExpressionValidator.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/bel_parser/language/expression_validator.rb', line 12

def initialize(spec, namespaces, uri_reader, url_reader, will_match_partial = false)
  @spec                         = spec
  @namespaces                   = namespaces || {}

  # double negate truthy or falsey value to strict boolean
  @will_match_partial           = !!will_match_partial

  @syntax_functions             = Syntax.syntax_functions
  @semantics_functions          = Semantics.semantics_functions
  @default_namespace_transform  =
    ApplyDefaultNamespace.new(@spec, @namespaces, uri_reader, url_reader)
  @namespace_encoding_transform =
    ApplyNamespaceEncoding.new(@spec, @namespaces, uri_reader, url_reader)

end

Instance Method Details

#validate(expression_node) ⇒ BELParser::Language::Syntax::SyntaxResult

Validate the syntax and semantics of expression_node.

Parameters:

  • to validate

Returns:

  • syntax results



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/bel_parser/language/expression_validator.rb', line 33

def validate(expression_node)
  if @spec.version >= "2.0"
    expression_node = @default_namespace_transform.process(expression_node)
  end

  @namespace_encoding_transform.process(expression_node)

  case expression_node
  when BELParser::Parsers::AST::SimpleStatement
    SimpleStatementResult.new(
      expression_node,
      validate(expression_node.statement.subject.term),
      validate(expression_node.statement.object.child),
      syntax(expression_node),
      semantics(expression_node))
  when BELParser::Parsers::AST::ObservedTerm
    ObservedTermResult.new(
      expression_node,
      validate(expression_node.statement.subject.term),
      syntax(expression_node),
      semantics(expression_node))
  when BELParser::Parsers::AST::NestedStatement
    NestedStatementResult.new(
      expression_node,
      validate(expression_node.statement.subject.term),
      validate(expression_node.statement.object.child),
      syntax(expression_node),
      semantics(expression_node))
  when BELParser::Parsers::AST::Statement
    SimpleStatementResult.new(
      expression_node,
      validate(expression_node.subject.term),
      validate(expression_node.object.child),
      syntax(expression_node),
      semantics(expression_node))
  when BELParser::Parsers::AST::Term
    TermResult.new(
      expression_node,
      syntax(expression_node),
      semantics(expression_node))
  when BELParser::Parsers::AST::Parameter
    ParameterResult.new(
      expression_node,
      syntax(expression_node),
      semantics(expression_node))
  else
    nil
  end
end