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-02-26 05:01:10 +03:00
|
|
|
The module `Text.Parsing.Parser.DataView` provides primitive parsers for parsing
|
|
|
|
DataViews on Javascript ArrayBuffers 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 16:28:59 +03:00
|
|
|
This module brings the input stream support of __purescript-parsing__ to be
|
|
|
|
able to roughly match 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-27 16:51:03 +03:00
|
|
|
| Binary blobs | __Text.Parsing.Parser.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
|
|
|
|
|
|
|
|
Parse a `String` as UTF8 with a length prefix in a
|
|
|
|
way that's compatible with the
|
|
|
|
[`Binary.Put.putStringUtf8`](https://hackage.haskell.org/package/binary/docs/Data-Binary-Put.html#v:putStringUtf8)
|
|
|
|
function from the Haskell
|
|
|
|
[__binary__](https://hackage.haskell.org/package/binary)
|
|
|
|
library.
|
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
|
|
|
|
do
|
|
|
|
result <- Text.Parsing.Parser.runParserT dataview $ do
|
2020-07-27 16:51:03 +03:00
|
|
|
-- Parse 32 bits of the big-endian 64-bit length prefix.
|
2020-07-27 16:28:59 +03:00
|
|
|
_ <- anyUint32be
|
|
|
|
length <- anyUint32be
|
|
|
|
stringview <- takeN $ UInt.toInt length
|
|
|
|
stringarray <- 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-07-27 16:28:59 +03:00
|
|
|
|
|
|
|
where
|
|
|
|
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
|
|
|
|
pure $ Data.ArrayBuffer.Typed.part buffer byteOffset byteLength
|
2020-02-26 05:54:03 +03:00
|
|
|
```
|
2020-02-26 04:35:19 +03:00
|
|
|
|
|
|
|
|