Skip to content

Commit

Permalink
edit parser to parse input correctly by ignoring suffix of keywords.
Browse files Browse the repository at this point in the history
Eg. it will not capture the "end" in friend as a keyword for an end date.
  • Loading branch information
annabeleng committed Nov 3, 2016
1 parent 4e7eb1c commit ae3c82b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 47 deletions.
53 changes: 23 additions & 30 deletions src/main/java/seedu/malitio/logic/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class Parser {
private static final Pattern EDIT_DATA_ARGS_FORMAT =

Pattern.compile("(?<targetIndex>[e|d|f|E|D|F]\\d+)"
+ "(?<name>(?:[^/]+)?)"
+ "(?<name>(?:\\s[^/]+)?)"
+ "(?<tagArguments>(?: t/[^/]+)*)");

private static final Pattern COMPLETE_INDEX_ARGS_FORMAT = Pattern.compile("(?<targetIndex>[d|f|D|F]\\d+)");
Expand All @@ -42,8 +42,6 @@ public class Parser {

public static final String MESSAGE_MISSING_START_END = "Expecting start and end times\nExample: start thursday 800 end thursday 900";

public static final String MESSAGE_CONFLICTING_ARG = "Expecting either a due date or start and end time.";

public Parser() {}

/**
Expand Down Expand Up @@ -154,25 +152,21 @@ private Command prepareAdd(String args){
}
try {
String name = matcher.group("name");


String deadline = getDeadlineFromArgs(StringUtil.removeTagsFromString(name));
if (!deadline.isEmpty()) {
name = name.replaceAll(" by " + deadline, "");
}
String start = getStartFromArgs(StringUtil.removeTagsFromString(name));
if (!start.isEmpty()) {
name = name.replaceAll("start " + start, "");
name = name.replaceAll(" start " + start, "");
hasStart = true;
}

}
String end = getEndFromArgs(StringUtil.removeTagsFromString(name));
if (!end.isEmpty()) {
name = name.replaceAll("end " + end, "");
name = name.replaceAll(" end " + end, "");
hasEnd = true;
}

String deadline = getDeadlineFromArgs(StringUtil.removeTagsFromString(name));
}
if (!deadline.isEmpty()) {
name = name.replaceAll("by " + deadline, "");
}
if (!deadline.isEmpty() && !hasStart && !hasEnd) {
return new AddCommand(
name,
deadline,
Expand All @@ -185,8 +179,6 @@ private Command prepareAdd(String args){
end,
getTagsFromArgs(matcher.group("tagArguments"))
);
} else if ((!deadline.isEmpty() && hasStart) || (!deadline.isEmpty() && hasEnd)) {
return new IncorrectCommand(MESSAGE_CONFLICTING_ARG);
} else if (hasStart ^ hasEnd) {
return new IncorrectCommand(MESSAGE_MISSING_START_END);
}
Expand Down Expand Up @@ -218,8 +210,8 @@ private Command prepareEdit(String args) {
}
char taskType = index.charAt(0);
int taskNum = Integer.parseInt(index.substring(1));

String name = matcher.group("name");
System.out.println(name);
if (name.equals("") && getTagsFromArgs(matcher.group("tagArguments")).isEmpty()) {
return new IncorrectCommand(String.format(MESSAGE_INVALID_COMMAND_FORMAT, EditCommand.MESSAGE_USAGE));
}
Expand All @@ -237,6 +229,7 @@ private Command prepareEdit(String args) {
if (!end.isEmpty()) {
name = name.replaceAll(" end " + end, "");
}
name = name.trim();
return new EditCommand(
taskType,
taskNum,
Expand Down Expand Up @@ -441,10 +434,10 @@ private String parseIndex(String command) {
* Extracts the task's deadline from the command's arguments string.
*/
private static String getDeadlineFromArgs(String args) throws IllegalValueException {
int byIndex = args.lastIndexOf("by ");
int byIndex = args.lastIndexOf(" by ");
String deadline = "";
if(byIndex > 0 && byIndex < args.length() - 2) {
deadline = args.substring(byIndex + 3);
if(byIndex >= 0 && byIndex < args.length() - 4) {
deadline = args.substring(byIndex + 4);
}
return deadline;
}
Expand All @@ -453,12 +446,12 @@ private static String getDeadlineFromArgs(String args) throws IllegalValueExcept
* Extracts the task's event start from the command's arguments string.
*/
private static String getStartFromArgs(String args) throws IllegalValueException {
int startIndex = args.lastIndexOf("start ");
int endIndex = args.lastIndexOf("end");
if (startIndex > 0 && endIndex > 0) {
return args.substring(startIndex + 6, endIndex - 1);
} else if (startIndex > 0 && endIndex < 0) {
return args.substring(startIndex + 6);
int startIndex = args.lastIndexOf(" start ");
int endIndex = args.lastIndexOf(" end");
if (startIndex >= 0 && endIndex > 0) {
return args.substring(startIndex + 7, endIndex);
} else if (startIndex >= 0 && endIndex < 0) {
return args.substring(startIndex + 7);
} else {
return "";
}
Expand All @@ -468,9 +461,9 @@ private static String getStartFromArgs(String args) throws IllegalValueException
* Extracts the task's event end from the command's arguments string.
*/
private static String getEndFromArgs(String args) throws IllegalValueException {
int endIndex = args.lastIndexOf("end ");
if (endIndex > 0) {
return args.substring(endIndex + 4);
int endIndex = args.lastIndexOf(" end ");
if (endIndex >= 0) {
return args.substring(endIndex + 5);
} else {
return "";
}
Expand Down
18 changes: 1 addition & 17 deletions src/test/java/seedu/malitio/logic/LogicManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,13 @@
import seedu.malitio.logic.Logic;
import seedu.malitio.logic.LogicManager;
import seedu.malitio.logic.commands.*;
import seedu.malitio.logic.parser.Parser;
import seedu.malitio.model.Malitio;
import seedu.malitio.model.Model;
import seedu.malitio.model.ModelManager;
import seedu.malitio.model.ReadOnlyMalitio;
import seedu.malitio.model.tag.Tag;
import seedu.malitio.model.tag.UniqueTagList;
import seedu.malitio.model.task.*;
import seedu.malitio.model.task.UniqueDeadlineList.DuplicateDeadlineException;
import seedu.malitio.model.task.UniqueEventList.DuplicateEventException;
import seedu.malitio.model.task.UniqueFloatingTaskList.DuplicateFloatingTaskException;
import seedu.malitio.storage.StorageManager;

import java.util.ArrayList;
Expand Down Expand Up @@ -185,17 +181,7 @@ public void execute_add_invalidEvent() throws Exception {
String expectedMessage = Event.MESSAGE_INVALID_EVENT;
assertCommandBehavior(
"add do now start today end yesterday", expectedMessage);
}

@Test
public void execute_add_unclearTask() throws Exception {
String expectedMessage = Parser.MESSAGE_CONFLICTING_ARG;
assertCommandBehavior(
"add do now by today start tomorrow", expectedMessage);
assertCommandBehavior(
"add do now by today end tomorrow", expectedMessage);
}

}

/**
* Test to make sure all three types of task can be added
Expand Down Expand Up @@ -640,8 +626,6 @@ public void execute_find_withinEachTask() throws Exception {

//Find within events
expectedEventList = helper.generateEventList(eTarget1, eTarget2);

//Find within events
assertCommandBehavior("find e key rAnDoM",
Command.getMessageForTaskListShownSummary(expectedDeadlineList.size()),
expectedAB,
Expand Down

0 comments on commit ae3c82b

Please sign in to comment.