-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsurlyParse.java
More file actions
151 lines (142 loc) · 3.66 KB
/
surlyParse.java
File metadata and controls
151 lines (142 loc) · 3.66 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//Surly ArgParse
// Tony Bhangal
// 10/16/2017
import java.util.*;
import java.io.*;
import java.util.Scanner;
public class surlyParse {
public Scanner line;
public Database db;
public surlyParse(Database db){
this.db = db;
}
public void parse(String args[], LinkedList<String> errorsList){
try{
File file = new File (args[0]);
Scanner input = new Scanner(file);
while (input.hasNextLine()) {
line = new Scanner(input.nextLine());
if (line.hasNext()) {
String word = nextSymbol(line);
switch(word){
case "RELATION":
cmdRelation r = new cmdRelation(line, db, errorsList);
break;
case "INSERT":
cmdInsert i = new cmdInsert(line, db, errorsList);
break;
case "PRINT":
cmdPrint p = new cmdPrint(line, db, errorsList);
break;
case "DESTROY":
cmdDestroy dstr = new cmdDestroy(line, db, errorsList);
break;
case "DELETE":
cmdDelete del = new cmdDelete(line, db, errorsList);
break;
case "READ":
cmdRead read = new cmdRead(line, db, errorsList);
break;
case "WRITE":
cmdWrite write = new cmdWrite(line, db, errorsList);
break;
case "STATISTICS":
cmdStats s = new cmdStats(line, db, errorsList);
break;
default:
relExp(word, line, errorsList);
break;
}
}
}
input.close();
} catch (ArrayIndexOutOfBoundsException a) {
System.err.println("ERROR: NO ARGUMENTS SPECIFIED");
} catch (FileNotFoundException e) {
System.err.println("ERROR: FILE NOT FOUND");
}
}
public boolean relExp(String name, Scanner line, LinkedList<String> errorsList) {
boolean recognizeCmd = false;
String nextWord = nextSymbol(line);
if (nextWord.equals("=")) {
String cmd = nextSymbol(line);
switch(cmd) {
case "PROJECT":
recognizeCmd = true;
cmdProject pro = new cmdProject(line, db, errorsList);
break;
case "SELECT":
recognizeCmd = true;
cmdSelect s = new cmdSelect(line, db, errorsList);
break;
case "JOIN":
recognizeCmd = true;
cmdJoin j = new cmdJoin(line, db, errorsList);
break;
case "COPY":
recognizeCmd = true;
cmdCopy c = new cmdCopy(line, db, errorsList);
break;
}
if (recognizeCmd) {
db.getTmpRelations().getLast().setTitle(name);
} else {
errorsList.add("UNRECOGNIZED COMMAND \"" + cmd + "\"");
}
} else {
errorsList.add("UNRECOGNIZED COMMAND \"" + name + "\"");
}
return recognizeCmd;
}
public String nextSymbol(Scanner read) {
String fix = read.next();
String better = "";
boolean quote = false;
for(int i =0; i < fix.length(); i++){
char a = fix.charAt(i);
if(a == '\''){
quote = true;
}
else if(a == '('){
}
else if(a == ')'){
}
else if(a == ','){
}
else if(a == ';'){
}
else if (a == '<' || a == '>' || a == '~') {
better += a;
}
else{
better += a;
}
}
while(quote == true){
better += " ";
fix = read.next();
for(int j = 0; j < fix.length(); j++){
char a = fix.charAt(j);
if(a == '\''){
quote = false;
}
else if(a == '('){
}
else if(a == ')'){
}
else if(a == ','){
}
else if(a == ';'){
}
else if (a == '<' || a == '>' || a == '~') {
better += a;
}
else{
better += a;
}
}
}
return better;
}
}