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.



46
47
48
49
50
51
# File 'lib/activefacts/cql/parser.rb', line 46

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.



43
44
45
# File 'lib/activefacts/cql/parser.rb', line 43

def global_term
  @global_term
end

#termObject (readonly)

Returns the value of attribute term.



43
44
45
# File 'lib/activefacts/cql/parser.rb', line 43

def term
  @term
end

#termsObject (readonly)

Returns the value of attribute terms.



44
45
46
# File 'lib/activefacts/cql/parser.rb', line 44

def terms
  @terms
end

Instance Method Details

#allowed_forward_terms(terms) ⇒ Object



63
64
65
# File 'lib/activefacts/cql/parser.rb', line 63

def allowed_forward_terms(terms)
  @allowed_forward_terms = terms
end

#global_term_continues?(s) ⇒ Boolean

Returns:

  • (Boolean)


160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/activefacts/cql/parser.rb', line 160

def global_term_continues?(s)
  @term_part = "#{@term_part} #{s}"
  t = @terms[@term_part]
  if t
    trace :context, "Multi-word term #{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
      trace :context, "saving context #{@term}/#{@global_term}"
      # trace :context, "@terms =\n\t#{@terms.map{|k,v| "#{k} => #{v}"} * "\n\t"}"
      @context_saver.context = {:term => @term, :global_term => @global_term }
    end
  end
  t
end

#global_term_starts?(s, context_saver) ⇒ Boolean

Returns:

  • (Boolean)


138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/activefacts/cql/parser.rb', line 138

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

  @term_part = s
  @context_saver = context_saver
  t = @terms[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
    trace :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 }
    trace :context, "Term #{s} is an allowed forward"
    return true
  end
  t
end

#new_leading_adjective_term(adj, term) ⇒ Object



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

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

#new_trailing_adjective_term(adj, term) ⇒ Object



72
73
74
75
# File 'lib/activefacts/cql/parser.rb', line 72

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

#object_type(name, kind) ⇒ Object



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

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

#reset_role_namesObject



58
59
60
61
# File 'lib/activefacts/cql/parser.rb', line 58

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

#role_name(name) ⇒ Object



77
78
79
80
# File 'lib/activefacts/cql/parser.rb', line 77

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

#system_term(s) ⇒ Object



178
179
180
# File 'lib/activefacts/cql/parser.rb', line 178

def system_term(s)
  false
end

#term_complete?Boolean

Returns:

  • (Boolean)


130
131
132
133
134
135
136
# File 'lib/activefacts/cql/parser.rb', line 130

def term_complete?
  return true if @allowed_forward_terms.include?(@term)
  return true if system_term(@term)
  result = ((t = @terms[@term] and t[@term]) or (t = @role_names[@term] and t[@term]))
  trace :context, "term #{@term} is #{result ? '' : 'in'}complete"
  result
end

#term_continues?(s) ⇒ Boolean

Returns:

  • (Boolean)


104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/activefacts/cql/parser.rb', line 104

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
    trace :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
      trace :context, "saving context #{@term}/#{@global_term}"
      # trace :context, "@terms =\n\t#{@terms.map{|k,v| "#{k} => #{v}"} * "\n\t"}"
      # trace :context, "@role_names =\n\t#{@role_names.map{|k,v| "#{k} => #{v}"} * "\n\t"}"
      @context_saver.context = {:term => @term, :global_term => @global_term }
    end
  end
  t
end

#term_starts?(s, context_saver) ⇒ Boolean

Returns:

  • (Boolean)


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/activefacts/cql/parser.rb', line 82

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
    trace :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 }
    trace :context, "Term #{s} is an allowed forward"
    return true
  end
  t
end

#unit?(s) ⇒ Boolean

Returns:

  • (Boolean)


182
183
184
# File 'lib/activefacts/cql/parser.rb', line 182

def unit? s
  @parser.unit? s
end