Parser Stream #
Parsers read input tokens from a stream. To help with error reporting and backtracking, the
Parser.Stream class extends the basic Stream class with functionality to save and restore
stream positions.
The simple way to implement backtracking after a parsing error is to first save the stream state
before parsing and, upon encountering an error, restore the saved stream state. The issue with this
strategy is that each backtrack point adds a reference to the entire stream state. This prevents
linear use of the stream state. The Parser.Stream class allows users to work around this issue.
The Parser.Stream.Position type is intended to store just enough information to reconstruct the
stream state at a save point without having to save the entire stream state.
Parser stream class
This class extends the basic Stream class with position features needed by parsers for
backtracking and error reporting.
- The type
Positionis used to record position data for the stream type. getPosition (s : σ) : Positionreturns the current position of streams.setPosition (s : σ) (p : Position) : σrestores streamsto positionp.
Implementations should try to make the Position type as lightweight as possible for getPosition
and setPosition to work properly. Often Position is just a scalar type or another simple type.
This may allow for parsers to use the stream state more efficiently.
- Position : Type u_3
Parser stream class
This class extends the basic
Streamclass with position features needed by parsers for backtracking and error reporting.- The type
Positionis used to record position data for the stream type. getPosition (s : σ) : Positionreturns the current position of streams.setPosition (s : σ) (p : Position) : σrestores streamsto positionp.
Implementations should try to make the
Positiontype as lightweight as possible forgetPositionandsetPositionto work properly. OftenPositionis just a scalar type or another simple type. This may allow for parsers to use the stream state more efficiently. - The type
- getPosition : σ → Position σ
Parser stream class
This class extends the basic
Streamclass with position features needed by parsers for backtracking and error reporting.- The type
Positionis used to record position data for the stream type. getPosition (s : σ) : Positionreturns the current position of streams.setPosition (s : σ) (p : Position) : σrestores streamsto positionp.
Implementations should try to make the
Positiontype as lightweight as possible forgetPositionandsetPositionto work properly. OftenPositionis just a scalar type or another simple type. This may allow for parsers to use the stream state more efficiently. - The type
- setPosition : σ → Position σ → σ
Parser stream class
This class extends the basic
Streamclass with position features needed by parsers for backtracking and error reporting.- The type
Positionis used to record position data for the stream type. getPosition (s : σ) : Positionreturns the current position of streams.setPosition (s : σ) (p : Position) : σrestores streamsto positionp.
Implementations should try to make the
Positiontype as lightweight as possible forgetPositionandsetPositionto work properly. OftenPositionis just a scalar type or another simple type. This may allow for parsers to use the stream state more efficiently. - The type
Instances
Stream segment type.
Equations
Instances For
Start position of stream segment.
Instances For
Stop position of stream segment.
Instances For
Default wrapper to make a Parser.Stream from a plain Stream.
This wrapper uses the entire stream state as position information; this is not efficient. Always
prefer tailored Parser.Stream instances to this default.
Equations
- Parser.Stream.mkDefault σ τ = σ
Instances For
Equations
- One or more equations did not get rendered due to their size.
Equations
- Parser.Stream.instSliceChar = { toStream := instStreamSliceChar_parser, Position := String.Slice, getPosition := fun (s : String.Slice) => s, setPosition := fun (x s : String.Slice) => s }
Equations
- One or more equations did not get rendered due to their size.
Equations
- One or more equations did not get rendered due to their size.
Equations
- One or more equations did not get rendered due to their size.
Restore a list stream to a given position.
Equations
Instances For
Internal for OfList.setPosition.
Equations
- Parser.Stream.OfList.setPosition.fwd k.succ { next := x_2 :: rest, past := past } = Parser.Stream.OfList.setPosition.fwd k { next := rest, past := x_2 :: past }
- Parser.Stream.OfList.setPosition.fwd x✝¹ x✝ = x✝
Instances For
Internal for OfList.setPosition.
Equations
- Parser.Stream.OfList.setPosition.rev k.succ { next := rest, past := x_2 :: past } = Parser.Stream.OfList.setPosition.rev k { next := x_2 :: rest, past := past }
- Parser.Stream.OfList.setPosition.rev x✝¹ x✝ = x✝
Instances For
Make a Parser.Stream from a List.
Equations
- Parser.Stream.mkOfList data pos = { next := data }.setPosition pos
Instances For
Equations
- One or more equations did not get rendered due to their size.