summaryrefslogtreecommitdiff
path: root/src/generic/apt/aptitude_resolver_cost_syntax.cc
blob: d120a54cb0932ff8e11af04f38ea30b4aaf6cd1c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
156
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
/** \file aptitude_resolver_cost_syntax.cc */


// Copyright (C) 2010 Daniel Burrows

// 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; see the file COPYING.  If not, write to
// the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA.

#include "aptitude_resolver_cost_syntax.h"

#include <ostream>

#include <generic/util/parsers.h>

namespace
{
  // Parser for individual cost entries.
  //
  // Cost entries consist of either a name or a scaling factor.  Names
  // have the form [a-zA-Z][a-zA-Z0-9-_]*; i.e, they must lead with a
  // letter, but later letters can include other alphanumerics and
  // hyphens.  (quoted strings might come into play eventually, to
  // support GUI users who don't want to know weird syntactic
  // restrictions)
  //
  // Note that this is a lexeme parser and doesn't need to be wrapped.
  class cost_entry_parser : public parsers::parser_base<cost_entry_parser, cost_component_structure::entry>
  {
    struct make_entry
    {
      typedef cost_component_structure::entry result_type;

      result_type operator()(int scaling_factor, const boost::shared_ptr<std::string> &name) const
      {
        return result_type(*name, scaling_factor);
      }
    };

  public:
    template<typename ParseInput>
    cost_component_structure::entry do_parse(ParseInput &input) const
    {
      using namespace parsers;

      // TODO: a convenience routine to parse character sets would be
      // nice.

      // Note that "max" is not an allowed cost name, to make room for
      // using it as an operator in future expansions.

      return apply(make_entry(),
                   (  ( (lexeme(integer()) << lexeme(ch('*'))) | val(1) ) << notFollowedBy(str("max")),
                      container(std::string(), lexeme(alpha() + many(alnum() | ch('-') | ch('_'))))  )).parse(input);
    }

    void get_expected(std::ostream &out) const
    {
      out << "cost value";
    }
  };

  class cost_component_structure_parser : public parsers::parser_base<cost_component_structure_parser, cost_component_structure>
  {
    struct make_cost_component_structure
    {
      typedef cost_component_structure result_type;

      result_type operator()(cost_component_structure::op combining_op,
                             const boost::shared_ptr<std::vector<cost_component_structure::entry> > &entries) const
      {
        return cost_component_structure(combining_op, *entries);
      }
    };

    // Probably the easiest way to decide whether to combine with
    // "add" or "none".  I could do this with just parsers below, but
    // it would get ugly.
    struct make_add_or_none_cost_component_structure
    {
      typedef cost_component_structure result_type;

      result_type operator()(const boost::shared_ptr<std::vector<cost_component_structure::entry> > &entries) const
      {
        return cost_component_structure(entries->size() == 1
                                          ? cost_component_structure::combine_none
                                          : cost_component_structure::combine_add,
                                        *entries);
      }

    };

  public:
    template<typename ParseInput>
    cost_component_structure do_parse(ParseInput &input) const
    {
      using namespace parsers;

      // Parse plan: check for "max" up front first (and check that it
      // can't be a cost component name).  If we see it, parse an
      // argument list to max().  Otherwise, parse one or more cost
      // entries separated by '+'.  If there's only one, we return a
      // structure with the operator set to "none"; otherwise we set
      // it to "add".
      return (  (maybe(lexeme(str("max"))) >>
                 notFollowedBy(alnum() | ch('-') | ch('_')) >>
                 apply(make_cost_component_structure(),
                       (   val(cost_component_structure::combine_max)
                        ,  lexeme(ch('('))
                        >> sepByPlus(lexeme(ch(',')),
                                     cost_entry_parser())
                        << lexeme(ch(')')) )))
               | apply(make_add_or_none_cost_component_structure(),
                       sepByPlus(lexeme(ch('+')),
                                 cost_entry_parser()))  ).parse(input);
    }

    void get_expected(std::ostream &out) const
    {
      out << "a combination of cost components";
    }
  };
}

class unpack_parse_result_visitor : public boost::static_visitor<boost::shared_ptr<std::vector<cost_component_structure> > >
{
public:
  boost::shared_ptr<std::vector<cost_component_structure> > operator()(const parsers::ParseException &ex) const
  {
    throw ResolverCostParseException(ex.what(),
                                     ex.get_raw_msg(),
                                     ex.get_line_number(),
                                     ex.get_column_number());
  }

  boost::shared_ptr<std::vector<cost_component_structure> > operator()(const boost::shared_ptr<std::vector<cost_component_structure> > &rval) const
  {
    return rval;
  }
};

boost::shared_ptr<std::vector<cost_component_structure> >
parse_cost_settings(const std::string &settings)
{
  using namespace parsers;

  boost::variant<boost::shared_ptr<std::vector<cost_component_structure> >, ParseException>
    result = parsers::parse(settings, sepByPlus(lexeme(ch(',')), cost_component_structure_parser()) << eof());

  return boost::apply_visitor(unpack_parse_result_visitor(),
                              result);
}


void dump_settings(std::ostream &out, const boost::shared_ptr<std::vector<cost_component_structure> > &settings)
{
  out << *settings;
}

std::ostream &operator<<(std::ostream &out, const std::vector<cost_component_structure> &settings)
{
  bool first_component = true;
  for(std::vector<cost_component_structure>::const_iterator
        settings_it = settings.begin(); settings_it != settings.end(); ++settings_it)
    {
      const std::vector<cost_component_structure::entry> &entries =
        *settings_it->get_entries();

      if(entries.empty())
        continue;

      if(first_component)
        first_component = false;
      else
        out << ", ";

      out << *settings_it;
    }

  return out;
}

std::ostream &operator<<(std::ostream &out, const boost::shared_ptr<std::vector<cost_component_structure> > &settings)
{
  dump_settings(out, settings);

  return out;
}