Skip to content

Commit

Permalink
#162: More cleanup of code around trampolines.
Browse files Browse the repository at this point in the history
  • Loading branch information
jvdb committed May 27, 2017
1 parent 30df29e commit 5f11f2d
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 12 deletions.
8 changes: 3 additions & 5 deletions core/src/main/java/io/parsingdata/metal/token/Cho.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,9 @@ private Trampoline<Optional<Environment>> iterate(final String scope, final Opti
if (list.isEmpty()) {
return complete(Util::failure);
}
final Optional<Environment> result = list.head.parse(scope, environment.get(), encoding);
if (result.isPresent()) {
return complete(() -> success(result.get().closeBranch()));
}
return intermediate(() -> iterate(scope, environment, encoding, list.tail));
return list.head.parse(scope, environment.get(), encoding)
.map(result -> complete(() -> success(result.closeBranch())))
.orElseGet(() -> intermediate(() -> iterate(scope, environment, encoding, list.tail)));
}

@Override
Expand Down
9 changes: 4 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 @@ -53,11 +53,10 @@ protected Optional<Environment> parseImpl(final String scope, final Environment
return iterate(scope, Optional.of(input), encoding, input).computeResult();
}

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

@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 @@ -66,7 +66,7 @@ protected Optional<Environment> parseImpl(final String scope, final Environment
return iterate(scope, Optional.of(environment.addBranch(this)), encoding, counts.head.get().asNumeric().longValue()).computeResult();
}

private Trampoline<Optional<Environment>> iterate(final String scope, final Optional<Environment> environment, final Encoding encoding, final long count) throws IOException {
private Trampoline<Optional<Environment>> iterate(final String scope, final Optional<Environment> environment, final Encoding encoding, final long count) {
if (!environment.isPresent()) {
return complete(Util::failure);
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/io/parsingdata/metal/token/Seq.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ protected Optional<Environment> parseImpl(final String scope, final Environment
return iterate(scope, Optional.of(environment.addBranch(this)), encoding, tokens).computeResult();
}

private Trampoline<Optional<Environment>> iterate(final String scope, final Optional<Environment> environment, final Encoding encoding, final ImmutableList<Token> list) throws IOException {
private Trampoline<Optional<Environment>> iterate(final String scope, final Optional<Environment> environment, final Encoding encoding, final ImmutableList<Token> list) {
if (!environment.isPresent()) {
return complete(Util::failure);
}
Expand Down

0 comments on commit 5f11f2d

Please sign in to comment.