2020-02-26 05:30:28 +03:00
|
|
|
# purescript-parsing-dataview
|
2020-02-26 04:35:19 +03:00
|
|
|
|
2020-02-28 15:00:45 +03:00
|
|
|
[![CI](https://github.com/jamesdbrock/purescript-parsing-dataview/workflows/CI/badge.svg?branch=master)](https://github.com/jamesdbrock/purescript-parsing-dataview/actions)
|
2020-02-26 05:01:10 +03:00
|
|
|
[![Pursuit](http://pursuit.purescript.org/packages/purescript-parsing-dataview/badge)](http://pursuit.purescript.org/packages/purescript-parsing-dataview/)
|
2020-02-26 04:35:19 +03:00
|
|
|
|
2020-07-27 17:58:17 +03:00
|
|
|
Primitive parsers for parsing
|
|
|
|
`DataView`s on Javascript `ArrayBuffer`s with the package
|
2020-07-27 16:28:59 +03:00
|
|
|
[__purescript-parsing__](https://pursuit.purescript.org/packages/purescript-parsing/).
|
2020-02-26 04:35:19 +03:00
|
|
|
|
2020-07-27 17:58:17 +03:00
|
|
|
With this package, the input stream support of __purescript-parsing__
|
|
|
|
roughly matches the built-in stream support of __Megaparsec__:
|
2020-02-26 04:35:19 +03:00
|
|
|
|
2020-02-26 05:34:10 +03:00
|
|
|
| Stream type | purescript-parsing | Megaparsec |
|
2020-02-26 05:01:10 +03:00
|
|
|
|----|-----|----|
|
|
|
|
| UTF-16 strings | String | Text |
|
|
|
|
| Listy strings | Token | String |
|
2020-07-28 14:10:03 +03:00
|
|
|
| Binary blobs | __DataView__ | ByteString |
|
2020-02-26 04:35:19 +03:00
|
|
|
|
2020-07-27 16:28:59 +03:00
|
|
|
## Usage examples
|
2020-02-26 04:35:19 +03:00
|
|
|
|
2020-07-27 16:28:59 +03:00
|
|
|
Parse values out of a `dataview :: Data.ArrayBuffer.Types.DataView`. All
|
|
|
|
`DataView` parsing must be done in an `Effect` context. The `result` will be
|
|
|
|
`Either` a parse error or the parsed value.
|
|
|
|
|
|
|
|
### Parse two numbers
|
|
|
|
|
|
|
|
Parse two big-endian IEEE 754 double-precision floats.
|
|
|
|
|
|
|
|
```purescript
|
|
|
|
do
|
|
|
|
result <- Text.Parsing.Parser.runParserT dataview $ do
|
|
|
|
float1 <- anyFloat64be
|
|
|
|
float2 <- anyFloat64be
|
|
|
|
pure $ Tuple float1 float2
|
2020-02-26 05:54:03 +03:00
|
|
|
```
|
2020-07-27 16:28:59 +03:00
|
|
|
|
|
|
|
### Parse an array
|
|
|
|
|
2020-07-27 16:51:03 +03:00
|
|
|
Parse an array of `n` 32-bit signed integers.
|
2020-07-27 16:28:59 +03:00
|
|
|
|
|
|
|
```purescript
|
|
|
|
do
|
|
|
|
result <- Text.Parsing.Parser.runParserT dataview $ replicateA n anyInt32be
|
|
|
|
```
|
|
|
|
|
|
|
|
### Parse UTF8
|
|
|
|
|
2020-07-28 09:25:22 +03:00
|
|
|
Parse a `String` as UTF8 with a length prefix.
|
2020-07-27 16:51:03 +03:00
|
|
|
|
2020-07-27 16:28:59 +03:00
|
|
|
We give this as an example, rather than supporting it in the library, because
|
|
|
|
it depends on
|
|
|
|
[`Data.TextDecoding.decodeUtf8`](https://pursuit.purescript.org/packages/purescript-text-encoding/docs/Data.TextDecoding#v:decodeUtf8).
|
|
|
|
|
|
|
|
```purescript
|
2020-09-07 11:07:46 +03:00
|
|
|
import Effect (Effect, liftEffect)
|
|
|
|
import Control.Monad.Trans.Class (lift)
|
|
|
|
|
2020-07-28 09:25:22 +03:00
|
|
|
mkTypedArray :: Data.ArrayBuffer.Types.DataView -> Effect Data.ArrayBuffer.Types.Uint8Array
|
|
|
|
mkTypedArray dv = do
|
|
|
|
let buffer = Data.ArrayBuffer.DataView.buffer dv
|
|
|
|
byteOffset = Data.ArrayBuffer.DataView.byteOffset dv
|
|
|
|
byteLength = Data.ArrayBuffer.DataView.byteLength dv
|
|
|
|
Data.ArrayBuffer.Typed.part buffer byteOffset byteLength
|
|
|
|
|
2020-07-27 16:28:59 +03:00
|
|
|
do
|
|
|
|
result <- Text.Parsing.Parser.runParserT dataview $ do
|
2020-07-28 09:25:22 +03:00
|
|
|
-- Parse a 32-bit big-endian length prefix for the length of the utf8 string,
|
|
|
|
-- in bytes.
|
2020-07-27 17:45:05 +03:00
|
|
|
length <- anyUint32be
|
|
|
|
stringview <- takeN $ UInt.toInt length
|
2020-09-07 11:07:46 +03:00
|
|
|
stringarray <- lift $ liftEffect $ mkTypedArray stringview
|
2020-07-27 16:51:03 +03:00
|
|
|
case Data.TextDecoding.decodeUtf8 stringarray of
|
|
|
|
Left err -> Data.Parsing.Parser.fail $ show err
|
|
|
|
Right s -> pure s
|
2020-02-26 05:54:03 +03:00
|
|
|
```
|
2020-02-26 04:35:19 +03:00
|
|
|
|
2020-07-27 17:45:05 +03:00
|
|
|
## Serialization
|
2020-02-26 04:35:19 +03:00
|
|
|
|
2020-07-27 17:45:05 +03:00
|
|
|
This package is for reading (`DataView`s on) `ArrayBuffer`s, not writing
|
|
|
|
them. See the package
|
|
|
|
[__purescript-arraybuffer-builder__](https://pursuit.purescript.org/packages/purescript-arraybuffer-builder/)
|
|
|
|
for a way to
|
|
|
|
serialize and build `ArrayBuffer`s.
|
|
|
|
|
|
|
|
|
|
|
|
## References
|
|
|
|
|
|
|
|
* [MDN `ArrayBuffer`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer)
|
2020-07-29 02:58:20 +03:00
|
|
|
* [MDN `DataView`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView)
|
|
|
|
|
|
|
|
## Notes
|
|
|
|
|
|
|
|
Perhaps some day this package can be
|
|
|
|
[merged into __purescript-parsing__](https://github.com/purescript-contrib/purescript-parsing/issues/88),
|
|
|
|
but for now it has too many non-*purescript-contrib* dependencies.
|