Git Product home page Git Product logo

bnf-parser's Introduction

BNF-Parser

Test

Compile your bnfs down to WebAssembly for maximum parsing speed; with generated type definitions to make using the syntax tree outputs a breeze. The compiled output from this library is platform agnostic, so it can run anywhere new WebAssembly.Instance() is a valid function. It bundles the WebAssembly module inside of a single js file so it can be easily incorporated into any bundler. The type definitions for a given bnf are just that, a definitions file - so if you don't want to use typescript or type hints you can go wild by ignoring it.

program ::= chunk+ ;
chunk ::= "a"+ "b"+ ;
npx bnf-compile ./syntax.bnf
import * as syntax from "./bnf/syntax.js";

const tree    = syntax.Parse_Program("abbaabab").root;
const chunk   = program.value[0]; // typescript knows this this **will** be of type `Term_Chunk`
const firstBs = program.value[1];
const bCount: number = firstBs.value.length; // typescript knows this **will** be a number

Built to be a devDependency - if you use the included cli tool to generate your syntax parser you don't need to include this library as your dependency, you can just import those artifacts.

Documentation

See https://bnf-parser.ajanibilby.com/

API

See https://bnf-parser.ajanibilby.com/api

BNF Syntax

See https://bnf-parser.ajanibilby.com/api

Try it Online

Try it in your browser https://bnf-parser.ajanibilby.com/test

bnf-parser's People

Contributors

ajanibilby avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

pauldraper

bnf-parser's Issues

Not getting correct end bytes for repetitions of a select

Repetition of a select not working correctly:

# input: $abcHello $abc World
program ::= ("$abc" | !"$"+)+ ;
{
  "root": {
    "type": "program",
    "start": 0,
    "end": 0,
    "count": 1,
    "value": [
      {
        "type": "(...)+",
        "start": 0,
        "end": 0,
        "count": 4,
        "value": [
          {
            "type": "literal",
            "start": 0,
            "end": 4,
            "count": 4,
            "value": "$abc",
            "ref": {}
          }, {}, {},
          {
            "type": "literal",
            "start": 14,
            "end": 20,
            "count": 6,
            "value": " World",
            "ref": { }
          }
        ],
        "ref": {}
      }
    ],
    "ref": {},
  "reachBytes": 20,
  "reach": {
    "line": 1,
    "col": 21,
    "index": 20
  },
  "isPartial": true
}

This leads also to things being incorrectly labeled as partial, because reachBytes !== end

Repetition of sequences work correctly:

# input: $abcHello $abc World
program ::= ("$abc" !"$"+)+ ;

+Show bnf stack trace on error

Include a list of strings within BNF_SyntaxError for the current BNF term that failed, and what terms it was "called" from. This can then be used to print out better syntax errors, allowing you to more easily debug BNFs due to the fact you can ensure that it is attempting to match the fail point with the correct term.

Error with default BNF example

Hello,

Running through the homepage example, I use the following BNF:

program ::= %w* statement+ ;
block   ::= %("block" w+) ...name %(w* "{" w*) seq_stmt* %("}" w*) ;

And run:

npx bnf-compile ./syntax.bnf

And I get:

Found: ./syntax.bnf

 - Compiling syntax
   Encoding Parser
Parse Error: Unknown term name "w" (1:14) -> (1:16)

Cannot read properties of undefined (reading `'getReach'`) since v3.1.2

Hello!

bnf-parser 3.1.2 seems to have introduced a regression. We're getting the following crash when calling parse:

TypeError: Cannot read properties of undefined (reading 'getReach')
      at SyntaxNode.getReach (/Users/jeffrey.fowler/development/sequelize-v7-alias-bug/node_modules/bnf-parser/bin/syntax.js:70:50)
      at Sequence.parseSingle (/Users/jeffrey.fowler/development/sequelize-v7-alias-bug/node_modules/bnf-parser/bin/parser.js:347:32)
      at Sequence.parse (/Users/jeffrey.fowler/development/sequelize-v7-alias-bug/node_modules/bnf-parser/bin/parser.js:271:28)
      at Sequence.parse (/Users/jeffrey.fowler/development/sequelize-v7-alias-bug/node_modules/bnf-parser/bin/parser.js:323:25)
      at Rule.parse (/Users/jeffrey.fowler/development/sequelize-v7-alias-bug/node_modules/bnf-parser/bin/parser.js:389:28)
      at Parser.parse (/Users/jeffrey.fowler/development/sequelize-v7-alias-bug/node_modules/bnf-parser/bin/parser.js:425:29)
      at parseAttributeSyntaxInternal (/Users/jeffrey.fowler/development/sequelize-v7-alias-bug/node_modules/@sequelize/core/lib/utils/attribute-syntax.js:97:34)
      at memoized (/Users/jeffrey.fowler/development/sequelize-v7-alias-bug/node_modules/lodash/memoize.js:62:23)
      at /Users/jeffrey.fowler/development/sequelize-v7-alias-bug/node_modules/@sequelize/core/lib/dialects/abstract/where-sql-builder.js:215:88
      at Array.map (<anonymous>)

This is the BNF in question: https://github.com/sequelize/sequelize/blob/e6690cdf27980d57be1bb9f138b04124775b153b/packages/core/src/utils/attribute-syntax.ts#L56-L87
attributeParser.parse from the above code fails when we feed something like 'prop' into it.

Do you know if we're doing something incorrectly or if this is an actual bug?

Related threads: sequelize/sequelize#15749, sequelize/sequelize#15792

Parse error due to select option order

// OK
let result = BNF.parse(`program ::= "aa" | "a";`);
let tree = Compile(result);
let syntax = tree.parse("aa");
console.log("result", result, "syntax", syntax);
// Not OK
let result = BNF.parse(`program ::= "a" | "aa";`);
let tree = Compile(result);
let syntax = tree.parse("aa");
console.log("result", result, "syntax", syntax);

Edit by @AjaniBilby:
Answer

Unparse

Very cool project!

What would be also nice is to have reverse parse/serialize functionality.

Should be possible without ambiguity (choose fewest possible repetitions).

Wasm-Parser: Upper case ranges not working

With the new wasm parser upper case ranges don't appear to work at all

This example will always fail

program ::= letter+ ;
letter ::= "a"->"z" | "A"->"Z" ;
snakeCase

Version 4.0 Release Checklist

  • All issues related to release closed
  • Documentation
    • CLI usage
    • Shared Artifact API outline
    • Bnf artifact API outline
    • Whole library API outline

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.