efaf934f7001613aa33599fac04f52f3ba62d361
[sdk] / compiler / libec2 / src / astNode.ec
1 import "lexing"
2
3 public class ASTNode : Container
4 {
5 public:
6    Location loc;
7
8    virtual void print();
9 }
10
11 public class ASTList : ASTNode
12 {
13    List<ASTNode> list { };
14
15    IteratorPointer GetFirst()                             { return list ? list.GetFirst() : 0; }
16    IteratorPointer GetLast()                              { return list ? list.GetLast() : 0; }
17    IteratorPointer GetPrev(IteratorPointer pointer)       { return list ? list.GetPrev(pointer) : 0; }
18    IteratorPointer GetNext(IteratorPointer pointer)       { return list ? list.GetNext(pointer) : 0; }
19    bool SetData(IteratorPointer pointer, D data)          { return list ? list.SetData(pointer, (ASTNode)data) : 0; }
20    D GetData(IteratorPointer pointer)                     { return list ? list.GetData(pointer) : (D)0; }
21    IteratorPointer GetAtPosition(I pos, bool create)      { return list ? list.GetAtPosition((int)pos, create) : 0; }
22    IteratorPointer Insert(Link after, T value)            { return list ? list.Insert(after, (void *)value) : 0; }
23    IteratorPointer Add(T value)                           { return list ? list.Add((void *)value) : 0; }
24    void Remove(IteratorPointer it)                        { if(list) list.Remove(it); }
25    void Move(IteratorPointer it, IteratorPointer after)   { if(list) list.Move(it, after); }
26    void RemoveAll()                                       { if(list) list.RemoveAll(); }
27    void Copy(Container<T> source)                         { if(list) list.Copy(source); }
28    IteratorPointer Find(D value)                          { return list ? list.Find((void *)value) : 0; }
29    void FreeIterator(IteratorPointer it)                  { if(list) list.FreeIterator(it); }
30    int GetCount()                                         { return list ? list.GetCount() : 0; }
31    void Free()                                            { if(list) list.Free(); }
32    void Delete(IteratorPointer i)                         { if(list) list.Delete(i); }
33
34 public:
35    virtual void printSep()
36    {
37       Print(", ");
38    }
39
40    void print()
41    {
42       Iterator<ASTNode> it { list };
43       while(it.Next())
44       {
45          it.data.print();
46          if(list.GetNext(it.pointer))
47             printSep();
48       }
49    }
50
51    Container ::parse(subclass(Container) c, ASTNode parser(), char sep)
52    {
53       Container<ASTNode> list = null;
54       while(true)
55       {
56          ASTNode e = parser();
57          if(e)
58          {
59             if(!list) list = eInstance_New(c);
60             list.Add(e);
61          }
62          if(peekToken().type == sep)
63             readToken();
64          else
65             break;
66       }
67       return list;
68    }
69
70    ~ASTList()
71    {
72       list.Free();
73    }
74 }