Skip to content

Commit

Permalink
#162: Simplified and unified parseImple() and iterate() methods in To…
Browse files Browse the repository at this point in the history
…ken implementations.
  • Loading branch information
jvdb committed Jun 13, 2017
1 parent a1e4bcc commit 4f0a8d9
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 50 deletions.
10 changes: 4 additions & 6 deletions core/src/main/java/io/parsingdata/metal/token/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.Objects;
import java.util.Optional;

import io.parsingdata.metal.Util;
import io.parsingdata.metal.data.Environment;
import io.parsingdata.metal.encoding.Encoding;
import io.parsingdata.metal.expression.Expression;
Expand Down Expand Up @@ -52,12 +53,9 @@ public Post(final String name, final Token token, final Expression predicate, fi

@Override
protected Optional<Environment> parseImpl(final String scope, final Environment environment, final Encoding encoding) throws IOException {
final Optional<Environment> result = token.parse(scope, environment.addBranch(this), encoding);
if (result.isPresent()) {
final Environment newEnvironment = result.get();
return predicate.eval(newEnvironment.order, encoding) ? success(newEnvironment.closeBranch()) : failure();
}
return failure();
return token.parse(scope, environment.addBranch(this), encoding)
.map(nextEnvironment -> predicate.eval(nextEnvironment.order, encoding) ? success(nextEnvironment.closeBranch()) : failure())
.orElseGet(Util::failure);
}

@Override
Expand Down
3 changes: 1 addition & 2 deletions core/src/main/java/io/parsingdata/metal/token/Pre.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ protected Optional<Environment> parseImpl(final String scope, final Environment
if (!predicate.eval(environment.order, encoding)) {
return failure();
}
final Optional<Environment> result = token.parse(scope, environment.addBranch(this), encoding);
return result
return token.parse(scope, environment.addBranch(this), encoding)
.map(resultEnvironment -> success(resultEnvironment.closeBranch()))
.orElseGet(Util::failure);
}
Expand Down
11 changes: 5 additions & 6 deletions core/src/main/java/io/parsingdata/metal/token/Rep.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,13 @@ public Rep(final String name, final Token token, final Encoding encoding) {

@Override
protected Optional<Environment> parseImpl(final String scope, final Environment environment, final Encoding encoding) throws IOException {
final Environment input = environment.addBranch(this);
return iterate(scope, Optional.of(input), encoding, input).computeResult();
return iterate(scope, environment.addBranch(this), encoding).computeResult();
}

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())));
private Trampoline<Optional<Environment>> iterate(final String scope, final Environment environment, final Encoding encoding) throws IOException {
return token.parse(scope, environment, encoding)
.map(nextEnvironment -> intermediate(() -> iterate(scope, nextEnvironment, encoding)))
.orElseGet(() -> complete(() -> success(environment.closeBranch())));
}

@Override
Expand Down
13 changes: 6 additions & 7 deletions core/src/main/java/io/parsingdata/metal/token/RepN.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,16 @@ protected Optional<Environment> parseImpl(final String scope, final Environment
if (counts.size != 1 || !counts.head.isPresent()) {
return failure();
}
return iterate(scope, Optional.of(environment.addBranch(this)), encoding, counts.head.get().asNumeric().longValue()).computeResult();
return iterate(scope, 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) {
if (!environment.isPresent()) {
return complete(Util::failure);
}
private Trampoline<Optional<Environment>> iterate(final String scope, final Environment environment, final Encoding encoding, final long count) throws IOException {
if (count <= 0) {
return complete(() -> success(environment.get().closeBranch()));
return complete(() -> success(environment.closeBranch()));
}
return intermediate(() -> iterate(scope, token.parse(scope, environment.get(), encoding), encoding, count - 1));
return token.parse(scope, environment, encoding)
.map(nextEnvironment -> intermediate(() -> iterate(scope, nextEnvironment, encoding, count - 1)))
.orElseGet(() -> complete(Util::failure));
}

@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 @@ -53,17 +53,16 @@ public Seq(final String name, final Encoding encoding, final Token token1, final

@Override
protected Optional<Environment> parseImpl(final String scope, final Environment environment, final Encoding encoding) throws IOException {
return iterate(scope, Optional.of(environment.addBranch(this)), encoding, tokens).computeResult();
return iterate(scope, 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) {
if (!environment.isPresent()) {
return complete(Util::failure);
}
private Trampoline<Optional<Environment>> iterate(final String scope, final Environment environment, final Encoding encoding, final ImmutableList<Token> list) throws IOException {
if (list.isEmpty()) {
return complete(() -> success(environment.get().closeBranch()));
return complete(() -> success(environment.closeBranch()));
}
return intermediate(() -> iterate(scope, list.head.parse(scope, environment.get(), encoding), encoding, list.tail));
return list.head.parse(scope, environment, encoding)
.map(nextEnvironment -> intermediate(() -> iterate(scope, nextEnvironment, encoding, list.tail)))
.orElseGet(() -> complete(Util::failure));
}

@Override
Expand Down
17 changes: 9 additions & 8 deletions core/src/main/java/io/parsingdata/metal/token/Sub.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,20 @@ protected Optional<Environment> parseImpl(final String scope, final Environment
if (addresses.isEmpty()) {
return failure();
}
return iterate(scope, addresses, environment.offset, environment.addBranch(this), encoding).computeResult();
return iterate(scope, addresses, environment.addBranch(this), encoding).computeResult()
.flatMap(nextEnvironment -> success(nextEnvironment.seek(environment.offset)));
}

private Trampoline<Optional<Environment>> iterate(final String scope, final ImmutableList<Optional<Value>> addresses, final long returnOffset, 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.isEmpty()) {
return complete(() -> success(environment.closeBranch()));
}
if (!addresses.head.isPresent()) {
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 complete(() -> success(result.get().closeBranch().seek(returnOffset))); }
return intermediate(() -> iterate(scope, addresses.tail, returnOffset, result.get(), encoding));
}
return complete(Util::failure);
return parse(scope, addresses.head.get().asNumeric().longValue(), environment.source, environment, encoding)
.map(nextEnvironment -> intermediate(() -> iterate(scope, addresses.tail, nextEnvironment, encoding)))
.orElseGet(() -> 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
12 changes: 6 additions & 6 deletions core/src/main/java/io/parsingdata/metal/token/Tie.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,15 @@ 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.isEmpty()) {
return complete(() -> success(new Environment(environment.closeBranch().order, returnEnvironment.source, returnEnvironment.offset, returnEnvironment.callbacks)));
}
if (!values.head.isPresent()) {
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 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 complete(Util::failure);
return token.parse(scope, environment.source(dataExpression, index, environment, encoding), encoding)
.map(nextEnvironment -> intermediate(() -> iterate(scope, values.tail, index + 1, returnEnvironment, nextEnvironment, encoding)))
.orElseGet(() -> complete(Util::failure));
}

@Override
Expand Down
15 changes: 7 additions & 8 deletions core/src/main/java/io/parsingdata/metal/token/While.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,16 @@ public While(final String name, final Token token, final Expression predicate, f

@Override
protected Optional<Environment> parseImpl(final String scope, final Environment environment, final Encoding encoding) throws IOException {
return iterate(scope, Optional.of(environment.addBranch(this)), encoding).computeResult();
return iterate(scope, environment.addBranch(this), encoding).computeResult();
}

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

@Override
Expand Down

0 comments on commit 4f0a8d9

Please sign in to comment.