bnf-parser
Advanced tools
Comparing version 3.1.0 to 3.1.1
@@ -176,4 +176,5 @@ "use strict"; | ||
} | ||
syntax.link(); | ||
return syntax; | ||
} | ||
exports.Compile = Compile; |
@@ -14,4 +14,7 @@ import { SyntaxNode, ParseError, Reference } from "./syntax"; | ||
constructor(json: any); | ||
parse(input: string, ctx: Parser, cursor: Reference): SyntaxNode | ParseError; | ||
parse(input: string, cursor: Reference): SyntaxNode | ParseError; | ||
match(input: string, cursor: Reference): Boolean; | ||
link(ctx: Parser): void; | ||
random(depth: number): string; | ||
randomSingle(): string; | ||
serialize(): any; | ||
@@ -24,2 +27,3 @@ } | ||
matchChar(char: string, offset: number): boolean; | ||
randomSingle(): string; | ||
serialize(): any; | ||
@@ -30,7 +34,9 @@ } | ||
constructor(json: any); | ||
parse(input: string, ctx: Parser, cursor: Reference): SyntaxNode | ParseError; | ||
parse(input: string, cursor: Reference): SyntaxNode | ParseError; | ||
link(ctx: Parser): void; | ||
random(depth: number): string; | ||
serialize(): any; | ||
} | ||
export declare class Omit extends Gather { | ||
parse(input: string, ctx: Parser, cursor: Reference): SyntaxNode | ParseError; | ||
parse(input: string, cursor: Reference): SyntaxNode | ParseError; | ||
serialize(): any; | ||
@@ -42,10 +48,15 @@ } | ||
constructor(json: any); | ||
parse(input: string, ctx: Parser, cursor: Reference): SyntaxNode | ParseError; | ||
parse(input: string, cursor: Reference): SyntaxNode | ParseError; | ||
link(ctx: Parser): void; | ||
random(depth: number): string; | ||
serialize(): any; | ||
} | ||
export declare class Term { | ||
expr: Rule | null; | ||
value: string; | ||
count: Count; | ||
constructor(json: any); | ||
parse(input: string, ctx: Parser, cursor: Reference): SyntaxNode | ParseError; | ||
parse(input: string, cursor: Reference): SyntaxNode | ParseError; | ||
link(ctx: Parser): void; | ||
random(depth: number): string; | ||
serialize(): any; | ||
@@ -57,4 +68,7 @@ } | ||
constructor(json: any); | ||
parse(input: string, ctx: Parser, cursor: Reference): SyntaxNode | ParseError; | ||
parseSingle(input: string, ctx: Parser, cursor: Reference): SyntaxNode | ParseError; | ||
parse(input: string, cursor: Reference): SyntaxNode | ParseError; | ||
parseSingle(input: string, cursor: Reference): SyntaxNode | ParseError; | ||
link(ctx: Parser): void; | ||
random(depth: number): string; | ||
randomSingle(depth: number): string; | ||
serialize(): any; | ||
@@ -64,4 +78,5 @@ } | ||
constructor(json: any); | ||
parse(input: string, ctx: Parser, cursor: Reference): SyntaxNode | ParseError; | ||
parseSingle(input: string, ctx: Parser, cursor: Reference): SyntaxNode | ParseError; | ||
parse(input: string, cursor: Reference): SyntaxNode | ParseError; | ||
parseSingle(input: string, cursor: Reference): SyntaxNode | ParseError; | ||
randomSingle(depth: number): string; | ||
serialize(): any; | ||
@@ -71,6 +86,8 @@ } | ||
name: string; | ||
seq: Expression; | ||
expr: Expression; | ||
verbose: boolean; | ||
constructor(name: string, json: any); | ||
parse(input: string, ctx: Parser, cursor: Reference): SyntaxNode | ParseError; | ||
parse(input: string, cursor: Reference): SyntaxNode | ParseError; | ||
link(ctx: Parser): void; | ||
random(depth: number): string; | ||
setVerbose(mode: boolean): void; | ||
@@ -85,4 +102,6 @@ serialize(): any; | ||
parse(input: string, partial?: boolean, entry?: string): SyntaxNode | ParseError; | ||
link(): void; | ||
random(entry?: string): string; | ||
setVerbose(mode: boolean): void; | ||
serialize(): any; | ||
} |
@@ -50,2 +50,20 @@ "use strict"; | ||
} | ||
function RandInt(min, max) { | ||
return Math.round(Math.random() * (max - min) + min); | ||
} | ||
function RandomFromCount(limit, min = 1, max = 5) { | ||
switch (limit) { | ||
case Count.One: | ||
return 1; | ||
case Count.ZeroToOne: | ||
return Math.round(Math.random()); | ||
case Count.OneToMany: | ||
min = Math.max(min, 1); | ||
// continue to next case | ||
case Count.ZeroToMany: | ||
return RandInt(min, max); | ||
default: | ||
throw new Error(`Unknown count type ${limit}`); | ||
} | ||
} | ||
class Literal { | ||
@@ -56,3 +74,3 @@ constructor(json) { | ||
} | ||
parse(input, ctx, cursor) { | ||
parse(input, cursor) { | ||
let start = cursor.clone(); | ||
@@ -94,2 +112,14 @@ let consumption = 0; | ||
} | ||
link(ctx) { } | ||
random(depth) { | ||
let ittr = RandomFromCount(this.count, 1, depth * 0.5 + 10); | ||
let out = ""; | ||
for (let i = 0; i < ittr; i++) { | ||
out += this.randomSingle(); | ||
} | ||
return out; | ||
} | ||
randomSingle() { | ||
return this.value; | ||
} | ||
serialize() { | ||
@@ -122,2 +152,5 @@ return { | ||
} | ||
randomSingle() { | ||
return String.fromCharCode(RandInt(this.value.charCodeAt(0), this.to.charCodeAt(0))); | ||
} | ||
serialize() { | ||
@@ -135,4 +168,4 @@ let out = super.serialize(); | ||
} | ||
parse(input, ctx, cursor) { | ||
let res = this.expr.parse(input, ctx, cursor); | ||
parse(input, cursor) { | ||
let res = this.expr.parse(input, cursor); | ||
if (res instanceof syntax_1.ParseError) { | ||
@@ -144,2 +177,8 @@ return res; | ||
} | ||
link(ctx) { | ||
this.expr.link(ctx); | ||
} | ||
random(depth) { | ||
return this.expr.random(depth); | ||
} | ||
serialize() { | ||
@@ -154,4 +193,4 @@ return { | ||
class Omit extends Gather { | ||
parse(input, ctx, cursor) { | ||
let res = this.expr.parse(input, ctx, cursor); | ||
parse(input, cursor) { | ||
let res = this.expr.parse(input, cursor); | ||
if (res instanceof syntax_1.ParseError) { | ||
@@ -174,3 +213,3 @@ return res; | ||
} | ||
parse(input, ctx, cursor) { | ||
parse(input, cursor) { | ||
let start = cursor.clone(); | ||
@@ -185,3 +224,3 @@ let consumption = 0; | ||
} | ||
let check = this.expr.parse(input, ctx, cursor.clone()); | ||
let check = this.expr.parse(input, cursor.clone()); | ||
if (check instanceof syntax_1.ParseError) { | ||
@@ -201,2 +240,8 @@ cursor.advance(input[cursor.index] == "\n"); | ||
} | ||
link(ctx) { | ||
this.expr.link(ctx); | ||
} | ||
random(depth) { | ||
return "{{NOT}}"; | ||
} | ||
serialize() { | ||
@@ -215,5 +260,6 @@ return { | ||
this.count = ParseCount(json['count']); | ||
this.expr = null; | ||
} | ||
parse(input, ctx, cursor) { | ||
let expr = ctx.getRule(this.value); | ||
parse(input, cursor) { | ||
var _a; | ||
let start = cursor.clone(); | ||
@@ -223,2 +269,5 @@ let consumption = 0; | ||
let nodes = []; | ||
if (!(this.expr instanceof Rule)) { | ||
throw new TypeError("Attempting ot parse with unlinked Term"); | ||
} | ||
while (true) { | ||
@@ -231,3 +280,3 @@ if (consumption >= 1 && (this.count == Count.One || this.count == Count.ZeroToOne)) { | ||
} | ||
let res = expr.parse(input, ctx, cursor.clone()); | ||
let res = (_a = this.expr) === null || _a === void 0 ? void 0 : _a.parse(input, cursor.clone()); | ||
if (res instanceof syntax_1.ParseError) { | ||
@@ -256,2 +305,8 @@ err = res; | ||
} | ||
link(ctx) { | ||
this.expr = ctx.getRule(this.value); | ||
} | ||
random(depth) { | ||
return this.expr.random(depth - 1); | ||
} | ||
serialize() { | ||
@@ -274,3 +329,3 @@ return { | ||
} | ||
parse(input, ctx, cursor) { | ||
parse(input, cursor) { | ||
let count = 0; | ||
@@ -284,3 +339,3 @@ let start = cursor.clone(); | ||
} | ||
let res = this.parseSingle(input, ctx, cursor.clone()); | ||
let res = this.parseSingle(input, cursor.clone()); | ||
if (res instanceof syntax_1.ParseError) { | ||
@@ -303,7 +358,7 @@ err = res; | ||
} | ||
parseSingle(input, ctx, cursor) { | ||
parseSingle(input, cursor) { | ||
let span = new syntax_1.ReferenceRange(cursor.clone(), cursor.clone()); | ||
let err = ""; | ||
for (let opt of this.exprs) { | ||
let res = opt.parse(input, ctx, cursor.clone()); | ||
let res = opt.parse(input, cursor.clone()); | ||
if (res instanceof syntax_1.ParseError) { | ||
@@ -322,2 +377,19 @@ span.span(res.ref); | ||
} | ||
link(ctx) { | ||
for (let expr of this.exprs) { | ||
expr.link(ctx); | ||
} | ||
} | ||
random(depth) { | ||
let iter = RandomFromCount(this.count, 0, 5); | ||
let out = ""; | ||
for (let i = 0; i < iter; i++) { | ||
out += this.randomSingle(depth - 1); | ||
} | ||
return out; | ||
} | ||
randomSingle(depth) { | ||
let index = RandInt(0, this.exprs.length - 1); | ||
return this.exprs[index].random(depth - 1); | ||
} | ||
serialize() { | ||
@@ -336,4 +408,4 @@ return { | ||
} | ||
parse(input, ctx, cursor) { | ||
let out = super.parse(input, ctx, cursor); | ||
parse(input, cursor) { | ||
let out = super.parse(input, cursor); | ||
if (out instanceof syntax_1.ParseError) { | ||
@@ -347,3 +419,3 @@ return out; | ||
} | ||
parseSingle(input, ctx, cursor) { | ||
parseSingle(input, cursor) { | ||
let start = cursor.clone(); | ||
@@ -353,3 +425,3 @@ let nodes = []; | ||
for (let rule of this.exprs) { | ||
let res = rule.parse(input, ctx, cursor.clone()); | ||
let res = rule.parse(input, cursor.clone()); | ||
if (res instanceof syntax_1.ParseError) { | ||
@@ -377,2 +449,9 @@ res.ref.span(new syntax_1.ReferenceRange(start, cursor)); | ||
} | ||
randomSingle(depth) { | ||
let out = ""; | ||
for (let expr of this.exprs) { | ||
out += expr.random(depth); | ||
} | ||
return out; | ||
} | ||
serialize() { | ||
@@ -388,10 +467,10 @@ let out = super.serialize(); | ||
this.name = name; | ||
this.seq = ParseExpression(json); | ||
this.expr = ParseExpression(json); | ||
this.verbose = false; | ||
} | ||
parse(input, ctx, cursor) { | ||
parse(input, cursor) { | ||
if (this.verbose) { | ||
console.log(`Parsing rule "${this.name}" at ${cursor.toString()}`); | ||
} | ||
let res = this.seq.parse(input, ctx, cursor); | ||
let res = this.expr.parse(input, cursor); | ||
if (res instanceof syntax_1.SyntaxNode) { | ||
@@ -402,2 +481,11 @@ res.type = this.name; | ||
} | ||
link(ctx) { | ||
this.expr.link(ctx); | ||
} | ||
random(depth) { | ||
if (depth <= 0) { | ||
return "{{CAP}}"; | ||
} | ||
return this.expr.random(depth - 1); | ||
} | ||
setVerbose(mode) { | ||
@@ -407,3 +495,3 @@ this.verbose = mode; | ||
serialize() { | ||
return this.seq.serialize(); | ||
return this.expr.serialize(); | ||
} | ||
@@ -418,2 +506,3 @@ } | ||
} | ||
this.link(); | ||
} | ||
@@ -435,3 +524,3 @@ getRule(name) { | ||
let entryTerm = this.getRule(entry); | ||
let res = entryTerm.parse(input, this, new syntax_1.Reference(1, 1, 0)); | ||
let res = entryTerm.parse(input, new syntax_1.Reference(1, 1, 0)); | ||
if (res instanceof syntax_1.ParseError) { | ||
@@ -445,2 +534,12 @@ return res; | ||
} | ||
link() { | ||
var _a; | ||
for (let key of this.terms.keys()) { | ||
(_a = this.terms.get(key)) === null || _a === void 0 ? void 0 : _a.link(this); | ||
} | ||
} | ||
random(entry = "program") { | ||
let entryTerm = this.getRule(entry); | ||
return entryTerm.random(50); | ||
} | ||
setVerbose(mode) { | ||
@@ -447,0 +546,0 @@ var _a; |
# Changelog | ||
## Version 3.1.1 | ||
### Fixes: | ||
- [x] Incorrect referencing for syntax errors. | ||
## Version 3.1.0 | ||
@@ -4,0 +8,0 @@ |
{ | ||
"name": "bnf-parser", | ||
"version": "3.1.0", | ||
"version": "3.1.1", | ||
"description": "Deterministic BNF compiler/parser", | ||
@@ -5,0 +5,0 @@ "main": "./bin/index.js", |
47220
983