-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve abstraction of parse state #221
Conversation
…a ParseState argument instead of a ParseGraph argument.
Codecov Report
@@ Coverage Diff @@
## master #221 +/- ##
=======================================
Coverage 100% 100%
- Complexity 952 967 +15
=======================================
Files 86 88 +2
Lines 1314 1331 +17
Branches 134 137 +3
=======================================
+ Hits 1314 1331 +17
Continue to review full report at Codecov.
|
…, Callbacks and Encoding values.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!
return ImmutableList.create(Optional.<Value>empty()).add(Optional.of(new Value(createFromBytes(new byte[] { 1, 2 }), enc()))); | ||
} | ||
}, Shorthand::add).eval(stream(0).order, enc()).isEmpty()); | ||
assertTrue(foldRight((parseState, encoding) -> ImmutableList.create(Optional.<Value>empty()).add(Optional.of(new Value(createFromBytes(new byte[] { 1, 2 }), enc()))), Shorthand::add).eval(stream(0), enc()).isEmpty()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is one big line of code. ;)
private Optional<Environment> checkFullParse(Token token, byte[] data) throws IOException { | ||
final Optional<Environment> result = token.parse(new Environment(new InMemoryByteStream(data)), enc()); | ||
private Optional<ParseState> checkFullParse(Token token, byte[] data) throws IOException { | ||
final Optional<ParseState> result = token.parse(createFromByteStream(new InMemoryByteStream(data)), enc()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can use the ParseStateFactory.stream(bytes) for this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
stream()
takes a vararg of integers, not an array :(
@@ -73,15 +73,15 @@ public void multiValueInRepN() throws IOException { | |||
any("b"), | |||
repn(dummy, ref("b")) | |||
); | |||
Optional<Environment> result = multiRepN.parse(stream(2, 2, 2, 2), enc()); | |||
Optional<ParseState> result = multiRepN.parse(stream(2, 2, 2, 2), enc()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor, but result can be made final.
if (values.isEmpty()) { | ||
return complete(() -> success(new Environment(environment.closeBranch().order, returnEnvironment.source, returnEnvironment.offset, returnEnvironment.callbacks))); | ||
return complete(() -> success(new ParseState(parseState.closeBranch().order, returnParseState.source, returnParseState.offset))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not using a factory method here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suppose that would have to be a method comparable to seek()
that replaces the ParseGraph
on returnEnvironment
. It could be done, but this seems to be a rather isolated use.
public final BigInteger offset; | ||
public final Source source; | ||
|
||
public ParseState(final ParseGraph order, final Source source, final BigInteger offset) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it is possible you may consider making this ParseState constructor private and always use the ParseStateFactory instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As discussed, ParseStateFactory
is a test class. I think this single constructor makes sense because it only stores fields.
this.offset = checkNotNegative(offset, "offset"); | ||
} | ||
|
||
public static ParseState createFromByteStream(final ByteStream input, final BigInteger offset) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this method be part of the ParseStateFactory?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See above :)
&& Objects.equals(encoding, ((DataExpressionSource)obj).encoding); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(getClass().hashCode(), dataExpression, index, graph, encoding); | ||
return Objects.hash(getClass().hashCode(), dataExpression, index, parseState, encoding); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can probably remove the calls to hashcode everywhere where using Objects#hash
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems to get called on it anyway and all tests are green. Good catch!
@@ -86,7 +87,7 @@ | |||
|
|||
public static final Token EMPTY = def(EMPTY_NAME, 0L); | |||
public static final ValueExpression SELF = new Self(); | |||
public static final ValueExpression CURRENT_OFFSET = elvis(add(offset(SELF), len(SELF)), con(0)); | |||
public static final ValueExpression CURRENT_OFFSET = new CurrentOffset(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's back with a vengeance!
import io.parsingdata.metal.expression.value.ValueExpression; | ||
import io.parsingdata.metal.token.Token; | ||
|
||
public class ParseState { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps make final?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's probably worth considering in general which classes should be made final
: #223.
import io.parsingdata.metal.expression.value.ValueExpression; | ||
import io.parsingdata.metal.token.Token; | ||
|
||
public class ParseState { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps add some documentation 😈
…Environment.extendScope() method, based on PR feedback by @mvanaken.
Recreated an Environment class to hold common values during parsing.
Resolves #216.
This PR tackles some abstraction issues. The resolution resolves a functional bug. The six commits show the following six changes:
Callbacks
was extracted from theEnvironment
class.Environment
class was renamed toParseState
.ParseState
, two are renamed to factory methods to better reflect their functionality.equals()
andhashCode()
methods were implemented onParseState
.eval()
methods ofValueExpression
andExpression
were modified to accept aParseState
instead of aParseGraph
.Shorthand
approximate implementation ofCURRENT_OFFSET
is replaced with an implementation that returns the actualoffset
field of theParseState
.After that, another branch was merged that recreates an
Environment
class: #222.