Skip to content

Commit

Permalink
Merge pull request #235 from parsingdata/gardening-for-7-1
Browse files Browse the repository at this point in the history
Small gardening changes for the 7.1 release.
  • Loading branch information
jvdb authored Dec 14, 2017
2 parents 23c194c + a9dd000 commit da7e817
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 59 deletions.
4 changes: 4 additions & 0 deletions core/src/main/java/io/parsingdata/metal/Shorthand.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ private Shorthand() {}
public static Token until(final String name, final ValueExpression initialSize, final Token terminator) { return until(name, initialSize, null, terminator, null); }
public static Token until(final String name, final Token terminator, final Encoding encoding) { return until(name, null, terminator, encoding); }
public static Token until(final String name, final Token terminator) { return until(name, terminator, null); }
public static Token when(final String name, final Token token, final Expression predicate, final Encoding encoding) { return cho(name, encoding, pre(def(EMPTY_NAME, 0), not(predicate)), token); }
public static Token when(final String name, final Token token, final Expression predicate) { return when(name, token, predicate, null); }
public static Token when(final Token token, final Expression predicate, final Encoding encoding) { return when(EMPTY_NAME, token, predicate, encoding); }
public static Token when(final Token token, final Expression predicate) { return when(token, predicate, null); }

public static BinaryValueExpression add(final ValueExpression left, final ValueExpression right) { return new Add(left, right); }
public static BinaryValueExpression div(final ValueExpression left, final ValueExpression right) { return new Div(left, right); }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import static io.parsingdata.metal.Trampoline.intermediate;
import static io.parsingdata.metal.Util.checkNotNegative;
import static io.parsingdata.metal.Util.checkNotNull;
import static io.parsingdata.metal.data.Slice.createFromSource;

import java.math.BigInteger;
import java.util.Objects;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import static io.parsingdata.metal.Util.checkNotNull;

import java.math.BigInteger;
import java.util.Arrays;
import java.util.Objects;
import java.util.Optional;

Expand Down Expand Up @@ -76,7 +75,7 @@ private synchronized byte[] getValue() {
.map(Value::getValue)
.orElseThrow(() -> new IllegalStateException("ValueExpression dataExpression yields empty Value at index " + index + "."));
}
return cache.clone();
return cache;
}

private Trampoline<Optional<Value>> getValueAtIndex(final ImmutableList<Optional<Value>> results, final int index, final int current) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import static io.parsingdata.metal.data.Slice.createFromSource;

import java.math.BigInteger;
import java.util.Optional;

import io.parsingdata.metal.data.ConcatenatedValueSource;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,15 @@

import static java.math.BigInteger.ZERO;

import static io.parsingdata.metal.Trampoline.complete;
import static io.parsingdata.metal.Trampoline.intermediate;
import static io.parsingdata.metal.data.Slice.createFromSource;

import java.math.BigInteger;
import java.util.Objects;
import java.util.Optional;

import io.parsingdata.metal.Trampoline;
import io.parsingdata.metal.Util;
import io.parsingdata.metal.data.ConcatenatedValueSource;
import io.parsingdata.metal.data.ImmutableList;
import io.parsingdata.metal.data.ParseState;
import io.parsingdata.metal.data.Slice;
import io.parsingdata.metal.encoding.Encoding;

/**
Expand Down
107 changes: 64 additions & 43 deletions core/src/test/java/io/parsingdata/metal/ShorthandsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import static io.parsingdata.metal.Shorthand.ltNum;
import static io.parsingdata.metal.Shorthand.mapLeft;
import static io.parsingdata.metal.Shorthand.mapRight;
import static io.parsingdata.metal.Shorthand.not;
import static io.parsingdata.metal.Shorthand.opt;
import static io.parsingdata.metal.Shorthand.pre;
import static io.parsingdata.metal.Shorthand.ref;
Expand All @@ -45,6 +46,7 @@
import static io.parsingdata.metal.Shorthand.seq;
import static io.parsingdata.metal.Shorthand.sub;
import static io.parsingdata.metal.Shorthand.tie;
import static io.parsingdata.metal.Shorthand.when;
import static io.parsingdata.metal.data.ParseState.createFromByteStream;
import static io.parsingdata.metal.expression.value.ExpandTest.createParseValue;
import static io.parsingdata.metal.util.EncodingFactory.enc;
Expand All @@ -53,7 +55,6 @@
import static io.parsingdata.metal.util.TokenDefinitions.any;
import static junit.framework.TestCase.assertFalse;

import java.io.IOException;
import java.util.Optional;

import org.junit.Rule;
Expand All @@ -63,6 +64,7 @@
import io.parsingdata.metal.data.ImmutableList;
import io.parsingdata.metal.data.ParseState;
import io.parsingdata.metal.data.ParseValue;
import io.parsingdata.metal.data.Selection;
import io.parsingdata.metal.expression.value.Value;
import io.parsingdata.metal.token.Cho;
import io.parsingdata.metal.token.Seq;
Expand All @@ -76,12 +78,12 @@ public class ShorthandsTest {
def("c", con(1), eq(con(3))));

@Test
public void sequenceMultiMatch() throws IOException {
public void sequenceMultiMatch() {
assertTrue(multiSequence.parse(env(stream(1, 2, 3))).isPresent());
}

@Test
public void sequenceMultiNoMatch() throws IOException {
public void sequenceMultiNoMatch() {
assertFalse(multiSequence.parse(env(stream(1, 2, 2))).isPresent());
}

Expand All @@ -91,17 +93,17 @@ public void sequenceMultiNoMatch() throws IOException {
def("c", con(1), gtNum(con(0))));

@Test
public void choiceMultiMatchA() throws IOException {
public void choiceMultiMatchA() {
runChoice(3, "a");
}

@Test
public void choiceMultiMatchB() throws IOException {
public void choiceMultiMatchB() {
runChoice(2, "b");
}

@Test
public void choiceMultiMatchC() throws IOException {
public void choiceMultiMatchC() {
runChoice(1, "c");
}

Expand All @@ -112,7 +114,7 @@ private void runChoice(final int data, final String matched) {
}

@Test
public void choiceMultiNoMatch() throws IOException {
public void choiceMultiNoMatch() {
assertFalse(multiChoice.parse(env(stream(0))).isPresent());
}

Expand All @@ -130,37 +132,37 @@ public void choiceMultiNoMatch() throws IOException {
);

@Test
public void nonLocalCompare() throws IOException {
public void nonLocalCompare() {
assertTrue(nonLocalCompare.parse(env(stream(1, 'a', 'b', 'c', 0, 0))).isPresent());
}

@Test
public void allTokensNamed() throws IOException {
public void allTokensNamed() {
final Optional<ParseState> result =
rep("rep",
repn("repn",
seq("seq",
pre("pre",
opt("opt",
any("a")),
TRUE),
cho("cho",
def("def0", con(1), eq(con(0))),
def("def1", con(1), eq(con(1)))),
sub("sub",
def("def2", con(1), eq(con(2))),
con(2)),
tie("tie",
def("def3", con(1), eq(con(1))),
last(ref("def1")))
), con(1)
)
).parse(env(stream(2, 1, 2)));
when("when",
rep("rep",
repn("repn",
seq("seq",
pre("pre",
opt("opt",
any("a")), TRUE),
cho("cho",
def("def0", con(1), eq(con(0))),
def("def1", con(1), eq(con(1)))),
sub("sub",
def("def2", con(1), eq(con(2))),
con(2)),
tie("tie",
def("def3", con(1), eq(con(1))),
last(ref("def1")))
), con(1)
)
), TRUE).parse(env(stream(2, 1, 2)));
assertTrue(result.isPresent());
checkNameAndValue("rep.repn.seq.pre.opt.a", 2, result.get());
checkNameAndValue("rep.repn.seq.cho.def1", 1, result.get());
checkNameAndValue("rep.repn.seq.sub.def2", 2, result.get());
checkNameAndValue("rep.repn.seq.tie.def3", 1, result.get());
checkNameAndValue("when.rep.repn.seq.pre.opt.a", 2, result.get());
checkNameAndValue("when.rep.repn.seq.cho.def1", 1, result.get());
checkNameAndValue("when.rep.repn.seq.sub.def2", 2, result.get());
checkNameAndValue("when.rep.repn.seq.tie.def3", 1, result.get());
}

private void checkNameAndValue(final String name, final int value, final ParseState parseState) {
Expand All @@ -180,32 +182,32 @@ private void checkNameAndValue(final String name, final int value, final ParseSt
@Rule
public ExpectedException thrown = ExpectedException.none();

public static final Token DEFA = any("a");
public static final Token DEFB = any("b");
public static final Token DEF_A = any("a");
public static final Token DEF_B = any("b");

@Test
public void checkChoTokens() {
final Token choToken = cho(DEFA, DEFB);
final Token choToken = cho(DEF_A, DEF_B);
final Cho cho = (Cho)choToken;
assertEquals(2, cho.tokens.size);
assertEquals(DEFA, cho.tokens.head);
assertEquals(DEFB, cho.tokens.tail.head);
assertEquals(DEF_A, cho.tokens.head);
assertEquals(DEF_B, cho.tokens.tail.head);
}

@Test
public void checkSeqTokens() {
final Token seqToken = seq(DEFA, DEFB);
final Token seqToken = seq(DEF_A, DEF_B);
final Seq seq = (Seq)seqToken;
assertEquals(2, seq.tokens.size);
assertEquals(DEFA, seq.tokens.head);
assertEquals(DEFB, seq.tokens.tail.head);
assertEquals(DEF_A, seq.tokens.head);
assertEquals(DEF_B, seq.tokens.tail.head);
}

final ParseState PARSESTATE = createFromByteStream(DUMMY_STREAM).add(createParseValue("a", 126)).add(createParseValue("a", 84)).add(createParseValue("a", 42));
final ParseState PARSE_STATE = createFromByteStream(DUMMY_STREAM).add(createParseValue("a", 126)).add(createParseValue("a", 84)).add(createParseValue("a", 42));

@Test
public void mapLeftWithSub() {
ImmutableList<Optional<Value>> result = mapLeft(Shorthand::sub, ref("a"), con(2)).eval(PARSESTATE, enc());
ImmutableList<Optional<Value>> result = mapLeft(Shorthand::sub, ref("a"), con(2)).eval(PARSE_STATE, enc());
assertEquals(3, result.size);
for (int i = 0; i < 3; i++) {
assertTrue(result.head.isPresent());
Expand All @@ -216,7 +218,7 @@ public void mapLeftWithSub() {

@Test
public void mapRightWithSub() {
ImmutableList<Optional<Value>> result = mapRight(Shorthand::sub, con(126), ref("a")).eval(PARSESTATE, enc());
ImmutableList<Optional<Value>> result = mapRight(Shorthand::sub, con(126), ref("a")).eval(PARSE_STATE, enc());
assertEquals(3, result.size);
for (int i = 0; i < 3; i++) {
assertTrue(result.head.isPresent());
Expand All @@ -225,4 +227,23 @@ public void mapRightWithSub() {
}
}

@Test
public void whenTrue() {
Optional<ParseState> result = when(def("name", con(1), eq(con(1))), TRUE).parse(env(stream(1)));
assertTrue(result.isPresent());
assertEquals(1, result.get().offset.intValueExact());
assertEquals(1, Selection.getAllValues(result.get().order, parseValue -> parseValue.matches("name") && parseValue.getValue().length == 1 && parseValue.getValue()[0] == 1).size);
}

@Test
public void whenFalse() {
Optional<ParseState> result =
seq(
when(def("name1", con(1), eq(con(1))), not(TRUE)),
def("name2", con(1), eq(con(2)))).parse(env(stream(2)));
assertTrue(result.isPresent());
assertEquals(1, result.get().offset.intValueExact());
assertEquals(1, Selection.getAllValues(result.get().order, parseValue -> parseValue.matches("name2") && parseValue.getValue().length == 1 && parseValue.getValue()[0] == 2).size);
}

}
1 change: 0 additions & 1 deletion core/src/test/java/io/parsingdata/metal/ToStringTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import static io.parsingdata.metal.Shorthand.and;
import static io.parsingdata.metal.Shorthand.bytes;
import static io.parsingdata.metal.Shorthand.cat;
import static io.parsingdata.metal.Shorthand.cat;
import static io.parsingdata.metal.Shorthand.cho;
import static io.parsingdata.metal.Shorthand.con;
import static io.parsingdata.metal.Shorthand.count;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,9 @@

package io.parsingdata.metal.data;

import static java.math.BigInteger.ONE;
import static java.math.BigInteger.TEN;
import static java.math.BigInteger.ZERO;

import static org.junit.Assert.assertFalse;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

public class ConcatenatedValueSourceErrorTest {

Expand Down

0 comments on commit da7e817

Please sign in to comment.