Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#367 Use scope of environment for ParseValue in Until instead of the direct name. #368

Merged
merged 1 commit into from
Mar 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/src/main/java/io/parsingdata/metal/token/Until.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ private Trampoline<Optional<ParseState>> iterate(final Environment environment,
}

private Trampoline<Optional<ParseState>> parseSlice(final Environment environment, final BigInteger currentSize, final BigInteger stepSize, final BigInteger maxSize, final Slice slice) {
return (currentSize.compareTo(ZERO) == 0 ? Optional.of(environment.parseState) : environment.parseState.add(new ParseValue(name, this, slice, environment.encoding)).seek(environment.parseState.offset.add(currentSize)))
return (currentSize.compareTo(ZERO) == 0 ? Optional.of(environment.parseState) : environment.parseState.add(new ParseValue(environment.scope, this, slice, environment.encoding)).seek(environment.parseState.offset.add(currentSize)))
.map(preparedParseState -> terminator.parse(environment.withParseState(preparedParseState)))
.orElseGet(Util::failure)
.map(parsedParseState -> complete(() -> success(parsedParseState)))
Expand Down
13 changes: 13 additions & 0 deletions core/src/test/java/io/parsingdata/metal/token/UntilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package io.parsingdata.metal.token;

import static io.parsingdata.metal.Shorthand.CURRENT_OFFSET;
import static io.parsingdata.metal.Shorthand.seq;
import static io.parsingdata.metal.Shorthand.sub;
import static java.nio.charset.StandardCharsets.US_ASCII;

Expand Down Expand Up @@ -127,4 +128,16 @@ private Token createToken(final ValueExpression initialSize, final Token termina
return repn(until("line", initialSize, terminator), con(3));
}

@Test
public void nameScope() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Woah 😲

final Token terminator = def("terminator", con(1), eq(con(0x00)));
final Token token = seq("struct", until("value", terminator), terminator);
final Optional<ParseState> parse = token.parse(env(stream('d', 'a', 't', 'a', 0, 0)));
assertTrue(parse.isPresent());
assertEquals(1, getAllValues(parse.get().order, "struct.terminator").size);
assertEquals(1, getAllValues(parse.get().order, "struct.value").size);
assertEquals(1, getAllValues(parse.get().order, "struct.value.terminator").size);
assertEquals("data", getAllValues(parse.get().order, "struct.value").head.asString());
}

}