Class: ActiveFacts::CQL::Parser::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/activefacts/cql/parser.rb

Overview

The Context manages some key information revealed or needed during parsing These methods are semantic predicates; if they return false this parse rule will fail.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parser) ⇒ Context

Returns a new instance of Context.



53
54
55
56
57
58
# File 'lib/activefacts/cql/parser.rb', line 53

def initialize(parser)
  @parser = parser
  @terms = {}
  @role_names = {}
  @allowed_forward_terms = []
end

Instance Attribute Details

#global_termObject (readonly)

Returns the value of attribute global_term.



50
51
52
# File 'lib/activefacts/cql/parser.rb', line 50

def global_term
  @global_term
end

#termObject (readonly)

Returns the value of attribute term.



50
51
52
# File 'lib/activefacts/cql/parser.rb', line 50

def term
  @term
end

#termsObject (readonly)

Returns the value of attribute terms.



51
52
53
# File 'lib/activefacts/cql/parser.rb', line 51

def terms
  @terms
end

Instance Method Details

#allowed_forward_terms(terms) ⇒ Object



70
71
72
# File 'lib/activefacts/cql/parser.rb', line 70

def allowed_forward_terms(terms)
  @allowed_forward_terms = terms
end

#new_leading_adjective_term(adj, term) ⇒ Object



74
75
76
77
# File 'lib/activefacts/cql/parser.rb', line 74

def new_leading_adjective_term(adj, term)
  index_name(@role_names, "#{adj} #{term}", term) && debug(:context, "new compound term '#{adj}- #{term}'")
  true
end

#new_trailing_adjective_term(adj, term) ⇒ Object



79
80
81
82
# File 'lib/activefacts/cql/parser.rb', line 79

def new_trailing_adjective_term(adj, term)
  index_name(@role_names, "#{term} #{adj}", term) && debug(:context, "new compound term '#{term} -#{adj}'")
  true
end

#object_type(name, kind) ⇒ Object



60
61
62
63
# File 'lib/activefacts/cql/parser.rb', line 60

def object_type(name, kind)
  index_name(@terms, name) && debug(:context, "new #{kind} '#{name}'")
  true
end

#reset_role_namesObject



65
66
67
68
# File 'lib/activefacts/cql/parser.rb', line 65

def reset_role_names
  debug :context, "\tresetting role names #{@role_names.keys.sort*", "}" if @role_names && @role_names.size > 0
  @role_names = {}
end

#role_name(name) ⇒ Object



84
85
86
87
# File 'lib/activefacts/cql/parser.rb', line 84

def role_name(name)
  index_name(@role_names, name) && debug(:context, "new role '#{name}'")
  true
end

#system_term(s) ⇒ Object



142
143
144
# File 'lib/activefacts/cql/parser.rb', line 142

def system_term(s)
  false
end

#term_complete?Boolean

Returns:

  • (Boolean)


135
136
137
138
139
140
# File 'lib/activefacts/cql/parser.rb', line 135

def term_complete?
  return true if @allowed_forward_terms.include?(@term)
  return true if system_term(@term)
  (t = @terms[@term] and t[@term]) or
    (t = @role_names[@term] and t[@term])
end

#term_continues?(s) ⇒ Boolean

Returns:

  • (Boolean)


111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/activefacts/cql/parser.rb', line 111

def term_continues?(s)
  @term_part = "#{@term_part} #{s}"
  t = @terms[@term_part]
  r = @role_names[@term_part]
  if t && (!r || !r[@term_part])    # Part of a term and not a complete role name
    w = "term"
  else
    t = r
    w = "role_name"
  end
  if t
    debug :context, "Multi-word #{w} #{t[@term_part] ? 'ends at' : 'continues to'} #{@term_part.inspect}"

    # Record the name of the full term and the underlying global term:
    if t[@term_part]
      @term = @term_part if t[@term_part]
      @global_term = (t = t[@term_part]) == true ? @term_part : t
      debug :context, "saving context #{@term}/#{@global_term}"
      @context_saver.context = {:term => @term, :global_term => @global_term }
    end
  end
  t
end

#term_starts?(s, context_saver) ⇒ Boolean

Returns:

  • (Boolean)


89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/activefacts/cql/parser.rb', line 89

def term_starts?(s, context_saver)
  @term = @global_term = nil

  @term_part = s
  @context_saver = context_saver
  t = @terms[s] || @role_names[s] || system_term(s)
  if t
    # s is a prefix of the keys of t.
    if t[s]
      @global_term = @term = @term_part
      @context_saver.context = {:term => @term, :global_term => @global_term }
    end
    debug :context, "Term #{t[s] ? "is" : "starts"} '#{@term_part}'"
  elsif @allowed_forward_terms.include?(@term_part)
    @term = @term_part
    @context_saver.context = {:term => @term, :global_term => @term }
    debug :context, "Term #{s} is an allowed forward"
    return true
  end
  t
end

#unit?(s) ⇒ Boolean

Returns:

  • (Boolean)


146
147
148
# File 'lib/activefacts/cql/parser.rb', line 146

def unit? s
  @parser.unit? s
end