array_def.hpp 4.97 KB
Newer Older
Christof Metzger-Kraus's avatar
Christof Metzger-Kraus committed
1
//
frey_m's avatar
frey_m committed
2 3
// Struct array
//
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
//
#ifndef ARRAY_DEF_HPP_
#define ARRAY_DEF_HPP_

#include "array.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_bind.hpp>

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

        // qi::_1_type _1;
        qi::_3_type _3;
        qi::_4_type _4;
        qi::lexeme_type lexeme;
        qi::char_type char_;
        qi::lit_type lit;
        qi::long_type long_;
        qi::short_type short_;
        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 %= lexeme[alpha >> *(alpha | '/')]
                 | lit('1');

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

        array_name = lit("mode") > '=' > string;
        array_symbol = lit("symbol") > '=' > string;
        array_units = lit("units") > '=' > units;
        array_description = lit("description") > '=' > string;
        array_format = lit("format_string") > '=' > string;
        array_group = lit("group_name") > '=' > string;
        array_type = lit("type") > '=' > arraytype;
        array_field = lit("field_length") > '=' > long_;
        array_dimensions = lit("dimensions") > '=' > long_;

        auto complainArray = phx::bind(&array::complainUnsupported<array::ARRAY>::apply);
        auto complainName = phx::bind(&array::complainUnsupported<array::NAME>::apply);
        auto complainSymbol= phx::bind(&array::complainUnsupported<array::SYMBOL>::apply);
        auto complainUnits = phx::bind(&array::complainUnsupported<array::UNITS>::apply);
        auto complainDescription = phx::bind(&array::complainUnsupported<array::DESCRIPTION>::apply);
        auto complainFormat= phx::bind(&array::complainUnsupported<array::FORMAT_STRING>::apply);
        auto complainGroup = phx::bind(&array::complainUnsupported<array::GROUP_NAME>::apply);
        auto complainType = phx::bind(&array::complainUnsupported<array::TYPE>::apply);
        auto complainField = phx::bind(&array::complainUnsupported<array::FIELD_LENGTH>::apply);
        auto complainDimensions = phx::bind(&array::complainUnsupported<array::DIMENSIONS>::apply);

        array_unsupported_pre =
                ((',' > array_name[_pass = complainName])
                 ^ (',' > array_symbol[_pass = complainSymbol])
                 ^ (',' > array_units[_pass = complainUnits])
                 ^ (',' > array_description[_pass = complainDescription])
                 ^ (',' > array_format[_pass = complainFormat])
                 ^ (',' > array_group[_pass = complainGroup])
                 ^ (',' > array_type[_pass = complainType])
                 ^ (',' > array_field[_pass = complainField])
                 ^ (',' > array_dimensions[_pass = complainDimensions])
        );
        array_unsupported_post =
                ((array_name[_pass = complainName] > ',')
                 ^ (array_symbol[_pass = complainSymbol] > ',')
                 ^ (array_units[_pass = complainUnits] > ',')
                 ^ (array_description[_pass = complainDescription] > ',')
                 ^ (array_format[_pass = complainFormat] > ',')
                 ^ (array_group[_pass = complainGroup] > ',')
                 ^ (array_type[_pass = complainType] > ',')
                 ^ (array_field[_pass = complainField] > ',')
                 ^ (array_dimensions[_pass = complainDimensions] > ',')
        );

        start =
                lit("&array")[_pass = complainArray]
                > lit("&end");

        BOOST_SPIRIT_DEBUG_NODES(
            (start)
        )

        on_error<fail>(start,
            error_handler_function(_error_handler)(
121
                  std::string("Error! Expecting "), _4, _3));
Christof Metzger-Kraus's avatar
Christof Metzger-Kraus committed
122 123 124
    }
}}
#endif /* ARRAY_DEF_HPP_ */