summaryrefslogtreecommitdiff
path: root/tiger-compiler/src/parse/tweast.hh
blob: 910f694ec0ce5ba1af7195c79d48ae5e95a96d93 (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
/**
 ** \file parse/tweast.hh
 ** \brief Declaration of parse::Tweast.
 */

#pragma once

#include <iostream>
#include <map>
#include <sstream>

#include <ast/fwd.hh>

#include <misc/map.hh>
#include <misc/symbol.hh>
#include <parse/metavar-map.hh>

namespace parse
{
  /// \brief TWEAST stands for ``Text With Embedded Abstract Syntax Trees''.
  ///
  /// Aggregate string to parse and tables of metavariables.
  class Tweast
    : public MetavarMap<ast::Exp>
    , public MetavarMap<ast::Var>
    , public MetavarMap<ast::NameTy>
    , public MetavarMap<ast::ChunkList>
    , public MetavarMap<parse::Tweast>
  {
  public:
    Tweast();
    Tweast(const std::string& str);

    /// \brief Stream manipulator.
    ///
    /// Append Tiger expressions to the string to parse.
    template <typename T> Tweast& operator<<(const T& t);

    /// Metavariables manipulator.
    template <typename T> T* take(unsigned s);

    /// Move the contents of all aggregated Tweast metavariables into
    /// the current Tweast.
    void flatten();

    /// Get the current input string.
    std::string input_get() const;

    /// Print the table
    std::ostream& dump(std::ostream& ostr) const;

  protected:
    // Insert base class members in the current scope.
    using MetavarMap<ast::Exp>::append_;
    using MetavarMap<ast::Var>::append_;
    using MetavarMap<ast::NameTy>::append_;
    using MetavarMap<ast::ChunkList>::append_;
    using MetavarMap<Tweast>::append_;

    /// Fake append (default case, i.e. when \a data is not a metavariable).
    template <typename T> T& append_(unsigned&, T& data) const;

    template <typename T>
    void move_metavars_(Tweast& tweast, std::string& input);

  protected:
    /// The next identifier suffix to create.
    static unsigned count_;

    /// The string to parse.
    std::stringstream input_;
  };

  /// Display the content of the tweast.
  std::ostream& operator<<(std::ostream& ostr, const Tweast& in);

} // namespace parse

#include <parse/tweast.hxx>