Class: Astrapi::RubyGenerator
- Inherits:
-
Object
- Object
- Astrapi::RubyGenerator
- Defined in:
- lib/ruby_generator.rb
Constant Summary collapse
- BASIC_TYPES =
Astrapi basic types :
[:integer,:float,:range,:ident,:string]
Instance Attribute Summary collapse
-
#mm ⇒ Object
Returns the value of attribute mm.
Instance Method Summary collapse
- #code_for(klass) ⇒ Object
- #compute_possible_starters(type) ⇒ Object
- #core_parsing_for(klass) ⇒ Object
- #find_mother_of(klass) ⇒ Object
- #find_sons_of(klass) ⇒ Object
- #generate(mm) ⇒ Object
- #generate_ast ⇒ Object
- #generate_dot_ast_printer ⇒ Object
- #generate_dsl_compiler ⇒ Object
- #generate_misc ⇒ Object
- #generate_sexp_lexer ⇒ Object
- #generate_sexp_parser ⇒ Object
- #generate_sexp_pretty_printer ⇒ Object
- #generate_visitor ⇒ Object
- #hierarchical_attrs(klass) ⇒ Object
- #is_basic_type(type) ⇒ Object
- #parsing_code_for(attr, ast_node_var) ⇒ Object
Instance Attribute Details
#mm ⇒ Object
Returns the value of attribute mm.
8 9 10 |
# File 'lib/ruby_generator.rb', line 8 def mm @mm end |
Instance Method Details
#code_for(klass) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/ruby_generator.rb', line 64 def code_for klass code=Code.new mother = klass.inheritance inheritance="< #{klass.inheritance}" if mother inheritance ||= "< Ast" code << "class #{klass.name} #{inheritance}" code.indent=2 code << "attr_accessor #{klass.attrs.collect{|atr| ':'+atr.name.to_s}.join(',')}" code << "def initialize" code.indent=4 klass.attrs.each do |atr| init=(atr.type.is_a? ArrayOf) ? "[]" : "nil" code << "@#{atr.name} = #{init}" end code.indent=2 code << "end" code.indent=0 code << "end" code end |
#compute_possible_starters(type) ⇒ Object
321 322 323 324 325 326 327 |
# File 'lib/ruby_generator.rb', line 321 def compute_possible_starters type ret=[] sons=find_sons_of(type) starters= (sons << type).flatten ret=starters.collect{|starter| starter.name.to_s.downcase.to_sym} ret=ret.collect{|sym| (BASIC_TYPES.include? sym) ? (sym.to_s+"_lit").to_sym : sym} end |
#core_parsing_for(klass) ⇒ Object
243 244 245 246 247 248 249 250 251 |
# File 'lib/ruby_generator.rb', line 243 def core_parsing_for klass kname=klass.name.to_s #puts "#{kname}".center(80,'=') v=kname.downcase attrs = hierarchical_attrs(klass) + klass.attrs #note the order! code=Code.new attrs.each{|attr| code << parsing_code_for(attr,v)} return code end |
#find_mother_of(klass) ⇒ Object
262 263 264 265 266 267 |
# File 'lib/ruby_generator.rb', line 262 def find_mother_of klass if id=klass.inheritance #identifier return mother=mm.classes.find{|k| k.name==id} end nil end |
#find_sons_of(klass) ⇒ Object
269 270 271 272 |
# File 'lib/ruby_generator.rb', line 269 def find_sons_of klass ret=mm.classes.select{|k| k.inheritance==klass.name} ret end |
#generate(mm) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/ruby_generator.rb', line 13 def generate mm @mm=mm @path=__dir__ #ruby > 2.0 @path+="/" generate_misc generate_ast generate_dot_ast_printer generate_sexp_lexer generate_sexp_parser generate_sexp_pretty_printer generate_visitor generate_dsl_compiler end |
#generate_ast ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/ruby_generator.rb', line 27 def generate_ast puts "----> generating #{mm.name} DSL AST classes" mm_name=mm.name code = Code.new code << "# code generated automatically by M3E" code << "module #{mm_name}" code.newline code.indent=2 code << "class Ast" code.indent=4 code << "attr_accessor :comments_" code << "def accept(visitor, arg=nil)" code << " name = self.class.name.split(/::/)[1]" code << " visitor.send(\"visit\#{name}\".to_sym, self ,arg) # Metaprograming !" code << "end" code.newline code.indent=2 code << "end" code.newline code.indent=2 code << "class Comment_ < Ast" code.indent=4 code << "attr_accessor :list" code.indent=2 code << "end" code.newline code.indent=2 mm.classes.each{|k| code << code_for(k) code.newline } code.indent=0 code << "end" code.save_as("#{mm_name.to_s.downcase}_ast.rb",false) end |
#generate_dot_ast_printer ⇒ Object
218 219 220 221 222 223 224 |
# File 'lib/ruby_generator.rb', line 218 def generate_dot_ast_printer puts "----> generating #{mm.name} DSL AST printer" #...... call templating engine engine = ERB.new(IO.read(@path+"template_ast_printer.rb")) printer_rb_code=engine.result(binding) File.open("#{mm.name.to_s.downcase}_ast_printer.rb",'w'){|f| f.puts printer_rb_code} end |
#generate_dsl_compiler ⇒ Object
226 227 228 229 230 231 232 |
# File 'lib/ruby_generator.rb', line 226 def generate_dsl_compiler puts "----> generating #{mm.name} DSL compiler" #...... call templating engine engine = ERB.new(IO.read(@path+"template_compiler.rb")) code=engine.result(binding) File.open("#{mm.name.to_s.downcase}_compiler.rb",'w'){|f| f.puts code} end |
#generate_misc ⇒ Object
234 235 236 237 238 239 240 241 |
# File 'lib/ruby_generator.rb', line 234 def generate_misc engine = ERB.new(IO.read(@path+"template_indent.rb")) code=engine.result(binding) File.open("indent.rb",'w'){|f| f.puts code} engine = ERB.new(IO.read(@path+"template_code.rb")) code=engine.result(binding) File.open("code.rb",'w'){|f| f.puts code} end |
#generate_sexp_lexer ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/ruby_generator.rb', line 85 def generate_sexp_lexer puts "----> generating #{mm.name} DSL lexer" keywords=mm.classes.collect{|k| k.name.to_s}.map{|k| k.downcase.to_sym} regexp_code=Code.new #to be inserted in ERB template regexp_code.indent=4 keywords.each do |kw| regexp_code << "#{kw.upcase.to_s.ljust(30)} = /\\A#{kw}/" end keywords_regexp=regexp_code.finalize #....... regexp_code=Code.new regexp_code.indent=8 keywords.each do |kw| regexp_code << "when value = text.match(#{kw.upcase})" regexp_code << " return Token.new [:#{kw},text,position]" end apply_regexp=regexp_code.finalize #...... call templating engine engine = ERB.new(IO.read(@path+"template_lexer.rb")) lexer_rb_code=engine.result(binding) File.open("#{mm.name.to_s.downcase}_lexer.rb",'w'){|f| f.puts lexer_rb_code} end |
#generate_sexp_parser ⇒ Object
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 |
# File 'lib/ruby_generator.rb', line 108 def generate_sexp_parser puts "----> generating #{mm.name} DSL parser" code=Code.new code.indent=4 mm.classes.each do |klass| kname=klass.name.to_s v=kname.downcase code.newline code << "def parse#{kname}" code.indent=6 code << "indent \"#{klass.name}\"" code << "if n=nil_maybe?" code << " return n" code << "end" code << "#{v}_ = #{mm.name}::#{kname}.new" code << "#{v}_.comments_=parse_comments()" code << "expect :lparen" code << "expect :#{v}" code << core_parsing_for(klass) code << "expect :rparen" code << "dedent" code << "return #{v}_" code.indent=4 code << "end" end parsing_methods=code.finalize #...... call templating engine engine = ERB.new(IO.read(@path+"template_parser.rb")) parser_rb_code=engine.result(binding) File.open("#{mm.name.to_s.downcase}_parser.rb",'w'){|f| f.puts parser_rb_code} end |
#generate_sexp_pretty_printer ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/ruby_generator.rb', line 140 def generate_sexp_pretty_printer puts "----> generating #{mm.name} DSL pretty printer" code=Code.new code.indent=4 mm.classes.each do |klass| kname=klass.name.to_s v=kname.downcase code.newline code << "def visit#{kname}(#{v}_,args=nil)" code.indent=6 code << "indent \"#{klass.name}\"" code << "ary=[]" code << "ary << #{v}_.comments_.val if #{v}_.comments_" code << "ary << #{v}_.class.to_s.downcase.split('::')[1].to_sym" attrs = hierarchical_attrs(klass) + klass.attrs #note the order! code_for_attrs=attrs.collect{|atr| if atr.type.is_a? ArrayOf e=((name=atr.name.to_s).end_with?('s')) ? name[0..-2] : "e" code << "#{v}_.#{atr.name}.each do |#{e}|" code.indent=8 code << "ary << #{e}.accept(self,args)" code.indent=6 code << "end" else code << "ary << #{v}_.#{atr.name}.accept(self)" end } code << "dedent" code << "return ary" code.indent=4 code << "end" end visiting_methods=code.finalize #...... call templating engine engine = ERB.new(IO.read(@path+"template_pretty_printer.rb")) parser_rb_code=engine.result(binding) File.open("#{mm.name.to_s.downcase}_pp.rb",'w'){|f| f.puts parser_rb_code } end |
#generate_visitor ⇒ Object
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/ruby_generator.rb', line 181 def generate_visitor puts "----> generating #{mm.name} DSL visitor" code=Code.new code.indent=4 mm.classes.each do |klass| kname=klass.name.to_s v=kname.downcase code.newline code << "def visit#{kname}(#{v}_,args=nil)" code.indent=6 code << "indent \"#{klass.name}\"" attrs = hierarchical_attrs(klass) + klass.attrs #note the order! code_for_attrs=attrs.collect{|atr| if atr.type.is_a? ArrayOf e=((name=atr.name.to_s).end_with?('s')) ? name[0..-2] : "e" code << "#{v}_.#{atr.name}.each do |#{e}|" code.indent=8 code << "#{e}.accept(self,args)" code.indent=6 code << "end" else code << "#{v}_.#{atr.name}.accept(self)" end } code << "dedent" code.indent=4 code << "end" end visiting_methods=code.finalize #...... call templating engine engine = ERB.new(IO.read(@path+"template_visitor.rb")) parser_rb_code=engine.result(binding) File.open("#{mm.name.to_s.downcase}_visitor.rb",'w'){|f| f.puts parser_rb_code } end |
#hierarchical_attrs(klass) ⇒ Object
253 254 255 256 257 258 259 260 |
# File 'lib/ruby_generator.rb', line 253 def hierarchical_attrs klass inherited_attrs=[] if mother=find_mother_of(klass) inherited_attrs << mother.attrs inherited_attrs << hierarchical_attrs(mother) end return inherited_attrs.flatten end |
#is_basic_type(type) ⇒ Object
317 318 319 |
# File 'lib/ruby_generator.rb', line 317 def is_basic_type type type.name.to_s.upcase==type.name.to_s end |
#parsing_code_for(attr, ast_node_var) ⇒ Object
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 |
# File 'lib/ruby_generator.rb', line 274 def parsing_code_for attr,ast_node_var code=Code.new case type=attr.type when ArrayOf #note the position, before 'when Type' ! possible_starters=compute_possible_starters(type.type) code << "comments=parse_comments()" lookahead=possible_starters.first.to_s.end_with?("_lit") ? 0 : 1 starters=possible_starters.collect{|sym| ":#{sym}"}.join(',') code << "while showNext(#{lookahead}) && showNext(#{lookahead}).is_a?([#{starters}])" code.indent=2 code << "case showNext(#{lookahead}).kind" possible_starters.each do |starter| code << "when :#{starter}" code.indent=4 if starter.to_s.end_with?("_lit") #base types code << "#{ast_node_var}_.#{attr.name} << acceptIt" else code << "#{ast_node_var}_.#{attr.name} << parse#{starter.capitalize}()" end #code << "#{ast_node_var}_.#{attr.name} << parse#{starter.capitalize}()" code.indent=2 end code << "else " code << " abort \"ERROR when parsing attribute #{attr.name} : expecting one of [#{starters}].\n Got \#{showNext(1).kind}\"" code << "end" code << "if #{ast_node_var}_.#{attr.name}.last.respond_to? :comments_" code.indent=4 code << "#{ast_node_var}_.#{attr.name}.last.comments_=comments" code.indent=2 code << "end" code << "comments=parse_comments()" code.indent=0 code << "end" when Type if is_basic_type(type) #base types code << "#{ast_node_var}_.#{attr.name} = acceptIt" else code << "#{ast_node_var}_.#{attr.name} = parse#{type.name}()" end end return code end |