diff --git a/src/Regexer.js b/src/Regexer.js index fcefa1a..4516205 100644 --- a/src/Regexer.js +++ b/src/Regexer.js @@ -158,7 +158,7 @@ export default class Regexer { // Combinators /** - * @template {Regexer[]} P + * @template {[Regexer, Regexer, ...Regexer[]]} P * @param {P} parsers * @returns {Regexer>>} */ @@ -169,7 +169,7 @@ export default class Regexer { } /** - * @template {Regexer[]} P + * @template {[Regexer, Regexer, ...Regexer[]]} P * @param {P} parsers * @returns {Regexer>>} */ diff --git a/src/transformers/AlternativesMergeTransformer.js b/src/transformers/AlternativesMergeTransformer.js deleted file mode 100644 index c194b4c..0000000 --- a/src/transformers/AlternativesMergeTransformer.js +++ /dev/null @@ -1,26 +0,0 @@ -import AlternativeParser from "../parser/AlternativeParser.js" -import Transformer from "./Transformer.js" - -export default class AlternativesMergeTransformer extends Transformer { - - /** - * @template T - * @param {Parser} parser - * @return {Parser} - */ - doTransform(parser) { - if (parser instanceof AlternativeParser) { - /** @type {[Parser, ...Parser[]]} */ - let children = parser.parsers - const writableChildren = () => children === parser.parsers - ? children = [...children] - : children - for (let i = 0; i < children.length; ++i) { - for (let j = i + 1; j < children.length; ++j) { - - } - } - } - return parser - } -} diff --git a/src/transformers/DeadParserElimination.js b/src/transformers/DeadParserElimination.js deleted file mode 100644 index c30e0ec..0000000 --- a/src/transformers/DeadParserElimination.js +++ /dev/null @@ -1,26 +0,0 @@ -import AlternativeParser from "../parser/AlternativeParser.js" -import Transformer from "./Transformer.js" - -export default class DeadParserElimination extends Transformer { - - /** - * @template T - * @param {Parser} parser - * @return {Parser} - */ - doTransform(parser) { - if (parser instanceof AlternativeParser) { - /** @type {[Parser, ...Parser[]]} */ - let children = parser.parsers - const writableChildren = () => children === parser.parsers - ? children = [...children] - : children - for (let i = 0; i < children.length; ++i) { - for (let j = i + 1; j < children.length; ++j) { - - } - } - } - return parser - } -} diff --git a/tests/transformers.spec.js b/tests/transformers.spec.js index a308205..18e8980 100644 --- a/tests/transformers.spec.js +++ b/tests/transformers.spec.js @@ -96,7 +96,8 @@ test("Inline parsers 2", ({ page }) => { R.grp( R.grp( R.seq( - R.grp(R.grp(R.str("3"), "charlie").map(f2), "delta") + R.grp(R.grp(R.str("3"), "charlie").map(f2), "delta"), + R.success(), ).map(f3), "echo" ).map(f1), @@ -113,7 +114,8 @@ test("Inline parsers 2", ({ page }) => { R.alt( R.nonGrp( R.lazy(() => RegExpGrammar.regexp.parse(/london|paris|madrid|milan/.source)) - ) + ), + R.failure(), ) ).map(f1), "india" @@ -139,13 +141,17 @@ test("Inline parsers 2", ({ page }) => { R.grp( R.grp( R.grp( - R.grp( - R.grp( - R.str("3"), - "charlie" - ).map(f2), - "delta" - ).map(f3), + R.grp(R.grp(R.str("3"), "charlie").map(f2), "delta").map(f3), + "echo" + ).map(f1), + "foxtrot" + ).map(f2).map(f2), + "hotel" + ), + R.grp( + R.grp( + R.grp( + R.success().map(f3), "echo" ).map(f1), "foxtrot" @@ -169,6 +175,7 @@ test("Inline parsers 2", ({ page }) => { R.nonGrp(R.str("paris")), R.nonGrp(R.str("madrid")), R.nonGrp(R.str("milan")), + R.failure(), ) ).map(f1), "india" @@ -178,6 +185,114 @@ test("Inline parsers 2", ({ page }) => { )).toBeTruthy() }) +test("Inline parsers 3", ({ page }) => { + const inlineParsers = new InlineParsersTransformer() + expect( + R.equals( + inlineParsers.transform( + R.lazy(() => R.grp( + R.seq( + R.anyChar().times(20), + R.lookaround( + R.seq( + R.str("a"), + R.seq(R.str("b"), R.str("c")), + R.alt( + R.str("A"), + R.alt( + R.alt( + R.str("B"), + R.str("C"), + ), + R.alt( + R.str("D"), + R.str("E"), + R.alt( + R.str("F"), + R.str("G"), + ) + ), + R.str("H"), + ), + ), + R.str("d"), + R.seq( + R.seq(R.seq(R.str("e"), R.str("f")), R.str("g")), + R.str("h"), + R.alt( + R.alt( + R.regexp(/1/), + R.regexp(/2/), + R.alt( + R.alt( + R.regexp(/3/), + R.regexp(/4/), + ), + R.regexp(/5/), + R.alt( + R.regexp(/6/), + R.alt( + R.regexp(/7/), + R.regexp(/8/), + ) + ), + ), + ), + R.regexp(/9/), + ), + ), + ), + LookaroundParser.Type.NEGATIVE_BEHIND + ), + ), + "aaa" + )) + ), + R.lazy(() => R.grp( + R.seq( + R.anyChar().times(20), + R.lookaround( + R.seq( + R.str("a"), + R.str("b"), + R.str("c"), + R.alt( + R.str("A"), + R.str("B"), + R.str("C"), + R.str("D"), + R.str("E"), + R.str("F"), + R.str("G"), + R.str("H"), + ), + R.str("d"), + R.str("e"), + R.str("f"), + R.str("g"), + R.str("h"), + R.alt( + R.regexp(/1/), + R.regexp(/2/), + R.regexp(/3/), + R.regexp(/4/), + R.regexp(/5/), + R.regexp(/6/), + R.regexp(/7/), + R.regexp(/8/), + R.regexp(/9/), + ), + ), + LookaroundParser.Type.NEGATIVE_BEHIND + ), + ), + "aaa" + )), + true + ) + ).toBeTruthy() +}) + test("Remove discarded map 1", ({ page }) => { const removeDiscardedMap = new RemoveDiscardedMapTransformer() expect(