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.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/modl/parser/global_parse_context.rb', line 11

def initialize
  # 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.



9
10
11
# File 'lib/modl/parser/global_parse_context.rb', line 9

def interpreter_syntax_version
  @interpreter_syntax_version
end

#syntax_versionObject

Returns the value of attribute syntax_version.



8
9
10
# File 'lib/modl/parser/global_parse_context.rb', line 8

def syntax_version
  @syntax_version
end

Instance Method Details

#add_to_index(item) ⇒ Object



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

def add_to_index(item)
  @index << item
end

#allow_listObject



167
168
169
170
171
172
173
# File 'lib/modl/parser/global_parse_context.rb', line 167

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



151
152
153
154
155
156
157
# File 'lib/modl/parser/global_parse_context.rb', line 151

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

#class_listObject



105
106
107
108
109
110
111
112
113
# File 'lib/modl/parser/global_parse_context.rb', line 105

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



58
59
60
61
62
63
64
65
66
67
# File 'lib/modl/parser/global_parse_context.rb', line 58

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



44
45
46
# File 'lib/modl/parser/global_parse_context.rb', line 44

def enter_condition
  @conditional += 1
end

#exit_conditionObject



48
49
50
# File 'lib/modl/parser/global_parse_context.rb', line 48

def exit_condition
  @conditional -= 1
end

#file_listObject



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

def file_list
  @loaded_files.dup
end

#has_class?(key) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#has_pairs?Boolean

Returns:

  • (Boolean)


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

def has_pairs?
  @pairs.length.positive?
end

#has_user_method?(key) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#id_listObject



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

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)


40
41
42
# File 'lib/modl/parser/global_parse_context.rb', line 40

def in_condition?
  @conditional.positive?
end

#index_value(n, default) ⇒ Object



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

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

  @index[n]
end

#loaded_file(str) ⇒ Object



30
31
32
# File 'lib/modl/parser/global_parse_context.rb', line 30

def loaded_file(str)
  @loaded_files << str unless str.nil?
end

#merge_classes(other) ⇒ Object



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

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

#merge_pairs(other) ⇒ Object



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

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

#method_listObject



115
116
117
118
119
120
121
122
123
# File 'lib/modl/parser/global_parse_context.rb', line 115

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



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

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



52
53
54
55
56
# File 'lib/modl/parser/global_parse_context.rb', line 52

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

  @pairs[key] = val
end

#superclass_listObject



143
144
145
146
147
148
149
# File 'lib/modl/parser/global_parse_context.rb', line 143

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

#transform_listObject



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

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



94
95
96
97
98
# File 'lib/modl/parser/global_parse_context.rb', line 94

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

  @methods_hash[key] = val
end

#user_method_id(key, val) ⇒ Object



100
101
102
103
# File 'lib/modl/parser/global_parse_context.rb', line 100

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