10 Token::Token(char *s, int tt) {
15 Token::Token(const Token & t) {
16 token_type=t.token_type;
26 Token& Token::operator=(const Token &right) {
28 token_type=right.token_type;
31 str=copystr(right.str);
47 Reader::Reader(istream * is) {
53 Token Reader::peakahead() {
55 if (t.token_type!=-1) {
59 int nextchar=readerin->get();
66 if (readerin->peek()=='*') {
67 /*have comment region */
70 for(int ch=readerin->get();ch!=EOF&&state!=2;ch=readerin->get()) {
83 if (state!=2) error();
96 Token Reader::readnext() {
102 Token Reader::checktoken() {
104 if (pos==0) return Token();
107 return Token(copystr(buf),TOKEN_OPENBRACK);
109 return Token(copystr(buf),TOKEN_CLOSEBRACK);
111 return Token(copystr(buf),TOKEN_OPENBRACE);
113 return Token(copystr(buf),TOKEN_CLOSEBRACE);
115 return Token(copystr(buf),TOKEN_OPENPAREN);
117 return Token(copystr(buf),TOKEN_CLOSEPAREN);
119 return Token(copystr(buf),TOKEN_COMMA);
121 return Token(copystr(buf),TOKEN_SEMI);
123 return Token(copystr(buf),TOKEN_COLON);
126 if (readerin->peek()!='>')
127 return Token(copystr(buf),TOKEN_EQUALS);
129 return Token(copystr(buf),TOKEN_IMPLIES);
133 if (readerin->peek()!='=')
134 return Token(copystr(buf),TOKEN_LT);
136 return Token(copystr(buf),TOKEN_LTE);
140 if (readerin->peek()!='=')
141 return Token(copystr(buf),TOKEN_GT);
143 return Token(copystr(buf),TOKEN_GTE);
147 if (readerin->peek()!='~')
148 return Token(copystr(buf),TOKEN_DOT);
150 return Token(copystr(buf),TOKEN_DOTINV);
153 return Token(copystr(buf),TOKEN_BAR);
155 return Token(copystr(buf),TOKEN_NOT);
158 if (readerin->peek()!='>')
159 return Token(copystr(buf),TOKEN_SUB);
161 return Token(copystr(buf),TOKEN_ARROW);
164 return Token(copystr(buf),TOKEN_ADD);
166 return Token(copystr(buf),TOKEN_MULT);
168 return Token(copystr(buf),TOKEN_DIV);
170 return Token(copystr(buf),TOKEN_EOL);
172 return Token(copystr(buf),TOKEN_EOF);
174 if(breakchar(readerin->peek())) {
176 if (strcmp(buf,"in")==0)
177 return Token(copystr(buf),TOKEN_IN);
178 if (strcmp(buf,"isvalid")==0)
179 return Token(copystr(buf),TOKEN_ISVALID);
180 if (strcmp(buf,"and")==0)
181 return Token(copystr(buf),TOKEN_AND);
182 if (strcmp(buf,"or")==0)
183 return Token(copystr(buf),TOKEN_OR);
184 if (strcmp(buf,"crash")==0)
185 return Token(copystr(buf),TOKEN_CRASH);
186 if (strcmp(buf,"cast")==0)
187 return Token(copystr(buf),TOKEN_CAST);
188 if (strcmp(buf,"NULL")==0)
189 return Token(copystr(buf),TOKEN_NULL);
190 if (strcmp(buf,"partition")==0)
191 return Token(copystr(buf),TOKEN_PARTITION);
192 if (strcmp(buf,"many")==0)
193 return Token(copystr(buf),TOKEN_MANY);
194 if (strcmp(buf,"set")==0)
195 return Token(copystr(buf),TOKEN_SET);
196 if (strcmp(buf,"structure")==0)
197 return Token(copystr(buf),TOKEN_STRUCTURE);
198 if (strcmp(buf,"reserved")==0)
199 return Token(copystr(buf),TOKEN_RESERVED);
200 if (strcmp(buf,"label")==0)
201 return Token(copystr(buf),TOKEN_LABEL);
202 if (strcmp(buf,"int")==0)
203 return Token(copystr(buf),TOKEN_INT);
204 if (strcmp(buf,"short")==0)
205 return Token(copystr(buf),TOKEN_SHORT);
206 if (strcmp(buf,"bit")==0)
207 return Token(copystr(buf),TOKEN_BIT);
208 if (strcmp(buf,"byte")==0)
209 return Token(copystr(buf),TOKEN_BYTE);
210 if (strcmp(buf,"subtype")==0)
211 return Token(copystr(buf),TOKEN_SUBTYPE);
212 if (strcmp(buf,"of")==0)
213 return Token(copystr(buf),TOKEN_OF);
214 if (strcmp(buf,"element")==0)
215 return Token(copystr(buf),TOKEN_ELEMENT);
216 if (strcmp(buf,"forall")==0)
217 return Token(copystr(buf),TOKEN_FORALL);
218 if (strcmp(buf,"for")==0)
219 return Token(copystr(buf),TOKEN_FOR);
220 if (strcmp(buf,"sizeof")==0)
221 return Token(copystr(buf),TOKEN_SIZEOF);
222 if (strcmp(buf,"literal")==0)
223 return Token(copystr(buf),TOKEN_LITERAL);
224 if (strcmp(buf,"param")==0)
225 return Token(copystr(buf),TOKEN_PARAM);
226 if (strcmp(buf,"1")==0)
227 return Token(copystr(buf),TOKEN_ONE);
228 if (strcmp(buf,"true")==0)
229 return Token(copystr(buf),TOKEN_TRUE);
230 if (strcmp(buf,"to")==0)
231 return Token(copystr(buf),TOKEN_TO);
232 if (strcmp(buf,"delay")==0)
233 return Token(copystr(buf),TOKEN_DELAY);
234 if (strcmp(buf,"static")==0)
235 return Token(copystr(buf),TOKEN_STATIC);
236 return Token(copystr(buf),0);
243 // return true if the given char is a separator
244 bool Reader::breakchar(int chr) {
293 void Reader::error() {
299 void tokenname(int t) {
301 case TOKEN_OPENBRACK:
304 case TOKEN_CLOSEBRACK:
313 case TOKEN_OPENBRACE:
316 case TOKEN_CLOSEBRACE:
325 case TOKEN_OPENPAREN:
328 case TOKEN_CLOSEPAREN:
397 case TOKEN_STRUCTURE:
439 case TOKEN_PARTITION:
446 printf("undefined token");