From 4371289ed39d8135d407bc7bbbfbedea0cfd6dde Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sat, 13 Sep 2008 18:03:04 +0000 Subject: [PATCH] Emit source file markers in compiled files Use said markers in reporting errors --- source/parser.cpp | 12 ++++++++++-- source/parser.h | 1 + source/parsermode.h | 2 +- tool/compiler.cpp | 36 ++++++++++++++++++++++++------------ tool/compiler.h | 3 +++ 5 files changed, 39 insertions(+), 15 deletions(-) diff --git a/source/parser.cpp b/source/parser.cpp index 06e5a8c..ade1dd9 100644 --- a/source/parser.cpp +++ b/source/parser.cpp @@ -4,8 +4,7 @@ This file is part of libmspdatafile Copyright © 2006 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ -#include -#include +#include #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(); + if(s.empty()) + src=main_src; + else + src=format("%s[%s]", main_src, s); + } else return st; } diff --git a/source/parser.h b/source/parser.h index 814fd27..3a42273 100644 --- a/source/parser.h +++ b/source/parser.h @@ -27,6 +27,7 @@ class Parser { private: Input in; + std::string main_src; std::string src; bool good; ParserMode *mode; diff --git a/source/parsermode.h b/source/parsermode.h index ae40756..78257ce 100644 --- a/source/parsermode.h +++ b/source/parsermode.h @@ -22,7 +22,7 @@ class ParserMode { protected: Input ∈ - std::string src; + const std::string &src; ParserMode(Input &i, const std::string &s): in(i), src(s) { } public: diff --git a/tool/compiler.cpp b/tool/compiler.cpp index 53c1b53..ed5710b 100644 --- a/tool/compiler.cpp +++ b/tool/compiler.cpp @@ -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 &patterns) void Compiler::write(const DataFile::Statement &st) { + if(reset_src) + { + writer.write((DataFile::Statement("__src"), string())); + reset_src=false; + } + for(list::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 &st) { - for(list::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::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) diff --git a/tool/compiler.h b/tool/compiler.h index f77f04b..2b3fb91 100644 --- a/tool/compiler.h +++ b/tool/compiler.h @@ -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: -- 2.43.0