36a4b37cd8eb7e904cdf828fe1d41052b41544b5
[sdk] / compiler / libec2 / src / specifiers.ec
1 import "astNode"
2 import "classes"
3
4 // Specifiers
5 public class ASTSpecifier : ASTNode
6 {
7 public:
8   /*    struct
9       {
10          //ExtDecl extDecl;
11          //Symbol symbol;
12          //OldList * templateArgs;
13       };
14       Expression expression;
15       NewSpecifier _class;
16       TemplateParameter templateParameter;
17    };
18       */
19 };
20
21 public class SpecsList : ASTList<ASTSpecifier>
22 {
23    void printSep()
24    {
25       Print(" ");
26    }
27
28    SpecsList ::parse()
29    {
30       SpecsList specs = null;
31       bool gotSpec = false;
32       while(true)
33       {
34          peekToken();
35          if(nextToken.type == STRUCT || nextToken.type == UNION)
36          {
37             ASTSpecifier s = SpecClass::parse();
38             if(s)
39             {
40                if(!specs) specs = { };
41                specs.Add(s);
42             }
43             break;
44          }
45          else if(nextToken.type.isSpecifier)
46          {
47             readToken();
48             if(!specs) specs = { };
49             specs.Add(SpecBase { specifier = token.type });
50             if(!token.type.isQualifier)
51                gotSpec = true;
52          }
53          else if(nextToken.type == IDENTIFIER)
54          {
55             bool isType = false;
56             if(isType || !gotSpec)
57             {
58                readToken();
59                if(!specs) specs = { };
60                specs.Add(SpecName { name = CopyString(token.text) });
61                gotSpec = true;
62             }
63             else
64                break;
65          }
66          else
67             break;
68       }
69       return specs;
70    }
71 }
72
73 public class SpecBase : ASTSpecifier
74 {
75    TokenType specifier;
76
77    void print()
78    {
79       specifier.print();
80    }
81 }
82
83 public class SpecName : ASTSpecifier
84 {
85    String name;
86
87    void print()
88    {
89       if(name) Print(name);
90    }
91 }
92
93 public class ASTEnumerator : struct
94 {
95 public:
96    ASTIdentifier id;
97    ASTExpression exp;
98 };
99
100 public class SpecClass : ASTSpecifier
101 {
102    TokenType type;
103    ASTIdentifier id;
104    List<ASTEnumerator> enumerators;
105    SpecsList baseSpecs;
106    ClassDefList definitions;
107    bool addNameSpace;
108    Context ctx;
109    // ExtDecl extDeclStruct;
110
111    SpecClass ::parse()
112    {
113       SpecClass spec { };
114       spec.type = readToken().type;
115       if(peekToken().type == IDENTIFIER)
116          spec.id = ASTIdentifier::parse();
117       if(peekToken().type == '{')
118       {
119          readToken();
120          spec.definitions = ClassDefList::parse();
121          if(peekToken().type == '}')
122             readToken();
123       }
124       return spec;
125    }
126
127    void print()
128    {
129       type.print();
130       Print(" ");
131       if(id) id.print();
132       if(definitions)
133       {
134          PrintLn("\n{");
135          indent++;
136          definitions.print();
137          indent--;
138          Print("\n}");
139       }
140    }
141 }
142
143 /*
144 public class Attribute : struct
145 {
146 public:
147    Attribute prev, next;
148    Location loc;
149    String attr;
150    Expression exp;
151 }
152
153 public class Attrib : struct
154 {
155 public:
156    Location loc;
157    int type;
158    OldList * attribs;
159 }
160
161 public class ExtDecl : struct
162 {
163 public:
164    Location loc;
165    ExtDeclType type;
166    union
167    {
168       String s;
169       Attrib attr;
170    };
171 }
172 */