column_def.hpp 4.93 KB
Newer Older
Christof Metzger-Kraus's avatar
Christof Metzger-Kraus committed
1
//
frey_m's avatar
frey_m committed
2 3
// Struct column
//
4
// Copyright (c) 2015, Christof Metzger-Kraus, Helmholtz-Zentrum Berlin
frey_m's avatar
frey_m committed
5 6 7 8 9 10 11 12 13 14 15
// All rights reserved
//
// This file is part of OPAL.
//
// OPAL 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 3 of the License, or
// (at your option) any later version.
//
// You should have received a copy of the GNU General Public License
// along with OPAL. If not, see <https://www.gnu.org/licenses/>.
Christof Metzger-Kraus's avatar
Christof Metzger-Kraus committed
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
//
#ifndef COLUMN_DEF_HPP_
#define COLUMN_DEF_HPP_

#include "column.hpp"

#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/phoenix_fusion.hpp>
#include <boost/spirit/include/phoenix_object.hpp>
#include <boost/spirit/include/phoenix_bind.hpp>

namespace SDDS { namespace parser
{
    template <typename Iterator>
    column_parser<Iterator>::column_parser(error_handler<Iterator> & _error_handler):
        column_parser::base_type(start)
    {
        using qi::on_error;
        using qi::on_success;
        using qi::fail;
        using phx::function;
        typedef function<error_handler<Iterator> > error_handler_function;

        // qi::_0_type _0;
        qi::_1_type _1;
        qi::_3_type _3;
        qi::_4_type _4;
        qi::lexeme_type lexeme;
        qi::char_type char_;
        qi::long_type long_;
        qi::lit_type lit;
        qi::alpha_type alpha;
        qi::alnum_type alnum;
        qi::_val_type _val;
        qi::_pass_type _pass;
        qi::eps_type eps;

        quoted_string %= lexeme['"' >> +(char_ - '"') >> '"'];
        string %= quoted_string
                | lexeme[(alpha | char_("@:#+-%._$&/")) >> *(alnum | char_("@:#+-%._$&/"))];
        units %= char_('1') | lexeme[alpha >> *(alpha | char_('/'))];

        datatype.add
            ("float", ast::FLOAT)
            ("double", ast::DOUBLE)
            ("short", ast::SHORT)
            ("long", ast::LONG)
            ("character", ast::CHARACTER)
            ("string", ast::STRING)
            ;

        column_name = lit("name") > '=' > string;
        column_units %= lit("units") > '=' > units;
        column_description %= lit("description") > '=' > string;
        column_type %= lit("type") > '=' > datatype;
        column_symbol = lit("symbol") > '=' > string;
        column_format = lit("format_string") > '=' > string;
        column_field = lit("field_length") > '=' > long_;

        auto complainSymbol = phx::bind(&column::complainUnsupported<column::SYMBOL>::apply);
        auto complainFormat = phx::bind(&column::complainUnsupported<column::FORMAT_STRING>::apply);
        auto complainField = phx::bind(&column::complainUnsupported<column::FIELD_LENGTH>::apply);

        column_unsupported_pre =
                ((',' > column_symbol[_pass = complainSymbol])
                 ^ (',' > column_format[_pass = complainFormat])
                 ^ (',' > column_field[_pass = complainField])
                 );
        column_unsupported_post =
                ((column_symbol[_pass = complainSymbol] > ',')
                 ^ (column_format[_pass = complainFormat] > ',')
                 ^ (column_field[_pass = complainField] > ',')
                 );

        start =
                lit("&column")
                > -column_unsupported_post
                >> ((column_name[phx::at_c<0>(_val) = _1]
                     >> ((',' >> column_type[phx::at_c<1>(_val) = _1])
                        ^(',' >> column_units[phx::at_c<2>(_val) = _1])
                        ^(',' >> column_description[phx::at_c<3>(_val) = _1])
                        ))
                    |(column_type[phx::at_c<1>(_val) = _1]
                      >> ((',' >> column_name[phx::at_c<0>(_val) = _1])
                         ^(',' >> column_units[phx::at_c<2>(_val) = _1])
                         ^(',' >> column_description[phx::at_c<3>(_val) = _1])
                         ))
                    |(column_units[phx::at_c<2>(_val) = _1]
                      >> ((',' >> column_type[phx::at_c<1>(_val) = _1])
                         ^(',' >> column_name[phx::at_c<0>(_val) = _1])
                         ^(',' >> column_description[phx::at_c<3>(_val) = _1])
                         ))
                    |(column_description[phx::at_c<3>(_val) = _1]
                      >> ((',' >> column_type[phx::at_c<1>(_val) = _1])
                         ^(',' >> column_units[phx::at_c<2>(_val) = _1])
                         ^(',' >> column_name[phx::at_c<0>(_val) = _1])
                         ))
                   )
                > -column_unsupported_pre
                > lit("&end")
                >> eps[_pass = phx::bind(&column::checkMandatories, _val)];

        BOOST_SPIRIT_DEBUG_NODES(
            (start)
        )

        on_error<fail>(start,
            error_handler_function(_error_handler)(
125
                  std::string("Error! Expecting "), _4, _3));
Christof Metzger-Kraus's avatar
Christof Metzger-Kraus committed
126 127 128 129

    }
}}
#endif /* COLUMN_DEF_HPP_ */