]> git.tdb.fi Git - libs/datafile.git/commitdiff
Emit source file markers in compiled files
authorMikko Rasa <tdb@tdb.fi>
Sat, 13 Sep 2008 18:03:04 +0000 (18:03 +0000)
committerMikko Rasa <tdb@tdb.fi>
Sat, 13 Sep 2008 18:03:04 +0000 (18:03 +0000)
Use said markers in reporting errors

source/parser.cpp
source/parser.h
source/parsermode.h
tool/compiler.cpp
tool/compiler.h

index 06e5a8c33745e78e3855a32f4148e939e93e390f..ade1dd98e40904412e842a91666b6670e6a124aa 100644 (file)
@@ -4,8 +4,7 @@ This file is part of libmspdatafile
 Copyright © 2006  Mikko Rasa, Mikkosoft Productions
 Distributed under the LGPL
 */
-#include <cctype>
-#include <sstream>
+#include <msp/strings/formatter.h>
 #include "binaryparser.h"
 #include "parser.h"
 #include "statement.h"
@@ -18,6 +17,7 @@ namespace DataFile {
 
 Parser::Parser(IO::Base &i, const string &s):
        in(i),
+       main_src(s),
        src(s),
        good(true),
        mode(new TextParser(in, src))
@@ -48,6 +48,14 @@ Statement Parser::parse()
                                delete mode;
                                mode=new TextParser(in, src);
                        }
+                       else if(st.keyword=="__src")
+                       {
+                               string s=st.args[0].get<string>();
+                               if(s.empty())
+                                       src=main_src;
+                               else
+                                       src=format("%s[%s]", main_src, s);
+                       }
                        else
                                return st;
                }
index 814fd2771b1240ccb14a86c4c8d5576f26c99d4b..3a42273eb783e98b74e13fb16e9f213917650c43 100644 (file)
@@ -27,6 +27,7 @@ class Parser
 {
 private:
        Input       in;
+       std::string main_src;
        std::string src;
        bool        good;
        ParserMode  *mode;
index ae407566a9d0442a96a699cbfde1b1387c1add91..78257ced8427e8e9b6cf5929121c30b517a2f894 100644 (file)
@@ -22,7 +22,7 @@ class ParserMode
 {
 protected:
        Input &in;
-       std::string src;
+       const std::string &src;
 
        ParserMode(Input &i, const std::string &s): in(i), src(s) { }
 public:
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)
index f77f04bdab2569ded7a82ba9520ac46e861f195d..2b3fb91ee7681044e5963e153b5fba1698009d7f 100644 (file)
@@ -20,6 +20,7 @@ class Compiler: public Msp::DataFile::Loader
 
 private:
        Msp::DataFile::Writer &writer;
+       bool reset_src;
 
 public:
        Compiler(Msp::DataFile::Writer &);
@@ -33,6 +34,7 @@ private:
        void process_file(const Msp::FS::Path &);
 };
 
+
 class File: public Msp::DataFile::Loader
 {
 private:
@@ -48,6 +50,7 @@ private:
        void write(const Msp::DataFile::Statement &);
 };
 
+
 class ForEach: public Msp::DataFile::Loader
 {
 private: