From c4b15aadf247303f17a25122fc98744451888500 Mon Sep 17 00:00:00 2001 From: Jeroen van den Bos Date: Fri, 14 Dec 2018 16:17:51 +0100 Subject: [PATCH 01/10] #272: Cleaned up Util and Ref classes. --- core/src/main/java/io/parsingdata/metal/Util.java | 10 +++++----- .../metal/expression/value/reference/Ref.java | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/core/src/main/java/io/parsingdata/metal/Util.java b/core/src/main/java/io/parsingdata/metal/Util.java index 0e0a8ed2..01754c42 100644 --- a/core/src/main/java/io/parsingdata/metal/Util.java +++ b/core/src/main/java/io/parsingdata/metal/Util.java @@ -33,13 +33,13 @@ public final class Util { - final private static char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray(); // Private because array content is mutable. + private static final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray(); // Private because array content is mutable. private Util() {} public static T checkNotNull(final T argument, final String name) { if (argument == null) { - throw new IllegalArgumentException("Argument " + name + " may not be null."); + throw new IllegalArgumentException(String.format("Argument %s may not be null.", name)); } return argument; } @@ -48,7 +48,7 @@ public static T[] checkContainsNoNulls(final T[] arguments, final String name checkNotNull(arguments, name); for (final T argument : arguments) { if (argument == null) { - throw new IllegalArgumentException("Value in array " + name + " may not be null."); + throw new IllegalArgumentException(String.format("Value in array %s may not be null.", name)); } } return arguments; @@ -56,7 +56,7 @@ public static T[] checkContainsNoNulls(final T[] arguments, final String name public static String checkNotEmpty(final String argument, final String name) { if (checkNotNull(argument, name).isEmpty()) { - throw new IllegalArgumentException("Argument " + name + " may not be empty."); + throw new IllegalArgumentException(String.format("Argument %s may not be empty.", name)); } return argument; } @@ -68,7 +68,7 @@ public static boolean notNullAndSameClass(final Object object, final Object othe public static BigInteger checkNotNegative(final BigInteger argument, final String name) { if (checkNotNull(argument, name).compareTo(ZERO) < 0) { - throw new IllegalArgumentException("Argument " + name + " may not be negative."); + throw new IllegalArgumentException(String.format("Argument %s may not be negative.", name)); } return argument; } diff --git a/core/src/main/java/io/parsingdata/metal/expression/value/reference/Ref.java b/core/src/main/java/io/parsingdata/metal/expression/value/reference/Ref.java index 3717787f..1e07ce05 100644 --- a/core/src/main/java/io/parsingdata/metal/expression/value/reference/Ref.java +++ b/core/src/main/java/io/parsingdata/metal/expression/value/reference/Ref.java @@ -59,12 +59,12 @@ private Ref(final T reference, final Predicate predicate, final Valu public static class NameRef extends Ref { public NameRef(final String reference) { this(reference, null); } - public NameRef(final String reference, final ValueExpression limit) { super(reference, (value) -> value.matches(reference), limit); } + public NameRef(final String reference, final ValueExpression limit) { super(reference, value -> value.matches(reference), limit); } } public static class DefinitionRef extends Ref { public DefinitionRef(final Token reference) { this(reference, null); } - public DefinitionRef(final Token reference, final ValueExpression limit) { super(reference, (value) -> value.definition.equals(reference), limit); } + public DefinitionRef(final Token reference, final ValueExpression limit) { super(reference, value -> value.definition.equals(reference), limit); } } @Override From fa9bb007627b02ea3f74c5ccd56164656f6d0db7 Mon Sep 17 00:00:00 2001 From: Jeroen van den Bos Date: Fri, 14 Dec 2018 16:19:44 +0100 Subject: [PATCH 02/10] #272: Removed OptionalValueTest, which existed to test our own implementation of Optional, but doesn't serve any purpose anymore. --- .../expression/value/OptionalValueTest.java | 59 ------------------- 1 file changed, 59 deletions(-) delete mode 100644 core/src/test/java/io/parsingdata/metal/expression/value/OptionalValueTest.java diff --git a/core/src/test/java/io/parsingdata/metal/expression/value/OptionalValueTest.java b/core/src/test/java/io/parsingdata/metal/expression/value/OptionalValueTest.java deleted file mode 100644 index ed0b8879..00000000 --- a/core/src/test/java/io/parsingdata/metal/expression/value/OptionalValueTest.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright 2013-2016 Netherlands Forensic Institute - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.parsingdata.metal.expression.value; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -import static io.parsingdata.metal.data.Slice.createFromBytes; -import static io.parsingdata.metal.util.EncodingFactory.enc; - -import java.util.NoSuchElementException; -import java.util.Optional; - -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; - -public class OptionalValueTest { - - private static final Value VALUE = new Value(createFromBytes(new byte[] { 1 }), enc()); - private static final Optional OPTIONAL_VALUE = Optional.of(VALUE); - private static final Optional EMPTY = Optional.empty(); - - @Rule - public final ExpectedException thrown = ExpectedException.none(); - - @Test - public void withValue() { - assertTrue(OPTIONAL_VALUE.isPresent()); - assertEquals("Optional[0x01]", OPTIONAL_VALUE.toString()); - assertEquals(OPTIONAL_VALUE.get(), VALUE); - } - - @Test - public void withoutValue() { - assertFalse(EMPTY.isPresent()); - assertEquals("Optional.empty", EMPTY.toString()); - - thrown.expectMessage("No value present"); - thrown.expect(NoSuchElementException.class); - EMPTY.get(); - } - -} \ No newline at end of file From 635c6f95c63b3ea41a051f3174506803bed4fec5 Mon Sep 17 00:00:00 2001 From: Jeroen van den Bos Date: Fri, 14 Dec 2018 16:25:17 +0100 Subject: [PATCH 03/10] #272: Reordered arguments in UntilTest unit tests. --- .../java/io/parsingdata/metal/token/UntilTest.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/core/src/test/java/io/parsingdata/metal/token/UntilTest.java b/core/src/test/java/io/parsingdata/metal/token/UntilTest.java index d6ea51ae..1f664609 100644 --- a/core/src/test/java/io/parsingdata/metal/token/UntilTest.java +++ b/core/src/test/java/io/parsingdata/metal/token/UntilTest.java @@ -59,9 +59,9 @@ public void threeNewLines() { assertTrue(parseState.isPresent()); ImmutableList values = getAllValues(parseState.get().order, "line"); assertEquals(3, values.size); - assertEquals(values.head.asString(), INPUT_3); - assertEquals(values.tail.head.asString(), INPUT_2); - assertEquals(values.tail.tail.head.asString(), INPUT_1); + assertEquals(INPUT_3, values.head.asString()); + assertEquals(INPUT_2, values.tail.head.asString()); + assertEquals(INPUT_1, values.tail.tail.head.asString()); } @Test @@ -70,9 +70,9 @@ public void untilInclusive() { assertTrue(parseState.isPresent()); ImmutableList values = getAllValues(parseState.get().order, "line"); assertEquals(3, values.size); - assertEquals(values.head.asString(), INPUT_3 + '\n'); - assertEquals(values.tail.head.asString(), INPUT_2 + '\n'); - assertEquals(values.tail.tail.head.asString(), INPUT_1 + '\n'); + assertEquals(INPUT_3 + '\n', values.head.asString()); + assertEquals(INPUT_2 + '\n', values.tail.head.asString()); + assertEquals(INPUT_1 + '\n', values.tail.tail.head.asString()); } @Test From 1d3d5c7a8faf58fa841ed587dc8354d20c9d5571 Mon Sep 17 00:00:00 2001 From: Jeroen van den Bos Date: Fri, 14 Dec 2018 16:39:22 +0100 Subject: [PATCH 04/10] #272: Refactored ParseState.source to ParseState.withSource and improved location of DataExpressionSource instatiation. --- .../src/main/java/io/parsingdata/metal/data/ParseState.java | 6 ++---- core/src/main/java/io/parsingdata/metal/token/Tie.java | 3 ++- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/io/parsingdata/metal/data/ParseState.java b/core/src/main/java/io/parsingdata/metal/data/ParseState.java index e394b4d4..7684f685 100644 --- a/core/src/main/java/io/parsingdata/metal/data/ParseState.java +++ b/core/src/main/java/io/parsingdata/metal/data/ParseState.java @@ -28,8 +28,6 @@ import java.util.Optional; import io.parsingdata.metal.Util; -import io.parsingdata.metal.encoding.Encoding; -import io.parsingdata.metal.expression.value.ValueExpression; import io.parsingdata.metal.token.Token; public class ParseState { @@ -81,8 +79,8 @@ public Optional seek(final BigInteger newOffset) { return newOffset.compareTo(ZERO) >= 0 ? Optional.of(new ParseState(order, source, newOffset, iterations)) : Optional.empty(); } - public ParseState source(final ValueExpression dataExpression, final int index, final ParseState parseState, final Encoding encoding) { - return new ParseState(order, new DataExpressionSource(dataExpression, index, parseState, encoding), ZERO, iterations); + public ParseState withSource(final Source source) { + return new ParseState(order, source, ZERO, iterations); } public Optional slice(final BigInteger length) { diff --git a/core/src/main/java/io/parsingdata/metal/token/Tie.java b/core/src/main/java/io/parsingdata/metal/token/Tie.java index 6a579426..8d649e87 100644 --- a/core/src/main/java/io/parsingdata/metal/token/Tie.java +++ b/core/src/main/java/io/parsingdata/metal/token/Tie.java @@ -27,6 +27,7 @@ import io.parsingdata.metal.Trampoline; import io.parsingdata.metal.Util; +import io.parsingdata.metal.data.DataExpressionSource; import io.parsingdata.metal.data.Environment; import io.parsingdata.metal.data.ImmutableList; import io.parsingdata.metal.data.ParseState; @@ -74,7 +75,7 @@ private Trampoline> iterate(final Environment environment, } return values.head .map(value -> token - .parse(environment.withParseState(environment.parseState.source(dataExpression, index, environment.parseState, environment.encoding))) + .parse(environment.withParseState(environment.parseState.withSource(new DataExpressionSource(dataExpression, index, environment.parseState, environment.encoding)))) .map(nextParseState -> intermediate(() -> iterate(environment.withParseState(nextParseState), values.tail, index + 1, returnParseState))) .orElseGet(() -> complete(Util::failure))) .orElseGet(() -> complete(Util::failure)); From 0e93cfa923a4caf1c1ef6026db92e32efeb6d55b Mon Sep 17 00:00:00 2001 From: Jeroen van den Bos Date: Mon, 17 Dec 2018 15:07:53 +0100 Subject: [PATCH 05/10] #272: Refactored all Def names with multiple uses to constants in example Token definitions. --- .../io/parsingdata/metal/format/JPEG.java | 29 ++++--- .../java/io/parsingdata/metal/format/PNG.java | 20 +++-- .../java/io/parsingdata/metal/format/ZIP.java | 77 +++++++++++-------- 3 files changed, 76 insertions(+), 50 deletions(-) diff --git a/formats/src/main/java/io/parsingdata/metal/format/JPEG.java b/formats/src/main/java/io/parsingdata/metal/format/JPEG.java index fde8b24d..ac44e829 100644 --- a/formats/src/main/java/io/parsingdata/metal/format/JPEG.java +++ b/formats/src/main/java/io/parsingdata/metal/format/JPEG.java @@ -39,31 +39,36 @@ */ public final class JPEG { + public static final String MARKER = "marker"; + public static final String IDENTIFIER = "identifier"; + public static final String LENGTH = "length"; + public static final String PAYLOAD = "payload"; + private JPEG() {} private static final Token HEADER = seq("start of image", - def("marker", con(1), eq(con(0xff))), - def("identifier", con(1), eq(con(0xd8)))); + def(MARKER, con(1), eq(con(0xff))), + def(IDENTIFIER, con(1), eq(con(0xd8)))); private static final Token FOOTER = seq("end of image", - def("marker", con(1), eq(con(0xff))), - def("identifier", con(1), eq(con(0xd9)))); + def(MARKER, con(1), eq(con(0xff))), + def(IDENTIFIER, con(1), eq(con(0xd9)))); private static final Token SIZED_SEGMENT = seq("sized segment", - def("marker", con(1), eq(con(0xff))), - def("identifier", con(1), or(ltNum(con(0xd8)), gtNum(con(0xda)))), - def("length", con(2)), - def("payload", sub(last(ref("length")), con(2)))); + def(MARKER, con(1), eq(con(0xff))), + def(IDENTIFIER, con(1), or(ltNum(con(0xd8)), gtNum(con(0xda)))), + def(LENGTH, con(2)), + def(PAYLOAD, sub(last(ref(LENGTH)), con(2)))); private static final Token SCAN_SEGMENT = seq("scan segment", - def("marker", con(1), eq(con(0xff))), - def("identifier", con(1), eq(con(0xda))), - def("length", con(2)), - def("payload", sub(last(ref("length")), con(2))), + def(MARKER, con(1), eq(con(0xff))), + def(IDENTIFIER, con(1), eq(con(0xda))), + def(LENGTH, con(2)), + def(PAYLOAD, sub(last(ref(LENGTH)), con(2))), rep(cho(def("scandata", con(1), not(eq(con(0xff)))), def("escape", con(2), or(eq(con(0xff00)), and(gtNum(con(0xffcf)), ltNum(con(0xffd8)))))))); diff --git a/formats/src/main/java/io/parsingdata/metal/format/PNG.java b/formats/src/main/java/io/parsingdata/metal/format/PNG.java index dc951fe0..30067def 100644 --- a/formats/src/main/java/io/parsingdata/metal/format/PNG.java +++ b/formats/src/main/java/io/parsingdata/metal/format/PNG.java @@ -33,6 +33,12 @@ public final class PNG { + public static final String LENGTH = "length"; + public static final String TYPE = "type"; + public static final String CRC_32 = "crc32"; + public static final String IEND = "IEND"; + public static final String DATA = "data"; + private PNG() {} private static final Token HEADER = @@ -43,16 +49,16 @@ private PNG() {} private static final Token FOOTER = seq("footer", - def("footerlength", con(4), eqNum(con(0))), - def("footertype", con(4), eq(con("IEND"))), - def("footercrc32", con(4), eq(con(0xae, 0x42, 0x60, 0x82)))); + def(LENGTH, con(4), eqNum(con(0))), + def(TYPE, con(4), eq(con(IEND))), + def(CRC_32, con(4), eq(con(0xae, 0x42, 0x60, 0x82)))); private static final Token STRUCT = seq("chunk", - def("length", con(4)), - def("chunktype", con(4), not(eq(con("IEND")))), - def("chunkdata", last(ref("length"))), - def("crc32", con(4), eq(crc32(cat(last(ref("chunktype")), last(ref("chunkdata"))))))); + def(LENGTH, con(4)), + def(TYPE, con(4), not(eq(con(IEND)))), + def(DATA, last(ref(LENGTH))), + def(CRC_32, con(4), eq(crc32(cat(last(ref(TYPE)), last(ref(DATA))))))); public static final Token FORMAT = seq("PNG", DEFAULT_ENCODING, diff --git a/formats/src/main/java/io/parsingdata/metal/format/ZIP.java b/formats/src/main/java/io/parsingdata/metal/format/ZIP.java index 9df67e6c..366fc903 100644 --- a/formats/src/main/java/io/parsingdata/metal/format/ZIP.java +++ b/formats/src/main/java/io/parsingdata/metal/format/ZIP.java @@ -46,38 +46,53 @@ */ public final class ZIP { + public static final String CRC_32 = "crc32"; + public static final String COMPRESSED_SIZE = "compressedsize"; + public static final String FILE_NAME_SIZE = "filenamesize"; + public static final String EXTRA_FIELD_SIZE = "extrafieldsize"; + public static final String EXTRACT_VERSION = "extractversion"; + public static final String BIT_FLAG = "bitflag"; + public static final String COMPRESSION_METHOD = "compressionmethod"; + public static final String LAST_MOD_TIME = "lastmodtime"; + public static final String LAST_MOD_DATE = "lastmoddate"; + public static final String UNCOMPRESSED_SIZE = "uncompressedsize"; + public static final String FILE_NAME = "filename"; + public static final String COMPRESSED_DATA = "compresseddata"; + public static final String DIR_SIGNATURE = "dirsignature"; + public static final String EXTRA_FIELD = "extrafield"; + private ZIP() {} private static Token localFileBody(final String name, final int cm, final Expression crc, final Expression cs, final Expression usp) { return seq(name, def("filesignature", con(4), eq(con(0x50, 0x4b, 0x03, 0x04))), - def("extractversion", con(2)), - def("bitflag", con(2)), - def("compressionmethod", con(2), eqNum(con(cm))), - def("lastmodtime", con(2)), - def("lastmoddate", con(2)), - def("crc32", con(4), crc), - def("compressedsize", con(4), cs), - def("uncompressedsize", con(4), usp), - def("filenamesize", con(2)), - def("extrafieldsize", con(2)), - def("filename", last(ref("filenamesize"))), - def("extrafield", last(ref("extrafieldsize")))); + def(EXTRACT_VERSION, con(2)), + def(BIT_FLAG, con(2)), + def(COMPRESSION_METHOD, con(2), eqNum(con(cm))), + def(LAST_MOD_TIME, con(2)), + def(LAST_MOD_DATE, con(2)), + def(CRC_32, con(4), crc), + def(COMPRESSED_SIZE, con(4), cs), + def(UNCOMPRESSED_SIZE, con(4), usp), + def(FILE_NAME_SIZE, con(2)), + def(EXTRA_FIELD_SIZE, con(2)), + def(FILE_NAME, last(ref(FILE_NAME_SIZE))), + def(EXTRA_FIELD, last(ref(EXTRA_FIELD_SIZE)))); } private static final Token LOCAL_DEFLATED_FILE = seq("localdeflatedfile", localFileBody("", 8, TRUE, TRUE, TRUE), - def("compresseddata", last(ref("compressedsize")), eqNum(crc32(inflate(SELF)), last(ref("crc32"))))); + def(COMPRESSED_DATA, last(ref(COMPRESSED_SIZE)), eqNum(crc32(inflate(SELF)), last(ref(CRC_32))))); private static final Token LOCAL_EMPTY_FILE = localFileBody("localemptyfile", 0, eqNum(con(0)), eqNum(con(0)), eqNum(con(0))); private static final Token LOCAL_STORED_FILE = seq("localstoredfile", - localFileBody("", 0, TRUE, TRUE, eq(last(ref("compressedsize")))), - def("compresseddata", last(ref("compressedsize")), eqNum(crc32(SELF), last(ref("crc32"))))); + localFileBody("", 0, TRUE, TRUE, eq(last(ref(COMPRESSED_SIZE)))), + def(COMPRESSED_DATA, last(ref(COMPRESSED_SIZE)), eqNum(crc32(SELF), last(ref(CRC_32))))); private static final Token FILES = rep("files", @@ -87,25 +102,25 @@ private static Token localFileBody(final String name, final int cm, final Expres private static final Token DIR_ENTRY = seq("direntry", - def("dirsignature", con(4), eq(con(0x50, 0x4b, 0x01, 0x02))), + def(DIR_SIGNATURE, con(4), eq(con(0x50, 0x4b, 0x01, 0x02))), def("makeversion", con(2)), - def("extractversion", con(2)), - def("bitflag", con(2)), - def("compressionmethod", con(2)), - def("lastmodtime", con(2)), - def("lastmoddate", con(2)), - def("crc32", con(4)), - def("compressedsize", con(4)), - def("uncompressedsize", con(4)), - def("filenamesize", con(2)), - def("extrafieldsize", con(2)), + def(EXTRACT_VERSION, con(2)), + def(BIT_FLAG, con(2)), + def(COMPRESSION_METHOD, con(2)), + def(LAST_MOD_TIME, con(2)), + def(LAST_MOD_DATE, con(2)), + def(CRC_32, con(4)), + def(COMPRESSED_SIZE, con(4)), + def(UNCOMPRESSED_SIZE, con(4)), + def(FILE_NAME_SIZE, con(2)), + def(EXTRA_FIELD_SIZE, con(2)), def("filecommentsize", con(2)), def("filedisk", con(2), eqNum(con(0))), def("intfileattr", con(2)), def("extfileattr", con(4)), def("offset", con(4)), - def("filename", last(ref("filenamesize"))), - def("extrafield", last(ref("extrafieldsize"))), + def(FILE_NAME, last(ref(FILE_NAME_SIZE))), + def(EXTRA_FIELD, last(ref(EXTRA_FIELD_SIZE))), def("filecomment", last(ref("filecommentsize")))); private static final Token DIRS = @@ -117,10 +132,10 @@ private static Token localFileBody(final String name, final int cm, final Expres def("endofdirsignature", con(4), eq(con(0x50, 0x4b, 0x05, 0x06))), def("disknumber", con(2), eqNum(con(0))), def("dirdisk", con(2), eqNum(con(0))), - def("numlocaldirs", con(2), eqNum(count(ref("dirsignature")))), + def("numlocaldirs", con(2), eqNum(count(ref(DIR_SIGNATURE)))), def("numtotaldirs", con(2), eqNum(last(ref("numlocaldirs")))), - def("dirsize", con(4), eqNum(sub(offset(last(ref("endofdirsignature"))), offset(first(ref("dirsignature")))))), - def("diroffset", con(4), eqNum(offset(first(ref("dirsignature"))))), + def("dirsize", con(4), eqNum(sub(offset(last(ref("endofdirsignature"))), offset(first(ref(DIR_SIGNATURE)))))), + def("diroffset", con(4), eqNum(offset(first(ref(DIR_SIGNATURE))))), def("commentsize", con(2)), def("comment", last(ref("commentsize")))); From 95b506634d63e790c2e06231f5007083bca08641 Mon Sep 17 00:00:00 2001 From: Jeroen van den Bos Date: Thu, 27 Dec 2018 12:01:54 +0100 Subject: [PATCH 06/10] #272: Resolved field shadowing. --- .../value/BinaryValueExpression.java | 2 +- .../metal/expression/value/Cat.java | 6 +-- .../metal/expression/value/Expand.java | 34 +++++++-------- .../metal/expression/value/Fold.java | 18 ++++---- .../metal/expression/value/FoldLeft.java | 4 +- .../metal/expression/value/FoldRight.java | 4 +- .../expression/value/arithmetic/Add.java | 4 +- .../expression/value/arithmetic/Div.java | 6 +-- .../expression/value/arithmetic/Mod.java | 6 +-- .../expression/value/arithmetic/Mul.java | 4 +- .../expression/value/arithmetic/Sub.java | 4 +- .../metal/expression/value/bitwise/And.java | 8 ++-- .../metal/expression/value/bitwise/Or.java | 8 ++-- .../expression/value/bitwise/ShiftLeft.java | 6 +-- .../expression/value/bitwise/ShiftRight.java | 8 ++-- .../value/reference/CurrentIteration.java | 18 ++++---- .../java/io/parsingdata/metal/token/Sub.java | 42 +++++++++---------- 17 files changed, 91 insertions(+), 91 deletions(-) diff --git a/core/src/main/java/io/parsingdata/metal/expression/value/BinaryValueExpression.java b/core/src/main/java/io/parsingdata/metal/expression/value/BinaryValueExpression.java index 35938341..a70c9338 100644 --- a/core/src/main/java/io/parsingdata/metal/expression/value/BinaryValueExpression.java +++ b/core/src/main/java/io/parsingdata/metal/expression/value/BinaryValueExpression.java @@ -61,7 +61,7 @@ public BinaryValueExpression(final ValueExpression left, final ValueExpression r this.right = checkNotNull(right, "right"); } - public abstract Optional eval(final Value left, final Value right, final ParseState parseState, final Encoding encoding); + public abstract Optional eval(final Value leftValue, final Value rightValue, final ParseState parseState, final Encoding encoding); @Override public ImmutableList> eval(final ParseState parseState, final Encoding encoding) { diff --git a/core/src/main/java/io/parsingdata/metal/expression/value/Cat.java b/core/src/main/java/io/parsingdata/metal/expression/value/Cat.java index 08dad19f..6bf9c574 100644 --- a/core/src/main/java/io/parsingdata/metal/expression/value/Cat.java +++ b/core/src/main/java/io/parsingdata/metal/expression/value/Cat.java @@ -37,9 +37,9 @@ public Cat(final ValueExpression left, final ValueExpression right) { } @Override - public Optional eval(final Value left, final Value right, final ParseState parseState, final Encoding encoding) { - return ConcatenatedValueSource.create(ImmutableList.create(Optional.of(left)).add(Optional.of(right))) - .flatMap(source -> createFromSource(source, ZERO, left.getLength().add(right.getLength()))) + public Optional eval(final Value leftValue, final Value rightValue, final ParseState parseState, final Encoding encoding) { + return ConcatenatedValueSource.create(ImmutableList.create(Optional.of(leftValue)).add(Optional.of(rightValue))) + .flatMap(source -> createFromSource(source, ZERO, leftValue.getLength().add(rightValue.getLength()))) .map(source -> new Value(source, encoding)); } diff --git a/core/src/main/java/io/parsingdata/metal/expression/value/Expand.java b/core/src/main/java/io/parsingdata/metal/expression/value/Expand.java index e50678c8..39a878a3 100644 --- a/core/src/main/java/io/parsingdata/metal/expression/value/Expand.java +++ b/core/src/main/java/io/parsingdata/metal/expression/value/Expand.java @@ -33,58 +33,58 @@ * A {@link ValueExpression} that expands a result by copying and concatenating * it a specified amount of times. *

- * An Expand expression has two operands: base and + * An Expand expression has two operands: bases and * count (both {@link ValueExpression}s). Both operands are * evaluated. An IllegalStateException is thrown if evaluating * count yields more than a single value. Multiple copies of the - * result of evaluating base are concatenated. The amount of copies + * result of evaluating bases are concatenated. The amount of copies * equals the result of evaluating count. */ public class Expand implements ValueExpression { - public final ValueExpression base; + public final ValueExpression bases; public final ValueExpression count; - public Expand(final ValueExpression base, final ValueExpression count) { - this.base = checkNotNull(base, "base"); + public Expand(final ValueExpression bases, final ValueExpression count) { + this.bases = checkNotNull(bases, "bases"); this.count = checkNotNull(count, "count"); } @Override public ImmutableList> eval(final ParseState parseState, final Encoding encoding) { - final ImmutableList> base = this.base.eval(parseState, encoding); - if (base.isEmpty()) { - return base; + final ImmutableList> baseList = bases.eval(parseState, encoding); + if (baseList.isEmpty()) { + return baseList; } - final ImmutableList> count = this.count.eval(parseState, encoding); - if (count.size != 1 || !count.head.isPresent()) { + final ImmutableList> countList = count.eval(parseState, encoding); + if (countList.size != 1 || !countList.head.isPresent()) { throw new IllegalArgumentException("Count must evaluate to a single non-empty value."); } - return expand(base, count.head.get().asNumeric().intValueExact(), new ImmutableList<>()).computeResult(); + return expand(baseList, countList.head.get().asNumeric().intValueExact(), new ImmutableList<>()).computeResult(); } - private Trampoline>> expand(final ImmutableList> base, final int count, final ImmutableList> aggregate) { - if (count < 1) { + private Trampoline>> expand(final ImmutableList> baseValues, final int countValue, final ImmutableList> aggregate) { + if (countValue < 1) { return complete(() -> aggregate); } - return intermediate(() -> expand(base, count - 1, aggregate.add(base))); + return intermediate(() -> expand(baseValues, countValue - 1, aggregate.add(baseValues))); } @Override public String toString() { - return getClass().getSimpleName() + "(" + base + "," + count + ")"; + return getClass().getSimpleName() + "(" + bases + "," + count + ")"; } @Override public boolean equals(final Object obj) { return Util.notNullAndSameClass(this, obj) - && Objects.equals(base, ((Expand)obj).base) + && Objects.equals(bases, ((Expand)obj).bases) && Objects.equals(count, ((Expand)obj).count); } @Override public int hashCode() { - return Objects.hash(getClass(), base, count); + return Objects.hash(getClass(), bases, count); } } diff --git a/core/src/main/java/io/parsingdata/metal/expression/value/Fold.java b/core/src/main/java/io/parsingdata/metal/expression/value/Fold.java index 08719fb4..b9440cd6 100644 --- a/core/src/main/java/io/parsingdata/metal/expression/value/Fold.java +++ b/core/src/main/java/io/parsingdata/metal/expression/value/Fold.java @@ -58,18 +58,18 @@ public Fold(final ValueExpression values, final BinaryOperator @Override public ImmutableList> eval(final ParseState parseState, final Encoding encoding) { - final ImmutableList> initial = this.initial != null ? this.initial.eval(parseState, encoding) : new ImmutableList<>(); - if (initial.size > 1) { + final ImmutableList> initialList = initial != null ? initial.eval(parseState, encoding) : new ImmutableList<>(); + if (initialList.size > 1) { return new ImmutableList<>(); } - final ImmutableList> values = prepareValues(this.values.eval(parseState, encoding)); - if (values.isEmpty() || containsEmpty(values).computeResult()) { - return initial; + final ImmutableList> valueList = prepareValues(this.values.eval(parseState, encoding)); + if (valueList.isEmpty() || containsEmpty(valueList).computeResult()) { + return initialList; } - if (!initial.isEmpty()) { - return ImmutableList.create(fold(parseState, encoding, reducer, initial.head, values).computeResult()); + if (!initialList.isEmpty()) { + return ImmutableList.create(fold(parseState, encoding, reducer, initialList.head, valueList).computeResult()); } - return ImmutableList.create(fold(parseState, encoding, reducer, values.head, values.tail).computeResult()); + return ImmutableList.create(fold(parseState, encoding, reducer, valueList.head, valueList.tail).computeResult()); } private Trampoline> fold(final ParseState parseState, final Encoding encoding, final BinaryOperator reducer, final Optional head, final ImmutableList> tail) { @@ -92,7 +92,7 @@ private Trampoline containsEmpty(final ImmutableList> l .orElseGet(() -> complete(() -> true)); } - protected abstract ImmutableList> prepareValues(ImmutableList> values); + protected abstract ImmutableList> prepareValues(ImmutableList> valueList); protected abstract ValueExpression reduce(BinaryOperator reducer, Value head, Value tail); diff --git a/core/src/main/java/io/parsingdata/metal/expression/value/FoldLeft.java b/core/src/main/java/io/parsingdata/metal/expression/value/FoldLeft.java index 61468b3f..ee27befb 100644 --- a/core/src/main/java/io/parsingdata/metal/expression/value/FoldLeft.java +++ b/core/src/main/java/io/parsingdata/metal/expression/value/FoldLeft.java @@ -39,8 +39,8 @@ public FoldLeft(final ValueExpression values, final BinaryOperator> prepareValues(final ImmutableList> values) { - return reverse(values); + protected ImmutableList> prepareValues(final ImmutableList> valueList) { + return reverse(valueList); } @Override diff --git a/core/src/main/java/io/parsingdata/metal/expression/value/FoldRight.java b/core/src/main/java/io/parsingdata/metal/expression/value/FoldRight.java index 9f87ac02..34b5008f 100644 --- a/core/src/main/java/io/parsingdata/metal/expression/value/FoldRight.java +++ b/core/src/main/java/io/parsingdata/metal/expression/value/FoldRight.java @@ -38,8 +38,8 @@ public FoldRight(final ValueExpression values, final BinaryOperator> prepareValues(final ImmutableList> values) { - return values; + protected ImmutableList> prepareValues(final ImmutableList> valueList) { + return valueList; } @Override diff --git a/core/src/main/java/io/parsingdata/metal/expression/value/arithmetic/Add.java b/core/src/main/java/io/parsingdata/metal/expression/value/arithmetic/Add.java index 09348c92..f5d667fb 100644 --- a/core/src/main/java/io/parsingdata/metal/expression/value/arithmetic/Add.java +++ b/core/src/main/java/io/parsingdata/metal/expression/value/arithmetic/Add.java @@ -35,8 +35,8 @@ public Add(final ValueExpression left, final ValueExpression right) { } @Override - public Optional eval(final Value left, final Value right, final ParseState parseState, final Encoding encoding) { - return Optional.of(ConstantFactory.createFromNumeric(left.asNumeric().add(right.asNumeric()), encoding)); + public Optional eval(final Value leftValue, final Value rightValue, final ParseState parseState, final Encoding encoding) { + return Optional.of(ConstantFactory.createFromNumeric(leftValue.asNumeric().add(rightValue.asNumeric()), encoding)); } } diff --git a/core/src/main/java/io/parsingdata/metal/expression/value/arithmetic/Div.java b/core/src/main/java/io/parsingdata/metal/expression/value/arithmetic/Div.java index 98967f6a..8d674a18 100644 --- a/core/src/main/java/io/parsingdata/metal/expression/value/arithmetic/Div.java +++ b/core/src/main/java/io/parsingdata/metal/expression/value/arithmetic/Div.java @@ -40,11 +40,11 @@ public Div(final ValueExpression left, final ValueExpression right) { } @Override - public Optional eval(final Value left, final Value right, final ParseState parseState, final Encoding encoding) { - if (right.asNumeric().equals(ZERO)) { + public Optional eval(final Value leftValue, final Value rightValue, final ParseState parseState, final Encoding encoding) { + if (rightValue.asNumeric().equals(ZERO)) { return Optional.empty(); } - return Optional.of(ConstantFactory.createFromNumeric(left.asNumeric().divide(right.asNumeric()), encoding)); + return Optional.of(ConstantFactory.createFromNumeric(leftValue.asNumeric().divide(rightValue.asNumeric()), encoding)); } } diff --git a/core/src/main/java/io/parsingdata/metal/expression/value/arithmetic/Mod.java b/core/src/main/java/io/parsingdata/metal/expression/value/arithmetic/Mod.java index a9bbac46..95d737e3 100644 --- a/core/src/main/java/io/parsingdata/metal/expression/value/arithmetic/Mod.java +++ b/core/src/main/java/io/parsingdata/metal/expression/value/arithmetic/Mod.java @@ -40,11 +40,11 @@ public Mod(final ValueExpression left, final ValueExpression right) { } @Override - public Optional eval(final Value left, final Value right, final ParseState parseState, final Encoding encoding) { - if (right.asNumeric().compareTo(ZERO) <= 0) { + public Optional eval(final Value leftValue, final Value rightValue, final ParseState parseState, final Encoding encoding) { + if (rightValue.asNumeric().compareTo(ZERO) <= 0) { return Optional.empty(); } - return Optional.of(ConstantFactory.createFromNumeric(left.asNumeric().mod(right.asNumeric()), encoding)); + return Optional.of(ConstantFactory.createFromNumeric(leftValue.asNumeric().mod(rightValue.asNumeric()), encoding)); } } diff --git a/core/src/main/java/io/parsingdata/metal/expression/value/arithmetic/Mul.java b/core/src/main/java/io/parsingdata/metal/expression/value/arithmetic/Mul.java index 86344701..65ac01f5 100644 --- a/core/src/main/java/io/parsingdata/metal/expression/value/arithmetic/Mul.java +++ b/core/src/main/java/io/parsingdata/metal/expression/value/arithmetic/Mul.java @@ -35,8 +35,8 @@ public Mul(final ValueExpression left, final ValueExpression right) { } @Override - public Optional eval(final Value left, final Value right, final ParseState parseState, final Encoding encoding) { - return Optional.of(ConstantFactory.createFromNumeric(left.asNumeric().multiply(right.asNumeric()), encoding)); + public Optional eval(final Value leftValue, final Value rightValue, final ParseState parseState, final Encoding encoding) { + return Optional.of(ConstantFactory.createFromNumeric(leftValue.asNumeric().multiply(rightValue.asNumeric()), encoding)); } } diff --git a/core/src/main/java/io/parsingdata/metal/expression/value/arithmetic/Sub.java b/core/src/main/java/io/parsingdata/metal/expression/value/arithmetic/Sub.java index faa636e6..68dc3fbf 100644 --- a/core/src/main/java/io/parsingdata/metal/expression/value/arithmetic/Sub.java +++ b/core/src/main/java/io/parsingdata/metal/expression/value/arithmetic/Sub.java @@ -35,8 +35,8 @@ public Sub(final ValueExpression left, final ValueExpression right) { } @Override - public Optional eval(final Value left, final Value right, final ParseState parseState, final Encoding encoding) { - return Optional.of(ConstantFactory.createFromNumeric(left.asNumeric().subtract(right.asNumeric()), encoding)); + public Optional eval(final Value leftValue, final Value rightValue, final ParseState parseState, final Encoding encoding) { + return Optional.of(ConstantFactory.createFromNumeric(leftValue.asNumeric().subtract(rightValue.asNumeric()), encoding)); } } diff --git a/core/src/main/java/io/parsingdata/metal/expression/value/bitwise/And.java b/core/src/main/java/io/parsingdata/metal/expression/value/bitwise/And.java index fa13706a..3186a593 100644 --- a/core/src/main/java/io/parsingdata/metal/expression/value/bitwise/And.java +++ b/core/src/main/java/io/parsingdata/metal/expression/value/bitwise/And.java @@ -36,10 +36,10 @@ public And(final ValueExpression left, final ValueExpression right) { } @Override - public Optional eval(final Value left, final Value right, final ParseState parseState, final Encoding encoding) { - final BitSet leftBits = left.asBitSet(); - leftBits.and(right.asBitSet()); - return Optional.of(ConstantFactory.createFromBitSet(leftBits, left.getValue().length, encoding)); + public Optional eval(final Value leftValue, final Value rightValue, final ParseState parseState, final Encoding encoding) { + final BitSet leftBits = leftValue.asBitSet(); + leftBits.and(rightValue.asBitSet()); + return Optional.of(ConstantFactory.createFromBitSet(leftBits, leftValue.getValue().length, encoding)); } } diff --git a/core/src/main/java/io/parsingdata/metal/expression/value/bitwise/Or.java b/core/src/main/java/io/parsingdata/metal/expression/value/bitwise/Or.java index bda70e2e..fa292b6a 100644 --- a/core/src/main/java/io/parsingdata/metal/expression/value/bitwise/Or.java +++ b/core/src/main/java/io/parsingdata/metal/expression/value/bitwise/Or.java @@ -36,10 +36,10 @@ public Or(final ValueExpression left, final ValueExpression right) { } @Override - public Optional eval(final Value left, final Value right, final ParseState parseState, final Encoding encoding) { - final BitSet leftBits = left.asBitSet(); - leftBits.or(right.asBitSet()); - final int minSize = Math.max(left.getValue().length, right.getValue().length); + public Optional eval(final Value leftValue, final Value rightValue, final ParseState parseState, final Encoding encoding) { + final BitSet leftBits = leftValue.asBitSet(); + leftBits.or(rightValue.asBitSet()); + final int minSize = Math.max(leftValue.getValue().length, rightValue.getValue().length); return Optional.of(ConstantFactory.createFromBitSet(leftBits, minSize, encoding)); } diff --git a/core/src/main/java/io/parsingdata/metal/expression/value/bitwise/ShiftLeft.java b/core/src/main/java/io/parsingdata/metal/expression/value/bitwise/ShiftLeft.java index 418a44aa..9abb2533 100644 --- a/core/src/main/java/io/parsingdata/metal/expression/value/bitwise/ShiftLeft.java +++ b/core/src/main/java/io/parsingdata/metal/expression/value/bitwise/ShiftLeft.java @@ -37,9 +37,9 @@ public ShiftLeft(final ValueExpression operand, final ValueExpression positions) } @Override - public Optional eval(final Value operand, final Value positions, final ParseState parseState, final Encoding encoding) { - final BitSet leftBits = operand.asBitSet(); - final int shiftLeft = positions.asNumeric().intValueExact(); + public Optional eval(final Value leftValue, final Value rightValue, final ParseState parseState, final Encoding encoding) { + final BitSet leftBits = leftValue.asBitSet(); + final int shiftLeft = rightValue.asNumeric().intValueExact(); final int bitCount = leftBits.length() + shiftLeft; final BitSet out = new BitSet(bitCount); for (int i = leftBits.nextSetBit(0); i >= 0; i = leftBits.nextSetBit(i+1)) { diff --git a/core/src/main/java/io/parsingdata/metal/expression/value/bitwise/ShiftRight.java b/core/src/main/java/io/parsingdata/metal/expression/value/bitwise/ShiftRight.java index 8e8faf59..0cfb8574 100644 --- a/core/src/main/java/io/parsingdata/metal/expression/value/bitwise/ShiftRight.java +++ b/core/src/main/java/io/parsingdata/metal/expression/value/bitwise/ShiftRight.java @@ -37,10 +37,10 @@ public ShiftRight(final ValueExpression operand, final ValueExpression positions } @Override - public Optional eval(final Value operand, final Value positions, final ParseState parseState, final Encoding encoding) { - final BitSet leftBits = operand.asBitSet(); - final int shift = positions.asNumeric().intValueExact(); - return Optional.of(ConstantFactory.createFromBitSet(leftBits.get(shift, Math.max(shift, leftBits.length())), operand.getValue().length, encoding)); + public Optional eval(final Value leftValue, final Value rightValue, final ParseState parseState, final Encoding encoding) { + final BitSet leftBits = leftValue.asBitSet(); + final int shift = rightValue.asNumeric().intValueExact(); + return Optional.of(ConstantFactory.createFromBitSet(leftBits.get(shift, Math.max(shift, leftBits.length())), leftValue.getValue().length, encoding)); } } diff --git a/core/src/main/java/io/parsingdata/metal/expression/value/reference/CurrentIteration.java b/core/src/main/java/io/parsingdata/metal/expression/value/reference/CurrentIteration.java index e16fe733..2aa05c1a 100644 --- a/core/src/main/java/io/parsingdata/metal/expression/value/reference/CurrentIteration.java +++ b/core/src/main/java/io/parsingdata/metal/expression/value/reference/CurrentIteration.java @@ -61,26 +61,26 @@ public ImmutableList> eval(final ParseState parseState, final En } private Optional getIteration(final ParseState parseState, final Encoding encoding) { - final BigInteger level = getLevel(parseState, encoding); - if (parseState.iterations.size <= level.longValue()) { + final BigInteger levelValue = getLevel(parseState, encoding); + if (parseState.iterations.size <= levelValue.longValue()) { return Optional.empty(); } - return getIterationRecursive(parseState.iterations, level).computeResult(); + return getIterationRecursive(parseState.iterations, levelValue).computeResult(); } private BigInteger getLevel(final ParseState parseState, final Encoding encoding) { - final ImmutableList> evaluatedLevel = level.eval(parseState, encoding); - if (evaluatedLevel.size != 1 || !evaluatedLevel.head.isPresent()) { + final ImmutableList> levelList = level.eval(parseState, encoding); + if (levelList.size != 1 || !levelList.head.isPresent()) { throw new IllegalArgumentException("Level must evaluate to a single non-empty value."); } - return evaluatedLevel.head.get().asNumeric(); + return levelList.head.get().asNumeric(); } - private Trampoline> getIterationRecursive(final ImmutableList> iterations, final BigInteger level) { - if (level.compareTo(ZERO) == 0) { + private Trampoline> getIterationRecursive(final ImmutableList> iterations, final BigInteger levelValue) { + if (levelValue.compareTo(ZERO) == 0) { return complete(() -> Optional.of(createFromNumeric(iterations.head.right, DEFAULT_ENCODING))); } - return intermediate(() -> getIterationRecursive(iterations.tail, level.subtract(ONE))); + return intermediate(() -> getIterationRecursive(iterations.tail, levelValue.subtract(ONE))); } @Override diff --git a/core/src/main/java/io/parsingdata/metal/token/Sub.java b/core/src/main/java/io/parsingdata/metal/token/Sub.java index 921efc23..707b2a04 100644 --- a/core/src/main/java/io/parsingdata/metal/token/Sub.java +++ b/core/src/main/java/io/parsingdata/metal/token/Sub.java @@ -42,11 +42,11 @@ * in the input. *

* A Sub consists of a token (a {@link Token}) and an - * address (a {@link ValueExpression}). First - * address is evaluated. Then each resulting value is used as a + * offsets (a {@link ValueExpression}). First + * offsets is evaluated. Then each resulting value is used as a * location in the input to parse token at. Sub succeeds if all * parses of token at all locations succeed. Sub fails if - * address evaluates to a list of locations that is either empty + * offsets evaluates to a list of locations that is either empty * or contains an invalid value. * * @see ValueExpression @@ -54,41 +54,41 @@ public class Sub extends Token { public final Token token; - public final ValueExpression address; + public final ValueExpression offsets; - public Sub(final String name, final Token token, final ValueExpression address, final Encoding encoding) { + public Sub(final String name, final Token token, final ValueExpression offsets, final Encoding encoding) { super(name, encoding); this.token = checkNotNull(token, "token"); - this.address = checkNotNull(address, "address"); + this.offsets = checkNotNull(offsets, "offsets"); } @Override protected Optional parseImpl(final Environment environment) { - final ImmutableList> addresses = address.eval(environment.parseState, environment.encoding); - if (addresses.isEmpty()) { + final ImmutableList> offsetList = offsets.eval(environment.parseState, environment.encoding); + if (offsetList.isEmpty()) { return failure(); } - return iterate(environment.addBranch(this), addresses) + return iterate(environment.addBranch(this), offsetList) .computeResult() .flatMap(nextParseState -> nextParseState.seek(environment.parseState.offset)); } - private Trampoline> iterate(final Environment environment, final ImmutableList> addresses) { - if (addresses.isEmpty()) { + private Trampoline> iterate(final Environment environment, final ImmutableList> offsetList) { + if (offsetList.isEmpty()) { return complete(() -> success(environment.parseState.closeBranch(this))); } - return addresses.head - .flatMap(address -> parse(environment, address.asNumeric())) - .map(nextParseState -> intermediate(() -> iterate(environment.withParseState(nextParseState), addresses.tail))) + return offsetList.head + .flatMap(offsetValue -> parse(environment, offsetValue.asNumeric())) + .map(nextParseState -> intermediate(() -> iterate(environment.withParseState(nextParseState), offsetList.tail))) .orElseGet(() -> complete(Util::failure)); } - private Optional parse(final Environment environment, final BigInteger offset) { - if (hasRootAtOffset(environment.parseState.order, token.getCanonical(environment.parseState), offset, environment.parseState.source)) { - return success(environment.parseState.add(new ParseReference(offset, environment.parseState.source, token.getCanonical(environment.parseState)))); + private Optional parse(final Environment environment, final BigInteger offsetValue) { + if (hasRootAtOffset(environment.parseState.order, token.getCanonical(environment.parseState), offsetValue, environment.parseState.source)) { + return success(environment.parseState.add(new ParseReference(offsetValue, environment.parseState.source, token.getCanonical(environment.parseState)))); } return environment.parseState - .seek(offset) + .seek(offsetValue) .map(newParseState -> token.parse(environment.withParseState(newParseState))) .orElseGet(Util::failure); } @@ -100,19 +100,19 @@ public boolean isLocal() { @Override public String toString() { - return getClass().getSimpleName() + "(" + makeNameFragment() + token + "," + address + ")"; + return getClass().getSimpleName() + "(" + makeNameFragment() + token + "," + offsets + ")"; } @Override public boolean equals(final Object obj) { return super.equals(obj) && Objects.equals(token, ((Sub)obj).token) - && Objects.equals(address, ((Sub)obj).address); + && Objects.equals(offsets, ((Sub)obj).offsets); } @Override public int hashCode() { - return Objects.hash(super.hashCode(), token, address); + return Objects.hash(super.hashCode(), token, offsets); } } From ace5cd4f5d844313dbb7ee0c1cb2ea2a6980cabd Mon Sep 17 00:00:00 2001 From: Jeroen van den Bos Date: Thu, 27 Dec 2018 12:39:28 +0100 Subject: [PATCH 07/10] #272: Replaced other concatenated strings in exception constructors with format strings as well. Included helper method to automatically use Locale.ENGLISH. --- core/src/main/java/io/parsingdata/metal/Util.java | 5 +++++ .../java/io/parsingdata/metal/data/ByteStreamSource.java | 3 ++- .../io/parsingdata/metal/data/ConcatenatedValueSource.java | 3 ++- .../java/io/parsingdata/metal/data/ConstantSource.java | 3 ++- .../io/parsingdata/metal/data/DataExpressionSource.java | 7 ++++--- .../java/io/parsingdata/metal/expression/value/GUID.java | 3 ++- 6 files changed, 17 insertions(+), 7 deletions(-) diff --git a/core/src/main/java/io/parsingdata/metal/Util.java b/core/src/main/java/io/parsingdata/metal/Util.java index 01754c42..33f7c8cf 100644 --- a/core/src/main/java/io/parsingdata/metal/Util.java +++ b/core/src/main/java/io/parsingdata/metal/Util.java @@ -20,6 +20,7 @@ import java.io.ByteArrayOutputStream; import java.math.BigInteger; +import java.util.Locale; import java.util.Optional; import java.util.zip.DataFormatException; import java.util.zip.Inflater; @@ -73,6 +74,10 @@ public static BigInteger checkNotNegative(final BigInteger argument, final Strin return argument; } + public static String format(final String format, Object... args) { + return String.format(Locale.ENGLISH, format, args); + } + public static String bytesToHexString(final byte[] bytes) { checkNotNull(bytes, "bytes"); char[] hexChars = new char[bytes.length * 2]; diff --git a/core/src/main/java/io/parsingdata/metal/data/ByteStreamSource.java b/core/src/main/java/io/parsingdata/metal/data/ByteStreamSource.java index 6f5fc27c..5260b6b8 100644 --- a/core/src/main/java/io/parsingdata/metal/data/ByteStreamSource.java +++ b/core/src/main/java/io/parsingdata/metal/data/ByteStreamSource.java @@ -18,6 +18,7 @@ import static io.parsingdata.metal.Util.checkNotNegative; import static io.parsingdata.metal.Util.checkNotNull; +import static io.parsingdata.metal.Util.format; import java.io.IOException; import java.io.UncheckedIOException; @@ -37,7 +38,7 @@ public class ByteStreamSource extends Source { @Override protected byte[] getData(final BigInteger offset, final BigInteger length) { if (!isAvailable(offset, length)) { - throw new IllegalStateException("Data to read is not available ([offset=" + offset + ";length=" + length + ";source=" + this + ")."); + throw new IllegalStateException(format("Data to read is not available ([offset=%d;length=%d;source=%s).", offset, length, this)); } try { return input.read(offset, length.intValueExact()); diff --git a/core/src/main/java/io/parsingdata/metal/data/ConcatenatedValueSource.java b/core/src/main/java/io/parsingdata/metal/data/ConcatenatedValueSource.java index dd070f08..ae220bfd 100644 --- a/core/src/main/java/io/parsingdata/metal/data/ConcatenatedValueSource.java +++ b/core/src/main/java/io/parsingdata/metal/data/ConcatenatedValueSource.java @@ -22,6 +22,7 @@ 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.Util.format; import java.math.BigInteger; import java.util.Objects; @@ -73,7 +74,7 @@ private static Trampoline calculateTotalSize(final ImmutableList 0) { - throw new IllegalStateException("Data to read is not available ([offset=" + offset + ";length=" + length + ";source=" + this + ")."); + throw new IllegalStateException(format("Data to read is not available ([offset=%d;length=%d;source=%s).", offset, length, this)); } final byte[] outputData = new byte[length.intValueExact()]; System.arraycopy(data, offset.intValueExact(), outputData, 0, outputData.length); @@ -68,12 +69,12 @@ private synchronized byte[] getValue() { if (cache == null) { final ImmutableList> results = dataExpression.eval(parseState, encoding); if (results.size <= index) { - throw new IllegalStateException("ValueExpression dataExpression yields " + results.size + " result(s) (expected at least " + (index + 1) + ")."); + throw new IllegalStateException(format("ValueExpression dataExpression yields %d result(s) (expected at least %d).", results.size, index+1)); } cache = getValueAtIndex(results, index, 0) .computeResult() .map(Value::getValue) - .orElseThrow(() -> new IllegalStateException("ValueExpression dataExpression yields empty Value at index " + index + ".")); + .orElseThrow(() -> new IllegalStateException(format("ValueExpression dataExpression yields empty Value at index %d.", index))); } return cache; } diff --git a/formats/src/main/java/io/parsingdata/metal/expression/value/GUID.java b/formats/src/main/java/io/parsingdata/metal/expression/value/GUID.java index 05002981..531f9deb 100644 --- a/formats/src/main/java/io/parsingdata/metal/expression/value/GUID.java +++ b/formats/src/main/java/io/parsingdata/metal/expression/value/GUID.java @@ -23,6 +23,7 @@ import static io.parsingdata.metal.Shorthand.cat; import static io.parsingdata.metal.Shorthand.con; import static io.parsingdata.metal.Util.checkNotNull; +import static io.parsingdata.metal.Util.format; import static io.parsingdata.metal.encoding.Encoding.DEFAULT_ENCODING; import java.util.Arrays; @@ -48,7 +49,7 @@ private GUID() {} public static ValueExpression guid(final String guid) { final String[] parts = checkNotNull(guid, "guid").split("-", -1); if (parts.length != 5) { - throw new IllegalArgumentException("Invalid GUID string: " + guid); + throw new IllegalArgumentException(format("Invalid GUID string: %s", guid)); } return (parseState, encoding) -> // Note that GUID bytes differ from UUID bytes, as the first 3 parts can be reversed From f1d66e2f24e4d3b7fb16efb593242364be6659c2 Mon Sep 17 00:00:00 2001 From: Jeroen van den Bos Date: Thu, 27 Dec 2018 12:47:35 +0100 Subject: [PATCH 08/10] #272: All String.format() invocations now use Util.format(). --- core/src/main/java/io/parsingdata/metal/Util.java | 8 ++++---- .../main/java/io/parsingdata/metal/data/ParseState.java | 3 ++- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/io/parsingdata/metal/Util.java b/core/src/main/java/io/parsingdata/metal/Util.java index 33f7c8cf..bc3b3430 100644 --- a/core/src/main/java/io/parsingdata/metal/Util.java +++ b/core/src/main/java/io/parsingdata/metal/Util.java @@ -40,7 +40,7 @@ private Util() {} public static T checkNotNull(final T argument, final String name) { if (argument == null) { - throw new IllegalArgumentException(String.format("Argument %s may not be null.", name)); + throw new IllegalArgumentException(format("Argument %s may not be null.", name)); } return argument; } @@ -49,7 +49,7 @@ public static T[] checkContainsNoNulls(final T[] arguments, final String name checkNotNull(arguments, name); for (final T argument : arguments) { if (argument == null) { - throw new IllegalArgumentException(String.format("Value in array %s may not be null.", name)); + throw new IllegalArgumentException(format("Value in array %s may not be null.", name)); } } return arguments; @@ -57,7 +57,7 @@ public static T[] checkContainsNoNulls(final T[] arguments, final String name public static String checkNotEmpty(final String argument, final String name) { if (checkNotNull(argument, name).isEmpty()) { - throw new IllegalArgumentException(String.format("Argument %s may not be empty.", name)); + throw new IllegalArgumentException(format("Argument %s may not be empty.", name)); } return argument; } @@ -69,7 +69,7 @@ public static boolean notNullAndSameClass(final Object object, final Object othe public static BigInteger checkNotNegative(final BigInteger argument, final String name) { if (checkNotNull(argument, name).compareTo(ZERO) < 0) { - throw new IllegalArgumentException(String.format("Argument %s may not be negative.", name)); + throw new IllegalArgumentException(format("Argument %s may not be negative.", name)); } return argument; } diff --git a/core/src/main/java/io/parsingdata/metal/data/ParseState.java b/core/src/main/java/io/parsingdata/metal/data/ParseState.java index 7684f685..a12d3fdd 100644 --- a/core/src/main/java/io/parsingdata/metal/data/ParseState.java +++ b/core/src/main/java/io/parsingdata/metal/data/ParseState.java @@ -21,6 +21,7 @@ import static io.parsingdata.metal.Util.checkNotNegative; import static io.parsingdata.metal.Util.checkNotNull; +import static io.parsingdata.metal.Util.format; import static io.parsingdata.metal.data.Slice.createFromSource; import java.math.BigInteger; @@ -58,7 +59,7 @@ public ParseState addBranch(final Token token) { public ParseState closeBranch(final Token token) { if (token.isIterable() && !iterations.head.left.equals(token)) { - throw new IllegalStateException(String.format("Cannot close branch for iterable token %s. Current iteration state is for token %s.", token.name, iterations.head.left.name)); + throw new IllegalStateException(format("Cannot close branch for iterable token %s. Current iteration state is for token %s.", token.name, iterations.head.left.name)); } return new ParseState(order.closeBranch(), source, offset, token.isIterable() ? iterations.tail : iterations); } From d45666fc298d7b9672219138036546bd459cd14d Mon Sep 17 00:00:00 2001 From: Jeroen van den Bos Date: Thu, 27 Dec 2018 21:04:00 +0100 Subject: [PATCH 09/10] #272: Final nitpick to unify the name changes. --- .../java/io/parsingdata/metal/expression/value/Expand.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/io/parsingdata/metal/expression/value/Expand.java b/core/src/main/java/io/parsingdata/metal/expression/value/Expand.java index 39a878a3..4be82c5d 100644 --- a/core/src/main/java/io/parsingdata/metal/expression/value/Expand.java +++ b/core/src/main/java/io/parsingdata/metal/expression/value/Expand.java @@ -63,11 +63,11 @@ public ImmutableList> eval(final ParseState parseState, final En return expand(baseList, countList.head.get().asNumeric().intValueExact(), new ImmutableList<>()).computeResult(); } - private Trampoline>> expand(final ImmutableList> baseValues, final int countValue, final ImmutableList> aggregate) { + private Trampoline>> expand(final ImmutableList> baseList, final int countValue, final ImmutableList> aggregate) { if (countValue < 1) { return complete(() -> aggregate); } - return intermediate(() -> expand(baseValues, countValue - 1, aggregate.add(baseValues))); + return intermediate(() -> expand(baseList, countValue - 1, aggregate.add(baseList))); } @Override From 9671985b67519eae900cdb145c4e333003d6c366 Mon Sep 17 00:00:00 2001 From: Jeroen van den Bos Date: Thu, 27 Dec 2018 21:55:37 +0100 Subject: [PATCH 10/10] #272: Missed a final... --- core/src/main/java/io/parsingdata/metal/Util.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/main/java/io/parsingdata/metal/Util.java b/core/src/main/java/io/parsingdata/metal/Util.java index bc3b3430..96f4728a 100644 --- a/core/src/main/java/io/parsingdata/metal/Util.java +++ b/core/src/main/java/io/parsingdata/metal/Util.java @@ -74,7 +74,7 @@ public static BigInteger checkNotNegative(final BigInteger argument, final Strin return argument; } - public static String format(final String format, Object... args) { + public static String format(final String format, final Object... args) { return String.format(Locale.ENGLISH, format, args); }