summaryrefslogtreecommitdiff
path: root/tiger-compiler/tcsh/src/tiger_parse.i
blob: 1d62bd8bf37b0dc0fc5a3c1e048968740529eae6 (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
// -*- C++ -*-

%module tiger_parse

%include "std_string.i"
%include "std_pair.i"

%{
  #include <sstream>
  #include <misc/error.hh>
  #include <parse/location.hh>
  #include <parse/tiger-driver.hh>
  #include <parse/libparse.hh>
%}

%import "tiger_misc.i"

%include "parse/fwd.hh"

// Explicit instantiation of MetavarMap's.
%include "parse/metavar-map.hh"
%template(MetavarMap) parse::MetavarMap<ast::Exp>;
%template(MetavarVar) parse::MetavarMap<ast::Var>;
%template(MetavarNameTy) parse::MetavarMap<ast::NameTy>;
%template(MetavarChunkList) parse::MetavarMap<ast::ChunkList>;
%template(MetavarTweast) parse::MetavarMap<parse::Tweast>;

/*------------------.
| parse::position.  |
`------------------*/

// Replace parse::position's ctor and ignore parse::position::initialize,
// as Python is unable to parse the literal `1u' used as default value
// of two of their arguments.
%extend parse::position
{
  // External ctor.  For more information, see
  // http://www.nabble.com/Java---adding-code-to-a-constructor-td20210601.html
  position (const std::string* f = nullptr,
            unsigned int l = 1, unsigned int c = 1)
  {
    return new parse::position(f, l, c);
  }
}
%ignore parse::position::position;
%ignore parse::position::initialize;
%extend parse::position
{
  std::string
  __str__ () const
  {
    std::ostringstream o;
    o << *$self;
    return o.str ();
  }
}

/*------------------.
| parse::location.  |
`------------------*/

// Ignore parse::location::initialize, as Python is unable to
// parse the literal `1u' used as default value of two of its
// arguments.
%ignore parse::location::initialize;
%include "parse/location.hh"
%extend parse::location
{
  std::string
  __str__ () const
  {
    std::ostringstream o;
    o << *$self;
    return o.str ();
  }
}

/*-----------.
| libparse.  |
`-----------*/

%import "parse/tweast.hh"
%include "parse/tiger-driver.hh"
%inline
{
  namespace parse
  {
    // Parse a Tiger file, return the corresponding abstract syntax.
    ast::ChunkList*
    parse (const std::string& prelude, const std::string& fname,
           misc::file_library& library)
    {
      std::pair<ast::ChunkList*, misc::error> res =
        parse (prelude, fname, library, false, false, true);
      res.second.exit_on_error();
      return res.first;
    }
  }

}

%include "parse/libparse.hh"