Skip to content

Commit c1e28fb

Browse files
author
Brian Mock
authored
Merge pull request #231 from jneen/topic/buffer-support
Buffer support
2 parents 13d84f2 + 3679ac4 commit c1e28fb

11 files changed

+526
-34
lines changed

API.md

+40-1
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,46 @@ var parser =
421421
notChar('b').times(5)
422422
);
423423
parser.parse('accccc');
424-
//=> {status: true, value: ['a', ['c', 'c', 'c', 'c', 'c']]}
424+
// => { status: true, value: ['a', ['c', 'c', 'c', 'c', 'c']] }
425+
```
426+
427+
# Binary constructors
428+
429+
The `Parsimmon.Binary` constructors parse binary content using Node.js Buffers. These constructors can be combined with the normal parser combinators such as `Parsimmon.seq`, `Parsimmon.seqObj`, and still have all the same methods as text-based parsers (e.g. `.map`, `.node`, etc.).
430+
431+
## Parsimmon.byte(int)
432+
433+
Returns a parser that yields a byte (as a number) that matches the given input; similar to `Parsimmon.digit` and `Parsimmon.letter`.
434+
435+
```javascript
436+
var parser = Parsimmon.Binary.byte(0x3f);
437+
parser.parse(Buffer.from([0x3f]));
438+
// => { status: true, value: 63 }
439+
```
440+
441+
## Parsimmon.bitSeq(alignments)
442+
443+
Parse a series of bits that do not have to be byte-aligned and consume them from a Buffer. The maximum number is 48 since more than 48 bits won't fit safely into a JavaScript number without losing precision. Also, the total of all bits in the sequence must be a multiple of 8 since parsing is still done at the byte level.
444+
445+
```javascript
446+
var parser = Parsimmon.Binary.bitSeq([3, 5, 5, 3]);
447+
parser.parse(Buffer.from([0x04, 0xff]));
448+
//=> { status: true, value: [0, 4, 31, 7] }
449+
```
450+
451+
## Parsimmon.bitSeqObj(namedAlignments)
452+
453+
Works like `Parsimmon.bitSeq` except each item in the array is either a number of bits or pair (array with length = 2) of name and bits. The bits are parsed in order and put into an object based on the name supplied. If there's no name for the bits, it will be parsed but discarded from the returned value.
454+
455+
```javascript
456+
var parser = Parsimmon.Binary.bitSeqObj([
457+
["a", 3],
458+
5,
459+
["b", 5],
460+
["c", 3]
461+
]);
462+
parser.parse(Buffer.from([0x04, 0xFF]));
463+
//=> { status: true, value: { a: 0, b: 31, c: 7 } }
425464
```
426465

427466
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
## version 1.7.0 (2018-03-10)
2+
3+
* Adds support for binary parsing using Node.js Buffers
4+
* Adds `Parsimmon.Binary.bitSeq`
5+
* Adds `Parsimmon.Binary.bitSeqObj`
6+
* Adds `Parsimmon.Binary.byte`
7+
18
## version 1.6.4 (2018-01-01)
29

310
* Fixes `parser.many()` to throw an error if it detects an infinite parse loop.

package-lock.json

+18-18
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "parsimmon",
3-
"version": "1.6.4",
3+
"version": "1.7.0",
44
"description": "A monadic LL(infinity) parser combinator library",
55
"keywords": ["parsing", "parse", "parsers", "parser combinators"],
66
"author": "Jeanine Adkisson <jneen at jneen dot net>",

0 commit comments

Comments
 (0)