0.1.41 • Published 16 days ago

gbnf v0.1.41

Weekly downloads
-
License
MIT
Repository
github
Last release
16 days ago

GBNF

A library for parsing .gbnf grammar files in Javascript.

Install

npm install gbnf

Usage

Pass your grammar to GBNF:

import GBNF from 'gbnf';
const state = GBNF(`
root  ::= "yes" | "no"
`)

If the grammar is invalid, GBNF will throw.

GBNF returns a state representing the parsed state:

import GBNF from 'gbnf';
const state = GBNF(`
root  ::= "yes" | "no"
`)
for (const rule of state) {
  console.log(rule); 
  // { type: "CHAR", value: ["y".charCodeAt(0)]}
  // { type: "CHAR", value: ["n".charCodeAt(0)]}
}

state can be iterated over. (You can also call the iterator method directly with state.rules()). state cannot be indexed directly, but can easily be cast to an array with [...state] and indexed that way.

States are immutable. To parse a new token, call state.add():

import GBNF from 'gbnf';
let state = GBNF(`
root  ::= "I like green eggs and ham"
`)
console.log([...state]); // [{ type: "CHAR", value: ["I".charCodeAt(0)]}]
state = state.add("I li");
console.log([...state]); // [{ type: "CHAR", value: ["k".charCodeAt(0)]}]
state = state.add("ke gree");
console.log([...state]); // [{ type: "CHAR", value: ["n".charCodeAt(0)]}]

The possible rules returned include:

  • CHAR - contains an array of either numbers representing code points to match, or an array of two numbers denoting a range within which a code point may appear.
  • CHAR_EXCLUDED - contains an array of numbers representing code points not to match, or an array of two numbers denoting a range within which a code point may not appear.
  • END - denotes a valid end of a string.
0.1.41

16 days ago

0.1.40

18 days ago

0.1.38

21 days ago

0.1.39

18 days ago

0.1.37

2 months ago

0.1.30

4 months ago

0.1.31

4 months ago

0.1.32

4 months ago

0.1.33

4 months ago

0.1.34

4 months ago

0.1.35

4 months ago

0.1.36

4 months ago

0.1.27

4 months ago

0.1.28

4 months ago

0.1.29

4 months ago

0.1.20

4 months ago

0.1.21

4 months ago

0.1.22

4 months ago

0.1.23

4 months ago

0.1.24

4 months ago

0.1.25

4 months ago

0.1.26

4 months ago

0.1.19

4 months ago

0.1.16

7 months ago

0.1.17

7 months ago

0.1.18

7 months ago

0.1.14

8 months ago

0.1.15

8 months ago

0.1.13

8 months ago

0.1.11

8 months ago

0.1.12

8 months ago

0.1.10

8 months ago

0.1.9

8 months ago

0.1.8

9 months ago

0.1.6

9 months ago

0.1.5

9 months ago

0.1.4

10 months ago

0.1.3

10 months ago

0.1.2

10 months ago

0.1.0

10 months ago

0.1.1

10 months ago

0.0.9

10 months ago

0.0.8

10 months ago

0.0.3

10 months ago

0.0.2

10 months ago

0.0.5

10 months ago

0.0.4

10 months ago

0.0.7

10 months ago

0.0.6

10 months ago

0.0.1

10 months ago