-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParse.java
129 lines (109 loc) · 5.45 KB
/
Parse.java
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
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.util.StringJoiner;
public class Parse {
// Various common constructs, simplifies parsing.
private static final String REST = "\\s*(.*)\\s*",
COMMA = "\\s*,\\s*",
AND = "\\s+and\\s+";
// Stage 1 syntax, contains the command name.
private static final Pattern CREATE_CMD = Pattern.compile("create table " + REST),
LOAD_CMD = Pattern.compile("load " + REST),
STORE_CMD = Pattern.compile("store " + REST),
DROP_CMD = Pattern.compile("drop table " + REST),
INSERT_CMD = Pattern.compile("insert into " + REST),
PRINT_CMD = Pattern.compile("print " + REST),
SELECT_CMD = Pattern.compile("select " + REST);
// Stage 2 syntax, contains the clauses of commands.
private static final Pattern CREATE_NEW = Pattern.compile("(\\S+)\\s+\\((\\S+\\s+\\S+\\s*" +
"(?:,\\s*\\S+\\s+\\S+\\s*)*)\\)"),
SELECT_CLS = Pattern.compile("([^,]+?(?:,[^,]+?)*)\\s+from\\s+" +
"(\\S+\\s*(?:,\\s*\\S+\\s*)*)(?:\\s+where\\s+" +
"([\\w\\s+\\-*/'<>=!.]+?(?:\\s+and\\s+" +
"[\\w\\s+\\-*/'<>=!.]+?)*))?"),
CREATE_SEL = Pattern.compile("(\\S+)\\s+as select\\s+" +
SELECT_CLS.pattern()),
INSERT_CLS = Pattern.compile("(\\S+)\\s+values\\s+(.+?" +
"\\s*(?:,\\s*.+?\\s*)*)");
public static void main(String[] args) {
if (args.length != 1) {
System.err.println("Expected a single query argument");
return;
}
eval(args[0]);
}
private static void eval(String query) {
Matcher m;
if ((m = CREATE_CMD.matcher(query)).matches()) {
createTable(m.group(1));
} else if ((m = LOAD_CMD.matcher(query)).matches()) {
loadTable(m.group(1));
} else if ((m = STORE_CMD.matcher(query)).matches()) {
storeTable(m.group(1));
} else if ((m = DROP_CMD.matcher(query)).matches()) {
dropTable(m.group(1));
} else if ((m = INSERT_CMD.matcher(query)).matches()) {
insertRow(m.group(1));
} else if ((m = PRINT_CMD.matcher(query)).matches()) {
printTable(m.group(1));
} else if ((m = SELECT_CMD.matcher(query)).matches()) {
select(m.group(1));
} else {
System.err.printf("Malformed query: %s\n", query);
}
}
private static void createTable(String expr) {
Matcher m;
if ((m = CREATE_NEW.matcher(expr)).matches()) {
createNewTable(m.group(1), m.group(2).split(COMMA));
} else if ((m = CREATE_SEL.matcher(expr)).matches()) {
createSelectedTable(m.group(1), m.group(2), m.group(3), m.group(4));
} else {
System.err.printf("Malformed create: %s\n", expr);
}
}
private static void createNewTable(String name, String[] cols) {
StringJoiner joiner = new StringJoiner(", ");
for (int i = 0; i < cols.length-1; i++) {
joiner.add(cols[i]);
}
String colSentence = joiner.toString() + " and " + cols[cols.length-1];
System.out.printf("You are trying to create a table named %s with the columns %s\n", name, colSentence);
}
private static void createSelectedTable(String name, String exprs, String tables, String conds) {
System.out.printf("You are trying to create a table named %s by selecting these expressions:" +
" '%s' from the join of these tables: '%s', filtered by these conditions: '%s'\n", name, exprs, tables, conds);
}
private static void loadTable(String name) {
System.out.printf("You are trying to load the table named %s\n", name);
}
private static void storeTable(String name) {
System.out.printf("You are trying to store the table named %s\n", name);
}
private static void dropTable(String name) {
System.out.printf("You are trying to drop the table named %s\n", name);
}
private static void insertRow(String expr) {
Matcher m = INSERT_CLS.matcher(expr);
if (!m.matches()) {
System.err.printf("Malformed insert: %s\n", expr);
return;
}
System.out.printf("You are trying to insert the row \"%s\" into the table %s\n", m.group(2), m.group(1));
}
private static void printTable(String name) {
System.out.printf("You are trying to print the table named %s\n", name);
}
private static void select(String expr) {
Matcher m = SELECT_CLS.matcher(expr);
if (!m.matches()) {
System.err.printf("Malformed select: %s\n", expr);
return;
}
select(m.group(1), m.group(2), m.group(3));
}
private static void select(String exprs, String tables, String conds) {
System.out.printf("You are trying to select these expressions:" +
" '%s' from the join of these tables: '%s', filtered by these conditions: '%s'\n", exprs, tables, conds);
}
}