Skip to content

Commit

Permalink
#70: Handled review comments from @rdvdijk and @ccreeten.
Browse files Browse the repository at this point in the history
  • Loading branch information
jvdb committed Feb 16, 2019
1 parent 3deb451 commit 60fedd2
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import static io.parsingdata.metal.expression.value.NotAValue.NOT_A_VALUE;

import java.util.Objects;
import java.util.Optional;

import io.parsingdata.metal.Trampoline;
import io.parsingdata.metal.Util;
Expand All @@ -40,7 +39,7 @@
* of the result of evaluating <code>bases</code> are concatenated. The amount
* of copies equals the result of evaluating <code>count</code>. If
* <code>count</code> evaluated to an empty value or <code>NOT_A_VALUE</code>,
* an IllegalArgumentException is thrown.
* an {@link IllegalArgumentException} is thrown.
*/
public class Expand implements ValueExpression {

Expand All @@ -58,11 +57,10 @@ public ImmutableList<Value> eval(final ParseState parseState, final Encoding enc
if (baseList.isEmpty()) {
return baseList;
}
final Optional<Value> countValue = count.evalSingle(parseState, encoding);
if (!countValue.isPresent() || countValue.get().equals(NOT_A_VALUE)) {
throw new IllegalArgumentException("Count must evaluate to a non-empty countable value.");
}
return expand(baseList, countValue.get().asNumeric().intValueExact(), new ImmutableList<>()).computeResult();
return count.evalSingle(parseState, encoding)
.filter(countValue -> !countValue.equals(NOT_A_VALUE))
.map(countValue -> expand(baseList, countValue.asNumeric().intValueExact(), new ImmutableList<>()).computeResult())
.orElseThrow(() -> new IllegalArgumentException("Count must evaluate to a non-empty countable value."));
}

private Trampoline<ImmutableList<Value>> expand(final ImmutableList<Value> baseList, final int countValue, final ImmutableList<Value> aggregate) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* Interface for all SingleValueExpression implementations.
* <p>
* A SingleValueExpression is an expression that is evaluated by executing its
* {@link #eval(ParseState, Encoding)} method. It yields an {@link Optional}
* {@link #evalSingle(ParseState, Encoding)} method. It yields an {@link Optional}
* {@link Value} object.
* <p>
* As context, it receives the current <code>ParseState</code> object as
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import io.parsingdata.metal.data.ImmutableList;
import io.parsingdata.metal.data.ParseState;
import io.parsingdata.metal.encoding.Encoding;
import io.parsingdata.metal.expression.value.NotAValue;
import io.parsingdata.metal.expression.value.Value;
import io.parsingdata.metal.expression.value.ValueExpression;

Expand All @@ -43,8 +44,8 @@
* <code>indices</code> (both {@link ValueExpression}s). Both operands are
* evaluated. Next, the resulting values of evaluating <code>indices</code> is
* used as a list of integer indices into the results of evaluating
* <code>values</code>. For every invalid index (<code>NOT_A_VALUE</code>, a
* negative value or an index that is out of bounds) <code>NOT_A_VALUE</code>
* <code>values</code>. For every invalid index ({@link NotAValue#NOT_A_VALUE}, a
* negative value or an index that is out of bounds) {@link NotAValue#NOT_A_VALUE}
* is returned.
*/
public class Nth implements ValueExpression {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package io.parsingdata.metal.expression.value.reference;

import static java.util.function.Function.identity;

import java.util.Optional;

import io.parsingdata.metal.Util;
Expand All @@ -32,7 +34,7 @@ public class Self implements SingleValueExpression {

@Override
public Optional<Value> evalSingle(final ParseState parseState, final Encoding encoding) {
return parseState.order.current().flatMap(Optional::of);
return parseState.order.current().map(identity());
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/io/parsingdata/metal/token/RepN.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ protected Optional<ParseState> parseImpl(final Environment environment) {
.filter(count -> !count.equals(NOT_A_VALUE))
.flatMap(count -> parse(environment, env -> env.parseState.iterations.head.right.compareTo(count.asNumeric()) >= 0, env -> failure()));
}

@Override
public String toString() {
return getClass().getSimpleName() + "(" + makeNameFragment() + token + "," + n + ")";
Expand Down

0 comments on commit 60fedd2

Please sign in to comment.