Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix AddParser #204

Merged
merged 12 commits into from
Nov 6, 2016
42 changes: 21 additions & 21 deletions src/main/java/seedu/agendum/logic/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,14 @@ public class Parser {
private static final Pattern UNALIAS_ARGS_FORMAT = Pattern.compile("(?<shorthand>[\\p{Alnum}]+)");

//@@author A0003878Y
private static final Pattern QUOTATION_FORMAT = Pattern.compile("\"([^\"]*)\"");
private static final Pattern ADD_SCHEDULE_ARGS_FORMAT = Pattern.compile("(?:.+?(?=(?:(?:by|from|to)\\s|$)))+?");
private static final Pattern QUOTATION_FORMAT = Pattern.compile("\'([^\']*)\'");
private static final Pattern ADD_SCHEDULE_ARGS_FORMAT = Pattern.compile("(?:.+?(?=(?:(?:(?i)by|from|to)\\s|$)))+?");

private static final String ARGS_FROM = "from";
private static final String ARGS_BY = "by";
private static final String ARGS_TO = "to";
private static final String FILLER_WORD = "FILLER ";
private static final String SINGLE_QUOTE = "\'";
private static final String DOUBLE_QUOTE= "\"";

private static final String[] TIME_TOKENS = new String[] { ARGS_FROM, ARGS_TO, ARGS_BY };

Expand Down Expand Up @@ -154,7 +153,7 @@ private Command prepareAdd(String args) {
// Check for quotation in args. If so, they're set as title
Optional<String> quotationCheck = checkForQuotation(args);
if (quotationCheck.isPresent()) {
titleBuilder.append(quotationCheck.get().replace(SINGLE_QUOTE,"").replace(DOUBLE_QUOTE,""));
titleBuilder.append(quotationCheck.get().replace(SINGLE_QUOTE,""));
args = FILLER_WORD + args.replace(quotationCheck.get(),""); // This will get removed later by regex
}

Expand Down Expand Up @@ -188,18 +187,19 @@ private Command prepareAdd(String args) {
return new IncorrectCommand(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddCommand.MESSAGE_USAGE));
}

if (dateTimeMap.containsKey(ARGS_BY)) {
boolean hasDeadlineKeyword = dateTimeMap.containsKey(ARGS_BY);
boolean hasStartTimeKeyword = dateTimeMap.containsKey(ARGS_FROM);
boolean hasEndTimeKeyword = dateTimeMap.containsKey(ARGS_TO);

if (hasDeadlineKeyword && !hasStartTimeKeyword && !hasEndTimeKeyword) {
return new AddCommand(title, dateTimeMap.get(ARGS_BY));
}

if (dateTimeMap.containsKey(ARGS_FROM)
&& dateTimeMap.containsKey(ARGS_TO)) {
if (!hasDeadlineKeyword && hasStartTimeKeyword && hasEndTimeKeyword) {
return new AddCommand(title, dateTimeMap.get(ARGS_FROM), dateTimeMap.get(ARGS_TO));
}

if (!dateTimeMap.containsKey(ARGS_FROM)
&& !dateTimeMap.containsKey(ARGS_TO)
&& !dateTimeMap.containsKey(ARGS_BY)) {
if (!hasDeadlineKeyword && !hasStartTimeKeyword && !hasEndTimeKeyword) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

return new AddCommand(title);
}

Expand Down Expand Up @@ -243,26 +243,26 @@ private Command prepareSchedule(String args) {
};
executeOnEveryMatcherToken(matcher, consumer);

if (dateTimeMap.containsKey(ARGS_BY)) {
boolean hasDeadlineKeyword = dateTimeMap.containsKey(ARGS_BY);
boolean hasStartTimeKeyword = dateTimeMap.containsKey(ARGS_FROM);
boolean hasEndTimeKeyword = dateTimeMap.containsKey(ARGS_TO);

if (hasDeadlineKeyword && !hasStartTimeKeyword && !hasEndTimeKeyword) {
return new ScheduleCommand(index, Optional.empty(), dateTimeMap.get(ARGS_BY));
}

if (dateTimeMap.containsKey(ARGS_FROM)
&& dateTimeMap.containsKey(ARGS_TO)) {
return new ScheduleCommand(index, dateTimeMap.get(ARGS_FROM), dateTimeMap.get(ARGS_TO));
}
if (!hasDeadlineKeyword && hasStartTimeKeyword && hasEndTimeKeyword) {
return new ScheduleCommand(index, dateTimeMap.get(ARGS_FROM), dateTimeMap.get(ARGS_TO));}

if (!dateTimeMap.containsKey(ARGS_FROM)
&& !dateTimeMap.containsKey(ARGS_TO)
&& !dateTimeMap.containsKey(ARGS_BY)) {
return new ScheduleCommand(index, Optional.empty(), Optional.empty());
if (!hasDeadlineKeyword && !hasStartTimeKeyword && !hasEndTimeKeyword) {
return new ScheduleCommand(index, Optional.empty(), Optional.empty());
}

return new IncorrectCommand( String.format(MESSAGE_INVALID_COMMAND_FORMAT, ScheduleCommand.MESSAGE_USAGE));
return new IncorrectCommand(String.format(MESSAGE_INVALID_COMMAND_FORMAT, ScheduleCommand.MESSAGE_USAGE));
}

/**
* Checks if there are ny quotation marks in the given string
* Checks if there are any quotation marks in the given string
*
* @param str
* @return returns the string inside the quote.
Expand Down
15 changes: 15 additions & 0 deletions src/test/java/seedu/agendum/logic/LogicManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,21 @@ public void execute_addEventTask_successful() throws Exception {
expectedTDL.getTaskList());
}

@Test
public void execute_addEscapeDateTimeParsing() throws Exception {
TestDataHelper helper = new TestDataHelper();
Task toBeAdded = helper.generateTaskWithName("drop from 21 to 1");
ToDoList expectedTDL = new ToDoList();
expectedTDL.addTask(toBeAdded);

// execute command and verify result
assertCommandBehavior("add 'drop from 21 to 1'",
String.format(AddCommand.MESSAGE_SUCCESS, toBeAdded),
expectedTDL,
expectedTDL.getTaskList());
}


@Test
public void executeAddDuplicateNotAllowed() throws Exception {
// setup expectations
Expand Down