From fc67be3d926d21194fca5e8ff733e0921f6e141c Mon Sep 17 00:00:00 2001 From: EuAndreh Date: Fri, 11 Jul 2025 15:41:22 -0300 Subject: src/paca.mjs (tokenizeRegexStep): Support tokenizing range exps {m,n} --- tests/paca.mjs | 540 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 540 insertions(+) (limited to 'tests/paca.mjs') diff --git a/tests/paca.mjs b/tests/paca.mjs index 82102f1..4f085e4 100644 --- a/tests/paca.mjs +++ b/tests/paca.mjs @@ -353,6 +353,546 @@ const test_tokenizeRegexStep = t => { ); } }); + + t.testing("multichar range operator {m,n} is parsed", () => { + const table = [{ + regex: "a{1,2}", + steps: [{ + in: { + out: [], + state: ConcatStep.ACCEPTING, + context: null, + }, + out: { + out: ["a", { operator: "concat" }], + state: ConcatStep.ACCEPTING, + context: null, + }, + }, { + in: { + out: [], + state: ConcatStep.ACCEPTING, + context: null, + }, + out: { + out: [], + state: ConcatStep.RANGE, + context: { + from: [], + to: [], + where: "from", + }, + }, + }, { + in: { + out: [], + state: ConcatStep.RANGE, + context: { + from: [], + to: [], + where: "from", + }, + }, + out: { + out: [], + state: ConcatStep.RANGE, + context: { + from: [ "1" ], + to: [], + where: "from", + }, + }, + }, { + in: { + out: [], + state: ConcatStep.RANGE, + context: { + from: [ "1" ], + to: [], + where: "from", + }, + }, + out: { + out: [], + state: ConcatStep.RANGE, + context: { + from: [ "1" ], + to: [], + where: "to", + }, + }, + }, { + in: { + out: [], + state: ConcatStep.RANGE, + context: { + from: [ "1" ], + to: [], + where: "to", + }, + }, + out: { + out: [], + state: ConcatStep.RANGE, + context: { + from: [ "1" ], + to: [ "2" ], + where: "to", + }, + }, + }, { + in: { + out: [], + state: ConcatStep.RANGE, + context: { + from: [ "1" ], + to: [ "2" ], + where: "to", + }, + }, + out: { + out: [{ + operator: "range", + from: 1, + to: 2, + }], + state: ConcatStep.ACCEPTING, + context: null, + }, + }], + }, { + regex: "a{,2}", + steps: [{ + in: { + out: [], + state: ConcatStep.ACCEPTING, + context: null, + }, + out: { + out: ["a", { operator: "concat" }], + state: ConcatStep.ACCEPTING, + context: null, + }, + }, { + in: { + out: [], + state: ConcatStep.ACCEPTING, + context: null, + }, + out: { + out: [], + state: ConcatStep.RANGE, + context: { + from: [], + to: [], + where: "from", + }, + }, + }, { + in: { + out: [], + state: ConcatStep.RANGE, + context: { + from: [], + to: [], + where: "from", + }, + }, + out: { + out: [], + state: ConcatStep.RANGE, + context: { + from: [], + to: [], + where: "to", + }, + }, + }, { + in: { + out: [], + state: ConcatStep.RANGE, + context: { + from: [], + to: [], + where: "to", + }, + }, + out: { + out: [], + state: ConcatStep.RANGE, + context: { + from: [], + to: [ "2" ], + where: "to", + }, + }, + }, { + in: { + out: [], + state: ConcatStep.RANGE, + context: { + from: [], + to: [ "2" ], + where: "to", + }, + }, + out: { + out: [{ + operator: "range", + from: -1, + to: 2, + }], + state: ConcatStep.ACCEPTING, + context: null, + }, + }], + }, { + regex: "a{1,}", + steps: [{ + in: { + out: [], + state: ConcatStep.ACCEPTING, + context: null, + }, + out: { + out: ["a", { operator: "concat" }], + state: ConcatStep.ACCEPTING, + context: null, + }, + }, { + in: { + out: [], + state: ConcatStep.ACCEPTING, + context: null, + }, + out: { + out: [], + state: ConcatStep.RANGE, + context: { + from: [], + to: [], + where: "from", + }, + }, + }, { + in: { + out: [], + state: ConcatStep.RANGE, + context: { + from: [], + to: [], + where: "from", + }, + }, + out: { + out: [], + state: ConcatStep.RANGE, + context: { + from: [ "1" ], + to: [], + where: "from", + }, + }, + }, { + in: { + out: [], + state: ConcatStep.RANGE, + context: { + from: [ "1" ], + to: [], + where: "from", + }, + }, + out: { + out: [], + state: ConcatStep.RANGE, + context: { + from: [ "1" ], + to: [], + where: "to", + }, + }, + }, { + in: { + out: [], + state: ConcatStep.RANGE, + context: { + from: [ "1" ], + to: [], + where: "to", + }, + }, + out: { + out: [{ + operator: "range", + from: 1, + to: -1, + }], + state: ConcatStep.ACCEPTING, + context: null, + }, + }], + }, { + regex: "a{,}", + steps: [{ + in: { + out: [], + state: ConcatStep.ACCEPTING, + context: null, + }, + out: { + out: ["a", { operator: "concat" }], + state: ConcatStep.ACCEPTING, + context: null, + }, + }, { + in: { + out: [], + state: ConcatStep.ACCEPTING, + context: null, + }, + out: { + out: [], + state: ConcatStep.RANGE, + context: { + from: [], + to: [], + where: "from", + }, + }, + }, { + in: { + out: [], + state: ConcatStep.RANGE, + context: { + from: [], + to: [], + where: "from", + }, + }, + out: { + out: [], + state: ConcatStep.RANGE, + context: { + from: [], + to: [], + where: "to", + }, + }, + }, { + in: { + out: [], + state: ConcatStep.RANGE, + context: { + from: [], + to: [], + where: "to", + }, + }, + out: { + out: [{ + operator: "range", + from: -1, + to: -1, + }], + state: ConcatStep.ACCEPTING, + context: null, + }, + }], + }, { + regex: "a{123,456}", + steps: [{ + in: { + out: [], + state: ConcatStep.ACCEPTING, + context: null, + }, + out: { + out: ["a", { operator: "concat" }], + state: ConcatStep.ACCEPTING, + context: null, + }, + }, { + in: { + out: [], + state: ConcatStep.ACCEPTING, + context: null, + }, + out: { + out: [], + state: ConcatStep.RANGE, + context: { + from: [], + to: [], + where: "from", + }, + }, + }, { + in: { + out: [], + state: ConcatStep.RANGE, + context: { + from: [], + to: [], + where: "from", + }, + }, + out: { + out: [], + state: ConcatStep.RANGE, + context: { + from: [ "1" ], + to: [], + where: "from", + }, + }, + }, { + in: { + out: [], + state: ConcatStep.RANGE, + context: { + from: [ "1" ], + to: [], + where: "from", + }, + }, + out: { + out: [], + state: ConcatStep.RANGE, + context: { + from: [ "1", "2" ], + to: [], + where: "from", + }, + }, + }, { + in: { + out: [], + state: ConcatStep.RANGE, + context: { + from: [ "1", "2" ], + to: [], + where: "from", + }, + }, + out: { + out: [], + state: ConcatStep.RANGE, + context: { + from: [ "1", "2", "3" ], + to: [], + where: "from", + }, + }, + }, { + in: { + out: [], + state: ConcatStep.RANGE, + context: { + from: [ "1", "2", "3" ], + to: [], + where: "from", + }, + }, + out: { + out: [], + state: ConcatStep.RANGE, + context: { + from: [ "1", "2", "3" ], + to: [], + where: "to", + }, + }, + }, { + in: { + out: [], + state: ConcatStep.RANGE, + context: { + from: [ "1", "2", "3" ], + to: [], + where: "to", + }, + }, + out: { + out: [], + state: ConcatStep.RANGE, + context: { + from: [ "1", "2", "3" ], + to: [ "4" ], + where: "to", + }, + }, + }, { + in: { + out: [], + state: ConcatStep.RANGE, + context: { + from: [ "1", "2", "3" ], + to: [ "4" ], + where: "to", + }, + }, + out: { + out: [], + state: ConcatStep.RANGE, + context: { + from: [ "1", "2", "3" ], + to: [ "4", "5" ], + where: "to", + }, + }, + }, { + in: { + out: [], + state: ConcatStep.RANGE, + context: { + from: [ "1", "2", "3" ], + to: [ "4", "5" ], + where: "to", + }, + }, + out: { + out: [], + state: ConcatStep.RANGE, + context: { + from: [ "1", "2", "3" ], + to: [ "4", "5", "6" ], + where: "to", + }, + }, + }, { + in: { + out: [], + state: ConcatStep.RANGE, + context: { + from: [ "1", "2", "3" ], + to: [ "4", "5", "6" ], + where: "to", + }, + }, + out: { + out: [{ + operator: "range", + from: 123, + to: 456, + }], + state: ConcatStep.ACCEPTING, + context: null, + }, + }], + }]; + for (const case_ of table) { + const stepFn = tokenizeRegexStep(case_.regex); + for (const i in case_.regex) { + const step = case_.steps[i]; + const char = case_.regex[i]; + t.assertEq( + stepFn(step.in, char, Number(i)), + step.out, + ); + } + } + }); }; const test_tokenizeRegexFn = t => { -- cgit v1.2.3