1
0
mirror of https://github.com/pdemian/human2regex.git synced 2025-05-16 12:30:09 -07:00

Fixed build

This commit is contained in:
Patrick Demian 2020-11-19 08:33:51 -05:00
parent cbb298ece6
commit 4b8073a7c4
9 changed files with 42 additions and 37 deletions

View File

@ -81,6 +81,7 @@ The API reference is available [here](API.md)
## Todo ## Todo
- Add more regex options such as back references, subroutines, conditions, and lookahead/behind - Add more regex options such as subroutines, conditions, and lookahead/behind
- Fix error messages (They sometimes point to the wrong location, off by 1 errors, etc) - Fix error messages (They sometimes point to the wrong location, off by 1 errors, etc)
- Add more useful lex/parse errors (What even is an EarlyExitException?)
- Use a different/better static site generation method - Use a different/better static site generation method

6
docs/bundle.min.js vendored

File diff suppressed because one or more lines are too long

View File

@ -476,10 +476,7 @@ class CountSubStatementCST extends H2RCST {
utilities_1.unusedParameter(language, "Count does not need checking"); utilities_1.unusedParameter(language, "Count does not need checking");
utilities_1.unusedParameter(context, "Context is not needed"); utilities_1.unusedParameter(context, "Context is not needed");
const errors = []; const errors = [];
if (this.from < 0) { if (this.to !== null && ((this.opt === "exclusive" && (this.to - 1) <= this.from) || this.to <= this.from)) {
errors.push(this.error("Value cannot be negative"));
}
else if (this.to !== null && ((this.opt === "exclusive" && (this.to - 1) <= this.from) || this.to <= this.from)) {
errors.push(this.error("Values must be in range of eachother")); errors.push(this.error("Values must be in range of eachother"));
} }
return errors; return errors;
@ -694,7 +691,7 @@ class BackrefStatementCST extends StatementCST {
validate(language, context) { validate(language, context) {
const errors = []; const errors = [];
if (!context.hasGroup(this.name)) { if (!context.hasGroup(this.name)) {
errors.push(this.error(`Cannot call group with name "${this.name}" as it was never defined`)); errors.push(this.error(`Cannot call group with name "${this.name}" as it was never previously defined`));
} }
if (this.count !== null) { if (this.count !== null) {
utilities_1.append(errors, this.count.validate(language, context)); utilities_1.append(errors, this.count.validate(language, context));

View File

@ -503,20 +503,19 @@ class Human2RegexParser extends chevrotain_1.EmbeddedActionsParser {
const tokens = []; const tokens = [];
let optional = false; let optional = false;
let count = null; let count = null;
$.OPTION4(() => { $.OPTION5(() => {
tokens.push($.CONSUME(T.Optional)); tokens.push($.CONSUME(T.Optional));
optional = true; optional = true;
}); });
tokens.push($.CONSUME(T.Call)); tokens.push($.CONSUME(T.Call));
$.OPTION5(() => count = $.SUBRULE(CountSubStatement)); $.OPTION6(() => count = $.SUBRULE(CountSubStatement));
$.OPTION6(() => { $.OPTION7(() => {
$.OPTION(() => $.CONSUME(T.The)); $.OPTION(() => $.CONSUME(T.The));
$.CONSUME(T.Group); $.CONSUME(T.Group);
$.OPTION2(() => $.CONSUME(T.Called)); $.OPTION2(() => $.CONSUME(T.Called));
}); });
const name_token = $.CONSUME(T.Identifier); const name = $.CONSUME(T.Identifier).image;
tokens.push(name_token); tokens.push($.CONSUME4(T.EndOfLine));
const name = name_token.image;
return new generator_1.BackrefStatementCST(tokens, optional, count, name); return new generator_1.BackrefStatementCST(tokens, optional, count, name);
}); });
// statement super class // statement super class

View File

@ -52,14 +52,14 @@ const chevrotain_1 = require("chevrotain");
/** @internal */ exports.From = chevrotain_1.createToken({ name: "From", pattern: /from/i }); /** @internal */ exports.From = chevrotain_1.createToken({ name: "From", pattern: /from/i });
/** @internal */ exports.To = chevrotain_1.createToken({ name: "To", pattern: /(to|through|thru|\-|\.\.\.?)/i }); /** @internal */ exports.To = chevrotain_1.createToken({ name: "To", pattern: /(to|through|thru|\-|\.\.\.?)/i });
/** @internal */ exports.Create = chevrotain_1.createToken({ name: "Create", pattern: /create(s)?/i }); /** @internal */ exports.Create = chevrotain_1.createToken({ name: "Create", pattern: /create(s)?/i });
/** @internal */ exports.Called = chevrotain_1.createToken({ name: "Called", pattern: /name(d)?|call(ed)?/i }); /** @internal */ exports.Called = chevrotain_1.createToken({ name: "Called", pattern: /named|called/i });
/** @internal */ exports.Repeat = chevrotain_1.createToken({ name: "Repeat", pattern: /repeat(s|ing)?/i }); /** @internal */ exports.Repeat = chevrotain_1.createToken({ name: "Repeat", pattern: /repeat(s|ing)?/i });
/** @internal */ exports.Newline = chevrotain_1.createToken({ name: "Newline", pattern: /(new line|newline)/i }); /** @internal */ exports.Newline = chevrotain_1.createToken({ name: "Newline", pattern: /(new line|newline)/i });
/** @internal */ exports.CarriageReturn = chevrotain_1.createToken({ name: "CarriageReturn", pattern: /carriage return/i }); /** @internal */ exports.CarriageReturn = chevrotain_1.createToken({ name: "CarriageReturn", pattern: /carriage return/i });
/** @internal */ exports.CaseInsensitive = chevrotain_1.createToken({ name: "CaseInsensitive", pattern: /case insensitive/i }); /** @internal */ exports.CaseInsensitive = chevrotain_1.createToken({ name: "CaseInsensitive", pattern: /case insensitive/i });
/** @internal */ exports.CaseSensitive = chevrotain_1.createToken({ name: "CaseSensitive", pattern: /case sensitive/i }); /** @internal */ exports.CaseSensitive = chevrotain_1.createToken({ name: "CaseSensitive", pattern: /case sensitive/i });
/** @internal */ exports.OrMore = chevrotain_1.createToken({ name: "OrMore", pattern: /\+|or more/i }); /** @internal */ exports.OrMore = chevrotain_1.createToken({ name: "OrMore", pattern: /\+|or more/i });
/** @internal */ exports.Call = chevrotain_1.createToken({ name: "Call", pattern: /call|invoke|execute|run|do|perform/i }); /** @internal */ exports.Call = chevrotain_1.createToken({ name: "Call", pattern: /call|invoke|execute|(re ?)?run/i });
/** @internal */ exports.The = chevrotain_1.createToken({ name: "The", pattern: /the/i }); /** @internal */ exports.The = chevrotain_1.createToken({ name: "The", pattern: /the/i });
/* /*
//Not being used currently //Not being used currently

View File

@ -536,10 +536,7 @@ export class CountSubStatementCST extends H2RCST {
const errors: ISemanticError[] = []; const errors: ISemanticError[] = [];
if (this.from < 0) { if (this.to !== null && ((this.opt === "exclusive" && (this.to-1) <= this.from) || this.to <= this.from)) {
errors.push(this.error("Value cannot be negative"));
}
else if (this.to !== null && ((this.opt === "exclusive" && (this.to-1) <= this.from) || this.to <= this.from)) {
errors.push(this.error("Values must be in range of eachother")); errors.push(this.error("Values must be in range of eachother"));
} }
@ -785,7 +782,7 @@ export class BackrefStatementCST extends StatementCST {
const errors: ISemanticError[] = []; const errors: ISemanticError[] = [];
if (!context.hasGroup(this.name)) { if (!context.hasGroup(this.name)) {
errors.push(this.error(`Cannot call group with name "${this.name}" as it was never defined`)); errors.push(this.error(`Cannot call group with name "${this.name}" as it was never previously defined`));
} }
if (this.count !== null) { if (this.count !== null) {

View File

@ -563,23 +563,23 @@ export class Human2RegexParser extends EmbeddedActionsParser {
let optional = false; let optional = false;
let count: CountSubStatementCST | null = null; let count: CountSubStatementCST | null = null;
$.OPTION4(() => { $.OPTION5(() => {
tokens.push($.CONSUME(T.Optional)); tokens.push($.CONSUME(T.Optional));
optional = true; optional = true;
}); });
tokens.push($.CONSUME(T.Call)); tokens.push($.CONSUME(T.Call));
$.OPTION5(() => count = $.SUBRULE(CountSubStatement)); $.OPTION6(() => count = $.SUBRULE(CountSubStatement));
$.OPTION6(() => { $.OPTION7(() => {
$.OPTION(() => $.CONSUME(T.The)); $.OPTION(() => $.CONSUME(T.The));
$.CONSUME(T.Group); $.CONSUME(T.Group);
$.OPTION2(() => $.CONSUME(T.Called)); $.OPTION2(() => $.CONSUME(T.Called));
}); });
const name_token = $.CONSUME(T.Identifier); const name = $.CONSUME(T.Identifier).image;
tokens.push(name_token);
const name = name_token.image; tokens.push($.CONSUME4(T.EndOfLine));
return new BackrefStatementCST(tokens, optional, count, name); return new BackrefStatementCST(tokens, optional, count, name);
}); });

View File

@ -53,15 +53,16 @@ import { createToken, Lexer } from "chevrotain";
/** @internal */ export const From = createToken({name: "From", pattern: /from/i}); /** @internal */ export const From = createToken({name: "From", pattern: /from/i});
/** @internal */ export const To = createToken({name: "To", pattern: /(to|through|thru|\-|\.\.\.?)/i}); /** @internal */ export const To = createToken({name: "To", pattern: /(to|through|thru|\-|\.\.\.?)/i});
/** @internal */ export const Create = createToken({name: "Create", pattern: /create(s)?/i}); /** @internal */ export const Create = createToken({name: "Create", pattern: /create(s)?/i});
/** @internal */ export const Called = createToken({name: "Called", pattern: /name(d)?|call(ed)?/i}); /** @internal */ export const Called = createToken({name: "Called", pattern: /named|called/i});
/** @internal */ export const Repeat = createToken({name: "Repeat", pattern: /repeat(s|ing)?/i}); /** @internal */ export const Repeat = createToken({name: "Repeat", pattern: /repeat(s|ing)?/i});
/** @internal */ export const Newline = createToken({name: "Newline", pattern: /(new line|newline)/i}); /** @internal */ export const Newline = createToken({name: "Newline", pattern: /(new line|newline)/i});
/** @internal */ export const CarriageReturn = createToken({name: "CarriageReturn", pattern: /carriage return/i}); /** @internal */ export const CarriageReturn = createToken({name: "CarriageReturn", pattern: /carriage return/i});
/** @internal */ export const CaseInsensitive = createToken({name: "CaseInsensitive", pattern: /case insensitive/i}); /** @internal */ export const CaseInsensitive = createToken({name: "CaseInsensitive", pattern: /case insensitive/i});
/** @internal */ export const CaseSensitive = createToken({name: "CaseSensitive", pattern: /case sensitive/i}); /** @internal */ export const CaseSensitive = createToken({name: "CaseSensitive", pattern: /case sensitive/i});
/** @internal */ export const OrMore = createToken({name: "OrMore", pattern: /\+|or more/i}); /** @internal */ export const OrMore = createToken({name: "OrMore", pattern: /\+|or more/i});
/** @internal */ export const Call = createToken({name: "Call", pattern: /call|invoke|execute|run|do|perform/i}); /** @internal */ export const Call = createToken({name: "Call", pattern: /call|invoke|execute|(re ?)?run/i });
/** @internal */ export const The = createToken({name: "The", pattern: /the/i }); /** @internal */ export const The = createToken({name: "The", pattern: /the/i });
/* /*
//Not being used currently //Not being used currently
export const Of = createToken({name: "Of", pattern: /of/i}); export const Of = createToken({name: "Of", pattern: /of/i});

View File

@ -68,9 +68,13 @@ describe("Generator functionality", function() {
const reg5 = parser.parse(toks5); const reg5 = parser.parse(toks5);
expect(reg5.validate(RegexDialect.JS).length).toBeGreaterThan(0); expect(reg5.validate(RegexDialect.JS).length).toBeGreaterThan(0);
const toks6 = lexer.tokenize("create a group called thing\ncreate a group called thing").tokens; const toks6 = lexer.tokenize('create a group called thing\n\tmatch "hi"\ncreate a group called thing\n\tmatch "hi"\n').tokens;
const reg6 = parser.parse(toks6); const reg6 = parser.parse(toks6);
expect(reg6.validate(RegexDialect.JS).length).toBeGreaterThan(0); expect(reg6.validate(RegexDialect.JS).length).toBeGreaterThan(0);
const toks7 = lexer.tokenize("invoke thing").tokens;
const reg7 = parser.parse(toks7);
expect(reg7.validate(RegexDialect.JS).length).toBeGreaterThan(0);
}); });
it("handles ranges", function() { it("handles ranges", function() {
@ -101,6 +105,12 @@ describe("Generator functionality", function() {
expect(reg2.validate(RegexDialect.JS).length).toBe(0); expect(reg2.validate(RegexDialect.JS).length).toBe(0);
expect(reg2.toRegex(RegexDialect.JS)).toBe("/[a-zA-Z][+-]?\\d+[+-]?(?:(?:\\d+[,.]?\\d*)|(?:[,.]\\d+))/"); expect(reg2.toRegex(RegexDialect.JS)).toBe("/[a-zA-Z][+-]?\\d+[+-]?(?:(?:\\d+[,.]?\\d*)|(?:[,.]\\d+))/");
expect(reg2.toRegex(RegexDialect.PCRE)).toBe("/[[:alpha:]][+-]?\\d+[+-]?(?:(?:\\d+[,.]?\\d*)|(?:[,.]\\d+))/"); expect(reg2.toRegex(RegexDialect.PCRE)).toBe("/[[:alpha:]][+-]?\\d+[+-]?(?:(?:\\d+[,.]?\\d*)|(?:[,.]\\d+))/");
const toks3 = lexer.tokenize("match not letter, not integer, not decimal").tokens;
const reg3 = parser.parse(toks3);
expect(reg3.validate(RegexDialect.JS).length).toBe(0);
expect(reg3.toRegex(RegexDialect.JS)).toBe("/[^a-zA-Z](?![+-]?\\d+)(?![+-]?(?:(?:\\d+[,.]?\\d*)|(?:[,.]\\d+)))/");
expect(reg3.toRegex(RegexDialect.PCRE)).toBe("/[^[:alpha:]](?![+-]?\\d+)(?![+-]?(?:(?:\\d+[,.]?\\d*)|(?:[,.]\\d+)))/");
}); });
it("doesn't clobber repetition", function() { it("doesn't clobber repetition", function() {
@ -164,14 +174,14 @@ describe("Generator functionality", function() {
}); });
it("can generate backreferences", function() { it("can generate backreferences", function() {
const toks0 = lexer.tokenize('create a group called thing\n\tmatch "Hello World\ncall thing\noptionally call 3 times the group called thing').tokens; const toks0 = lexer.tokenize('create a group called thing\n\tmatch "Hello World"\ninvoke thing\noptionally call 3 times the group called thing').tokens;
const reg0 = parser.parse(toks0); const reg0 = parser.parse(toks0);
expect(reg0.validate(RegexDialect.JS).length).toBe(0); expect(reg0.validate(RegexDialect.JS).length).toBe(0);
expect(reg0.toRegex(RegexDialect.JS)).toBe("/[ab]/"); expect(reg0.toRegex(RegexDialect.JS)).toBe("/(?<thing>Hello World)\\g<thing>(?:\\g<thing>{3})?/");
expect(reg0.toRegex(RegexDialect.PCRE)).toBe("/[ab]/"); expect(reg0.toRegex(RegexDialect.PCRE)).toBe("/(?P<thing>Hello World)\\g<thing>(?:\\g<thing>{3})?/");
expect(reg0.toRegex(RegexDialect.Python)).toBe("/[ab]/"); expect(reg0.toRegex(RegexDialect.Python)).toBe("/(?P<thing>Hello World)(?P=thing)(?:(?P=thing){3})?/");
expect(reg0.toRegex(RegexDialect.DotNet)).toBe("/[ab]/"); expect(reg0.toRegex(RegexDialect.DotNet)).toBe("/(?<thing>Hello World)\\k<thing>(?:\\k<thing>{3})?/");
}); });
it("generate dialect specific regex", function() { it("generate dialect specific regex", function() {