Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into #400_immutable_link…
Browse files Browse the repository at this point in the history
…edlist

core/src/main/java/io/parsingdata/metal/expression/value/Scope.java
core/src/main/java/io/parsingdata/metal/expression/value/reference/Ref.java
core/src/test/java/io/parsingdata/metal/expression/value/ScopeTest.java
  • Loading branch information
mvanaken committed Mar 15, 2024
2 parents aca9c71 + 8f0b3eb commit 450f28c
Show file tree
Hide file tree
Showing 206 changed files with 699 additions and 401 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ A Java library for parsing binary data formats, using declarative descriptions.

## Using Metal

Metal releases are available in the central Maven repository. To use the latest (9.0.0) release of Metal, include the following section in the pom.xml under dependencies:
Metal releases are available in the central Maven repository. To use the latest (10.0.0) release of Metal, include the following section in the pom.xml under dependencies:

```xml
<dependency>
<groupId>io.parsingdata</groupId>
<artifactId>metal-core</artifactId>
<version>9.0.0</version>
<version>10.0.0</version>
</dependency>
```

Expand All @@ -37,8 +37,8 @@ Please read the [Authenticating to GitHub Packages](https://docs.github.com/en/p

## License

Copyright 2013-2023 Netherlands Forensic Institute
Copyright 2021-2023 Infix Technologies B.V.
Copyright 2013-2024 Netherlands Forensic Institute
Copyright 2021-2024 Infix Technologies B.V.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
3 changes: 2 additions & 1 deletion core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>io.parsingdata</groupId>
<artifactId>metal</artifactId>
<version>10.0.0-SNAPSHOT</version>
<version>11.0.0-SNAPSHOT</version>
</parent>
<artifactId>metal-core</artifactId>
<name>${project.groupId}:${project.artifactId}</name>
Expand All @@ -14,6 +14,7 @@
<connection>scm:git:git@github.com:parsingdata/metal.git</connection>
<developerConnection>scm:git:git@github.com:parsingdata/metal.git</developerConnection>
<url>https://github.com/parsingdata/metal.git</url>
<tag>HEAD</tag>
</scm>

<build>
Expand Down
10 changes: 7 additions & 3 deletions core/src/main/java/io/parsingdata/metal/Shorthand.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright 2013-2021 Netherlands Forensic Institute
* Copyright 2013-2024 Netherlands Forensic Institute
* Copyright 2021-2024 Infix Technologies B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -53,7 +54,6 @@
import io.parsingdata.metal.expression.value.FoldRight;
import io.parsingdata.metal.expression.value.Join;
import io.parsingdata.metal.expression.value.Reverse;
import io.parsingdata.metal.expression.value.Scope;
import io.parsingdata.metal.expression.value.SingleValueExpression;
import io.parsingdata.metal.expression.value.UnaryValueExpression;
import io.parsingdata.metal.expression.value.Value;
Expand Down Expand Up @@ -348,7 +348,11 @@ private Shorthand() {}
public static BinaryValueExpression mapRight(final BiFunction<ValueExpression, ValueExpression, BinaryValueExpression> func, final SingleValueExpression leftExpand, final ValueExpression right) { return func.apply(exp(leftExpand, count(right)), right); }

/** @see Bytes */ public static ValueExpression bytes(final ValueExpression operand) { return new Bytes(operand); }
/** @see Scope */ public static ValueExpression scope(final ValueExpression scopedValueExpression, final SingleValueExpression scopeSize) { return new Scope(scopedValueExpression, scopeSize); }

/** @see Ref */ public static NameRef scope(final NameRef operand) { return scope(operand, con(0)); }
/** @see Ref */ public static NameRef scope(final NameRef operand, final SingleValueExpression scopeSize) { return operand.withScope(scopeSize); }
/** @see Ref */ public static DefinitionRef scope(final DefinitionRef operand) { return scope(operand, con(0)); }
/** @see Ref */ public static DefinitionRef scope(final DefinitionRef operand, final SingleValueExpression scopeSize) { return operand.withScope(scopeSize); }

/** @see And */ public static BinaryLogicalExpression and(final Expression left, final Expression right) { return new And(left, right); }
/** @see Or */ public static BinaryLogicalExpression or(final Expression left, final Expression right) { return new Or(left, right); }
Expand Down
3 changes: 2 additions & 1 deletion core/src/main/java/io/parsingdata/metal/Trampoline.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright 2013-2021 Netherlands Forensic Institute
* Copyright 2013-2024 Netherlands Forensic Institute
* Copyright 2021-2024 Infix Technologies B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
3 changes: 2 additions & 1 deletion core/src/main/java/io/parsingdata/metal/Util.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright 2013-2021 Netherlands Forensic Institute
* Copyright 2013-2024 Netherlands Forensic Institute
* Copyright 2021-2024 Infix Technologies B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
3 changes: 2 additions & 1 deletion core/src/main/java/io/parsingdata/metal/data/ByteStream.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright 2013-2021 Netherlands Forensic Institute
* Copyright 2013-2024 Netherlands Forensic Institute
* Copyright 2021-2024 Infix Technologies B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright 2013-2021 Netherlands Forensic Institute
* Copyright 2013-2024 Netherlands Forensic Institute
* Copyright 2021-2024 Infix Technologies B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright 2013-2021 Netherlands Forensic Institute
* Copyright 2013-2024 Netherlands Forensic Institute
* Copyright 2021-2024 Infix Technologies B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright 2013-2021 Netherlands Forensic Institute
* Copyright 2013-2024 Netherlands Forensic Institute
* Copyright 2021-2024 Infix Technologies B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright 2013-2021 Netherlands Forensic Institute
* Copyright 2013-2024 Netherlands Forensic Institute
* Copyright 2021-2024 Infix Technologies B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright 2013-2021 Netherlands Forensic Institute
* Copyright 2013-2024 Netherlands Forensic Institute
* Copyright 2021-2024 Infix Technologies B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright 2013-2021 Netherlands Forensic Institute
* Copyright 2013-2024 Netherlands Forensic Institute
* Copyright 2021-2024 Infix Technologies B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
3 changes: 2 additions & 1 deletion core/src/main/java/io/parsingdata/metal/data/ParseGraph.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright 2013-2021 Netherlands Forensic Institute
* Copyright 2013-2024 Netherlands Forensic Institute
* Copyright 2021-2024 Infix Technologies B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
3 changes: 2 additions & 1 deletion core/src/main/java/io/parsingdata/metal/data/ParseItem.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright 2013-2021 Netherlands Forensic Institute
* Copyright 2013-2024 Netherlands Forensic Institute
* Copyright 2021-2024 Infix Technologies B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright 2013-2021 Netherlands Forensic Institute
* Copyright 2013-2024 Netherlands Forensic Institute
* Copyright 2021-2024 Infix Technologies B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
34 changes: 19 additions & 15 deletions core/src/main/java/io/parsingdata/metal/data/ParseState.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright 2013-2021 Netherlands Forensic Institute
* Copyright 2013-2024 Netherlands Forensic Institute
* Copyright 2021-2024 Infix Technologies B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -41,61 +42,63 @@ public class ParseState extends ImmutableObject {
public final Source source;
public final ImmutableList<ImmutablePair<Token, BigInteger>> iterations;
public final ImmutableList<ParseReference> references;
public final int scopeDepth;

public ParseState(final ParseGraph order, final ParseValueCache cache, final Source source, final BigInteger offset, final ImmutableList<ImmutablePair<Token, BigInteger>> iterations, final ImmutableList<ParseReference> references) {
public ParseState(final ParseGraph order, final ParseValueCache cache, final Source source, final BigInteger offset, final ImmutableList<ImmutablePair<Token, BigInteger>> iterations, final ImmutableList<ParseReference> references, final int scopeDepth) {
this.order = checkNotNull(order, "order");
this.cache = checkNotNull(cache, "cache");
this.source = checkNotNull(source, "source");
this.offset = checkNotNegative(offset, "offset");
this.iterations = checkNotNull(iterations, "iterations");
this.references = checkNotNull(references, "references");
this.scopeDepth = scopeDepth;
}

public static ParseState createFromByteStream(final ByteStream input, final BigInteger offset) {
return new ParseState(ParseGraph.EMPTY, new ParseValueCache(), new ByteStreamSource(input), offset, new ImmutableList<>(), new ImmutableList<>());
return new ParseState(ParseGraph.EMPTY, new ParseValueCache(), new ByteStreamSource(input), offset, new ImmutableList<>(), new ImmutableList<>(), 0);
}

public static ParseState createFromByteStream(final ByteStream input) {
return createFromByteStream(input, ZERO);
}

public ParseState addBranch(final Token token) {
return new ParseState(order.addBranch(token), cache, source, offset, token.isIterable() ? iterations.addHead(new ImmutablePair<>(token, ZERO)) : iterations, references);
return new ParseState(order.addBranch(token), cache, source, offset, token.isIterable() ? iterations.addHead(new ImmutablePair<>(token, ZERO)) : iterations, references, token.isScopeDelimiter() ? scopeDepth + 1 : scopeDepth);
}

public ParseState closeBranch(final Token token) {
if (token.isIterable() && !iterations.head().left.equals(token)) {
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(), cache, source, offset, token.isIterable() ? iterations.tail() : iterations, references);
return new ParseState(order.closeBranch(), cache, source, offset, token.isIterable() ? iterations.tail() : iterations, references, token.isScopeDelimiter() ? scopeDepth - 1 : scopeDepth);
}

public ParseState add(final ParseReference parseReference) {
return new ParseState(order, cache, source, offset, iterations, references.addHead(parseReference));
return new ParseState(order, cache, source, offset, iterations, references.addHead(parseReference), scopeDepth);
}

public ParseState add(final ParseValue parseValue) {
return new ParseState(order.add(parseValue), cache.add(parseValue), source, offset, iterations, references);
return new ParseState(order.add(parseValue), cache.add(parseValue), source, offset, iterations, references, scopeDepth);
}

public ParseState createCycle(final ParseReference parseReference) {
return new ParseState(order.add(parseReference), cache, source, offset, iterations, references);
return new ParseState(order.add(parseReference), cache, source, offset, iterations, references, scopeDepth);
}

public ParseState iterate() {
return new ParseState(order, cache, source, offset, iterations.tail().addHead(new ImmutablePair<>(iterations.head().left, iterations.head().right.add(ONE))), references);
return new ParseState(order, cache, source, offset, iterations.tail().addHead(new ImmutablePair<>(iterations.head().left, iterations.head().right.add(ONE))), references, scopeDepth);
}

public Optional<ParseState> seek(final BigInteger newOffset) {
return newOffset.compareTo(ZERO) >= 0 ? Optional.of(new ParseState(order, cache, source, newOffset, iterations, references)) : Optional.empty();
return newOffset.compareTo(ZERO) >= 0 ? Optional.of(new ParseState(order, cache, source, newOffset, iterations, references, scopeDepth)) : Optional.empty();
}

public ParseState withOrder(final ParseGraph order) {
return new ParseState(order, NO_CACHE, source, offset, iterations, references);
return new ParseState(order, NO_CACHE, source, offset, iterations, references, scopeDepth);
}

public ParseState withSource(final Source source) {
return new ParseState(order, cache, source, ZERO, iterations, references);
return new ParseState(order, cache, source, ZERO, iterations, references, scopeDepth);
}

public Optional<Slice> slice(final BigInteger length) {
Expand All @@ -106,7 +109,7 @@ public Optional<Slice> slice(final BigInteger length) {
public String toString() {
final String iterationsString = iterations.isEmpty() ? "" : ";iterations:" + iterations;
final String referencesString = references.isEmpty() ? "" : ";references:" + references;
return getClass().getSimpleName() + "(source:" + source + ";offset:" + offset + ";order:" + order + iterationsString + referencesString + ";" + cache + ")";
return getClass().getSimpleName() + "(source:" + source + ";offset:" + offset + ";order:" + order + iterationsString + referencesString + ";scopeDepth:" + scopeDepth + ";" + cache + ")";
}

@Override
Expand All @@ -117,12 +120,13 @@ public boolean equals(final Object obj) {
&& Objects.equals(offset, ((ParseState)obj).offset)
&& Objects.equals(source, ((ParseState)obj).source)
&& Objects.equals(iterations, ((ParseState)obj).iterations)
&& Objects.equals(references, ((ParseState)obj).references);
&& Objects.equals(references, ((ParseState)obj).references)
&& Objects.equals(scopeDepth, ((ParseState)obj).scopeDepth);
}

@Override
public int immutableHashCode() {
return Objects.hash(getClass(), order, cache, offset, source, iterations, references);
return Objects.hash(getClass(), order, cache, offset, source, iterations, references, scopeDepth);
}

}
3 changes: 2 additions & 1 deletion core/src/main/java/io/parsingdata/metal/data/ParseValue.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright 2013-2021 Netherlands Forensic Institute
* Copyright 2013-2024 Netherlands Forensic Institute
* Copyright 2021-2024 Infix Technologies B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
17 changes: 16 additions & 1 deletion core/src/main/java/io/parsingdata/metal/data/Selection.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright 2013-2021 Netherlands Forensic Institute
* Copyright 2013-2024 Netherlands Forensic Institute
* Copyright 2021-2024 Infix Technologies B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -89,6 +90,20 @@ public static ImmutableList<ParseValue> getAllValues(final ParseGraph graph, fin
return getAllValues(graph, predicate, NO_LIMIT);
}

public static ImmutableList<ParseValue> getAllValues(final ParseGraph graph, final Predicate<ParseValue> predicate, final int limit, final int requestedScope, final int currentScope) {
return getAllScopedValues(graph, predicate, limit, requestedScope, currentScope).computeResult();
}

private static Trampoline<ImmutableList<ParseValue>> getAllScopedValues(final ParseGraph graph, final Predicate<ParseValue> predicate, final int limit, final int requestedScope, final int currentScope) {
if (graph.isEmpty() || limit == 0) {
return complete(ImmutableList::new);
}
if (requestedScope >= currentScope) {
return complete(() -> getAllValues(graph, predicate, limit));
}
return intermediate(() -> getAllScopedValues(graph.head.asGraph(), predicate, limit, requestedScope, graph.head.getDefinition().isScopeDelimiter() ? currentScope - 1 : currentScope));
}

private static Trampoline<ImmutableList<ParseValue>> getAllValues(final ImmutableList<ParseGraph> graphList, final ImmutableList<ParseValue> valueList, final Predicate<ParseValue> predicate, final int limit) {
if (graphList.isEmpty() || (long) valueList.size() == limit) {
return complete(() -> valueList);
Expand Down
3 changes: 2 additions & 1 deletion core/src/main/java/io/parsingdata/metal/data/Slice.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright 2013-2021 Netherlands Forensic Institute
* Copyright 2013-2024 Netherlands Forensic Institute
* Copyright 2021-2024 Infix Technologies B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
3 changes: 2 additions & 1 deletion core/src/main/java/io/parsingdata/metal/data/Source.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright 2013-2021 Netherlands Forensic Institute
* Copyright 2013-2024 Netherlands Forensic Institute
* Copyright 2021-2024 Infix Technologies B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright 2013-2021 Netherlands Forensic Institute
* Copyright 2013-2024 Netherlands Forensic Institute
* Copyright 2021-2024 Infix Technologies B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright 2013-2021 Netherlands Forensic Institute
* Copyright 2013-2024 Netherlands Forensic Institute
* Copyright 2021-2024 Infix Technologies B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright 2013-2021 Netherlands Forensic Institute
* Copyright 2013-2024 Netherlands Forensic Institute
* Copyright 2021-2024 Infix Technologies B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright 2013-2021 Netherlands Forensic Institute
* Copyright 2013-2024 Netherlands Forensic Institute
* Copyright 2021-2024 Infix Technologies B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
3 changes: 2 additions & 1 deletion core/src/main/java/io/parsingdata/metal/encoding/Sign.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright 2013-2021 Netherlands Forensic Institute
* Copyright 2013-2024 Netherlands Forensic Institute
* Copyright 2021-2024 Infix Technologies B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright 2013-2021 Netherlands Forensic Institute
* Copyright 2013-2024 Netherlands Forensic Institute
* Copyright 2021-2024 Infix Technologies B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
3 changes: 2 additions & 1 deletion core/src/main/java/io/parsingdata/metal/expression/True.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright 2013-2021 Netherlands Forensic Institute
* Copyright 2013-2024 Netherlands Forensic Institute
* Copyright 2021-2024 Infix Technologies B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright 2013-2021 Netherlands Forensic Institute
* Copyright 2013-2024 Netherlands Forensic Institute
* Copyright 2021-2024 Infix Technologies B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Loading

0 comments on commit 450f28c

Please sign in to comment.