Skip to content

Commit

Permalink
#162: Collapsed interfaces, added helper methods to get rid of casts.
Browse files Browse the repository at this point in the history
  • Loading branch information
jvdb committed May 26, 2017
1 parent 51187b3 commit 30df29e
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 99 deletions.
24 changes: 0 additions & 24 deletions core/src/main/java/io/parsingdata/metal/FinalTrampoline.java

This file was deleted.

This file was deleted.

20 changes: 19 additions & 1 deletion core/src/main/java/io/parsingdata/metal/Trampoline.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,28 @@ public interface Trampoline<T> {

default T computeResult() throws IOException {
Trampoline<T> current = this;
while(current.hasNext()) {
while (current.hasNext()) {
current = current.next();
}
return current.result();
}

static <T> Trampoline<T> complete(CompletedTrampoline<T> completedTrampoline) { return completedTrampoline; }

interface CompletedTrampoline<T> extends Trampoline<T> {

default boolean hasNext() { return false; }
default Trampoline<T> next() { throw new UnsupportedOperationException("A CompletedTrampoline does not have a next computation."); }

}

static <T> Trampoline<T> intermediate(IntermediateTrampoline<T> intermediateTrampoline) { return intermediateTrampoline; }

interface IntermediateTrampoline<T> extends Trampoline<T> {

default T result() { throw new UnsupportedOperationException("An IntermediateTrampoline does not have a result."); }
default boolean hasNext() { return true; }

}

}
13 changes: 6 additions & 7 deletions core/src/main/java/io/parsingdata/metal/token/Cho.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,21 @@

package io.parsingdata.metal.token;

import static io.parsingdata.metal.Trampoline.complete;
import static io.parsingdata.metal.Trampoline.intermediate;
import static io.parsingdata.metal.Util.checkContainsNoNulls;
import static io.parsingdata.metal.Util.failure;
import static io.parsingdata.metal.Util.success;
import static io.parsingdata.metal.data.ImmutableList.create;

import java.io.IOException;
import java.util.Objects;
import java.util.Optional;

import io.parsingdata.metal.Trampoline;
import io.parsingdata.metal.Util;
import io.parsingdata.metal.data.Environment;
import io.parsingdata.metal.data.ImmutableList;
import io.parsingdata.metal.encoding.Encoding;
import io.parsingdata.metal.FinalTrampoline;
import io.parsingdata.metal.IntermediateTrampoline;
import io.parsingdata.metal.Trampoline;

/**
* A {@link Token} that specifies a choice out of a list of tokens.
Expand All @@ -57,13 +56,13 @@ protected Optional<Environment> parseImpl(final String scope, final Environment

private Trampoline<Optional<Environment>> iterate(final String scope, final Optional<Environment> environment, final Encoding encoding, final ImmutableList<Token> list) throws IOException {
if (list.isEmpty()) {
return (FinalTrampoline<Optional<Environment>>) Util::failure;
return complete(Util::failure);
}
final Optional<Environment> result = list.head.parse(scope, environment.get(), encoding);
if (result.isPresent()) {
return (FinalTrampoline<Optional<Environment>>) () -> success(result.get().closeBranch());
return complete(() -> success(result.get().closeBranch()));
}
return (IntermediateTrampoline<Optional<Environment>>) () -> iterate(scope, environment, encoding, list.tail);
return intermediate(() -> iterate(scope, environment, encoding, list.tail));
}

@Override
Expand Down
10 changes: 5 additions & 5 deletions core/src/main/java/io/parsingdata/metal/token/Rep.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@

package io.parsingdata.metal.token;

import static io.parsingdata.metal.Trampoline.complete;
import static io.parsingdata.metal.Trampoline.intermediate;
import static io.parsingdata.metal.Util.checkNotNull;
import static io.parsingdata.metal.Util.success;

import java.io.IOException;
import java.util.Objects;
import java.util.Optional;

import io.parsingdata.metal.Trampoline;
import io.parsingdata.metal.data.Environment;
import io.parsingdata.metal.encoding.Encoding;
import io.parsingdata.metal.FinalTrampoline;
import io.parsingdata.metal.IntermediateTrampoline;
import io.parsingdata.metal.Trampoline;

/**
* A {@link Token} that specifies a possible repetition of a token.
Expand Down Expand Up @@ -55,9 +55,9 @@ protected Optional<Environment> parseImpl(final String scope, final Environment

private Trampoline<Optional<Environment>> iterate(final String scope, final Optional<Environment> environment, final Encoding encoding, final Environment previous) throws IOException {
if (environment.isPresent()) {
return (IntermediateTrampoline<Optional<Environment>>) () -> iterate(scope, token.parse(scope, environment.get(), encoding), encoding, environment.get());
return intermediate(() -> iterate(scope, token.parse(scope, environment.get(), encoding), encoding, environment.get()));
}
return (FinalTrampoline<Optional<Environment>>) () -> success(previous.closeBranch());
return complete(() -> success(previous.closeBranch()));
}

@Override
Expand Down
12 changes: 6 additions & 6 deletions core/src/main/java/io/parsingdata/metal/token/RepN.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package io.parsingdata.metal.token;

import static io.parsingdata.metal.Trampoline.complete;
import static io.parsingdata.metal.Trampoline.intermediate;
import static io.parsingdata.metal.Util.checkNotNull;
import static io.parsingdata.metal.Util.failure;
import static io.parsingdata.metal.Util.success;
Expand All @@ -24,15 +26,13 @@
import java.util.Objects;
import java.util.Optional;

import io.parsingdata.metal.Trampoline;
import io.parsingdata.metal.Util;
import io.parsingdata.metal.data.Environment;
import io.parsingdata.metal.data.ImmutableList;
import io.parsingdata.metal.encoding.Encoding;
import io.parsingdata.metal.expression.value.Value;
import io.parsingdata.metal.expression.value.ValueExpression;
import io.parsingdata.metal.FinalTrampoline;
import io.parsingdata.metal.IntermediateTrampoline;
import io.parsingdata.metal.Trampoline;

/**
* A {@link Token} that specifies a bounded repetition of a token.
Expand Down Expand Up @@ -68,12 +68,12 @@ protected Optional<Environment> parseImpl(final String scope, final Environment

private Trampoline<Optional<Environment>> iterate(final String scope, final Optional<Environment> environment, final Encoding encoding, final long count) throws IOException {
if (!environment.isPresent()) {
return (FinalTrampoline<Optional<Environment>>) Util::failure;
return complete(Util::failure);
}
if (count <= 0) {
return (FinalTrampoline<Optional<Environment>>) () -> success(environment.get().closeBranch());
return complete(() -> success(environment.get().closeBranch()));
}
return (IntermediateTrampoline<Optional<Environment>>) () -> iterate(scope, token.parse(scope, environment.get(), encoding), encoding, count - 1);
return intermediate(() -> iterate(scope, token.parse(scope, environment.get(), encoding), encoding, count - 1));
}

@Override
Expand Down
13 changes: 6 additions & 7 deletions core/src/main/java/io/parsingdata/metal/token/Seq.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,21 @@

package io.parsingdata.metal.token;

import static io.parsingdata.metal.Trampoline.complete;
import static io.parsingdata.metal.Trampoline.intermediate;
import static io.parsingdata.metal.Util.checkContainsNoNulls;
import static io.parsingdata.metal.Util.failure;
import static io.parsingdata.metal.Util.success;
import static io.parsingdata.metal.data.ImmutableList.create;

import java.io.IOException;
import java.util.Objects;
import java.util.Optional;

import io.parsingdata.metal.Trampoline;
import io.parsingdata.metal.Util;
import io.parsingdata.metal.data.Environment;
import io.parsingdata.metal.data.ImmutableList;
import io.parsingdata.metal.encoding.Encoding;
import io.parsingdata.metal.FinalTrampoline;
import io.parsingdata.metal.IntermediateTrampoline;
import io.parsingdata.metal.Trampoline;

/**
* A {@link Token} that specifies a dependency between a list of tokens.
Expand All @@ -57,12 +56,12 @@ protected Optional<Environment> parseImpl(final String scope, final Environment

private Trampoline<Optional<Environment>> iterate(final String scope, final Optional<Environment> environment, final Encoding encoding, final ImmutableList<Token> list) throws IOException {
if (!environment.isPresent()) {
return (FinalTrampoline<Optional<Environment>>) Util::failure;
return complete(Util::failure);
}
if (list.isEmpty()) {
return (FinalTrampoline<Optional<Environment>>) () -> success(environment.get().closeBranch());
return complete(() -> success(environment.get().closeBranch()));
}
return (IntermediateTrampoline<Optional<Environment>>) () -> iterate(scope, list.head.parse(scope, environment.get(), encoding), encoding, list.tail);
return intermediate(() -> iterate(scope, list.head.parse(scope, environment.get(), encoding), encoding, list.tail));
}

@Override
Expand Down
14 changes: 7 additions & 7 deletions core/src/main/java/io/parsingdata/metal/token/Sub.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package io.parsingdata.metal.token;

import static io.parsingdata.metal.Trampoline.complete;
import static io.parsingdata.metal.Trampoline.intermediate;
import static io.parsingdata.metal.Util.checkNotNull;
import static io.parsingdata.metal.Util.failure;
import static io.parsingdata.metal.Util.success;
Expand All @@ -25,6 +27,7 @@
import java.util.Objects;
import java.util.Optional;

import io.parsingdata.metal.Trampoline;
import io.parsingdata.metal.Util;
import io.parsingdata.metal.data.Environment;
import io.parsingdata.metal.data.ImmutableList;
Expand All @@ -33,9 +36,6 @@
import io.parsingdata.metal.encoding.Encoding;
import io.parsingdata.metal.expression.value.Value;
import io.parsingdata.metal.expression.value.ValueExpression;
import io.parsingdata.metal.FinalTrampoline;
import io.parsingdata.metal.IntermediateTrampoline;
import io.parsingdata.metal.Trampoline;

/**
* A {@link Token} that specifies a token to be parsed at a specific location
Expand Down Expand Up @@ -73,14 +73,14 @@ protected Optional<Environment> parseImpl(final String scope, final Environment

private Trampoline<Optional<Environment>> iterate(final String scope, final ImmutableList<Optional<Value>> addresses, final long returnOffset, final Environment environment, final Encoding encoding) throws IOException {
if (!addresses.head.isPresent()) {
return (FinalTrampoline<Optional<Environment>>) Util::failure;
return complete(Util::failure);
}
final Optional<Environment> result = parse(scope, addresses.head.get().asNumeric().longValue(), environment.source, environment, encoding);
if (result.isPresent()) {
if (addresses.tail.isEmpty()) { return (FinalTrampoline<Optional<Environment>>) () -> success(result.get().closeBranch().seek(returnOffset)); }
return (IntermediateTrampoline<Optional<Environment>>) () -> iterate(scope, addresses.tail, returnOffset, result.get(), encoding);
if (addresses.tail.isEmpty()) { return complete(() -> success(result.get().closeBranch().seek(returnOffset))); }
return intermediate(() -> iterate(scope, addresses.tail, returnOffset, result.get(), encoding));
}
return (FinalTrampoline<Optional<Environment>>) Util::failure;
return complete(Util::failure);
}

private Optional<Environment> parse(final String scope, final long offset, final Source source, final Environment environment, final Encoding encoding) throws IOException {
Expand Down
14 changes: 7 additions & 7 deletions core/src/main/java/io/parsingdata/metal/token/Tie.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package io.parsingdata.metal.token;

import static io.parsingdata.metal.Trampoline.complete;
import static io.parsingdata.metal.Trampoline.intermediate;
import static io.parsingdata.metal.Util.checkNotNull;
import static io.parsingdata.metal.Util.failure;
import static io.parsingdata.metal.Util.success;
Expand All @@ -24,15 +26,13 @@
import java.util.Objects;
import java.util.Optional;

import io.parsingdata.metal.Trampoline;
import io.parsingdata.metal.Util;
import io.parsingdata.metal.data.Environment;
import io.parsingdata.metal.data.ImmutableList;
import io.parsingdata.metal.encoding.Encoding;
import io.parsingdata.metal.expression.value.Value;
import io.parsingdata.metal.expression.value.ValueExpression;
import io.parsingdata.metal.FinalTrampoline;
import io.parsingdata.metal.IntermediateTrampoline;
import io.parsingdata.metal.Trampoline;

/**
* A {@link Token} that specifies a token to be parsed inside the result of a
Expand Down Expand Up @@ -70,14 +70,14 @@ protected Optional<Environment> parseImpl(final String scope, final Environment

private Trampoline<Optional<Environment>> iterate(final String scope, final ImmutableList<Optional<Value>> values, final int index, final Environment returnEnvironment, final Environment environment, final Encoding encoding) throws IOException {
if (!values.head.isPresent()) {
return (FinalTrampoline<Optional<Environment>>) Util::failure;
return complete(Util::failure);
}
final Optional<Environment> result = token.parse(scope, environment.source(dataExpression, index, environment, encoding), encoding);
if (result.isPresent()) {
if (values.tail.isEmpty()) { return (FinalTrampoline<Optional<Environment>>) () -> success(new Environment(result.get().closeBranch().order, returnEnvironment.source, returnEnvironment.offset, returnEnvironment.callbacks)); }
return (IntermediateTrampoline<Optional<Environment>>) () -> iterate(scope, values.tail, index + 1, returnEnvironment, result.get(), encoding);
if (values.tail.isEmpty()) { return complete(() -> success(new Environment(result.get().closeBranch().order, returnEnvironment.source, returnEnvironment.offset, returnEnvironment.callbacks))); }
return intermediate(() -> iterate(scope, values.tail, index + 1, returnEnvironment, result.get(), encoding));
}
return (FinalTrampoline<Optional<Environment>>) Util::failure;
return complete(Util::failure);
}

@Override
Expand Down
13 changes: 6 additions & 7 deletions core/src/main/java/io/parsingdata/metal/token/While.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,20 @@
package io.parsingdata.metal.token;

import static io.parsingdata.metal.Shorthand.expTrue;
import static io.parsingdata.metal.Trampoline.complete;
import static io.parsingdata.metal.Trampoline.intermediate;
import static io.parsingdata.metal.Util.checkNotNull;
import static io.parsingdata.metal.Util.failure;
import static io.parsingdata.metal.Util.success;

import java.io.IOException;
import java.util.Objects;
import java.util.Optional;

import io.parsingdata.metal.Trampoline;
import io.parsingdata.metal.Util;
import io.parsingdata.metal.data.Environment;
import io.parsingdata.metal.encoding.Encoding;
import io.parsingdata.metal.expression.Expression;
import io.parsingdata.metal.FinalTrampoline;
import io.parsingdata.metal.IntermediateTrampoline;
import io.parsingdata.metal.Trampoline;

/**
* A {@link Token} that specifies a conditional repetition of a token.
Expand Down Expand Up @@ -64,12 +63,12 @@ protected Optional<Environment> parseImpl(final String scope, final Environment

private Trampoline<Optional<Environment>> iterate(final String scope, final Optional<Environment> environment, final Encoding encoding) {
if (!environment.isPresent()) {
return (FinalTrampoline<Optional<Environment>>) Util::failure;
return complete(Util::failure);
}
if (predicate.eval(environment.get().order, encoding)) {
return (IntermediateTrampoline<Optional<Environment>>) () -> iterate(scope, token.parse(scope, environment.get(), encoding), encoding);
return intermediate(() -> iterate(scope, token.parse(scope, environment.get(), encoding), encoding));
}
return (FinalTrampoline<Optional<Environment>>) () -> success(environment.get().closeBranch());
return complete(() -> success(environment.get().closeBranch()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
import org.junit.Test;
import org.junit.rules.ExpectedException;

import io.parsingdata.metal.FinalTrampoline;
import io.parsingdata.metal.IntermediateTrampoline;
import io.parsingdata.metal.Trampoline.CompletedTrampoline;
import io.parsingdata.metal.Trampoline.IntermediateTrampoline;

public class TrampolineTest {

Expand All @@ -38,8 +38,8 @@ public void resultOnIntermediateTrampoline() {
@Test
public void nextOnFinalTrampoline() {
thrown.expect(UnsupportedOperationException.class);
thrown.expectMessage("A FinalTrampoline does not have a next computation.");
((FinalTrampoline<Integer>) () -> 42).next();
thrown.expectMessage("A CompletedTrampoline does not have a next computation.");
((CompletedTrampoline<Integer>) () -> 42).next();
}

}

0 comments on commit 30df29e

Please sign in to comment.