Class: Glaemscribe::API::ModeParser

Inherits:
Object
  • Object
show all
Defined in:
lib/api/mode_parser.rb

Instance Method Summary collapse

Constructor Details

#initializeModeParser

Returns a new instance of ModeParser.


28
29
30
# File 'lib/api/mode_parser.rb', line 28

def initialize
  @mode = nil
end

Instance Method Details

#create_if_cond_for_if_term(line, if_term, cond) ⇒ Object


89
90
91
92
93
94
95
# File 'lib/api/mode_parser.rb', line 89

def create_if_cond_for_if_term(line, if_term, cond)      
  ifcond                          = IfTree::IfCond.new(line, if_term, cond)
  child_code_block                = IfTree::CodeBlock.new(ifcond)
  ifcond.child_code_block         = child_code_block                
  if_term.if_conds << ifcond 
  ifcond            
end

#parse(file_path, mode_options = {}) ⇒ Object


193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# File 'lib/api/mode_parser.rb', line 193

def parse(file_path, mode_options = {})
        
  @mode = Mode.new(ResourceManager::mode_name_from_file_path(file_path))        
        
  raw   = File.open(file_path,"rb:utf-8").read
  doc   = Glaeml::Parser.new.parse(raw)
  
  if(doc.errors.any?)
    @mode.errors = doc.errors
    return @mode
  end
  
  verify_mode_glaeml(doc)
  
  return @mode if(@mode.errors.any?)
  
  # Get the attributes of the mode
  @mode.language      = doc.root_node.gpath('language').first.args.first
  @mode.writing       = doc.root_node.gpath('writing').first.args.first
  @mode.human_name    = doc.root_node.gpath('mode').first.args.first
  @mode.authors       = doc.root_node.gpath('authors').first.args.first
  @mode.version       = doc.root_node.gpath('version').first.args.first
  @mode.raw_mode_name = doc.root_node.gpath('raw_mode').first.args.first if doc.root_node.gpath('raw_mode').first
  @mode.invention     = doc.root_node.gpath('invention').first.args.first if doc.root_node.gpath('invention').first
  @mode.world         = doc.root_node.gpath('world').first.args.first if doc.root_node.gpath('world').first
          
  doc.root_node.gpath("options.option").each{ |option_element|
    values      = {}
    visibility  = nil
  
    option_element.gpath("value").each{ |value_element|
      value_name                = value_element.args.first
      values[value_name]        = value_element.args.last.to_i
    }
    option_element.gpath("visible_when").each{ |visible_element|
      visibility = visible_element.args.first
    }
  
    option_name_at          = option_element.args[0]
    option_default_val_at   = option_element.args[1]
    # TODO: check syntax of the option name
    
    if(option_default_val_at.nil?)
      @mode.errors << Glaeml::Error.new(option_element.line, "Missing option default value.")
    end
      
    option        = Option.new(@mode, option_name_at, option_default_val_at, values, visibility)
    @mode.options[option.name] = option
  }
  
  # Read the supported font list
  doc.root_node.gpath("charset").each { |charset_element|
    charset_name                      = charset_element.args.first
    charset                           = ResourceManager::charset(charset_name)
    
    # Load the charset if we don't have it
    if !charset
      ResourceManager::load_charsets([charset_name])
      charset                         = ResourceManager::charset(charset_name)  
    end
    if charset
      if charset.errors.any?
        charset.errors.each{ |e|
          @mode.errors << Glaeml::Error.new(charset_element.line, "#{charset_name}:#{e.line}:#{e.text}")
        }
        return @mode
      end
      
      @mode.supported_charsets[charset_name]    = charset
      @mode.default_charset                     = charset if  charset_element.args[1] &&  charset_element.args[1] == "true"
    else
      @mode.warnings << Glaeml::Error.new(charset_element.line,"Failed to load charset '#{charset_name}'.")
    end
  }
  
  if !@mode.default_charset
    @mode.warnings << Glaeml::Error.new(0,"No default charset defined!!")
  end
   
  # Read the preprocessor
  doc.root_node.gpath("preprocessor").each{ |preprocessor_element|
    self.parse_pre_post_processor(preprocessor_element, true)
  }
  
  # Read the postprocessor
  doc.root_node.gpath("postprocessor").each{ |postprocessor_element|
    self.parse_pre_post_processor(postprocessor_element, false)
  }

  # Read the processor
  doc.root_node.gpath("outspace").each{ |outspace_element|
    val                             = outspace_element.args[0]
    @mode.post_processor.out_space  = val.split.reject{|token| token.empty? }       
  }      
  
  doc.root_node.gpath("processor.rules").each{ |rules_element|
  
    rule_group_name                               = rules_element.args.first
    rule_group                                    = RuleGroup.new(@mode,rule_group_name)
    @mode.processor.rule_groups[rule_group_name]  = rule_group
    
    text_procedure = Proc.new { |current_parent_code_block, element|            
      # A block of code lines. Put them in a codelinesterm.   
      term = current_parent_code_block.terms.last
      if(!term || !term.is_code_lines?)
        term = IfTree::CodeLinesTerm.new(current_parent_code_block) 
        current_parent_code_block.terms << term
      end
                             
      lcount  = element.line
      element.args[0].lines.to_a.each{ |l| 
        l       = l.strip
        term.code_lines << IfTree::CodeLine.new(l, lcount)           
        lcount  += 1                         
      }
    }
    
    element_procedure = Proc.new { |current_parent_code_block, element|
      # This is fatal.
      @mode.errors << Glaeml::Error.new(element.line, "Unknown directive #{element.name}.")      
    }  
    
    self.traverse_if_tree( rule_group.root_code_block, rules_element, text_procedure, element_procedure )                 
  }
                                                   
  @mode.finalize(mode_options) if !@mode.errors.any?
  
  @mode
end

#parse_pre_post_processor(processor_element, pre_not_post) ⇒ Object


161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/api/mode_parser.rb', line 161

def parse_pre_post_processor(processor_element, pre_not_post)
  # Do nothing with text elements
  text_procedure = Proc.new { |current_parent_code_block, element| }           
  
  element_procedure = Proc.new { |current_parent_code_block, element|
    
    # A block of code lines. Put them in a codelinesterm.   
    term = current_parent_code_block.terms.last
    if(!term || !term.is_pre_post_processor_operators?)
      term = IfTree::PrePostProcessorOperatorsTerm.new(current_parent_code_block) 
      current_parent_code_block.terms << term
    end
    
    operator_name   = element.name      
    operator_class  = if pre_not_post
      ResourceManager::class_for_pre_processor_operator_name(operator_name)
    else
      ResourceManager::class_for_post_processor_operator_name(operator_name)
    end
    
    if !operator_class
      @mode.errors << Glaeml::Error.new(element.line,"Operator #{operator_name} is unknown.")
    else            
      term.operators << operator_class.new(element.clone)
    end  
  }  
  
  root_code_block = ((pre_not_post)?(@mode.pre_processor.root_code_block):(@mode.post_processor.root_code_block))
         
  self.traverse_if_tree(root_code_block, processor_element, text_procedure, element_procedure )                       
end

#traverse_if_tree(root_code_block, root_element, text_procedure, element_procedure) ⇒ Object


97
98
99
100
101
102
103
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/api/mode_parser.rb', line 97

def traverse_if_tree(root_code_block, root_element, text_procedure, element_procedure)
  current_parent_code_block = root_code_block
  
  root_element.children.each{ |child|
    if(child.text?)
      # Do something with this text 
      text_procedure.call(current_parent_code_block, child)           
    elsif(child.element?)
      case child.name
      when 'if'
        cond_attribute                  = child.args.first
        if_term                         = IfTree::IfTerm.new(current_parent_code_block)
        current_parent_code_block.terms << if_term                  
        if_cond                         = create_if_cond_for_if_term(child.line, if_term, cond_attribute)             
        current_parent_code_block       = if_cond.child_code_block
       
      when 'elsif'
       
        cond_attribute                  = child.args.first
        if_term                         = current_parent_code_block.parent_if_cond.parent_if_term  
        
        if !if_term
          @mode.errors << Glaeml::Error.new(child.line, " 'elsif' without a 'if'.")
          return
        end
        
        # TODO : check that precendent one is a elsif, not a else
        if_cond                         = create_if_cond_for_if_term(child.line, if_term, cond_attribute)             
        current_parent_code_block       = if_cond.child_code_block                                       
        
      when 'else'
        
        if_term                         = current_parent_code_block.parent_if_cond.parent_if_term  
        
        if !if_term
          @mode.errors << Glaeml::Error.new(child.line, " 'else' without a 'if'.")
          return
        end
        
        if_cond                         = create_if_cond_for_if_term(child.line, if_term, "true")             
        current_parent_code_block       = if_cond.child_code_block
                                                        
      when 'endif'
        if_term                         = current_parent_code_block.parent_if_cond.parent_if_term  
      
        if !if_term
          @mode.errors << Glaeml::Error.new(child.line, " 'endif' without a 'if'.")
          return
        end
        
        current_parent_code_block       = if_term.parent_code_block
        
      else
        # Do something with this child element
        element_procedure.call(current_parent_code_block, child)            
      end
    end
  }
  
  if(current_parent_code_block.parent_if_cond)
    @mode.errors <<  Glaeml::Error.new(root_element.line, "Unended 'if' at the end of this '#{root_element.name}' element.")
  end
end

#validate_presence_of_args(node, arg_count = nil) ⇒ Object


32
33
34
35
36
37
38
# File 'lib/api/mode_parser.rb', line 32

def validate_presence_of_args(node, arg_count = nil)
  if(arg_count)
    if(node.args.count != arg_count)
      @mode.errors << Glaeml::Error.new(node.line,"Element '#{node.name}' should have #{arg_count} arguments.")
    end
  end
end

#validate_presence_of_children(parent_node, elt_name, elt_count = nil, arg_count = nil) ⇒ Object


40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/api/mode_parser.rb', line 40

def validate_presence_of_children(parent_node, elt_name, elt_count = nil, arg_count = nil)
  res = parent_node.gpath(elt_name)
  if(elt_count)
    if(res.count != elt_count)
      @mode.errors << Glaeml::Error.new(parent_node.line,"Element '#{parent_node.name}' should have exactly #{elt_count} children of type '#{elt_name}'.")
    end
  end
  if(arg_count)
    res.each{ |child_node|
      validate_presence_of_args(child_node, arg_count)
    }
  end
end

#verify_mode_glaeml(doc) ⇒ Object

Very simplified ‘dtd’ like verification


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/api/mode_parser.rb', line 55

def verify_mode_glaeml(doc)
  validate_presence_of_children(doc.root_node, "language", 1, 1)
  validate_presence_of_children(doc.root_node, "writing", 1, 1)
  validate_presence_of_children(doc.root_node, "mode", 1, 1)
  validate_presence_of_children(doc.root_node, "authors", 1, 1)
  validate_presence_of_children(doc.root_node, "version", 1, 1)
 
  doc.root_node.gpath("charset").each{ |charset_element|
    validate_presence_of_args(charset_element, 2)        
  }
 
  doc.root_node.gpath("options.option").each{ |option_element|
    validate_presence_of_args(option_element, 2)
    option_element.gpath("value").each{ |value_element|
      validate_presence_of_args(value_element, 2)
    }
  }
  
  doc.root_node.gpath("postprocessor.outspace").each{ |outspace_element|
    validate_presence_of_args(outspace_element, 1)          
  }
  
  doc.root_node.gpath("processor.rules").each{ |rules_element|
    validate_presence_of_args(rules_element, 1)       
    validate_presence_of_children(rules_element,"if",nil,1);  
    validate_presence_of_children(rules_element,"elsif",nil,1);  
  }        

  doc.root_node.gpath("preprocessor.if").each{ |e| validate_presence_of_args(e,  1) }    
  doc.root_node.gpath("preprocessor.elsif").each{ |e| validate_presence_of_args(e,  1) }    
  doc.root_node.gpath("postprocessor.if").each{ |e| validate_presence_of_args(e,  1) }    
  doc.root_node.gpath("postprocessor.elsif").each{ |e| validate_presence_of_args(e,  1) }    
end