]> git.tdb.fi Git - libs/datafile.git/blobdiff - tool/compiler.cpp
Emit source file markers in compiled files
[libs/datafile.git] / tool / compiler.cpp
index 53c1b5388c4792829743b2db13092c005b125573..ed5710b97d3bd8d3ffac53d88f4c41855b2866fd 100644 (file)
@@ -15,7 +15,8 @@ using namespace std;
 using namespace Msp;
 
 Compiler::Compiler(DataFile::Writer &w):
-       writer(w)
+       writer(w),
+       reset_src(false)
 {
        add("file",     &Compiler::file);
        add("for_each", &Compiler::for_each);
@@ -36,6 +37,12 @@ void Compiler::for_each(const vector<string> &patterns)
 
 void Compiler::write(const DataFile::Statement &st)
 {
+       if(reset_src)
+       {
+               writer.write((DataFile::Statement("__src"), string()));
+               reset_src=false;
+       }
+
        for(list<DataFile::Statement>::const_iterator i=st.sub.begin(); i!=st.sub.end(); ++i)
                writer.write(*i);
 }
@@ -89,15 +96,23 @@ bool Compiler::process_statement(const FS::Path &fn, DataFile::Statement &st)
 
 void Compiler::process_file(const FS::Path &fn, const list<DataFile::Statement> &st)
 {
-       for(list<DataFile::Statement>::const_iterator i=st.begin(); i!=st.end(); ++i)
+       writer.write((DataFile::Statement("__src"), FS::basename(fn.str())));
+       reset_src=true;
+
+       if(st.empty())
+               process_file(fn);
+       else
        {
-               if(i->keyword=="_content")
-                       process_file(fn);
-               else
+               for(list<DataFile::Statement>::const_iterator i=st.begin(); i!=st.end(); ++i)
                {
-                       DataFile::Statement s=*i;
-                       process_statement(fn, s);
-                       writer.write(s);
+                       if(i->keyword=="_content")
+                               process_file(fn);
+                       else
+                       {
+                               DataFile::Statement s=*i;
+                               process_statement(fn, s);
+                               writer.write(s);
+                       }
                }
        }
 }
@@ -126,10 +141,7 @@ File::File(Compiler &c, const FS::Path &fn):
 
 void File::finish()
 {
-       if(write_st.empty())
-               compiler.process_file(filename);
-       else
-               compiler.process_file(filename, write_st);
+       compiler.process_file(filename, write_st);
 }
 
 void File::write(const DataFile::Statement &st)