bnf-parser
Advanced tools
Comparing version 3.0.2 to 3.0.3
@@ -242,3 +242,3 @@ "use strict"; | ||
} | ||
return new syntax_1.SyntaxNode(this.value, nodes, range); | ||
return new syntax_1.SyntaxNode(this.value + this.count, nodes, range); | ||
} | ||
@@ -290,5 +290,3 @@ serialize() { | ||
} | ||
else { | ||
return new syntax_1.SyntaxNode(`(...)${this.count}`, nodes, range); | ||
} | ||
return new syntax_1.SyntaxNode(`(...)${this.count}`, nodes, range); | ||
} | ||
@@ -334,3 +332,6 @@ parseSingle(input, ctx, cursor) { | ||
cursor = res.ref.end; | ||
if (res.type != "omit") { | ||
if (rule instanceof Omit) { | ||
continue; // skip omitted operands | ||
} | ||
else { | ||
nodes.push(res); | ||
@@ -387,2 +388,5 @@ } | ||
addRule(name, rule) { | ||
if (this.terms.has(name)) { | ||
throw new Error(`Attempting to add rule "${name}" to a parser which already has a rule of that name`); | ||
} | ||
this.terms.set(name, rule); | ||
@@ -389,0 +393,0 @@ } |
{ | ||
"name": "bnf-parser", | ||
"version": "3.0.2", | ||
"version": "3.0.3", | ||
"description": "Deterministic BNF compiler/parser", | ||
"main": "./bin/index.js", | ||
"scripts": { | ||
"test": "node ./bin/build.js --verify --verbose", | ||
"build": "tsc & node ./bin/build.js --verbose" | ||
"test": "node ./test", | ||
"build": "npm run build-ts & npm run build-syntax", | ||
"build-ts": "tsc", | ||
"build-syntax": "node ./bin/build.js --verbose" | ||
}, | ||
@@ -10,0 +14,0 @@ "repository": { |
104
readme.md
# BNF-Parser <!-- no toc --> | ||
[![Test](https://github.com/AjaniBilby/BNF-parser/actions/workflows/test.yml/badge.svg?branch=master)](https://github.com/AjaniBilby/BNF-parser/actions/workflows/test.yml) | ||
- [BNF-Parser ](#bnf-parser-) | ||
@@ -29,6 +31,6 @@ - [Example](#example) | ||
let result: SyntaxNode = BNF.parse(language_syntax); | ||
let tree = Compile(tree); | ||
let result = BNF.parse(LANGUAGE_BNF); | ||
let tree = Compile(result); | ||
let syntax = tree.parse(file); | ||
let syntax = tree.parse(FILE); | ||
``` | ||
@@ -40,3 +42,3 @@ | ||
// Save the syntax tree | ||
fs.writeFileSync(path, JSON.stringify(tree.serialize();)); | ||
fs.writeFileSync(path, JSON.stringify(tree.serialize())); | ||
@@ -53,3 +55,3 @@ // Load the compiled syntax tree | ||
This is a pre-initalised BNF parser, which can be given a BNF string input. | ||
This is a pre-initialised BNF parser, which can be given a BNF string input. | ||
@@ -62,3 +64,3 @@ ```ts | ||
Is initialised with a built syntax tree. Once initalized it can be given input strings to generate output syntax trees for a given language. | ||
Is initialised with a built syntax tree. Once initialized it can be given input strings to generate output syntax trees for a given language. | ||
@@ -68,3 +70,9 @@ ```ts | ||
constructor(blob: any) | ||
parse(input: string, partial = false, entry = "program"): SyntaxNode | ParseError | ||
// Attempts to parse a language into a syntax tree | ||
parse( | ||
input: string, // The text to be parsed | ||
partial = false, // Whether the entire string needs to be consucanmed | ||
entry = "program" // Where parsing should start from in the BNF definition | ||
): SyntaxNode | ParseError | ||
setVerbose(mode: boolean) { } | ||
@@ -84,3 +92,7 @@ } | ||
class Reference { | ||
// Returns a deep copy of itself | ||
clone(): Reference | ||
// Stringifies itself for printing/debug | ||
toString(): string | ||
@@ -93,3 +105,7 @@ } | ||
constructor(from: Reference, to: Reference) | ||
// Returns a deep copy of itself | ||
clone(): ReferenceRange | ||
// Stringifies itself for printing/debug | ||
toString(): string | ||
@@ -102,2 +118,4 @@ } | ||
constructor(msg: string, ref: ReferenceRange) | ||
// Stringifies itself for printing/debug | ||
toString(): string | ||
@@ -111,8 +129,10 @@ } | ||
class SyntaxNode { | ||
type: string; | ||
value: SyntaxValue; | ||
ref: ReferenceRange; | ||
type: string; | ||
value: SyntaxValue; | ||
ref: ReferenceRange; | ||
constructor(type: string, value: SyntaxValue, ref: ReferenceRange) {}; | ||
flat(): string {}; | ||
constructor(type: string, value: SyntaxValue, ref: ReferenceRange) {}; | ||
// Merges all of it's child syntax node values into a single string | ||
flat(): string {}; | ||
} | ||
@@ -125,11 +145,18 @@ ``` | ||
class ParseError { | ||
stack: string[] | ||
msg: string | ||
ref: ReferenceRange | ||
stack: string[] | ||
msg: string | ||
ref: ReferenceRange | ||
constructor(msg: string, ref: ReferenceRange) { } | ||
constructor(msg: string, ref: ReferenceRange) { } | ||
add_stack(elm: string) { } | ||
hasStack(): boolean { } | ||
toString() { } | ||
// Adds a string to the top of the call stack | ||
// (for internal use) | ||
add_stack(elm: string) { } | ||
// If this error contains a pass stack | ||
// (for internal use) | ||
hasStack(): boolean { } | ||
// Stringifies itself for printing/debug | ||
toString() { } | ||
} | ||
@@ -142,10 +169,16 @@ ``` | ||
class Reference { | ||
line: number; | ||
col: number; | ||
index: number; | ||
line: number; | ||
col: number; | ||
index: number; | ||
constructor(line: number, col: number, index: number) { } | ||
advance(newline: boolean = false) { } | ||
clone(): Reference { } | ||
toString(): string { } | ||
constructor(line: number, col: number, index: number) { } | ||
// Will shift the reference one position forwards | ||
advance(newline: boolean = false) { } | ||
// Returns a deep copy of itself | ||
clone(): Reference { } | ||
// Stringifies itself for printing/debug | ||
toString(): string { } | ||
} | ||
@@ -158,9 +191,16 @@ ``` | ||
class ReferenceRange { | ||
start: Reference; | ||
end: Reference; | ||
start: Reference; | ||
end: Reference; | ||
constructor(from: Reference, to: Reference) { } | ||
span(other: ReferenceRange) { } | ||
clone(): ReferenceRange { } | ||
toString(): string { } | ||
constructor(from: Reference, to: Reference) { } | ||
// Alters itself so the rang supplied now fits within the range of itself | ||
// Basically takes the min from, and the max to references and applies them to itself | ||
span(other: ReferenceRange) { } | ||
// Returns a deep copy of itself | ||
clone(): ReferenceRange { } | ||
// Stringifies itself for printing/debug | ||
toString(): string { } | ||
} | ||
@@ -167,0 +207,0 @@ ``` |
265
39308
16
817