- java.lang.Object
-
- swim.codec.Input
-
- Direct Known Subclasses:
InputBuffer
public abstract class Input extends Object
Non-blocking token stream reader, with single token lookahead.Inputenable incremental, interruptible parsing of network protocols and data formats.Input tokens
Input tokens are modeled as primitive
ints, commonly representing Unicode code points, or raw octets; eachInputimplementation specifies the semantic type of its tokens. Thehead()method peeks at the current lookahead token, without consuming it, and thestep()method advances the input to the next token.Input states
Inputis always in one of four states: continue, empty, done, or error. The cont state indicates that a lookahead token is immediately available; the empty state indicates that no additional tokens are available at this time, but that the stream may logically resume in the future; the done state indicates that the stream has terminated nominally; and the error state indicates that the stream has terminated abnormally.isCont()returnstruewhen in the cont state;isEmpty()returnstruewhen in the empty state;isDone()returnstruewhen in the done state; andisError()returnstruewhen in the error state.Non-blocking semantics
Inputnever blocks. AnInputthat would otherwise block awaiting additional tokens instead enters the empty state, signaling the input consumer to back off processing the input, but to remain prepared to process additional input in the future. AnInputenters the done state when it encounters the final end of its input stream, signaling the input consumer to terminate processing.isPart()returnstrueif theInputwill enter the empty state after it consumes the last immediately available token; it returnsfalseif theInputwill enter the done state after it consumes the last immediately available token.empty()returns anInputin the empty state.done()returns anInputin the done state.Position tracking
The logical position of the current lookahead token is made available via the
mark()method, with optimized callouts for the byteoffset, one-basedlinenumber, and one-basedcolumnin the current line. Theid()method returns a diagnostic identifier for the token stream.Cloning
An
Inputmay beclonedto provide an indepently mutable position into a shared token stream. Not allInputimplementations support cloning.- See Also:
InputSettings,Parser
-
-
Constructor Summary
Constructors Constructor Description Input()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description abstract Inputclone()Returns an independently positioned view into the token stream, initialized with identical state to thisInput.intcolumn()Returns the one-based column number of the current lookahead token, relative to the current line in the stream.static Inputdone()Returns anInputin the done state.static Inputdone(Object id, Mark mark)Returns anInputin the done state, at themarkposition of a token stream logically identified byid.static Inputdone(Object id, Mark mark, InputSettings settings)Returns anInputin the done state, at themarkposition of a token stream logically identified byid, with the givensettings.static Inputdone(InputSettings settings)Returns anInputin the done state, with the givensettings.static Inputempty()Returns anInputin the empty state.static Inputempty(Object id, Mark mark)Returns anInputin the empty state, at themarkposition of a token stream logically identified byid.static Inputempty(Object id, Mark mark, InputSettings settings)Returns anInputin the empty state, at themarkposition of a token stream logically identified byid, with the givensettings.static Inputempty(InputSettings settings)Returns anInputin the empty state, with the givensettings.static Inputerror(Throwable error)Returns anInputin the error state, with the given inputerror.static Inputerror(Throwable error, Object id, Mark mark)Returns anInputin the error state, with the given inputerror, at themarkposition of a token stream logically identified byid.static Inputerror(Throwable error, Object id, Mark mark, InputSettings settings)Returns anInputin the error state, with the given inputerror, at themarkposition of a token stream logically identified byid, with the givensettings.static Inputerror(Throwable error, InputSettings settings)Returns anInputin the error state, with the given inputerrorandsettings.Inputfork(Object condition)Returns anInputequivalent to thisInput, but whose behavior may be altered by the given out-of-bandcondition.abstract inthead()Returns the current lookahead token, if thisInputis in the cont state.abstract Objectid()Returns an object that identifies the token stream, ornullif the stream is unidentified.abstract Inputid(Object id)Returns anInputequivalent to thisInput, but logically identified by the given–possiblynull–id.abstract booleanisCont()Returnstruewhen alookeaheadtoken is immediately available.abstract booleanisDone()Returnstruewhen no lookahead token is currently available, and no additional input will ever become available.abstract booleanisEmpty()Returnstruewhen no lookahead token is currently available, but additional input may be available in the future.abstract booleanisError()Returnstruewhen no lookahead token is currently available due to an error with the token stream.abstract booleanisPart()Returnstrueif this is a partialInputwill that enter the empty state after it consumes the last available input token.abstract InputisPart(boolean isPart)Returns a partialInputequivalent to thisInput, ifisPartistrue; returns a finalInputequivalent to thisInputifisPartisfalse.intline()Returns the one-based line number of the current lookahead token, relative to the start of the stream.abstract Markmark()Returns the position of the current lookahead token, relative to the start of the stream.abstract Inputmark(Mark mark)Returns anInputequivalent to thisInput, but logically positioned at the givenmark.longoffset()Returns the byte offset of the current lookahead token, relative to the start of the stream.abstract Inputseek(Mark mark)Returns anInputequivalent to thisInput, but repositioned to the givenmark.abstract InputSettingssettings()Returns theInputSettingsused to configure the behavior of input consumers that read from thisInput.abstract Inputsettings(InputSettings settings)Returns anInputequivalent to thisInput, but with the given inputsettings.abstract Inputstep()Returns anInputequivalent to thisInput, but advanced to the next token.Throwabletrap()Returns the input error.
-
-
-
Method Detail
-
isCont
public abstract boolean isCont()
Returnstruewhen alookeaheadtoken is immediately available. i.e. thisInputis in the cont state.
-
isEmpty
public abstract boolean isEmpty()
Returnstruewhen no lookahead token is currently available, but additional input may be available in the future. i.e. thisInputis in the empty state.
-
isDone
public abstract boolean isDone()
Returnstruewhen no lookahead token is currently available, and no additional input will ever become available. i.e. thisInputis in the done state.
-
isError
public abstract boolean isError()
Returnstruewhen no lookahead token is currently available due to an error with the token stream. i.e. thisInputis in the error state. Whentrue,trap()will return the input error.
-
isPart
public abstract boolean isPart()
Returnstrueif this is a partialInputwill that enter the empty state after it consumes the last available input token.
-
isPart
public abstract Input isPart(boolean isPart)
Returns a partialInputequivalent to thisInput, ifisPartistrue; returns a finalInputequivalent to thisInputifisPartisfalse. The caller's reference tothisInputshould be replaced by the returnedInput.
-
head
public abstract int head()
Returns the current lookahead token, if thisInputis in the cont state.- Throws:
InputException- if thisInputis not in the cont state.
-
step
public abstract Input step()
Returns anInputequivalent to thisInput, but advanced to the next token. Returns anInputin the error state if thisInputis not in the cont state. The caller's reference tothisInputshould be replaced by the returnedInput.
-
seek
public abstract Input seek(Mark mark)
Returns anInputequivalent to thisInput, but repositioned to the givenmark. Returns anInputin the error state if thisInputdoes not support seeking, or if thisInputis unable to reposition to the givenmark. The caller's reference tothisInputshould be replaced by the returnedInput.
-
fork
public Input fork(Object condition)
Returns anInputequivalent to thisInput, but whose behavior may be altered by the given out-of-bandcondition. The caller's reference tothisInputshould be replaced by the returnedInput.
-
trap
public Throwable trap()
Returns the input error. Only guaranteed to return an error when in the error state.- Throws:
InputException- if thisInputis not in the error state.
-
id
public abstract Object id()
Returns an object that identifies the token stream, ornullif the stream is unidentified.
-
id
public abstract Input id(Object id)
Returns anInputequivalent to thisInput, but logically identified by the given–possiblynull–id. The caller's reference tothisInputshould be replaced by the returnedInput.
-
mark
public abstract Mark mark()
Returns the position of the current lookahead token, relative to the start of the stream.
-
mark
public abstract Input mark(Mark mark)
Returns anInputequivalent to thisInput, but logically positioned at the givenmark. The physical position in the input stream is not modified. The caller's reference tothisInputshould be replaced by the returnedInput.
-
offset
public long offset()
Returns the byte offset of the current lookahead token, relative to the start of the stream.
-
line
public int line()
Returns the one-based line number of the current lookahead token, relative to the start of the stream.
-
column
public int column()
Returns the one-based column number of the current lookahead token, relative to the current line in the stream.
-
settings
public abstract InputSettings settings()
Returns theInputSettingsused to configure the behavior of input consumers that read from thisInput.
-
settings
public abstract Input settings(InputSettings settings)
Returns anInputequivalent to thisInput, but with the given inputsettings. The caller's reference tothisInputshould be replaced by the returnedInput.
-
clone
public abstract Input clone()
Returns an independently positioned view into the token stream, initialized with identical state to thisInput.- Overrides:
clonein classObject- Throws:
UnsupportedOperationException- if thisInputcannot be cloned.
-
empty
public static Input empty()
Returns anInputin the empty state.
-
empty
public static Input empty(InputSettings settings)
Returns anInputin the empty state, with the givensettings.
-
empty
public static Input empty(Object id, Mark mark)
Returns anInputin the empty state, at themarkposition of a token stream logically identified byid.
-
empty
public static Input empty(Object id, Mark mark, InputSettings settings)
Returns anInputin the empty state, at themarkposition of a token stream logically identified byid, with the givensettings.
-
done
public static Input done()
Returns anInputin the done state.
-
done
public static Input done(InputSettings settings)
Returns anInputin the done state, with the givensettings.
-
done
public static Input done(Object id, Mark mark)
Returns anInputin the done state, at themarkposition of a token stream logically identified byid.
-
done
public static Input done(Object id, Mark mark, InputSettings settings)
Returns anInputin the done state, at themarkposition of a token stream logically identified byid, with the givensettings.
-
error
public static Input error(Throwable error)
Returns anInputin the error state, with the given inputerror.
-
error
public static Input error(Throwable error, InputSettings settings)
Returns anInputin the error state, with the given inputerrorandsettings.
-
error
public static Input error(Throwable error, Object id, Mark mark)
Returns anInputin the error state, with the given inputerror, at themarkposition of a token stream logically identified byid.
-
error
public static Input error(Throwable error, Object id, Mark mark, InputSettings settings)
Returns anInputin the error state, with the given inputerror, at themarkposition of a token stream logically identified byid, with the givensettings.
-
-