about summary refs log tree commit diff
path: root/src/libutil/aterm.cc
blob: fb734b3a08473a097db3900a5ecf3c6f4a6f7979 (plain) (blame)
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
#include "aterm.hh"


string atPrint(ATerm t)
{
    if (!t) throw Error("attempt to print null aterm");
    char * s = ATwriteToString(t);
    if (!s) throw Error("cannot print term");
    return s;
}


ostream & operator << (ostream & stream, ATerm e)
{
    return stream << atPrint(e);
}


ATMatcher & atMatch(ATMatcher & pos, ATerm t)
{
    pos.t = t;
    pos.pos = ATMatcher::funPos;
    return pos;
}


static inline bool failed(const ATMatcher & pos)
{
    return pos.pos == ATMatcher::failPos;
}


static inline ATMatcher & fail(ATMatcher & pos)
{
    pos.pos = ATMatcher::failPos;
    return pos;
}


ATMatcher & operator >> (ATMatcher & pos, ATerm & out)
{
    out = 0;
    if (failed(pos)) return pos;
    if (pos.pos == ATMatcher::funPos || 
        ATgetType(pos.t) != AT_APPL ||
        pos.pos >= (int) ATgetArity(ATgetAFun(pos.t)))
        return fail(pos);
    out = ATgetArgument(pos.t, pos.pos);
    pos.pos++;
    return pos;
}


ATMatcher & operator >> (ATMatcher & pos, string & out)
{
    out = "";
    if (pos.pos == ATMatcher::funPos) {
        if (ATgetType(pos.t) != AT_APPL) return fail(pos);
        out = ATgetName(ATgetAFun(pos.t));
        pos.pos = 0;
    } else {
        ATerm t;
        pos = pos >> t;
        if (failed(pos)) return pos;
        if (ATgetType(t) != AT_APPL ||
            ATgetArity(ATgetAFun(t)) != 0)
            return fail(pos);
        out = ATgetName(ATgetAFun(t));
    }
    return pos;
}


ATMatcher & operator >> (ATMatcher & pos, const string & s)
{
    string s2;
    pos = pos >> s2;
    if (failed(pos)) return pos;
    if (s != s2) return fail(pos);
    return pos;
}


ATMatcher & operator >> (ATMatcher & pos, int & n)
{
    n = 0;
    ATerm t;
    pos = pos >> t;
    if (failed(pos)) return pos;
    if (ATgetType(t) != AT_INT) return fail(pos);
    n = ATgetInt((ATermInt) t);
    return pos;
}


ATMatcher & operator >> (ATMatcher & pos, ATermList & out)
{
    out = 0;
    ATerm t;
    pos = pos >> t;
    if (failed(pos)) return pos;
    if (ATgetType(t) != AT_LIST) return fail(pos);
    out = (ATermList) t;
    return pos;
}


Error badTerm(const format & f, ATerm t)
{
    char * s = ATwriteToString(t);
    if (!s) throw Error("cannot print term");
    if (strlen(s) > 1000) {
        int len;
        s = ATwriteToSharedString(t, &len);
        if (!s) throw Error("cannot print term");
    }
    return Error(format("%1%, in `%2%'") % f.str() % (string) s);
}