Skip to content

Commit

Permalink
-#360: Removed the includeTerminator parameter from the Until constru…
Browse files Browse the repository at this point in the history
…ctor since it was always false.
  • Loading branch information
mvanaken committed Mar 2, 2023
1 parent f28ffe9 commit 0e6710d
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 14 deletions.
2 changes: 1 addition & 1 deletion core/src/main/java/io/parsingdata/metal/Shorthand.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ private Shorthand() {}


/** "DEFinition": Instantiates a {@link Until} where the size of the def is dynamically determined. */
public static Token def(final String name, final ValueExpression initialSize, final ValueExpression stepSize, final ValueExpression maxSize, final Token terminator, final Encoding encoding) { return new Until(name, initialSize, stepSize, maxSize, terminator, false, encoding); }
public static Token def(final String name, final ValueExpression initialSize, final ValueExpression stepSize, final ValueExpression maxSize, final Token terminator, final Encoding encoding) { return new Until(name, initialSize, stepSize, maxSize, terminator, encoding); }

/** "DEFinition": Instantiates a {@link Until} where the size of the def is dynamically determined with {@code encoding = null}. */
public static Token def(final String name, final ValueExpression initialSize, final ValueExpression stepSize, final ValueExpression maxSize, final Token terminator) { return def(name, initialSize, stepSize, maxSize, terminator, null); }
Expand Down
14 changes: 3 additions & 11 deletions core/src/main/java/io/parsingdata/metal/token/Until.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,13 @@ public class Until extends Token {
public final ValueExpression stepSize;
public final ValueExpression maxSize;
public final Token terminator;
public final boolean includeTerminator;

public Until(final String name, final ValueExpression initialSize, final ValueExpression stepSize, final ValueExpression maxSize, final Token terminator, final boolean includeTerminator, final Encoding encoding) {
public Until(final String name, final ValueExpression initialSize, final ValueExpression stepSize, final ValueExpression maxSize, final Token terminator, final Encoding encoding) {
super(checkNotEmpty(name, "name"), encoding);
this.initialSize = initialSize == null ? DEFAULT_INITIAL : initialSize;
this.stepSize = stepSize == null ? DEFAULT_STEP : stepSize;
this.maxSize = maxSize == null ? DEFAULT_MAX : maxSize;
this.terminator = checkNotNull(terminator, "terminator");
this.includeTerminator = includeTerminator;
}

@Override
Expand Down Expand Up @@ -120,17 +118,12 @@ private Trampoline<Optional<ParseState>> iterate(final Environment environment,

private Trampoline<Optional<ParseState>> parseSlice(final Environment environment, final BigInteger currentSize, final BigInteger stepSize, final BigInteger maxSize, final Slice slice) {
return (currentSize.compareTo(ZERO) == 0 ? Optional.of(environment.parseState) : environment.parseState.add(new ParseValue(environment.scope, this, slice, environment.encoding)).seek(environment.parseState.offset.add(currentSize)))
.map(preparedParseState -> parseTerminator(environment, preparedParseState))
.map(preparedParseState -> terminator.parse(environment.withParseState(preparedParseState)).map(ignore -> preparedParseState))
.orElseGet(Util::failure)
.map(parseState -> complete(() -> success(parseState)))
.orElseGet(() -> intermediate(() -> iterate(environment, currentSize.add(stepSize), stepSize, maxSize)));
}

private Optional<ParseState> parseTerminator(final Environment environment, final ParseState parseStateExcludingTerminator) {
return terminator.parse(environment.withParseState(parseStateExcludingTerminator))
.map(parseStateIncludingTerminator -> includeTerminator ? parseStateIncludingTerminator : parseStateExcludingTerminator);
}

private boolean checkNotValidList(final ImmutableList<Value> list) {
return list.isEmpty() || list.head.equals(NOT_A_VALUE);
}
Expand All @@ -150,8 +143,7 @@ public boolean equals(final Object obj) {
&& Objects.equals(initialSize, ((Until)obj).initialSize)
&& Objects.equals(stepSize, ((Until)obj).stepSize)
&& Objects.equals(maxSize, ((Until)obj).maxSize)
&& Objects.equals(terminator, ((Until)obj).terminator)
&& Objects.equals(includeTerminator, ((Until)obj).includeTerminator);
&& Objects.equals(terminator, ((Until)obj).terminator);
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions core/src/test/java/io/parsingdata/metal/ArgumentsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ public static Collection<Object[]> arguments() {
{ TokenRef.class, new Object[] { null, VALID_NAME, null } },
{ TokenRef.class, new Object[] { null, null, null } },
{ TokenRef.class, new Object[] { VALID_NAME, EMPTY_NAME, null } },
{ Until.class, new Object[] { null, VALID_VE, VALID_VE, VALID_VE, VALID_T, true, null }},
{ Until.class, new Object[] { VALID_NAME, VALID_VE, VALID_VE, VALID_VE, null, false, null }}
{ Until.class, new Object[] { null, VALID_VE, VALID_VE, VALID_VE, VALID_T, null }},
{ Until.class, new Object[] { VALID_NAME, VALID_VE, VALID_VE, VALID_VE, null, null }}
});
}

Expand Down

0 comments on commit 0e6710d

Please sign in to comment.