You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
66 lines
1.4 KiB
JavaScript
66 lines
1.4 KiB
JavaScript
3 years ago
|
"use strict";
|
||
|
|
||
|
const { regexp: regexp_, anyCharOf, letter, digit } = require("parjs");
|
||
|
const { many, must, map, or, manySepBy, then, each } = require("parjs/combinators");
|
||
|
|
||
|
let first = map((items) => items[0]);
|
||
|
|
||
|
// TODO:
|
||
|
// label combinator, for error labeling
|
||
|
// named regexes
|
||
|
// named 'must' predicates
|
||
|
|
||
|
function sequence(items) {
|
||
|
let mappedItems = items.map((item) => {
|
||
|
if (typeof item === "string") {
|
||
|
return string(item);
|
||
|
} else if (item instanceof RegExp) {
|
||
|
return regexp(item);
|
||
|
} else {
|
||
|
return item;
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
|
||
|
function regexp(regex) {
|
||
|
// The `.call` is a workaround for https://github.com/GregRos/parjs/pull/30
|
||
|
return regexp_.call({}, regex);
|
||
|
}
|
||
|
|
||
|
let ipv4Digit = regexp(/[0-9]{1,3}/).pipe(first);
|
||
|
|
||
|
let ipv6Address = regexp(/[a-f0-9:.]{2,45}/i).pipe(
|
||
|
first
|
||
|
);
|
||
|
|
||
|
// FIXME: Find a way to compose `manySepBy` and `exactly`
|
||
|
let ipv4Address = ipv4Digit.pipe(
|
||
|
manySepBy("."),
|
||
|
must((results) => results.length === 4),
|
||
|
map((octets) => octets.join("."))
|
||
|
);
|
||
|
|
||
|
let ipAddress = ipv4Address.pipe(
|
||
|
or(ipv6Address)
|
||
|
);
|
||
|
|
||
|
// let ipv4Address = regexp.call();
|
||
|
|
||
|
// console.log(ipAddress.parse(process.argv[2]));
|
||
|
|
||
|
function combine([ previous, current ]) {
|
||
|
return previous.concat([ current ]);
|
||
|
}
|
||
|
|
||
|
let test = letter().pipe(
|
||
|
then(digit()),
|
||
|
then(letter()),
|
||
|
combine,
|
||
|
// then(combine),
|
||
|
then(digit()),
|
||
|
then(combine),
|
||
|
then(letter())
|
||
|
);
|
||
|
|
||
|
console.log(test.parse(process.argv[2]));
|