Moved makelib
[repair.git] / Repair / RepairInterpreter / rparser.cc
1 #include <string.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include "rparser.h"
5 #include "list.h"
6 #include "common.h"
7 #include "token.h"
8 #include "set.h"
9 #include "classlist.h"
10
11
12 // class RParser
13
14 RParser::RParser(Reader *r) 
15 {
16   reader=r;
17 }
18
19
20 // returns the name of the relation whose range is defined
21 char* RParser::parserelation()
22 {
23   Token token = reader->peakahead();
24
25   while(token.token_type==TOKEN_EOL) 
26     {
27       skiptoken();
28       token = reader->peakahead();
29     }
30   
31   if (token.token_type==TOKEN_EOF)
32     return NULL;
33
34   needtoken(0);
35   needtoken(TOKEN_COLON); // we need a colon
36
37   char* relation = new char[strlen(token.str)+1];
38   strcpy(relation, token.str);
39   return relation;
40 }
41
42
43
44 WorkSet* RParser::parseworkset()
45 {
46 #ifdef DEBUGMANYMESSAGES
47   printf("Parsing a new workset... \n");
48 #endif
49
50   WorkSet *wset = new WorkSet(true);
51   needtoken(TOKEN_OPENBRACE);  // need an open brace
52
53   Token token = reader->readnext();
54
55   while (token.token_type != TOKEN_CLOSEBRACE)
56     {      
57 #ifdef DEBUGMESSAGES
58       //printf("Adding %s...\n", token.str);
59       //fflush(NULL);
60 #endif
61       char* newtoken = (char*) malloc(strlen(token.str));
62       strcpy(newtoken, token.str);
63
64       wset->addobject(newtoken);
65       
66       token = reader->readnext();
67       
68       if (token.token_type == TOKEN_COMMA)
69         token = reader->readnext();
70     }
71
72   return wset;
73 }
74
75
76
77 void RParser::error() 
78 {
79   printf("ERROR\n");
80   reader->error();
81   exit(-1);
82 }
83
84
85 void RParser::skiptoken() 
86 {
87   reader->readnext();
88 }
89
90
91 void RParser::needtoken(int token) 
92 {
93   Token t=reader->readnext();
94   if (!(t.token_type==token)) 
95     {
96       printf("Needed token: ");
97       tokenname(token);
98       printf("\n Got token: %s ",t.str);
99       tokenname(t.token_type);
100       error();
101     }
102 }