Skip to content

Commit

Permalink
#162: Reimplemented Sub with trampoline.
Browse files Browse the repository at this point in the history
  • Loading branch information
jvdb committed Apr 20, 2017
1 parent c4cb37f commit bd24aa0
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions core/src/main/java/io/parsingdata/metal/token/Sub.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,17 @@
import java.util.Objects;
import java.util.Optional;

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

/**
* A {@link Token} that specifies a token to be parsed at a specific location
Expand Down Expand Up @@ -64,25 +68,23 @@ protected Optional<Environment> parseImpl(final String scope, final Environment
if (addresses.isEmpty()) {
return failure();
}
final Optional<Environment> result = iterate(scope, addresses, environment.addBranch(this), encoding);
final Optional<Environment> result = iterate(scope, addresses, environment.addBranch(this), encoding).computeResult();
if (result.isPresent()) {
return success(result.get().closeBranch().seek(environment.offset));
}
return failure();
}

private Optional<Environment> iterate(final String scope, final ImmutableList<Optional<Value>> addresses, final Environment environment, final Encoding encoding) throws IOException {
private Trampoline<Optional<Environment>> iterate(final String scope, final ImmutableList<Optional<Value>> addresses, final Environment environment, final Encoding encoding) throws IOException {
if (!addresses.head.isPresent()) {
return failure();
return (FinalTrampoline<Optional<Environment>>) Util::failure;
}
final long offset = addresses.head.get().asNumeric().longValue();
final Source source = environment.source;
final Optional<Environment> result = parse(scope, offset, source, environment, encoding);
final Optional<Environment> result = parse(scope, addresses.head.get().asNumeric().longValue(), environment.source, environment, encoding);
if (result.isPresent()) {
if (addresses.tail.isEmpty()) { return result; }
return iterate(scope, addresses.tail, result.get(), encoding);
if (addresses.tail.isEmpty()) { return (FinalTrampoline<Optional<Environment>>) () -> result; }
return (IntermediateTrampoline<Optional<Environment>>) () -> iterate(scope, addresses.tail, result.get(), encoding);
}
return failure();
return (FinalTrampoline<Optional<Environment>>) 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

0 comments on commit bd24aa0

Please sign in to comment.