Tie extends Token { public Tie(final String name, final Token token, final ValueExpression dataExpression, final Encoding encoding) { super(name, encoding); this.token = checkNotNull(token, "token"); this.dataExpression = checkNotNull(dataExpression, "dataExpression"); } Tie(final String name, final Token token, final ValueExpression dataExpression, final Encoding encoding); @Override String toString(); @Override boolean equals(final Object obj); @Override int hashCode(); final Token token; final ValueExpression dataExpression; }
@Test public void multiLevelContainers() { final byte[] l3Data = generateIncreasing(880); final Token l3Token = INC_PREV_MOD_100; checkFullParse(l3Token, l3Data); final byte[] l2Data = flipBlocks(l3Data, 40); final Token l2Token = seq(rep(seq(def("left", con(40)), def("right", con(40)))), tie(l3Token, fold(cat(ref("right"), ref("left")), Shorthand::cat))); checkFullParse(l2Token, l2Data); final byte[] l1Data = prefixSize(deflate(l2Data)); final Token l1Token = seq(def("size", con(4)), def("data", last(ref("size"))), tie(l2Token, inflate(last(ref("data"))))); final Optional<ParseState> result = checkFullParse(l1Token, l1Data); assertEquals(80, result.get().order.head.asGraph().head.asGraph().head.asGraph().head.asGraph().head.asGraph().head.asGraph().head.asValue().asNumeric().intValueExact()); }
@Test public void tieAndSubOnSameData() { final Token nestedSeq = seq(def("d", con(3)), tie(SIMPLE_SEQ, ref("d")), sub(SIMPLE_SEQ, con(0))); final Optional<ParseState> result = nestedSeq.parse(env(stream(1, 2, 3))); assertTrue(result.isPresent()); assertEquals(0, getReferences(result.get().order).size); }
@Test public void multiTie() { final Token multiTie = seq(def("d", con(3)), def("d", con(3)), tie(SIMPLE_SEQ, ref("d"))); final Optional<ParseState> result = multiTie.parse(env(stream(1, 2, 3, 1, 2, 3))); assertTrue(result.isPresent()); assertEquals(0, getReferences(result.get().order).size); final String[] names = { "a", "b", "c", "d" }; for (String name : names) { ImmutableList<ParseValue> values = getAllValues(result.get().order, name); assertEquals(2, values.size); } }
@Test public void tieWithDuplicate() { final ValueExpression refD = ref("d"); final Token duplicateTie = seq(def("d", con(3)), tie(SIMPLE_SEQ, refD), tie(SIMPLE_SEQ, refD)); final Optional<ParseState> result = duplicateTie.parse(env(stream(1, 2, 3))); assertTrue(result.isPresent()); assertEquals(0, getReferences(result.get().order).size); assertEquals(1, getAllValues(result.get().order, "d").size); final String[] names = { "a", "b", "c" }; for (String name : names) { ImmutableList<ParseValue> values = getAllValues(result.get().order, name); assertEquals(2, values.size); } }
@Test public void tieWithEmptyListFromDataExpression() { final Token token = seq(any("a"), tie(any("b"), last(ref("c")))); final Optional<ParseState> result = token.parse(env(stream(0))); assertTrue(result.isPresent()); assertTrue(result.get().order.head.asGraph().head.asGraph().isEmpty()); }
@Test public void tieFail() { final Token token = seq(def("a", con(1), eq(con(0))), tie(def("b", con(1), eq(con(1))), last(ref("a")))); assertFalse(token.parse(env(stream(0))).isPresent()); }
@Test public void tieWithEmptyValueFromDataExpression() { final Token token = seq(any("a"), tie(any("b"), div(con(1), con(0)))); assertFalse(token.parse(env(stream(0))).isPresent()); }
@Test public void tieOnConstant() { final Token strictSeq = seq(def("a", con(1), eq(con(1))), def("b", con(1), eq(con(2))), def("c", con(1), eq(con(3)))); assertTrue(tie(strictSeq, con(1, 2, 3)).parse(env(stream())).isPresent()); assertFalse(tie(strictSeq, con(1, 2, 4)).parse(env(stream())).isPresent()); }