askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
dron_regex_reference.js · 1.5 KB · 38 lines
| 1 | // The ECMAScript reading of the DRON regular-expression cases (docs/dron.md, |
| 2 | // "Regular expressions"). It reads {"patterns": [...], "refused": [...], |
| 3 | // "samples": [...]} on stdin and writes, for each pattern, the samples the |
| 4 | // WHOLE of which it matches under the `u` flag: `^(?:pattern)$`. |
| 5 | // |
| 6 | // python tests/test_dron_regex.py | node tests/dron_regex_reference.js \ |
| 7 | // > tests/dron_regex_ecmascript.json |
| 8 | // |
| 9 | // tests/test_dron_regex.py compares the engine with the file this writes, and |
| 10 | // runs this script itself when `node` is installed. |
| 11 | "use strict"; |
| 12 | |
| 13 | let input = ""; |
| 14 | process.stdin.setEncoding("utf8"); |
| 15 | process.stdin.on("data", (chunk) => { input += chunk; }); |
| 16 | process.stdin.on("end", () => { |
| 17 | const cases = JSON.parse(input); |
| 18 | const compiles = (pattern, flags) => { |
| 19 | try { new RegExp("^(?:" + pattern + ")$", flags); return true; } catch (error) { return false; } |
| 20 | }; |
| 21 | const matches = {}; |
| 22 | for (const pattern of cases.patterns) { |
| 23 | const whole = new RegExp("^(?:" + pattern + ")$", "u"); |
| 24 | matches[pattern] = cases.samples |
| 25 | .map((sample, index) => (whole.test(sample) ? index : -1)) |
| 26 | .filter((index) => index >= 0); |
| 27 | } |
| 28 | const refused = {}; |
| 29 | for (const pattern of cases.refused) { |
| 30 | refused[pattern] = compiles(pattern, "u"); |
| 31 | } |
| 32 | process.stdout.write(JSON.stringify({ |
| 33 | samples: cases.samples, |
| 34 | matches, |
| 35 | refused_compiles_in_ecmascript: refused, |
| 36 | }, null, 1).replace(/[^\x00-\x7e]/g, (unit) => |
| 37 | "\\u" + unit.charCodeAt(0).toString(16).padStart(4, "0")) + "\n"); |
| 38 | }); |