column.hpp 6.71 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
//
#ifndef COLUMN_HPP_
#define COLUMN_HPP_

#include "ast.hpp"
#include "skipper.hpp"
#include "error_handler.hpp"

#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/optional.hpp>

29
#include <vector>
Christof Metzger-Kraus's avatar
Christof Metzger-Kraus committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

#define BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
#define BOOST_SPIRIT_QI_DEBUG

namespace SDDS {
    struct column
    {
        enum attributes { NAME
                        , SYMBOL
                        , UNITS
                        , DESCRIPTION
                        , FORMAT_STRING
                        , TYPE
                        , FIELD_LENGTH
        };

        unsigned int order_m;
        boost::optional<std::string> name_m;
        boost::optional<std::string> units_m;
        boost::optional<std::string> description_m;
        boost::optional<ast::datatype> type_m;
51
        ast::columnData_t values_m;
Christof Metzger-Kraus's avatar
Christof Metzger-Kraus committed
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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
        static unsigned int count_m;

        bool checkMandatories() const
        {
            return name_m && type_m;
        }

        template <attributes A>
        struct complainUnsupported
        {
            static bool apply()
            {
                std::string attributeString;
                switch(A)
                {
                case SYMBOL:
                    attributeString = "symbol";
                    break;
                case FORMAT_STRING:
                    attributeString = "format_string";
                    break;
                case FIELD_LENGTH:
                    attributeString = "field_length";
                    break;
                default:
                    return true;
                }
                std::cerr << attributeString << " not supported yet" << std::endl;
                return false;
            }
        };

        template <typename Iterator, typename Skipper>
        bool parse(
            Iterator& first
          , Iterator last
          , Skipper const& skipper)
        {
            switch(*this->type_m) {
            case ast::FLOAT:
            {
                float f = 0.0;
                boost::spirit::qi::float_type float_;
                if (phrase_parse(first, last, float_, skipper, f)) {
                    this->values_m.push_back(f);
                    return true;
                }
                break;
            }
            case ast::DOUBLE:
            {
                double d = 0.0;
                boost::spirit::qi::double_type double_;
                if (phrase_parse(first, last, double_, skipper, d)) {
                    this->values_m.push_back(d);
                    return true;
                }
                break;
            }
            case ast::SHORT:
            {
                short s = 0;
                boost::spirit::qi::short_type short_;
                if (phrase_parse(first, last, short_, skipper, s)) {
                    this->values_m.push_back(s);
                    return true;
                }
                break;
            }
            case ast::LONG:
            {
                long l = 0;
                boost::spirit::qi::long_type long_;
                if (phrase_parse(first, last, long_, skipper, l)) {
                    this->values_m.push_back(l);
                    return true;
                }
                break;
            }
            case ast::CHARACTER:
            {
                char c = '\0';
                boost::spirit::qi::char_type char_;
                if (phrase_parse(first, last, char_, skipper, c)) {
                    this->values_m.push_back(c);
                    return true;
                }
                break;
            }
            case ast::STRING:
            {
                std::string s("");
                parser::qstring<Iterator, Skipper> qstring;
                if (phrase_parse(first, last, qstring, skipper, s)) {
                    this->values_m.push_back(s);
                    return true;
                }
                break;
            }
            }
            return false;
        }
    };

156
    struct columnList: std::vector<column> {};
Christof Metzger-Kraus's avatar
Christof Metzger-Kraus committed
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214

    template <typename Iterator>
    struct columnOrder
    {
        template <typename, typename>
        struct result { typedef void type; };

        void operator()(column& col, Iterator) const
        {
            col.order_m = column::count_m ++;
        }
    };

    inline std::ostream& operator<<(std::ostream& out, const column& col) {
        if (col.name_m) out << "name = " << *col.name_m << ", ";
        if (col.type_m) out << "type = " << *col.type_m << ", ";
        if (col.units_m) out << "units = " << *col.units_m << ", ";
        if (col.description_m) out << "description = " << *col.description_m << ", ";
        out << "order = " << col.order_m;

        return out;
    }
}

BOOST_FUSION_ADAPT_STRUCT(
    SDDS::column,
    (boost::optional<std::string>, name_m)
    (boost::optional<SDDS::ast::datatype>, type_m)
    (boost::optional<std::string>, units_m)
    (boost::optional<std::string>, description_m)
    (SDDS::ast::variant_t, value_m)
)

namespace SDDS { namespace parser
{
    namespace qi = boost::spirit::qi;
    namespace ascii = boost::spirit::ascii;
    namespace phx = boost::phoenix;

    ///////////////////////////////////////////////////////////////////////////////
    //  The expression grammar
    ///////////////////////////////////////////////////////////////////////////////
    template <typename Iterator>
    struct column_parser: qi::grammar<Iterator, column(), skipper<Iterator> >
    {
        column_parser(error_handler<Iterator> & _error_handler);

        qi::rule<Iterator, std::string(), skipper<Iterator> > string, quoted_string, units;
        qi::rule<Iterator, std::string(), skipper<Iterator> > column_name, column_units,
                column_description, column_symbol, column_format;
        qi::rule<Iterator, ast::datatype(), skipper<Iterator> > column_type;
        qi::rule<Iterator, column(), skipper<Iterator> > start;
        qi::rule<Iterator, long(), skipper<Iterator> > column_field;
        qi::rule<Iterator, ast::nil(), skipper<Iterator> > column_unsupported_pre,
                column_unsupported_post;
        qi::symbols<char, ast::datatype> datatype;
    };
}}
215
#endif /* COLUMN_HPP_ */