Class: MODL::Parser::GlobalParseContext

Inherits:
Object
  • Object
show all
Defined in:
lib/modl/parser/global_parse_context.rb

Overview

Each time we run the parser on a MODL file we need to keep track of a few things so they can be made available to other areas of the code. This class is the container for that contextual information, which is gathered as the MODL::Parser:Parsed object processes the parse tree.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGlobalParseContext

Returns a new instance of GlobalParseContext.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/modl/parser/global_parse_context.rb', line 38

def initialize
  # This keeps track of all loaded structures from parsed files.
  @structures = []
  # This is set to true if we encounter a *L[OAD] instruction
  @max_files_allowed = 2147483647
  # Holds the index array from a MODL file, e.g. '?=a:b:c:d'
  @index = []
  # Contains all pairs as they are encountered in the parsing process.
  @pairs = {}
  # Contains all defined and loaded classes for the current MODL document.
  @classes_by_id = {}
  @classes_by_name = {}
  # Hold the user-defined methods.
  @methods_hash = {}
  @methods_by_id = {}
  # Tracks the nesting depth for conditional clauses.
  @conditional = 0
  # Defaults to 1 and can be overridden by the *version command.
  @syntax_version = 1
  @interpreter_syntax_version = 1
  @loaded_files = []
end

Instance Attribute Details

#interpreter_syntax_versionObject (readonly)

Returns the value of attribute interpreter_syntax_version.



35
36
37
# File 'lib/modl/parser/global_parse_context.rb', line 35

def interpreter_syntax_version
  @interpreter_syntax_version
end

#loaded_filesObject (readonly)

Returns the value of attribute loaded_files.



36
37
38
# File 'lib/modl/parser/global_parse_context.rb', line 36

def loaded_files
  @loaded_files
end

#max_files_allowedObject

Returns the value of attribute max_files_allowed.



33
34
35
# File 'lib/modl/parser/global_parse_context.rb', line 33

def max_files_allowed
  @max_files_allowed
end

#structuresObject

Returns the value of attribute structures.



34
35
36
# File 'lib/modl/parser/global_parse_context.rb', line 34

def structures
  @structures
end

#syntax_versionObject

Returns the value of attribute syntax_version.



32
33
34
# File 'lib/modl/parser/global_parse_context.rb', line 32

def syntax_version
  @syntax_version
end

Instance Method Details

#add_to_index(item) ⇒ Object



132
133
134
# File 'lib/modl/parser/global_parse_context.rb', line 132

def add_to_index(item)
  @index << item
end

#allow_listObject



209
210
211
212
213
214
215
# File 'lib/modl/parser/global_parse_context.rb', line 209

def allow_list
  result = {}
  @classes_by_id.each do |c|
    result[c[0]] = c[1].allow.nil? ? nil : c[1].allow.extract_hash
  end
  result
end

#assign_listObject



193
194
195
196
197
198
199
# File 'lib/modl/parser/global_parse_context.rb', line 193

def assign_list
  result = {}
  @classes_by_id.each do |c|
    result[c[0]] = c[1].assign
  end
  result
end

#class_listObject



147
148
149
150
151
152
153
154
155
# File 'lib/modl/parser/global_parse_context.rb', line 147

def class_list
  result = []
  @classes_by_id.values.each do |clazz|
    new_item = {}
    new_item[clazz.id] = class_to_hash(clazz)
    result << new_item
  end
  result
end

#classs(key) ⇒ Object



95
96
97
98
99
100
101
102
103
104
# File 'lib/modl/parser/global_parse_context.rb', line 95

def classs(key)
  if key.is_a? String
    result = @classes_by_id[key]
    result = @classes_by_name[key] if result.nil?
    result
  elsif key.is_a? MODLClass
    @classes_by_id[key.id] = key if key.id
    @classes_by_name[key.name] = key if key.name
  end
end

#enter_conditionObject



81
82
83
# File 'lib/modl/parser/global_parse_context.rb', line 81

def enter_condition
  @conditional += 1
end

#exit_conditionObject



85
86
87
# File 'lib/modl/parser/global_parse_context.rb', line 85

def exit_condition
  @conditional -= 1
end

#file_listObject



167
168
169
# File 'lib/modl/parser/global_parse_context.rb', line 167

def file_list
  @loaded_files.dup
end

#freeze_max_files(plus) ⇒ Object



77
78
79
# File 'lib/modl/parser/global_parse_context.rb', line 77

def freeze_max_files(plus)
  @max_files_allowed = @loaded_files.length + plus
end

#has_class?(key) ⇒ Boolean

Returns:

  • (Boolean)


124
125
126
# File 'lib/modl/parser/global_parse_context.rb', line 124

def has_class?(key)
  @classes_by_id.keys.include?(key) || @classes_by_name.keys.include?(key)
end

#has_pairs?Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/modl/parser/global_parse_context.rb', line 120

def has_pairs?
  @pairs.length.positive?
end

#has_user_method?(key) ⇒ Boolean

Returns:

  • (Boolean)


128
129
130
# File 'lib/modl/parser/global_parse_context.rb', line 128

def has_user_method?(key)
  @methods_hash.keys.include?(key)
end

#id_listObject



171
172
173
174
175
176
# File 'lib/modl/parser/global_parse_context.rb', line 171

def id_list
  ids = {}
  ids['methods'] = @methods_by_id.keys.dup.sort!
  ids['classes'] = @classes_by_id.keys.dup.sort!
  ids
end

#in_condition?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/modl/parser/global_parse_context.rb', line 73

def in_condition?
  @conditional.positive?
end

#index_value(n, default) ⇒ Object



67
68
69
70
71
# File 'lib/modl/parser/global_parse_context.rb', line 67

def index_value(n, default)
  return default if n > @index.length

  @index[n]
end

#loaded_file(str) ⇒ Object

Raises:



61
62
63
64
65
# File 'lib/modl/parser/global_parse_context.rb', line 61

def loaded_file(str)
  @loaded_files << str unless str.nil?
  raise InterpreterError, 'Cannot load multiple files after *LOAD instruction' if @loaded_files.length > @max_files_allowed

end

#merge_classes(other) ⇒ Object



110
111
112
113
# File 'lib/modl/parser/global_parse_context.rb', line 110

def merge_classes(other)
  @classes_by_id.merge!(other.all_classes_by_id)
  @classes_by_name.merge!(other.all_classes_by_name)
end

#merge_loaded_files(other) ⇒ Object

Raises:



115
116
117
118
# File 'lib/modl/parser/global_parse_context.rb', line 115

def merge_loaded_files(other)
  @loaded_files.concat(other.loaded_files)
  raise InterpreterError, 'Cannot load multiple files after *LOAD instruction' if @loaded_files.length > @max_files_allowed
end

#merge_pairs(other) ⇒ Object



106
107
108
# File 'lib/modl/parser/global_parse_context.rb', line 106

def merge_pairs(other)
  @pairs.merge!(other.all_pairs)
end

#method_listObject



157
158
159
160
161
162
163
164
165
# File 'lib/modl/parser/global_parse_context.rb', line 157

def method_list
  result = []
  @methods_by_id.values.each do |m|
    new_item = {}
    new_item[m.id] = method_to_hash(m)
    result << new_item
  end
  result
end

#name_listObject



178
179
180
181
182
183
# File 'lib/modl/parser/global_parse_context.rb', line 178

def name_list
  names = {}
  names['methods'] = @methods_hash.keys.dup.sort!
  names['classes'] = @classes_by_name.keys.dup.sort!
  names
end

#pair(key, val = nil) ⇒ Object



89
90
91
92
93
# File 'lib/modl/parser/global_parse_context.rb', line 89

def pair(key, val = nil)
  return @pairs[key] unless val

  @pairs[key] = val
end

#superclass_listObject



185
186
187
188
189
190
191
# File 'lib/modl/parser/global_parse_context.rb', line 185

def superclass_list
  result = {}
  @classes_by_id.each do |c|
    result[c[0]] = c[1].superclass
  end
  result
end

#transform_listObject



201
202
203
204
205
206
207
# File 'lib/modl/parser/global_parse_context.rb', line 201

def transform_list
  result = {}
  @methods_by_id.each do |c|
    result[c[0]] = c[1].transform
  end
  result
end

#user_method(key, val = nil) ⇒ Object



136
137
138
139
140
# File 'lib/modl/parser/global_parse_context.rb', line 136

def user_method(key, val = nil)
  return @methods_hash[key] unless val

  @methods_hash[key] = val
end

#user_method_id(key, val) ⇒ Object



142
143
144
145
# File 'lib/modl/parser/global_parse_context.rb', line 142

def user_method_id(key, val)
  @methods_hash[key] = val
  @methods_by_id[key] = val
end