summaryrefslogtreecommitdiff
path: root/tiger-compiler/src/parse/metavar-map.hh
blob: 113db0f0c676a160dbd8fff19a6b05efa667de2c (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
/**
 ** \file parse/metavar-map.hh
 ** \brief Declaration of parse::MetavarMap.
 */

#pragma once

#include <string>

#include <misc/map.hh>

namespace parse
{
  /// A generic map of metavariables.
  template <typename Data> class MetavarMap
  {
  public:
    /// Build a map of metavariables of kind \a name.
    MetavarMap(const std::string& name);
    virtual ~MetavarMap();

    /// Generate a (concrete syntax) Tiger statement for metavariable
    /// number \a key (of kind \p Data).
    std::string show(unsigned key) const;

    /// Print the MetavarMap on \a ostr.
    std::ostream& dump(std::ostream& ostr) const;

  protected:
    /// Append a metavariable to the map.
    virtual std::string append_(unsigned& key, Data* data);
    /// Extract a metavariable.
    virtual Data* take_(unsigned key);

    /// Name of the kind of variable.
    const std::string name_;
    /// Metavariables.
    using map_type = misc::map<unsigned, Data*>;
    map_type map_;
  };

  /// Output \a m onto \a ostr.
  template <typename Data>
  std::ostream& operator<<(std::ostream& ostr, const MetavarMap<Data>& m);

} // namespace parse

#include <parse/metavar-map.hxx>