Marmot Full Example
XML Sample File
Source Files
Generated Files
XML Sample File
<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\" ?>
<conf>
<parameters param1='0.57' param2='false' param3='30'
param4='this is a parameter'/>
<variable name='var1' val='30'>
<subvar>1</subvar>
<subvar>2</subvar>
<subvar>3</subvar>
</variable>
<variable name='var2' val='50'>
<subvar>4</subvar>
<subvar>5</subvar>
<subvar>6</subvar>
</variable>
</conf>
Source Files
First the Data type definition, and the last is the parser information.config.mm
%class Config
%language c++
%namespace Data::test
%{
//C++ code here
//it will be include before class declaration
#include <iostream>
#include "parameters.hh"
#include "variable.hh"
%}
%var Parameters param();
%var_list Variable vars();
%{
//C++ Code here
//it will be include in class declaration
%}
%%
//Code outside classe declaration
inline std::ostream& operator<<(std::ostream& s, const Config& v)
{
v.get_param().print(s);
for (std::list<Variables*>::const_iterator it = v.get_vars().begin();
it != v.get_vars().end(); ++it)
it->print(s);
return s;
}
parameters.mm
%class Parameters
%language c++
%namespace Data
%{
//C++ code here
//it will be include before class declaration
#include <iostream>
%}
%var float param1(0.0)
%var bool param2(false)
%var int param3(0)
%var std::string param4("")
%{
//C++ Code here
//it will be include in class declaration
public:
void print(std::ostream& s) const
{
s << _param1 << ' ' << _param2
<< ' ' << _param3 << ' ' << _param4
<< std::endl;
}
%}
%%
//Code outside classe declaration
inline std::ostream& operator<<(std::ostream& s, const Parameters& p)
{
p.print(s);
return s;
}
variable.mm
%class Variable
%language c++
%namespace Data
%{
//C++ code here
//it will be include before class declaration
#include <iostream>
%}
%var std::string name("")
%var int val(0)
%var_vector int subvars
%{
//C++ Code here
//it will be include in class declaration
public:
void print(std::ostream& s) const
{
s << _name << ' ' << _val << std::endl;
for (unsigned int i = 0; i < _subvars.size(); i++)
s << _subvars[i] << ' ';
s << std::endl;
}
%}
%%
//Code outside classe declaration
inline std::ostream& operator<<(std::ostream& s, const Variable& v)
{
v.print(s);
return s;
}
parser.mm
%backend xerces-sax
%language c++
%gen_data parameters.mm variable.mm config.mm
%{
//C++ code here
using namespace Data;
%}
%type Config* conf
%type Parameters* parameters
%type Variable* variable
start : conf
conf[1] : parameters variable
parameters[1] :
@read@
{
$$ = new Parameters(); // Build Parameters object
$$->set_param1(marmot::toFloat(@param1@));
$$->set_param2(marmot::toBool(@param2@));
$$->set_param3(marmot::toInt(@param3@));
$$->set_param4(@param4@);
}
@write@
{
$$ = $father->get_parameters(); // Get Parameters object
@param1@ = marmot::toString($$->get_param1());
@param2@ = marmot::toString($$->get_param2());
@param3@ = marmot::toString($$->get_param3());
@param4@ = $$->get_param4();
}
variable[*] : subvar
@read@
{
$$ = new Variable(); // Build Variable object;
$$->set_name(@name@);
$$->set_val(marmot::toInt(@val@));
$father->add_vars($$);
}
@write@
@for(std::list::const_iterator it = $father.get_vars().begin();
it != $father.get_vars().end(); ++it)@
{
$$ = *it; // Get Variable object
@name@ = $$->get_name();
@val@ = marmot::toString($$->get_val());
}
subvar[*] :
@read@
{
$father->add_subvars(marmot::toInt($data));
}
@write@
@for (unsigned int i =0; i < $father->get_subvars().size(); i++)@
{
$data = $father->get_subvars(i);
}
Generated Files
config.hh
/* ** config.hh ** Login :** Started on Tue Dec 3 21:23:01 2002 Nicolas ** $Id$ ** ** Copyright (C) 2002 Nicolas ** 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 */ #ifndef CONFIG_HH_ # define CONFIG_HH_ #include <iostream> #include <Parameters.hh> #include <Config.hh> #include <string> namespace Data { class Config { public: Config() {} const Parameters& get_param() const {return _param; } Parameters& get_param() {return _param; } void set_param(const Parameters& param) {_param = param; } const std::list & get_vars() const {return _vars; } std::list & get_vars() {return _vars; } void set_vars(const std::list & vars) {_vars = vars; } void add_vars(const Variable& vars) { _vars.push_back(vars); } protected: Parameters _param; std::list _vars; }; //Code outside classe declaration inline std::ostream& operator<<(std::ostream& s, const Config& v) { v.get_param().print(s); for (std::list ::const_iterator it = _vars.begin(); it != _vars.end(); ++it) it->print(s); return s; } } #endif /* !CONFIG_HH_ */
parameters.hh
/* ** parameters.hh ** Login :** Started on Tue Dec 3 21:24:01 2002 Nicolas ** $Id$ ** ** Copyright (C) 2002 Nicolas ** 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 */ #ifndef PARAMETERS_HH_ # define PARAMETERS_HH_ #include <iostream> #include <string> namespace Data { class Parameters { public: Parameters() : _param1(0.0), _param2(false), _param2(0), _param4(\"\") {} const float& get_param1() const {return _param1; } float& get_param1() {return _param1; } void set_param1(const float& param1) {_param1 = param1; } const bool& get_param2() const {return _param2; } bool& get_param2() {return _param2; } void set_param2(const bool& param2) {_param2 = param2; } const int& get_param3() const {return _param3; } int& get_param3() {return _param3; } void set_param3(const int& param3) {_param3 = param3; } const std::string& get_param4() const {return _param4; } std::string& get_param4() {return _param4; } void set_param3(const std::string& param4) {_param4 = param4; } protected: float _param1; bool _param2; int _param3; std::string _param4; public: void print(std::ostream& s) const { s << _param1 << ' ' << _param2 << ' ' << _param3 << ' ' << _param4 << std::endl; } }; //Code outside classe declaration inline std::ostream& operator<<(std::ostream& s, const Parameters& p) { p.print(s); return s; } } #endif /* !PARAMETERS_HH_ */
variable.hh
/* ** variable.hh ** Login :** Started on Tue Dec 3 21:25:02 2002 Nicolas ** $Id$ ** ** Copyright (C) 2002 Nicolas ** 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 */ #ifndef VARIABLE_HH_ # define VARIABLE_HH_ #include <iostream> #include <string> namespace Data { class Variable { public: Variable() : _name(\"\"), _val(0) {} const std::string& get_name() const {return _name; } std::string& get_name() {return _name; } void set_name(const std::string& name) {_name = name; } const int& get_val() const {return _val; } int& get_val() {return _val; } void set_val(const int& val) {_val = val; } const std::vector<int>& get_subvars() const { return _subvars; } std::vector<int>& get_subvars() { return _subvars; } const int& get_subvars(const int pos) const { return _subvars[pos]; } int& get_subvars(const int pos) { return _subvars[pos]; } void set_subvars(const std::vector<int>& subvars) { _subvars = subvars; } void add_subvars(const int& subvars) { _subvars.push_back(subvars); } protected: std::string _name; int _val; std::vector<int> _subvars; public: void print(std::ostream&s const) { s << _name << ' ' << _val << std::endl; for (unsigned int i = 0; i < _subvars.size(); i++) s << _subvars[i] << ' '; s << std::endl; } }; //Code outside classe declaration inline std::ostream& operator<<(std::ostream& s, const Variable& v) { v.print(s); return s; } } #endif /* !VARIABLE_HH_ */
parser.hh
/* ** parser.hh ** Login :** Started on Tue Dec 3 21:25:34 2002 Nicolas ** $Id$ ** ** Copyright (C) 2002 Nicolas ** 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 */ #ifndef PARSER_HH_ # define PARSER_HH_ // Still unimplemented #endif /* !PARSER_HH_ */