Skip to content

Commit

Permalink
Start adopting usage of ParserErrorHandler (#53)
Browse files Browse the repository at this point in the history
Modifies ParseState<T> and SingleCommandParser<T> to use
ParserErrorHandler and ParseResult where appropriate
  • Loading branch information
rvesse committed Nov 25, 2016
1 parent 3d024a2 commit 6ad6ebf
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.github.rvesse.airline.model.GlobalMetadata;
import com.github.rvesse.airline.model.OptionMetadata;
import com.github.rvesse.airline.model.ParserMetadata;
import com.github.rvesse.airline.parser.errors.ParseException;
import com.github.rvesse.airline.restrictions.ArgumentsRestriction;
import com.github.rvesse.airline.restrictions.OptionRestriction;
import com.github.rvesse.airline.utils.AirlineUtils;
Expand Down Expand Up @@ -88,22 +89,41 @@ public ParseState<T> popContext() {
public ParseState<T> withOptionValue(OptionMetadata option, String rawValue) {
// Pre-validate
for (OptionRestriction restriction : option.getRestrictions()) {
restriction.preValidate(this, option, rawValue);
try {
restriction.preValidate(this, option, rawValue);
} catch (ParseException e) {
this.parserConfig.getErrorHandler().handleError(e);
}
}

// Convert value
Object value = this.parserConfig.getTypeConverter().convert(option.getTitle(), option.getJavaType(), rawValue);

// Post-validate
for (OptionRestriction restriction : option.getRestrictions()) {
restriction.postValidate(this, option, value);
try {
// Convert value
Object value = this.parserConfig.getTypeConverter().convert(option.getTitle(), option.getJavaType(),
rawValue);

// Post-validate
for (OptionRestriction restriction : option.getRestrictions()) {
try {
restriction.postValidate(this, option, value);
} catch (ParseException e) {
this.parserConfig.getErrorHandler().handleError(e);
}
}

List<Pair<OptionMetadata, Object>> newOptions = AirlineUtils.listCopy(parsedOptions);
newOptions.add(Pair.of(option, value));

return new ParseState<T>(global, parserConfig, group, command, newOptions, locationStack, parsedArguments,
currentOption, unparsedInput);
} catch (ParseException e) {
this.parserConfig.getErrorHandler().handleError(e);

List<String> newUnparsed = AirlineUtils.listCopy(unparsedInput);
newUnparsed.add(rawValue);

return new ParseState<T>(global, parserConfig, group, command, parsedOptions, locationStack,
parsedArguments, currentOption, newUnparsed);
}

List<Pair<OptionMetadata, Object>> newOptions = AirlineUtils.listCopy(parsedOptions);
newOptions.add(Pair.of(option, value));

return new ParseState<T>(global, parserConfig, group, command, newOptions, locationStack, parsedArguments,
currentOption, unparsedInput);
}

public ParseState<T> withGlobal(GlobalMetadata<T> global) {
Expand Down Expand Up @@ -134,23 +154,41 @@ public ParseState<T> withOption(OptionMetadata option) {
public ParseState<T> withArgument(ArgumentsMetadata arguments, String rawValue) {
// Pre-validate
for (ArgumentsRestriction restriction : arguments.getRestrictions()) {
restriction.preValidate(this, arguments, rawValue);
try {
restriction.preValidate(this, arguments, rawValue);
} catch (ParseException e) {
this.parserConfig.getErrorHandler().handleError(e);
}
}

// Convert value
Object value = this.parserConfig.getTypeConverter().convert(arguments.getTitle().get(0),
arguments.getJavaType(), rawValue);

// Post-validate
for (ArgumentsRestriction restriction : arguments.getRestrictions()) {
restriction.postValidate(this, arguments, value);
try {
Object value = this.parserConfig.getTypeConverter().convert(arguments.getTitle().get(0),
arguments.getJavaType(), rawValue);

// Post-validate
for (ArgumentsRestriction restriction : arguments.getRestrictions()) {
try {
restriction.postValidate(this, arguments, value);
} catch (ParseException e) {
this.parserConfig.getErrorHandler().handleError(e);
}
}

List<Object> newArguments = AirlineUtils.listCopy(parsedArguments);
newArguments.add(value);

return new ParseState<T>(global, parserConfig, group, command, parsedOptions, locationStack, newArguments,
currentOption, unparsedInput);
} catch (ParseException e) {
this.parserConfig.getErrorHandler().handleError(e);

List<String> newUnparsed = AirlineUtils.listCopy(unparsedInput);
newUnparsed.add(rawValue);

return new ParseState<T>(global, parserConfig, group, command, parsedOptions, locationStack,
parsedArguments, currentOption, newUnparsed);
}

List<Object> newArguments = AirlineUtils.listCopy(parsedArguments);
newArguments.add(value);

return new ParseState<T>(global, parserConfig, group, command, parsedOptions, locationStack, newArguments,
currentOption, unparsedInput);
}

public ParseState<T> withUnparsedInput(String input) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,33 @@
import com.github.rvesse.airline.model.OptionMetadata;
import com.github.rvesse.airline.model.ParserMetadata;
import com.github.rvesse.airline.parser.AbstractCommandParser;
import com.github.rvesse.airline.parser.ParseResult;
import com.github.rvesse.airline.parser.ParseState;
import com.github.rvesse.airline.parser.errors.ParseException;
import com.github.rvesse.airline.restrictions.ArgumentsRestriction;
import com.github.rvesse.airline.restrictions.GlobalRestriction;
import com.github.rvesse.airline.restrictions.OptionRestriction;
import com.github.rvesse.airline.utils.AirlineUtils;

public class SingleCommandParser<T> extends AbstractCommandParser<T> {

public T parse(ParserMetadata<T> parserConfig, CommandMetadata commandMetadata,
public ParseResult<T> parseWithResult(ParserMetadata<T> parserConfig, CommandMetadata commandMetadata,
Iterable<GlobalRestriction> restrictions, Iterable<String> args) {
if (args == null)
throw new NullPointerException("args is null");

ParseState<T> state = tryParse(parserConfig, commandMetadata, args);
validate(state, IteratorUtils.toList(restrictions.iterator()));

return state.getParserConfiguration().getErrorHandler().finished(state);

}

public T parse(ParserMetadata<T> parserConfig, CommandMetadata commandMetadata,
Iterable<GlobalRestriction> restrictions, Iterable<String> args) {
ParseResult<T> result = parseWithResult(parserConfig, commandMetadata, restrictions, args);

ParseState<T> state = result.getState();
CommandMetadata command = state.getCommand();

//@formatter:off
Expand Down Expand Up @@ -72,7 +83,11 @@ protected void validate(ParseState<T> state, List<GlobalRestriction> restriction
for (GlobalRestriction restriction : restrictions) {
if (restriction == null)
continue;
restriction.validate(state);
try {
restriction.validate(state);
} catch (ParseException e) {
state.getParserConfiguration().getErrorHandler().handleError(e);
}
}
CommandMetadata command = state.getCommand();
if (command != null) {
Expand All @@ -82,7 +97,11 @@ protected void validate(ParseState<T> state, List<GlobalRestriction> restriction
for (ArgumentsRestriction restriction : arguments.getRestrictions()) {
if (restriction == null)
continue;
restriction.finalValidate(state, arguments);
try {
restriction.finalValidate(state, arguments);
} catch (ParseException e) {
state.getParserConfiguration().getErrorHandler().handleError(e);
}
}
}

Expand All @@ -93,7 +112,11 @@ protected void validate(ParseState<T> state, List<GlobalRestriction> restriction
for (OptionRestriction restriction : option.getRestrictions()) {
if (restriction == null)
continue;
restriction.finalValidate(state, option);
try {
restriction.finalValidate(state, option);
} catch (ParseException e) {
state.getParserConfiguration().getErrorHandler().handleError(e);
}
}
}
}
Expand Down

0 comments on commit 6ad6ebf

Please sign in to comment.