mirror of
https://github.com/pdemian/human2regex.git
synced 2025-05-15 20:10:19 -07:00
more bug fixes
This commit is contained in:
parent
efa41ff249
commit
0026264eef
@ -6,6 +6,7 @@ docs/
|
||||
src/
|
||||
tests/
|
||||
jest.config.ts
|
||||
config.json
|
||||
webpack.config.js
|
||||
tsconfig.json
|
||||
tslintconfig.json
|
||||
|
2
docs/bundle.min.js
vendored
2
docs/bundle.min.js
vendored
File diff suppressed because one or more lines are too long
2
lib/generator.d.ts
vendored
2
lib/generator.d.ts
vendored
@ -244,7 +244,7 @@ export declare class CountSubStatementCST extends H2RCST {
|
||||
*/
|
||||
constructor(tokens: IToken[], from: number, to?: number | null, opt?: "inclusive" | "exclusive" | "+" | null);
|
||||
validate(language: RegexDialect, context: GeneratorContext): ISemanticError[];
|
||||
toRegex(language: RegexDialect): string;
|
||||
toRegex(language: RegexDialect, args: GeneratorArguments | null): string;
|
||||
}
|
||||
/**
|
||||
* Concrete Syntax Tree for a Match statement
|
||||
|
@ -365,7 +365,7 @@ class MatchSubStatementCST extends H2RCST {
|
||||
}
|
||||
}
|
||||
let ret = "";
|
||||
if (args !== null && args.has_neighbours === true) {
|
||||
if (args === null || args === void 0 ? void 0 : args.has_neighbours) {
|
||||
ret = generator_helper_1.minimizeMatchString(matches, true);
|
||||
}
|
||||
else {
|
||||
@ -375,10 +375,13 @@ class MatchSubStatementCST extends H2RCST {
|
||||
if (matches.length === 1) {
|
||||
// we don't group if there's only 1 element
|
||||
// but we need to make sure we don't add an additional + or *
|
||||
ret = generator_helper_1.dontClobberRepetition(ret, this.count.toRegex(language));
|
||||
ret = generator_helper_1.dontClobberRepetition(ret, this.count.toRegex(language, null));
|
||||
}
|
||||
else {
|
||||
ret = generator_helper_1.groupIfRequired(ret) + this.count.toRegex(language);
|
||||
const count_string = this.count.toRegex(language, { "dont_loop_one": true });
|
||||
if (count_string !== "") {
|
||||
ret = generator_helper_1.groupIfRequired(ret) + count_string;
|
||||
}
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
@ -468,14 +471,19 @@ class CountSubStatementCST extends H2RCST {
|
||||
}
|
||||
return errors;
|
||||
}
|
||||
toRegex(language) {
|
||||
toRegex(language, args) {
|
||||
utilities_1.unusedParameter(language, "Count does not change from language");
|
||||
const from = this.from;
|
||||
let to = this.to;
|
||||
// if we only have a count of 1, we can ignore adding any extra text
|
||||
if (to === null) {
|
||||
if (from === 1) {
|
||||
return this.opt === "+" ? "+" : "*";
|
||||
if (args === null || args === void 0 ? void 0 : args.dont_loop_one) {
|
||||
return this.opt === "+" ? "+" : "";
|
||||
}
|
||||
else {
|
||||
return this.opt === "+" ? "+" : "*";
|
||||
}
|
||||
}
|
||||
else if (from === 0) {
|
||||
return this.opt === "+" ? "*" : "{0}";
|
||||
@ -568,7 +576,7 @@ class RepeatStatementCST extends StatementCST {
|
||||
toRegex(language) {
|
||||
let str = generator_helper_1.groupIfRequired(this.statements.map((x) => x.toRegex(language, null)).join(""));
|
||||
if (this.count) {
|
||||
str += this.count.toRegex(language);
|
||||
str += this.count.toRegex(language, null);
|
||||
// group for optionality because count would be incorrect otherwise
|
||||
if (this.optional) {
|
||||
str = "(?:" + str + ")?";
|
||||
@ -683,7 +691,7 @@ class BackrefStatementCST extends StatementCST {
|
||||
break;
|
||||
}
|
||||
if (this.count) {
|
||||
str += this.count.toRegex(language);
|
||||
str += this.count.toRegex(language, null);
|
||||
// group for optionality because count would be incorrect otherwise
|
||||
if (this.optional) {
|
||||
str = "(?:" + str + ")?";
|
||||
|
@ -425,7 +425,7 @@ export class MatchSubStatementCST extends H2RCST {
|
||||
}
|
||||
|
||||
let ret = "";
|
||||
if (args !== null && args.has_neighbours === true) {
|
||||
if (args?.has_neighbours) {
|
||||
ret = minimizeMatchString(matches, true);
|
||||
}
|
||||
else {
|
||||
@ -436,10 +436,14 @@ export class MatchSubStatementCST extends H2RCST {
|
||||
if (matches.length === 1) {
|
||||
// we don't group if there's only 1 element
|
||||
// but we need to make sure we don't add an additional + or *
|
||||
ret = dontClobberRepetition(ret, this.count.toRegex(language));
|
||||
ret = dontClobberRepetition(ret, this.count.toRegex(language, null));
|
||||
}
|
||||
else {
|
||||
ret = groupIfRequired(ret) + this.count.toRegex(language);
|
||||
const count_string = this.count.toRegex(language, { "dont_loop_one": true });
|
||||
|
||||
if (count_string !== "") {
|
||||
ret = groupIfRequired(ret) + count_string;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -541,7 +545,7 @@ export class CountSubStatementCST extends H2RCST {
|
||||
return errors;
|
||||
}
|
||||
|
||||
public toRegex(language: RegexDialect): string {
|
||||
public toRegex(language: RegexDialect, args: GeneratorArguments | null): string {
|
||||
unusedParameter(language, "Count does not change from language");
|
||||
|
||||
const from = this.from;
|
||||
@ -551,7 +555,12 @@ export class CountSubStatementCST extends H2RCST {
|
||||
// if we only have a count of 1, we can ignore adding any extra text
|
||||
if (to === null) {
|
||||
if (from === 1) {
|
||||
return this.opt === "+" ? "+" : "*";
|
||||
if (args?.dont_loop_one) {
|
||||
return this.opt === "+" ? "+" : "";
|
||||
}
|
||||
else {
|
||||
return this.opt === "+" ? "+" : "*";
|
||||
}
|
||||
}
|
||||
else if (from === 0) {
|
||||
return this.opt === "+" ? "*" : "{0}";
|
||||
@ -654,7 +663,7 @@ export class RepeatStatementCST extends StatementCST {
|
||||
let str = groupIfRequired(this.statements.map((x) => x.toRegex(language, null)).join(""));
|
||||
|
||||
if (this.count) {
|
||||
str += this.count.toRegex(language);
|
||||
str += this.count.toRegex(language, null);
|
||||
|
||||
// group for optionality because count would be incorrect otherwise
|
||||
if (this.optional) {
|
||||
@ -788,7 +797,7 @@ export class BackrefStatementCST extends StatementCST {
|
||||
}
|
||||
|
||||
if (this.count) {
|
||||
str += this.count.toRegex(language);
|
||||
str += this.count.toRegex(language, null);
|
||||
|
||||
// group for optionality because count would be incorrect otherwise
|
||||
if (this.optional) {
|
||||
|
@ -139,6 +139,18 @@ describe("Generator functionality", function() {
|
||||
|
||||
// should be (?!hello) not (?:(?!hello))
|
||||
expect(reg1.toRegex(RegexDialect.JS)).toBe("/(?!hello){1,6}/");
|
||||
|
||||
const toks2 = lexer.tokenize('match 1 words or "_" or "-"').tokens;
|
||||
const reg2 = parser.parse(toks2);
|
||||
expect(reg2.validate(RegexDialect.JS).length).toBe(0);
|
||||
|
||||
// should be /\w+|_|\-/ not /(?:\w+|_|\-)*/
|
||||
expect(reg2.toRegex(RegexDialect.JS)).toBe("/\\w+|_|\\-/");
|
||||
|
||||
const toks3 = lexer.tokenize('repeat 1\n\tmatch "http"').tokens;
|
||||
const reg3 = parser.parse(toks3);
|
||||
expect(reg3.validate(RegexDialect.JS).length).toBe(0);
|
||||
expect(reg3.toRegex(RegexDialect.JS)).toBe("/(?:http)*/");
|
||||
});
|
||||
|
||||
it("optimizes correctly", function() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user