Class: ActiveFacts::CQL::Compiler

Inherits:
Parser
  • Object
show all
Defined in:
lib/activefacts/cql/compiler.rb,
lib/activefacts/cql/compiler/fact.rb,
lib/activefacts/cql/compiler/shared.rb,
lib/activefacts/cql/compiler/reading.rb,
lib/activefacts/cql/compiler/fact_type.rb,
lib/activefacts/cql/compiler/constraint.rb,
lib/activefacts/cql/compiler/value_type.rb,
lib/activefacts/cql/compiler/entity_type.rb

Defined Under Namespace

Classes: Binding, CompilationContext, Concept, Constraint, ContextNote, Definition, Enforcement, EntityType, Fact, FactType, Import, PresenceConstraint, Quantifier, Reading, ReadingMatchSideEffect, ReadingMatchSideEffects, ReferenceMode, RingConstraint, RoleRef, SetComparisonConstraint, SetConstraint, SetEqualityConstraint, SetExclusionConstraint, SubsetConstraint, Unit, ValueConstraint, ValueType, Vocabulary

Constant Summary

Constants included from ActiveFacts

VERSION

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Parser

#context, #parse, #parse_all

Methods included from ActiveFacts

extract_hash_args

Constructor Details

#initialize(filename = "stdin") ⇒ Compiler

Returns a new instance of Compiler.



21
22
23
24
25
# File 'lib/activefacts/cql/compiler.rb', line 21

def initialize(filename = "stdin")
  @filename = filename
  @constellation = ActiveFacts::API::Constellation.new(ActiveFacts::Metamodel)
  debug :file, "Parsing '#{filename}'"
end

Instance Attribute Details

#vocabularyObject (readonly)

Returns the value of attribute vocabulary.



19
20
21
# File 'lib/activefacts/cql/compiler.rb', line 19

def vocabulary
  @vocabulary
end

Instance Method Details

#compile(input) ⇒ Object



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
# File 'lib/activefacts/cql/compiler.rb', line 36

def compile input
  @string = input
  # The syntax tree created from each parsed CQL statement gets passed to the block.
  # parse_all returns an array of the block's non-nil return values.
  ok = parse_all(@string, :definition) do |node|
    debug :parse, "Parsed '#{node.text_value.gsub(/\s+/,' ').strip}'" do
      begin
        ast = node.ast
        next unless ast
        debug :ast, ast.inspect
        ast.source = node.body
        ast.constellation = @constellation
        ast.vocabulary = @vocabulary
        value = compile_definition ast
        debug :definition, "Compiled to #{value.is_a?(Array) ? value.map{|v| v.verbalise}*', ' : value.verbalise}" if value
        @vocabulary = value if ast.is_a?(Compiler::Vocabulary)
      rescue => e
        # Augment the exception message, but preserve the backtrace
        start_line = @string.line_of(node.interval.first)
        end_line = @string.line_of(node.interval.last-1)
        lines = start_line != end_line ? "s #{start_line}-#{end_line}" : " #{start_line.to_s}"
        ne = StandardError.new("at line#{lines} #{e.message.strip}")
        ne.set_backtrace(e.backtrace)
        raise ne
      end
    end
  end
  raise failure_reason unless ok
  vocabulary
end

#compile_definition(ast) ⇒ Object



67
68
69
# File 'lib/activefacts/cql/compiler.rb', line 67

def compile_definition ast
  ast.compile
end

#compile_file(filename) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/activefacts/cql/compiler.rb', line 27

def compile_file filename
  old_filename = @filename
  @filename = filename
  File.open(filename) do |f|
    compile(f.read)
  end
  @filename = old_filename
end

#unit?(s) ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
74
75
76
# File 'lib/activefacts/cql/compiler.rb', line 71

def unit? s
  name = @constellation.Name[s]
  units = !name ? [] : name.all_unit.to_a + name.all_unit_as_plural_name.to_a
  debug :units, "Looking for unit #{s}, got #{units.map{|u|u.name}.inspect}"
  units.size > 0
end