summaryrefslogtreecommitdiff
path: root/i/marvin/src/config
diff options
context:
space:
mode:
Diffstat (limited to 'i/marvin/src/config')
-rw-r--r--i/marvin/src/config/Makefile.defs15
-rw-r--r--i/marvin/src/config/config_data.cc17
-rw-r--r--i/marvin/src/config/config_data.hh9
-rw-r--r--i/marvin/src/config/config_data.tcc11
-rw-r--r--i/marvin/src/config/config_parser.cc142
-rw-r--r--i/marvin/src/config/config_parser.hh77
-rw-r--r--i/marvin/src/config/lexer.ll118
-rw-r--r--i/marvin/src/config/parser.yy173
-rw-r--r--i/marvin/src/config/test_config.cc (renamed from i/marvin/src/config/test_config_data.cc)9
9 files changed, 23 insertions, 548 deletions
diff --git a/i/marvin/src/config/Makefile.defs b/i/marvin/src/config/Makefile.defs
index 823c9ea..a06f2a7 100644
--- a/i/marvin/src/config/Makefile.defs
+++ b/i/marvin/src/config/Makefile.defs
@@ -1,14 +1,5 @@
-PROGRAMS += test_config_data
+PROGRAMS += test_config
-config_OBJECTS = lexer.o parser.o config_data.o config_parser.o config.o
+config_OBJECTS = config_data.o config.o $(parser_OBJECTS)
-test_config_data_OBJECTS = $(config_OBJECTS) test_config_data.o
-
-EXTRA_CLEAN += parser.hh parser.cc lexer.hh lexer.cc
-
-parser.hh: parser.cc
-$(OBJ_DIR)/lexer.o: parser.hh
-
-lexer.hh: lexer.cc
-$(OBJ_DIR)/parser.o: lexer.hh
-$(OBJ_DIR)/config_parser.o: lexer.hh
+test_config_OBJECTS = $(config_OBJECTS) test_config.o
diff --git a/i/marvin/src/config/config_data.cc b/i/marvin/src/config/config_data.cc
index 2642e74..b126082 100644
--- a/i/marvin/src/config/config_data.cc
+++ b/i/marvin/src/config/config_data.cc
@@ -24,7 +24,7 @@
// }}}
#include "config.hh"
#include "config_data.hh"
-#include "config_parser.hh"
+#include "parser/parser.hh"
#include <stdexcept>
@@ -75,6 +75,21 @@ ConfigData::add (const std::string &id, any &val)
a.swap (val);
}
+/// Classe dérivé de Parser pour le parsing de conf.
+class ConfigParser : public Parser
+{
+ ConfigData &data_;
+ public:
+ /// Constructeur.
+ ConfigParser (ConfigData &data) : data_ (data) { }
+ /// Fonction appelée lors d'une affectation. VAL peut être modifié, il est
+ /// détruit suite à l'appel.
+ virtual void assign (const std::string &id, any &val)
+ {
+ data_.add (id, val);
+ }
+};
+
/// Initialise (lit la ligne de commande et les fichiers de config.
void
ConfigData::init (int &argc, char **&argv, const std::string &file)
diff --git a/i/marvin/src/config/config_data.hh b/i/marvin/src/config/config_data.hh
index 8c8bf1f..51caabd 100644
--- a/i/marvin/src/config/config_data.hh
+++ b/i/marvin/src/config/config_data.hh
@@ -58,18 +58,9 @@ class ConfigData
/// Ajoute une valeur de configuration. VAL prend l'ancienne valeur ou un
/// any vide.
void add (const std::string &id, any &val);
- /// Ajoute une valeur de configuration, fonction générique. Attention,
- /// cette fonction est plus couteuse car elle fait une copie de
- /// la valeur.
- template<typename T>
- void add (const std::string &id, const T &val);
private:
/// Initialise (lit la ligne de commande et les fichiers de config.
void init (int &argc, char **&argv, const std::string &file);
- /// Lance le parseur sur un fichier.
- void parseFile (const std::string &file);
- /// Lance le parseur sur une chaîne.
- void parseString (const std::string &s);
};
#include "config_data.tcc"
diff --git a/i/marvin/src/config/config_data.tcc b/i/marvin/src/config/config_data.tcc
index 7e5b905..71fa6d6 100644
--- a/i/marvin/src/config/config_data.tcc
+++ b/i/marvin/src/config/config_data.tcc
@@ -57,14 +57,3 @@ ConfigData::get (const std::string &id, const T &defaut) const
return *v;
}
-/// Ajoute une valeur de configuration, fonction générique. Attention,
-/// cette fonction est plus couteuse car elle fait une copie de
-/// la valeur.
-template<typename T>
-void
-ConfigData::add (const std::string &id, const T &val)
-{
- any a (val);
- add (id, a);
-}
-
diff --git a/i/marvin/src/config/config_parser.cc b/i/marvin/src/config/config_parser.cc
deleted file mode 100644
index ee0a6e3..0000000
--- a/i/marvin/src/config/config_parser.cc
+++ /dev/null
@@ -1,142 +0,0 @@
-// config_parser.cc
-// marvin - programme du robot 2006. {{{
-//
-// Copyright (C) 2006 Nicolas Schodet
-//
-// Robot APB Team/Efrei 2006.
-// Web: http://assos.efrei.fr/robot/
-// Email: robot AT efrei DOT fr
-//
-// This program is free software; you can redistribute it and/or modify
-// it under the terms of the GNU General Public License as published by
-// the Free Software Foundation; either version 2 of the License, or
-// (at your option) any later version.
-//
-// This program is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-// GNU General Public License for more details.
-//
-// You should have received a copy of the GNU General Public License
-// along with this program; if not, write to the Free Software
-// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-//
-// }}}
-#include "config_parser.hh"
-#include "config.hh"
-
-#include "parser.hh"
-#include "lexer.hh"
-
-#include <stdexcept>
-#include <sstream>
-
-int yyparse (void *);
-
-/// Constructeur.
-ConfigParser::ConfigParser (ConfigData &configData)
- : configData_ (configData)
-{
-}
-
-/// Lance le parseur sur un fichier.
-void
-ConfigParser::parseFile (const std::string &file)
-{
- FILE *f;
- f = fopen (file.c_str (), "r");
- if (!f)
- throw std::runtime_error ("can not open file \"" + file + "\"");
- try
- {
- // Crée un scanner, initialise son tampon d'entré, puis parse.
- yyscan_t scanner;
- YY_BUFFER_STATE buf;
- Extra e (configData_, *this, true);
- yylex_init (&scanner);
- yyset_extra (&e, scanner);
- buf = yy_create_buffer (f, YY_READ_BUF_SIZE, scanner);
- yy_switch_to_buffer (buf, scanner);
- int ret = yyparse (scanner);
- yy_delete_buffer (buf, scanner);
- yylex_destroy (scanner);
- if (ret)
- {
- if (e.errorValid_)
- throw std::runtime_error (e.error_);
- else
- throw std::runtime_error ("parse error");
- }
- }
- catch (const std::runtime_error &e)
- {
- fclose (f);
- throw std::runtime_error ("in file \"" + file + "\": " + e.what ());
- }
- fclose (f);
-}
-
-/// Lance le parseur sur une chaîne.
-void
-ConfigParser::parseString (const std::string &s)
-{
- try
- {
- // Crée un scanner, initialise son tampon d'entré, puis parse.
- yyscan_t scanner;
- YY_BUFFER_STATE buf;
- Extra e (configData_, *this, false);
- yylex_init (&scanner);
- yyset_extra (&e, scanner);
- buf = yy_scan_bytes (s.data (), s.size (), scanner);
- int ret = yyparse (scanner);
- yy_delete_buffer (buf, scanner);
- yylex_destroy (scanner);
- if (ret)
- {
- if (e.errorValid_)
- throw std::runtime_error (e.error_);
- else
- throw std::runtime_error ("parse error");
- }
- }
- catch (const std::runtime_error &e)
- {
- throw std::runtime_error ("in config string \"" + s + "\": "
- + e.what ());
- }
-}
-
-/// Constructeur pour initialiser les références.
-ConfigParser::Extra::Extra (ConfigData &configData_, ConfigParser
- &configParser_, bool useLine)
- : configData (configData_), configParser (configParser_),
- line (1), errorValid_ (false), useLine_ (useLine)
-{
-}
-
-/// Recueille l'erreur de bison.
-void
-ConfigParser::Extra::error (const std::string &e)
-{
- if (!errorValid_)
- {
- error_ = e;
- errorValid_ = true;
- if (useLine_)
- {
- std::stringstream os;
- os << line;
- error_ += ", line ";
- error_ += os.str ();
- }
- }
-}
-
-/* Shut up warning for this wrongly declared static function. */
-static int
-yy_init_globals (yyscan_t yyscanner)
-{
- return yy_init_globals (yyscanner);
-}
-
diff --git a/i/marvin/src/config/config_parser.hh b/i/marvin/src/config/config_parser.hh
deleted file mode 100644
index abbf214..0000000
--- a/i/marvin/src/config/config_parser.hh
+++ /dev/null
@@ -1,77 +0,0 @@
-#ifndef config_parser_hh
-#define config_parser_hh
-// config_parser.hh
-// marvin - programme du robot 2006. {{{
-//
-// Copyright (C) 2006 Nicolas Schodet
-//
-// Robot APB Team/Efrei 2006.
-// Web: http://assos.efrei.fr/robot/
-// Email: robot AT efrei DOT fr
-//
-// This program is free software; you can redistribute it and/or modify
-// it under the terms of the GNU General Public License as published by
-// the Free Software Foundation; either version 2 of the License, or
-// (at your option) any later version.
-//
-// This program is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-// GNU General Public License for more details.
-//
-// You should have received a copy of the GNU General Public License
-// along with this program; if not, write to the Free Software
-// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-//
-// }}}
-
-#include <string>
-
-class ConfigData;
-
-/// Structure d'information passée au parser.
-class ConfigParser
-{
- public:
- struct Extra
- {
- public:
- /// Chaîne temporaire utilisée pendant l'analyse lexicale.
- std::string tmp;
- /// Référence vers le ConfigData qui doit recevoir la configuration.
- ConfigData &configData;
- /// Référence vers l'instance de ConfigParser.
- ConfigParser &configParser;
- /// Ligne courante.
- int line;
- private:
- /// Permet de stocker l'erreur de bison.
- std::string error_;
- /// Indique si error est significatif.
- bool errorValid_;
- /// Utilise ou non le comptage de lignes.
- bool useLine_;
- public:
- /// Constructeur pour initialiser les références.
- Extra (ConfigData &configData_, ConfigParser &configParser_,
- bool useLine);
- /// Recueille l'erreur de bison.
- void error (const std::string &e);
- /// Accés réservé pour ConfigParser.
- friend class ConfigParser;
- };
- private:
- /// Référence vers le ConfigData qui doit recevoir la configuration.
- ConfigData &configData_;
- public:
- /// Constructeur.
- ConfigParser (ConfigData &configData);
- /// Lance le parseur sur un fichier.
- void parseFile (const std::string &file);
- /// Lance le parseur sur une chaîne.
- void parseString (const std::string &s);
-};
-
-#define YY_EXTRA_TYPE ConfigParser::Extra *
-
-#endif // config_parser_hh
diff --git a/i/marvin/src/config/lexer.ll b/i/marvin/src/config/lexer.ll
deleted file mode 100644
index 34522dc..0000000
--- a/i/marvin/src/config/lexer.ll
+++ /dev/null
@@ -1,118 +0,0 @@
-%{
-// marvin - programme du robot 2006. {{{
-//
-// Copyright (C) 2006 Nicolas Schodet
-//
-// Robot APB Team/Efrei 2006.
-// Web: http://assos.efrei.fr/robot/
-// Email: robot AT efrei DOT fr
-//
-// This program is free software; you can redistribute it and/or modify
-// it under the terms of the GNU General Public License as published by
-// the Free Software Foundation; either version 2 of the License, or
-// (at your option) any later version.
-//
-// This program is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-// GNU General Public License for more details.
-//
-// You should have received a copy of the GNU General Public License
-// along with this program; if not, write to the Free Software
-// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-//
-// }}}
-#include "config/config.hh"
-#include "config/config_parser.hh"
-#include "utils/any.hh"
-#include "parser.hh"
-%}
-
-%option reentrant
-%option header-file="lexer.hh"
-%option outfile="lexer.cc"
-%option bison-bridge
-%option noyywrap nodefault nounput
-
-%x strst
-
-INTDEC [+-]?[0-9]+
-INTHEX "0x"[0-9a-fA-F]+
-INTNUM {INTDEC}|{INTHEX}
-FLOAT1 [+-]?\.[0-9]+
-FLOAT2 [+-]?[0-9]+\.[0-9]*
-FLOATNUM {FLOAT1}|{FLOAT2}
-
-%%
-
-"true"|"on" {
- yylval->b = true;
- return BOOLEAN;
-}
-
-"false"|"off" {
- yylval->b = false;
- return BOOLEAN;
-}
-
-[a-zA-Z][_a-zA-Z0-9.-]* {
- yylval->s = new std::string (yytext);
- return ID;
-}
-
-{FLOATNUM} {
- yylval->f = strtod (yytext, 0);
- return FLOAT;
-}
-
-{INTNUM} {
- yylval->i = strtol (yytext, 0, 0);
- return INT;
-}
-
-\" {
- BEGIN(strst);
- yyextra->tmp.clear ();
-}
-
-\n {
- yyextra->line++;
- return yytext[0];
-}
-
-[=()] return yytext[0];
-
-[ \t]+ /* Skip. */
-#.* /* Skip comments. */
-
-. {
- yyextra->error (std::string ("unexpected character \'") \
- + yytext[0] + '\'');
- return UNKNOWN;
-}
-
-<strst>\" {
- BEGIN (INITIAL);
- yylval->s = new std::string (yyextra->tmp);
- return STRING;
-}
-
-<strst>\\n yyextra->tmp += '\n';
-<strst>\\r yyextra->tmp += '\r';
-<strst>\\t yyextra->tmp += '\t';
-<strst>\\. yyextra->tmp += yytext[1];
-<strst>\\\n {
- yyextra->line++;
- yyextra->tmp += yytext[1];
-}
-<strst>. yyextra->tmp += yytext[0];
-<strst>\n {
- yyextra->error ("unexpected end of line inside string");
- yyextra->line++;
- return UNKNOWN;
-}
-
-%%
-
-/* vim:ft=lex:
-*/
diff --git a/i/marvin/src/config/parser.yy b/i/marvin/src/config/parser.yy
deleted file mode 100644
index dc2be24..0000000
--- a/i/marvin/src/config/parser.yy
+++ /dev/null
@@ -1,173 +0,0 @@
-%{
-// marvin - programme du robot 2006. {{{
-//
-// Copyright (C) 2006 Nicolas Schodet
-//
-// Robot APB Team/Efrei 2006.
-// Web: http://assos.efrei.fr/robot/
-// Email: robot AT efrei DOT fr
-//
-// This program is free software; you can redistribute it and/or modify
-// it under the terms of the GNU General Public License as published by
-// the Free Software Foundation; either version 2 of the License, or
-// (at your option) any later version.
-//
-// This program is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-// GNU General Public License for more details.
-//
-// You should have received a copy of the GNU General Public License
-// along with this program; if not, write to the Free Software
-// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-//
-// }}}
-#include "config/config.hh"
-#include "config/config_data.hh"
-#include "config/config_parser.hh"
-
-// Fichiers d'en-tête générés.
-#include "parser.hh"
-#include "lexer.hh"
-
-#undef yyextra
-#define yyextra (yyget_extra (scanner))
-
-void yyerror (void *scanner, const char *e);
-
-%}
-
-%error-verbose
-%pure-parser
-%lex-param {void *scanner}
-%parse-param {void *scanner}
-%defines
-%union {
- char c;
- int i;
- double f;
- bool b;
- std::string *s;
- struct {
- any *a;
- Config::IntList *il;
- Config::FloatList *fl;
- Config::StringList *sl;
- } a;
-}
-
-%token<c> UNKNOWN
-%token<i> INT
-%token<f> FLOAT
-%token<s> ID STRING
-%token<b> BOOLEAN
-
-%type<a> int_list
-%type<a> float_list
-%type<a> string_list
-
-%destructor { delete $$; } ID STRING
-%destructor { delete $$.a; } int_list
-%destructor { delete $$.a; } float_list
-%destructor { delete $$.a; } string_list
-
-%%
-
-input:
- /* Nothing. */
- | confitem
- | input '\n' confitem
- | input '\n'
-;
-
-confitem:
- ID '=' BOOLEAN {
- yyextra->configData.add (*$1, $3);
- delete $1;
- }
- | ID '=' INT {
- yyextra->configData.add (*$1, $3);
- delete $1;
- }
- | ID '=' FLOAT {
- yyextra->configData.add (*$1, $3);
- delete $1;
- }
- | ID '=' STRING {
- yyextra->configData.add (*$1, *$3);
- delete $1;
- delete $3;
- }
- | ID '=' '(' int_list ')' {
- yyextra->configData.add (*$1, *$4.a);
- delete $1;
- delete $4.a;
- }
- | ID '=' '(' float_list ')' {
- yyextra->configData.add (*$1, *$4.a);
- delete $1;
- delete $4.a;
- }
- | ID '=' '(' string_list ')' {
- yyextra->configData.add (*$1, *$4.a);
- delete $1;
- delete $4.a;
- }
-;
-
-int_list:
- INT {
- $$.a = new any (Config::IntList ());
- $$.il = any_cast<Config::IntList> ($$.a);
- $$.il->push_back ($1);
- }
- | int_list INT {
- $1.il->push_back ($2);
- $$ = $1;
- }
-;
-
-float_list:
- FLOAT {
- $$.a = new any (Config::FloatList ());
- $$.fl = any_cast<Config::FloatList> ($$.a);
- $$.fl->push_back ($1);
- }
- | float_list FLOAT {
- $1.fl->push_back ($2);
- $$ = $1;
- }
-;
-
-string_list:
- STRING {
- $$.a = new any (Config::StringList ());
- $$.sl = any_cast<Config::StringList> ($$.a);
- $$.sl->push_back (*$1);
- delete $1;
- }
- | string_list STRING {
- $1.sl->push_back (*$2);
- $$ = $1;
- delete $2;
- }
-;
-
-%%
-
-/// Traite une erreur de Bison.
-void
-yyerror (void *scanner, const char *e)
-{
- yyextra->error (e);
-}
-
-/* Shut up warning for this wrongly declared static function. */
-static int
-yy_init_globals (yyscan_t yyscanner)
-{
- return yy_init_globals (yyscanner);
-}
-
-/* vim:ft=yacc:
-*/
diff --git a/i/marvin/src/config/test_config_data.cc b/i/marvin/src/config/test_config.cc
index 5643ce1..3b3e368 100644
--- a/i/marvin/src/config/test_config_data.cc
+++ b/i/marvin/src/config/test_config.cc
@@ -1,4 +1,4 @@
-// test_config_data.cc
+// test_config.cc
// marvin - programme du robot 2006. {{{
//
// Copyright (C) 2006 Nicolas Schodet
@@ -33,12 +33,11 @@ main (int argc, char **argv)
try
{
// Create manually the instance of the config class
- Config cd (argc, argv);
+ Config c (argc, argv);
// Example for getting the unique instance
- //Config &confInstance = Config::getInstance ();
-
+ Config &ci = Config::getInstance ();
for (int i = 1; i < argc; ++i)
- std::cout << cd.get (argv[i]) << std::endl;
+ std::cout << ci.get (argv[i]) << std::endl;
}
catch (const std::exception &e)
{