-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #242 from parsingdata/current-index
Resolves #236. Add support to allow access to value of current iteration inside iterable tokens (e.g., Rep, RepN and While).
- Loading branch information
Showing
20 changed files
with
389 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
core/src/main/java/io/parsingdata/metal/data/ImmutablePair.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* 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.data; | ||
|
||
import static io.parsingdata.metal.Util.checkNotNull; | ||
|
||
import java.util.Objects; | ||
|
||
import io.parsingdata.metal.Util; | ||
|
||
public class ImmutablePair<L, R> { | ||
|
||
public final L left; | ||
public final R right; | ||
|
||
public ImmutablePair(final L left, final R right) { | ||
this.left = checkNotNull(left, "left"); | ||
this.right = checkNotNull(right, "right"); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return left + "=" + right; | ||
} | ||
|
||
@Override | ||
public boolean equals(final Object obj) { | ||
return Util.notNullAndSameClass(this, obj) | ||
&& Objects.equals(left, ((ImmutablePair)obj).left) | ||
&& Objects.equals(right, ((ImmutablePair)obj).right); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(getClass(), left, right); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
core/src/main/java/io/parsingdata/metal/expression/value/reference/CurrentIteration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Copyright 2013-2018 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.reference; | ||
|
||
import static io.parsingdata.metal.expression.value.ConstantFactory.createFromNumeric; | ||
|
||
import java.util.Optional; | ||
|
||
import io.parsingdata.metal.Util; | ||
import io.parsingdata.metal.data.ImmutableList; | ||
import io.parsingdata.metal.data.ParseState; | ||
import io.parsingdata.metal.encoding.Encoding; | ||
import io.parsingdata.metal.expression.value.Value; | ||
import io.parsingdata.metal.expression.value.ValueExpression; | ||
import io.parsingdata.metal.token.Rep; | ||
import io.parsingdata.metal.token.RepN; | ||
import io.parsingdata.metal.token.Token; | ||
import io.parsingdata.metal.token.While; | ||
|
||
/** | ||
* A {@link ValueExpression} that represents the 0-based current iteration in an | ||
* iterable {@link Token} (when {@link Token#isIterable()} returns true, e.g. when | ||
* inside a {@link Rep}, {@link RepN}) or {@link While}). | ||
*/ | ||
public class CurrentIteration implements ValueExpression { | ||
|
||
@Override | ||
public ImmutableList<Optional<Value>> eval(final ParseState parseState, final Encoding encoding) { | ||
if (parseState.iterations.isEmpty()) { | ||
return ImmutableList.create(Optional.empty()); | ||
} | ||
return ImmutableList.create(Optional.of(createFromNumeric(parseState.iterations.head.right, new Encoding()))); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return getClass().getSimpleName(); | ||
} | ||
|
||
@Override | ||
public boolean equals(final Object obj) { | ||
return Util.notNullAndSameClass(this, obj); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return getClass().hashCode(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
core/src/main/java/io/parsingdata/metal/token/IterableToken.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* Copyright 2013-2018 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.token; | ||
|
||
import static io.parsingdata.metal.Trampoline.complete; | ||
import static io.parsingdata.metal.Trampoline.intermediate; | ||
import static io.parsingdata.metal.Util.checkNotNull; | ||
import static io.parsingdata.metal.Util.success; | ||
|
||
import java.util.Objects; | ||
import java.util.Optional; | ||
import java.util.function.Function; | ||
import java.util.function.Predicate; | ||
|
||
import io.parsingdata.metal.Trampoline; | ||
import io.parsingdata.metal.data.Environment; | ||
import io.parsingdata.metal.data.ParseState; | ||
import io.parsingdata.metal.encoding.Encoding; | ||
|
||
public abstract class IterableToken extends Token { | ||
|
||
public final Token token; | ||
|
||
IterableToken(final String name, final Token token, final Encoding encoding) { | ||
super(name, encoding); | ||
this.token = checkNotNull(token, "token"); | ||
} | ||
|
||
protected final Optional<ParseState> parse(final Environment environment, final Predicate<Environment> stopCondition, final Function<Environment, Optional<ParseState>> ifIterationFails) { | ||
return iterate(environment.addBranch(this), stopCondition, ifIterationFails).computeResult(); | ||
} | ||
|
||
/** | ||
* Iteratively parse iterations of the token, given a stop condition and the logic how to handle a failed parse. | ||
* | ||
* @param environment the environment to apply the parse to | ||
* @param stopCondition a function to determine when to stop the iteration | ||
* @param ifIterationFails a function to determine how to handle a failed parse | ||
* @return a trampolined {@code Optional<ParseState>} | ||
*/ | ||
private Trampoline<Optional<ParseState>> iterate(final Environment environment, final Predicate<Environment> stopCondition, final Function<Environment, Optional<ParseState>> ifIterationFails) { | ||
if (stopCondition.test(environment)) { | ||
return complete(() -> success(environment.parseState.closeBranch(this))); | ||
} | ||
return token | ||
.parse(environment) | ||
.map(nextParseState -> intermediate(() -> iterate(environment.withParseState(nextParseState.iterate()), stopCondition, ifIterationFails))) | ||
.orElseGet(() -> complete(() -> ifIterationFails.apply(environment))); | ||
} | ||
|
||
@Override | ||
public boolean isIterable() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean equals(final Object obj) { | ||
return super.equals(obj) | ||
&& Objects.equals(token, ((IterableToken)obj).token); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(super.hashCode(), token); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.