-
Notifications
You must be signed in to change notification settings - Fork 223
Expand file tree
/
Copy pathParser.java
More file actions
101 lines (93 loc) · 3.61 KB
/
Parser.java
File metadata and controls
101 lines (93 loc) · 3.61 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
package duke;
import duke.command.*;
import duke.exception.DukeException;
import duke.exception.EmptyTodoDescriptionException;
import duke.exception.UnknownCommandException;
import duke.task.Deadline;
import duke.task.Event;
import duke.task.Task;
import duke.task.ToDo;
import java.time.LocalDate;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* deals with parsing the user command
*/
public class Parser {
/**
* @param fullCommand the user command
* @return a duke.command.Command object which encapsulates the information of a parsed user command
* @throws DukeException exception when there is an parsing error
*/
public Command parseCommand(String fullCommand) throws DukeException {
if (fullCommand.equals("bye")) {
return new ExitCommand();
} else if (fullCommand.equals("list")) {
return new ListCommand();
} else {
String[] parsedCommand = fullCommand.split(" ");
try {
switch (parsedCommand[0]) {
case "done", "delete":
int taskId = Integer.parseInt(parsedCommand[1]) - 1;
if (parsedCommand[0].equals("done")) {
return new DoneCommand(fullCommand, taskId);
} else {
return new DeleteCommand(fullCommand, taskId);
}
case "find":
return new FindCommand(fullCommand, fullCommand.replaceFirst("find ", ""));
case "todo", "deadline", "event":
Task task = parseTask(parsedCommand[0], fullCommand.replaceFirst(parsedCommand[0] + " ", ""));
return new AddCommand(fullCommand, task);
default:
throw new UnknownCommandException();
}
} catch (DukeException e) {
throw e;
} catch (Exception e) {
throw new DukeException("duke.command.Command parsing error!");
}
}
}
/**
* @param taskType the type of the task
* @param taskStr part of the user command which represents a task
* @return a duke.task.Task object in the user command
* @throws DukeException exception when there is a task parsing error
*/
private Task parseTask(String taskType, String taskStr) throws DukeException {
String pattern;
Pattern r;
Matcher m;
if (taskType.equals("todo") && taskStr.isEmpty()) {
throw new EmptyTodoDescriptionException();
}
try {
switch (taskType) {
case "todo":
return new ToDo(taskStr);
case "deadline":
pattern = "(.*) /by (.*)$";
r = Pattern.compile(pattern);
m = r.matcher(taskStr);
if (m.find()) {
return new Deadline(m.group(1), LocalDate.parse(m.group(2)));
}
case "event":
pattern = "(.*) /at (.*)$";
r = Pattern.compile(pattern);
m = r.matcher(taskStr);
if (m.find()) {
return new Event(m.group(1), LocalDate.parse(m.group(2)));
}
default:
throw new UnknownCommandException();
}
} catch (DukeException e) {
throw e;
} catch (Exception e) {
throw new DukeException("duke.task.Task parsing error!");
}
}
}