From: Christian Szegedy Date: 2001-12-06T23:13:01+09:00 Subject: [ruby-talk:27681] Controlling the error messages of eval. I am writing a small preprocessor for C++ in Ruby, I it for example to generate functions and declaration for accessing data members, or to define enum types which works like bitmasks. The idea is that I interpret all code segments between /*% and */ as ruby code and the ouput of this code is written into the output file. The main loop of the preprocessor is the following function: def file_fusion(ifile_name) f = File.open(ifile_name) raise "Can't open file #{fname} for reading." if !f f.read.scan(/(.*?)^([ \t]*)\/\*%(.*?)\*\//m) do |before,$prefix,to_eval| $ofile.print before to_eval = (to_eval.split("\n").map!{ |l| l.strip }).join("\n") begin eval to_eval rescue p $! puts "This error occured while processing :",to_eval end end f.close $ofile.print $' end It works quite fine as long as there are no error messages, but if the eval encounters some parse error, then there is no exception, but simply the progcess simply aborts. E.g., I started it on a file with a line /*% x + * y */ It gave: ../fusion.rb:36:in `file_fusion': (eval):1:in `file_fusion': compile error (SyntaxError) (eval):1: unterminated string meets end of file (eval):1: parse error x + % y ^ from ./fusion.rb:31:in `eval' from ./fusion.rb:36:in `file_fusion' from ./fusion.rb:31:in `scan' from ./fusion.rb:31:in `file_fusion' from rcpppp.rb:203 What I would like that 1) eval reports the error in the correct place of the input file. 2) Should not terminate after each parse error. ( Point 2) is not so important.) For point 1), I thought on counting the \n and change the eval line to: eval("\n"*count_newlines+to_eval) It would be close to that what I want but n this case, but eval would not report the name of the input file. Has someone a good idea, how to make this work perfect? Best regards, Christian