Skip to content

Commit

Permalink
#162: Reimplemented TokenRef with trampoline.
Browse files Browse the repository at this point in the history
  • Loading branch information
jvdb committed May 29, 2017
1 parent 5f11f2d commit b6d7a5f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
2 changes: 1 addition & 1 deletion core/src/main/java/io/parsingdata/metal/token/Token.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ private String makeScope(final String scope) {

public boolean isLocal() { return true; }

public Token getCanonical(final Environment environment) { return this; }
public Token getCanonical(final Environment environment) throws IOException { return this; }

protected String makeNameFragment() {
return name.isEmpty() ? NO_NAME : name + ",";
Expand Down
21 changes: 12 additions & 9 deletions core/src/main/java/io/parsingdata/metal/token/TokenRef.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@

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 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.data.ParseItem;
import io.parsingdata.metal.encoding.Encoding;
Expand Down Expand Up @@ -60,22 +63,22 @@ public TokenRef(final String name, final String referenceName, final Encoding en

@Override
protected Optional<Environment> parseImpl(final String scope, final Environment environment, final Encoding encoding) throws IOException {
return lookup(environment.order, referenceName).parse(scope, environment, encoding);
return lookup(environment.order, referenceName).computeResult().parse(scope, environment, encoding);
}

private Token lookup(final ParseItem item, final String referenceName) {
private Trampoline<Token> lookup(final ParseItem item, final String referenceName) throws IOException {
if (item.getDefinition().name.equals(referenceName)) {
return item.getDefinition();
return complete(() -> item.getDefinition());
}
if (!item.isGraph() || item.asGraph().isEmpty()) { return LOOKUP_FAILED; }
final Token headResult = lookup(item.asGraph().head, referenceName);
if (headResult != LOOKUP_FAILED) { return headResult; }
return lookup(item.asGraph().tail, referenceName);
if (!item.isGraph() || item.asGraph().isEmpty()) { return complete(() -> LOOKUP_FAILED); }
final Token headResult = lookup(item.asGraph().head, referenceName).computeResult();
if (headResult != LOOKUP_FAILED) { return complete(() -> headResult); }
return intermediate(() -> lookup(item.asGraph().tail, referenceName));
}

@Override
public Token getCanonical(final Environment environment) {
return lookup(environment.order, referenceName);
public Token getCanonical(final Environment environment) throws IOException {
return lookup(environment.order, referenceName).computeResult();
}

@Override
Expand Down

0 comments on commit b6d7a5f

Please sign in to comment.