[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Wesnoth-cvs-commits] wesnoth/src game.cpp serialization/binary_or_te...
From: |
Guillaume Melquiond |
Subject: |
[Wesnoth-cvs-commits] wesnoth/src game.cpp serialization/binary_or_te... |
Date: |
Wed, 27 Apr 2005 18:17:37 -0400 |
CVSROOT: /cvsroot/wesnoth
Module name: wesnoth
Branch:
Changes by: Guillaume Melquiond <address@hidden> 05/04/27 22:17:37
Modified files:
src : game.cpp
src/serialization: binary_or_text.cpp parser.cpp parser.hpp
preprocessor.cpp preprocessor.hpp
tokenizer.cpp tokenizer.hpp
Log message:
Get rid of the static linesource vector, it was the last obstacle
before switching to an on-line preprocessor. Instead, directly embed the token
positions into the preprocessor output stream. Change the parser so that it can
take this information into account.
CVSWeb URLs:
http://savannah.gnu.org/cgi-bin/viewcvs/wesnoth/wesnoth/src/game.cpp.diff?tr1=1.239&tr2=1.240&r1=text&r2=text
http://savannah.gnu.org/cgi-bin/viewcvs/wesnoth/wesnoth/src/serialization/binary_or_text.cpp.diff?tr1=1.10&tr2=1.11&r1=text&r2=text
http://savannah.gnu.org/cgi-bin/viewcvs/wesnoth/wesnoth/src/serialization/parser.cpp.diff?tr1=1.15&tr2=1.16&r1=text&r2=text
http://savannah.gnu.org/cgi-bin/viewcvs/wesnoth/wesnoth/src/serialization/parser.hpp.diff?tr1=1.8&tr2=1.9&r1=text&r2=text
http://savannah.gnu.org/cgi-bin/viewcvs/wesnoth/wesnoth/src/serialization/preprocessor.cpp.diff?tr1=1.10&tr2=1.11&r1=text&r2=text
http://savannah.gnu.org/cgi-bin/viewcvs/wesnoth/wesnoth/src/serialization/preprocessor.hpp.diff?tr1=1.5&tr2=1.6&r1=text&r2=text
http://savannah.gnu.org/cgi-bin/viewcvs/wesnoth/wesnoth/src/serialization/tokenizer.cpp.diff?tr1=1.8&tr2=1.9&r1=text&r2=text
http://savannah.gnu.org/cgi-bin/viewcvs/wesnoth/wesnoth/src/serialization/tokenizer.hpp.diff?tr1=1.2&tr2=1.3&r1=text&r2=text
Patches:
Index: wesnoth/src/game.cpp
diff -u wesnoth/src/game.cpp:1.239 wesnoth/src/game.cpp:1.240
--- wesnoth/src/game.cpp:1.239 Wed Apr 27 21:11:44 2005
+++ wesnoth/src/game.cpp Wed Apr 27 22:17:37 2005
@@ -1,4 +1,4 @@
-/* $Id: game.cpp,v 1.239 2005/04/27 21:11:44 gruikya Exp $ */
+/* $Id: game.cpp,v 1.240 2005/04/27 22:17:37 silene Exp $ */
/*
Copyright (C) 2003 by David White <address@hidden>
Part of the Battle for Wesnoth Project http://wesnoth.whitevine.net
@@ -1236,7 +1236,7 @@
scoped_istream stream =
preprocess_file("data/game.cfg", &defines);
std::string error_log;
- read(cfg, *stream, NULL, &error_log);
+ read(cfg, *stream, &error_log);
if(!error_log.empty()) {
gui::show_error_message(disp(),
_("Warning: Errors
occurred while loading game configuration files: '") +
Index: wesnoth/src/serialization/binary_or_text.cpp
diff -u wesnoth/src/serialization/binary_or_text.cpp:1.10
wesnoth/src/serialization/binary_or_text.cpp:1.11
--- wesnoth/src/serialization/binary_or_text.cpp:1.10 Mon Apr 18 20:51:22 2005
+++ wesnoth/src/serialization/binary_or_text.cpp Wed Apr 27 22:17:37 2005
@@ -1,4 +1,4 @@
-/* $Id: binary_or_text.cpp,v 1.10 2005/04/18 20:51:22 gruikya Exp $ */
+/* $Id: binary_or_text.cpp,v 1.11 2005/04/27 22:17:37 silene Exp $ */
/*
Copyright (C) 2003 by David White <address@hidden>
Copyright (C) 2005 by Guillaume Melquiond <address@hidden>
@@ -26,7 +26,7 @@
read_compressed(cfg, in);
return true;
} else {
- read(cfg, in, NULL, error_log);
+ read(cfg, in, error_log);
return false;
}
}
Index: wesnoth/src/serialization/parser.cpp
diff -u wesnoth/src/serialization/parser.cpp:1.15
wesnoth/src/serialization/parser.cpp:1.16
--- wesnoth/src/serialization/parser.cpp:1.15 Mon Apr 18 20:51:22 2005
+++ wesnoth/src/serialization/parser.cpp Wed Apr 27 22:17:37 2005
@@ -1,4 +1,4 @@
-/* $Id: parser.cpp,v 1.15 2005/04/18 20:51:22 gruikya Exp $ */
+/* $Id: parser.cpp,v 1.16 2005/04/27 22:17:37 silene Exp $ */
/*
Copyright (C) 2003 by David White <address@hidden>
Copyright (C) 2005 by Guillaume Melquiond <address@hidden>
@@ -38,42 +38,28 @@
static const size_t max_recursion_levels = 100;
-line_source get_line_source(std::vector< line_source > const &line_src, int
line)
-{
- line_source res(line, "", 0);
- std::vector< line_source >::const_iterator it =
- std::upper_bound(line_src.begin(), line_src.end(), res);
- if (it != line_src.begin()) {
- --it;
- res.file = it->file;
- res.fileline = it->fileline + (line - it->linenum);
- }
-
- return res;
-}
-
namespace {
class parser
{
public:
- parser(config& cfg, std::istream& in, std::vector<line_source> const*
line_sources);
+ parser(config& cfg, std::istream& in);
void operator() (std::string* error_log=NULL);
private:
void parse_element();
void parse_variable();
void parse_directive();
- std::string lineno_string(utils::string_map& map, size_t lineno,
- const std::string& string1, const std::string& string2);
+ std::string lineno_string(utils::string_map &map, std::string const
&lineno,
+ std::string const &error_string);
void error(const std::string& message);
config& cfg_;
tokenizer tok_;
- std::vector<line_source> const* line_sources;
struct element {
- element(config* cfg, const std::string& name, size_t
start_line, const std::string& textdomain) :
+ element(config *cfg, std::string const &name, std::string const
&start_line,
+ std::string const &textdomain) :
cfg(cfg), name(name), textdomain(textdomain),
start_line(start_line){};
config* cfg;
@@ -81,7 +67,7 @@
std::map<std::string, config*> last_element_map;
std::string textdomain;
- size_t start_line;
+ std::string start_line;
};
std::stack<element> elements;
@@ -89,10 +75,9 @@
std::string current_textdomain_location;
};
-parser::parser(config &cfg, std::istream &in, std::vector<line_source> const
*line_sources) :
+parser::parser(config &cfg, std::istream &in) :
cfg_(cfg),
tok_(in),
- line_sources(line_sources),
current_textdomain_location("")
{
}
@@ -100,7 +85,7 @@
void parser::operator()(std::string* error_log)
{
cfg_.clear();
- elements.push(element(&cfg_, "", 0, PACKAGE));
+ elements.push(element(&cfg_, "", "", PACKAGE));
tok_.textdomain() = PACKAGE;
do {
@@ -143,8 +128,7 @@
utils::string_map i18n_symbols;
i18n_symbols["tag"] = elements.top().name;
error(lineno_string(i18n_symbols, elements.top().start_line,
- N_("Missing closing tag for tag $tag (file
$file, line $line)"),
- N_("Missing closing tag for tag $tag (line
$line)")));
+ N_("Missing closing tag for tag $tag at
$pos")));
}
}
@@ -200,8 +184,7 @@
utils::string_map i18n_symbols;
i18n_symbols["tag"] = elements.top().name;
error(lineno_string(i18n_symbols,
elements.top().start_line,
- N_("Found invalid closing tag for tag
$tag (file $file, line $line)"),
- N_("Found invalid closing tag for tag
$tag (line $line)")));
+ N_("Found invalid closing tag for tag
$tag at $pos")));
}
elements.pop();
@@ -303,25 +286,23 @@
}
}
-std::string parser::lineno_string(utils::string_map& i18n_symbols, size_t
lineno,
- const std::string& string1, const std::string& string2)
+std::string parser::lineno_string(utils::string_map &i18n_symbols, std::string
const &lineno,
+ std::string const &error_string)
{
+ std::vector< std::string > pos = utils::split(lineno, ' ');
+ std::vector< std::string >::const_iterator i = pos.begin(), end =
pos.end();
+ std::string included_from = _(" included from ");
std::string res;
-
- if(line_sources != NULL) {
- const line_source src = get_line_source(*line_sources, lineno);
- i18n_symbols["file"] = lexical_cast<std::string>(src.file);
- i18n_symbols["line"] = lexical_cast<std::string>(src.fileline);
- i18n_symbols["column"] =
lexical_cast<std::string>(tok_.get_column());
-
- res = vgettext(string1.c_str(), i18n_symbols);
- } else {
- i18n_symbols["line"] = lexical_cast<std::string>(lineno);
- i18n_symbols["column"] =
lexical_cast<std::string>(tok_.get_column());
-
- res = vgettext(string2.c_str(), i18n_symbols);
+ while (i != end) {
+ std::string const &line = *(i++);
+ std::string const &file = i != end ? *(i++) : "<unknown>";
+ if (!res.empty())
+ res += included_from;
+ res += file + ':' + line;
}
- return res;
+ if (res.empty()) res = "???";
+ i18n_symbols["pos"] = res;
+ return vgettext(error_string.c_str(), i18n_symbols);
}
void parser::error(const std::string& error_type)
@@ -329,17 +310,16 @@
utils::string_map i18n_symbols;
i18n_symbols["error"] = error_type;
- throw config::error(lineno_string(i18n_symbols, tok_.get_line(),
- N_("$error in file $file (line $line, column
$column)"),
- N_("$error (line $line, column $column)")));
+ throw config::error(
+ lineno_string(i18n_symbols, tok_.get_line(),
+ N_("$error at $pos")));
}
} // end anon namespace
-void read(config &cfg, std::istream &data_in, std::vector< line_source > const
*line_sources,
- std::string* error_log)
+void read(config &cfg, std::istream &data_in, std::string* error_log)
{
- parser(cfg, data_in, line_sources)(error_log);
+ parser(cfg, data_in)(error_log);
}
static char const *AttributeEquals = "=";
Index: wesnoth/src/serialization/parser.hpp
diff -u wesnoth/src/serialization/parser.hpp:1.8
wesnoth/src/serialization/parser.hpp:1.9
--- wesnoth/src/serialization/parser.hpp:1.8 Mon Apr 18 20:51:22 2005
+++ wesnoth/src/serialization/parser.hpp Wed Apr 27 22:17:37 2005
@@ -1,4 +1,4 @@
-/* $Id: parser.hpp,v 1.8 2005/04/18 20:51:22 gruikya Exp $ */
+/* $Id: parser.hpp,v 1.9 2005/04/27 22:17:37 silene Exp $ */
/*
Copyright (C) 2003 by David White <address@hidden>
Copyright (C) 2005 by Guillaume Melquiond <address@hidden>
@@ -18,13 +18,9 @@
#include <vector>
class config;
-struct line_source;
-
-line_source get_line_source(std::vector< line_source > const &line_src, int
line);
//read data in, clobbering existing data.
-void read(config &cfg, std::istream &in, std::vector< line_source > const
*lines = 0,
- std::string* error_log=NULL); //throws config::error
+void read(config &cfg, std::istream &in, std::string* error_log = NULL);
//throws config::error
void write(std::ostream &out, config const &cfg);
Index: wesnoth/src/serialization/preprocessor.cpp
diff -u wesnoth/src/serialization/preprocessor.cpp:1.10
wesnoth/src/serialization/preprocessor.cpp:1.11
--- wesnoth/src/serialization/preprocessor.cpp:1.10 Mon Apr 18 20:51:22 2005
+++ wesnoth/src/serialization/preprocessor.cpp Wed Apr 27 22:17:37 2005
@@ -1,4 +1,4 @@
-/* $Id: preprocessor.cpp,v 1.10 2005/04/18 20:51:22 gruikya Exp $ */
+/* $Id: preprocessor.cpp,v 1.11 2005/04/27 22:17:37 silene Exp $ */
/*
Copyright (C) 2003 by David White <address@hidden>
Copyright (C) 2005 by Guillaume Melquiond <address@hidden>
@@ -86,13 +86,15 @@
static void internal_preprocess_file(const std::string& fname,
preproc_map& defines_map,
int depth, std::ostream &out,
- std::vector<line_source>* lines_src, int&
line);
+ std::string const &included_from,
+ bool previous_was_newline);
static void internal_preprocess_data(std::istream &data_in,
preproc_map& defines_map,
int depth, std::ostream &out,
- std::vector<line_source>* lines_src, int&
line,
- const std::string& fname, int srcline)
+ std::string const &included_from,
+ bool need_linenum,
+ std::string const ¤t_directory)
{
std::string data_str;
{
@@ -104,6 +106,8 @@
std::string const &data = data_str;
bool in_quotes = false;
+ int current_line = 1;
+ bool previous_was_newline = !need_linenum;
for(std::string::const_iterator i = data.begin(); i != data.end(); ++i)
{
const char c = *i;
@@ -170,8 +174,15 @@
}
}
+ std::ostringstream from;
+ if (!in_quotes && !included_from.empty()) {
+ from << " {" << symbol << "} " <<
current_line << included_from;
+ if (previous_was_newline)
+ out << "#line 0" << from.str();
+ }
std::istringstream stream(str);
- internal_preprocess_data(stream, defines_map,
depth, out, NULL, line, fname, srcline);
+ internal_preprocess_data(stream, defines_map,
depth, out,
+ from.str(),
!previous_was_newline, "");
} else if(depth < 20) {
std::string prefix;
std::string nfname;
@@ -209,7 +220,7 @@
//being preprocessed
nfname = newfilename;
nfname.erase(nfname.begin(),nfname.begin()+2);
- nfname = directory_name(fname)
+ nfname;
+ nfname = current_directory +
nfname;
} else {
#ifdef USE_ZIPIOS
@@ -223,19 +234,19 @@
nfname = "data/" +
newfilename;
}
- internal_preprocess_file(nfname,
- defines_map,
depth+1, out,
-
lines_src,line);
+ std::ostringstream from;
+ if (!in_quotes &&
!included_from.empty())
+ from << ' ' << current_line <<
included_from;
+ internal_preprocess_file(nfname,
defines_map, depth + 1, out,
+ from.str(),
previous_was_newline);
}
} else {
- const std::string& str = read_file(newfilename);
- out.write(&*str.begin(), str.length());
- line += std::count(str.begin(),str.end(),'\n');
+ scoped_istream stream =
istream_file(newfilename);
+ out << stream->rdbuf();
}
- if(lines_src != NULL) {
-
lines_src->push_back(line_source(line,fname,srcline));
- }
+ previous_was_newline = false;
+ need_linenum = true;
} else if(c == '#' && !in_quotes) {
//we are about to skip some things, so keep track of
//the start of where we're skipping, so we can count
@@ -273,7 +284,8 @@
}
if(i > data.end() - hash_enddef.size()) {
- throw config::error("pre-processing
condition unterminated in '" + fname + "': '" + items + "'");
+ throw config::error("pre-processing
condition unterminated " +
+ included_from + ":
'" + items + "'");
}
i += hash_enddef.size();
@@ -356,17 +368,25 @@
if(i == data.end())
break;
- srcline += std::count(begin,i,'\n');
- ++line;
-
out.put('\n');
+ current_line += std::count(begin, i, '\n');
+ need_linenum = true;
+ goto linenum_output;
} else {
- if(c == '\n') {
- ++line;
- ++srcline;
+ if (c == '\n') {
+ linenum_output:
+ if (need_linenum && !in_quotes &&
!included_from.empty()) {
+ out << "#line " << current_line <<
included_from;
+ need_linenum = false;
+ } else
+ out.put('\n');
+ ++current_line;
+ previous_was_newline = true;
+ } else {
+ out.put(c);
+ if ((unsigned)c > 32)
+ previous_was_newline = false;
}
-
- out.put(c);
}
}
}
@@ -374,7 +394,8 @@
static void internal_preprocess_file(const std::string& fname,
preproc_map& defines_map,
int depth, std::ostream &out,
- std::vector<line_source>* lines_src, int&
line)
+ std::string const &included_from,
+ bool previous_was_newline)
{
//if it's a directory, we process all files in the directory
//that end in .cfg
@@ -386,32 +407,33 @@
for(std::vector<std::string>::const_iterator f = files.begin();
f != files.end(); ++f) {
if(is_directory(*f) || f->size() > 4 &&
std::equal(f->end()-4,f->end(),".cfg")) {
- internal_preprocess_file(*f, defines_map,
depth, out, lines_src, line);
+ internal_preprocess_file(*f, defines_map,
depth, out, included_from, true);
}
}
return;
}
- if(lines_src != NULL) {
- lines_src->push_back(line_source(line,fname,1));
+ std::string from;
+ if (!included_from.empty()) {
+ from = ' ' + fname + included_from;
+ if (previous_was_newline)
+ out << "#line 0" << from;
}
-
scoped_istream stream = istream_file(fname);
- internal_preprocess_data(*stream, defines_map, depth, out, lines_src,
line, fname, 1);
+ internal_preprocess_data(*stream, defines_map, depth, out, from,
!previous_was_newline,
+ directory_name(fname));
}
std::istream *preprocess_file(std::string const &fname,
- const preproc_map* defines,
- std::vector<line_source>* line_sources)
+ preproc_map const *defines)
{
log_scope("preprocessing file...");
preproc_map defines_copy;
if(defines != NULL)
defines_copy = *defines;
- int linenum = 0;
std::stringstream *stream = new std::stringstream;
- internal_preprocess_file(fname, defines_copy, 0, *stream, line_sources,
linenum);
+ internal_preprocess_file(fname, defines_copy, 0, *stream, "\n", true);
return stream;
}
Index: wesnoth/src/serialization/preprocessor.hpp
diff -u wesnoth/src/serialization/preprocessor.hpp:1.5
wesnoth/src/serialization/preprocessor.hpp:1.6
--- wesnoth/src/serialization/preprocessor.hpp:1.5 Mon Apr 18 20:51:22 2005
+++ wesnoth/src/serialization/preprocessor.hpp Wed Apr 27 22:17:37 2005
@@ -1,4 +1,4 @@
-/* $Id: preprocessor.hpp,v 1.5 2005/04/18 20:51:22 gruikya Exp $ */
+/* $Id: preprocessor.hpp,v 1.6 2005/04/27 22:17:37 silene Exp $ */
/*
Copyright (C) 2003 by David White <address@hidden>
Copyright (C) 2005 by Guillaume Melquiond <address@hidden>
@@ -19,19 +19,6 @@
#include <string>
#include <vector>
-//an object which defines the location an error occurred at when
-//parsing WML files
-struct line_source
-{
- line_source(int ln, std::string const &fname, int line)
- : linenum(ln), file(fname), fileline(line) {}
-
- int linenum;
- std::string file;
- int fileline;
- bool operator<(line_source const &v) const { return linenum <
v.linenum; }
-};
-
struct preproc_define
{
preproc_define() {}
@@ -47,10 +34,8 @@
typedef std::map< std::string, preproc_define > preproc_map;
//function to use the WML preprocessor on a file, and returns the resulting
-//preprocessed file data. defines is a map of symbols defined. src is used
-//internally and should be set to NULL
+//preprocessed file data. defines is a map of symbols defined.
std::istream *preprocess_file(std::string const &fname,
- preproc_map const *defines = NULL,
- std::vector< line_source > *src = NULL);
+ preproc_map const *defines = NULL);
#endif
Index: wesnoth/src/serialization/tokenizer.cpp
diff -u wesnoth/src/serialization/tokenizer.cpp:1.8
wesnoth/src/serialization/tokenizer.cpp:1.9
--- wesnoth/src/serialization/tokenizer.cpp:1.8 Sun Apr 24 19:15:39 2005
+++ wesnoth/src/serialization/tokenizer.cpp Wed Apr 27 22:17:37 2005
@@ -1,4 +1,4 @@
-/* $Id: tokenizer.cpp,v 1.8 2005/04/24 19:15:39 isaaccp Exp $ */
+/* $Id: tokenizer.cpp,v 1.9 2005/04/27 22:17:37 silene Exp $ */
/*
Copyright (C) 2004 by Philippe Plantier <address@hidden>
Part of the Battle for Wesnoth Project http://www.wesnoth.org
@@ -21,8 +21,7 @@
tokenizer::tokenizer(std::istream& in) :
in_(in),
- lineno_(0),
- colno_(0)
+ lineno_(1)
{
if(in_.good()) {
current_ = in_.get();
@@ -52,20 +51,16 @@
// Identifies and processes tokenizer directives
std::vector<std::string> comment_line = utils::split(comment, '
');
- if ((comment_line.size() == 2 || comment_line.size() == 3)
- && comment_line[0] == "#textdomain") {
+ if (comment_line.size() == 2 && comment_line[0] ==
"#textdomain")
textdomain_ = comment_line[1];
-#if 0
- std::string path;
- if (comment_line.size() == 3)
- path = comment_line[2];
- textdomain_init(textdomain_, path);
-#endif
+ else if (comment_line.size() > 3 && comment_line[0] == "#line")
{
+ lineno_ = atoi(comment_line[1].c_str());
+ comment_line.erase(comment_line.begin(),
comment_line.begin() + 2);
+ file_ = ' ' + utils::join(comment_line, ' ');
}
}
tokenstart_lineno_ = lineno_;
- tokenstart_colno_ = colno_;
switch(current_) {
case EOF:
@@ -121,12 +116,8 @@
void tokenizer::next_char()
{
- if(current_ == '\n') {
- colno_ = 0;
+ if (current_ == '\n')
lineno_++;
- } else {
- colno_++;
- }
do {
if(in_.good()) {
@@ -152,18 +143,14 @@
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' &&
c <= '9') || c == '_';
}
-const size_t tokenizer::get_line()
-{
- return tokenstart_lineno_ + 1;
-}
-
-const size_t tokenizer::get_column()
+std::string tokenizer::get_line()
{
- return tokenstart_colno_ + 1;
+ std::ostringstream s;
+ s << tokenstart_lineno_ << file_;
+ return s.str();
}
std::string& tokenizer::textdomain()
{
return textdomain_;
}
-
Index: wesnoth/src/serialization/tokenizer.hpp
diff -u wesnoth/src/serialization/tokenizer.hpp:1.2
wesnoth/src/serialization/tokenizer.hpp:1.3
--- wesnoth/src/serialization/tokenizer.hpp:1.2 Tue Mar 29 20:19:33 2005
+++ wesnoth/src/serialization/tokenizer.hpp Wed Apr 27 22:17:37 2005
@@ -1,4 +1,4 @@
-/* $Id: tokenizer.hpp,v 1.2 2005/03/29 20:19:33 gruikya Exp $ */
+/* $Id: tokenizer.hpp,v 1.3 2005/04/27 22:17:37 silene Exp $ */
/*
Copyright (C) 2004 by Philippe Plantier <address@hidden>
Part of the Battle for Wesnoth Project http://www.wesnoth.org
@@ -49,8 +49,7 @@
const token& next_token();
const token& current_token();
- const size_t get_line();
- const size_t get_column();
+ std::string get_line();
std::string& textdomain();
private:
void next_char();
@@ -58,16 +57,14 @@
int peek_char();
bool is_space(int c);
bool is_alnum(int c);
-// void textdomain_init(const std::string& domain, const std::string&
path);
std::istream& in_;
int current_;
std::string textdomain_;
+ std::string file_;
size_t tokenstart_lineno_;
- size_t tokenstart_colno_;
size_t lineno_;
- size_t colno_;
token token_;
};